-
Notifications
You must be signed in to change notification settings - Fork 0
/
osrm_c.cpp
268 lines (219 loc) · 6.98 KB
/
osrm_c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "osrm_c.h"
#include <osrm/osrm.hpp>
#include <osrm/json_container.hpp>
#include <osrm/libosrm_config.hpp>
#include <osrm/route_parameters.hpp>
#include <string.h>
#include <boost/fusion/container/vector.hpp>
extern "C" {
struct osrm_t { OSRM* rep; };
struct osrm_config_t { LibOSRMConfig* rep; };
struct osrm_query_t { RouteParameters* rep; };
struct osrm_json_handler_t {
void* state;
void (*push_object)(void*);
void (*push_array)(void*);
void (*pop)(void*);
void (*append_string)(void*, const char*, size_t);
void (*append_number)(void*, double);
void (*append_bool)(void*, unsigned char);
void (*append_null)(void*);
};
void WriteError(const char* errstr, char** errptr) {
assert(errptr != NULL);
assert(*errptr == NULL);
*errptr = (char*)malloc(strlen(errstr) + 1);
strcpy(*errptr, errstr);
}
struct JsonVisitor : mapbox::util::static_visitor<> {
explicit JsonVisitor(osrm_json_handler_t* handler) : handler_(handler) {
}
void operator()(const osrm::json::String &string) const {
handler_->append_string(
handler_->state, string.value.data(), string.value.size());
}
void operator()(const osrm::json::Number &number) const {
handler_->append_number(handler_->state, number.value);
}
void operator()(const osrm::json::Object &object) const {
handler_->push_object(handler_->state);
for (const auto& keyValue : object.values) {
handler_->append_string(
handler_->state, keyValue.first.data(), keyValue.first.size());
mapbox::util::apply_visitor<osrm::json::Value, JsonVisitor>(*this, keyValue.second);
}
handler_->pop(handler_->state);
}
void operator()(const osrm::json::Array &array) const
{
handler_->push_array(handler_->state);
for (auto i = 0u; i < array.values.size(); ++i) {
mapbox::util::apply_visitor<osrm::json::Value, JsonVisitor>(*this, array.values[i]);
}
handler_->pop(handler_->state);
}
void operator()(const osrm::json::True &) const {
handler_->append_bool(handler_->state, true);
}
void operator()(const osrm::json::False &) const {
handler_->append_bool(handler_->state, false);
}
void operator()(const osrm::json::Null &) const {
handler_->append_null(handler_->state);
}
private:
osrm_json_handler_t* handler_;
};
/* osrm_t */
osrm_t* osrm_create(const osrm_config_t* config, char** errptr) {
osrm_t* engine = new osrm_t;
try {
engine->rep = new OSRM(*config->rep);
} catch (std::exception const& ex) {
WriteError(ex.what(), errptr);
engine = NULL;
}
return engine;
}
void osrm_destroy(osrm_t* engine) {
delete engine->rep;
delete engine;
}
void osrm_run_query(
osrm_t* engine,
osrm_query_t* query,
int* code,
osrm_json_handler_t* json_handler,
char** errptr) {
osrm::json::Object json_object;
int result_code = 500;
try {
result_code = engine->rep->RunQuery(*query->rep, json_object);
} catch (std::exception const& ex) {
WriteError(ex.what(), errptr);
}
if (code != NULL) {
*code = result_code;
}
if (json_handler != NULL) {
JsonVisitor visitor(json_handler);
mapbox::util::apply_visitor<osrm::json::Value, JsonVisitor>(
visitor, json_object);
}
}
/* osrm_config_t */
osrm_config_t* osrm_config_create() {
osrm_config_t* config = new osrm_config_t;
config->rep = new LibOSRMConfig;
return config;
}
void osrm_config_destroy(osrm_config_t* config) {
delete config->rep;
delete config;
}
void osrm_config_set_path(
osrm_config_t* config, const char* key, const char* path) {
config->rep->server_paths[key] = path;
}
void osrm_config_set_max_locations_distance_table(
osrm_config_t* config,
int value) {
config->rep->max_locations_distance_table = value;
}
void osrm_config_set_max_locations_map_matching(
osrm_config_t* config,
int value) {
config->rep->max_locations_map_matching = value;
}
void osrm_config_set_use_shared_memory(
osrm_config_t* config,
unsigned char value) {
config->rep->use_shared_memory = value;
}
/* osrm_query_t */
osrm_query_t* osrm_query_create() {
osrm_query_t* params = new osrm_query_t;
params->rep = new RouteParameters;
return params;
}
void osrm_query_destroy(osrm_query_t* params) {
delete params->rep;
delete params;
}
void osrm_query_set_zoom_level(osrm_query_t* params, short value) {
params->rep->setZoomLevel(value);
}
void osrm_query_set_number_of_results(osrm_query_t* params, short value) {
params->rep->setNumberOfResults(value);
}
void osrm_query_set_alternate_route_flag(osrm_query_t* params, unsigned char value) {
params->rep->setAlternateRouteFlag(value);
}
void osrm_query_set_uturn(osrm_query_t* params, unsigned char value) {
params->rep->setUTurn(value);
}
void osrm_query_set_classify(osrm_query_t* params, unsigned char value) {
params->rep->setClassify(value);
}
void osrm_query_set_matching_beta(osrm_query_t* params, double value) {
params->rep->setMatchingBeta(value);
}
void osrm_query_set_gps_precision(osrm_query_t* params, double value) {
params->rep->setGPSPrecision(value);
}
void osrm_query_set_checksum(osrm_query_t* params, unsigned value) {
params->rep->setChecksum(value);
}
void osrm_query_set_instruction_flag(osrm_query_t* params, unsigned char value) {
params->rep->setInstructionFlag(value);
}
void osrm_query_set_service(osrm_query_t* params, const char* value) {
params->rep->setService(value);
}
void osrm_query_add_hint(osrm_query_t* params, const char* value) {
params->rep->addHint(value);
}
void osrm_query_add_timestamp(osrm_query_t* params, unsigned value) {
params->rep->addTimestamp(value);
}
void osrm_query_set_language(osrm_query_t* params, const char* value) {
params->rep->setLanguage(value);
}
void osrm_query_set_geometry_flag(osrm_query_t* params, unsigned char value) {
params->rep->setGeometryFlag(value);
}
void osrm_query_set_compression_flag(osrm_query_t* params, unsigned char value) {
params->rep->setCompressionFlag(value);
}
void osrm_query_add_coordinate(osrm_query_t* params, double lat, double lon) {
const boost::fusion::vector<double, double> received_coordinates(lat, lon);
params->rep->addCoordinate(received_coordinates);
}
/* osrm_json_handler_t */
osrm_json_handler_t* osrm_json_handler_create(
void* state,
void (*push_object)(void*),
void (*push_array)(void*),
void (*pop)(void*),
void (*append_string)(void*, const char*, size_t),
void (*append_number)(void*, double),
void (*append_bool)(void*, unsigned char),
void (*append_null)(void*)) {
osrm_json_handler_t* handler = new osrm_json_handler_t;
handler->state = state;
handler->push_object = push_object;
handler->push_array = push_array;
handler->pop = pop;
handler->append_string = append_string;
handler->append_number = append_number;
handler->append_bool = append_bool;
handler->append_null = append_null;
return handler;
}
void osrm_json_handler_destroy(osrm_json_handler_t* handler) {
delete handler;
}
void osrm_free(void* ptr) {
free(ptr);
}
}