-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathcinterface.cpp
437 lines (383 loc) · 10.3 KB
/
cinterface.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "cinterface.h"
#include <cstddef>
#include <cstdint>
#include <exception>
#include <functional>
#include <limits>
#include <new>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <typeinfo>
#include <variant>
#include "interface.h"
using Vizzu::Interface;
static_assert(sizeof(double) == 8);
static_assert(offsetof(Point, x) == 0);
static_assert(offsetof(Point, y) == 8);
static_assert(offsetof(Value, dimension) == 0);
static_assert(offsetof(Value, measureValue) == 8);
static_assert(offsetof(Value, dimensionValue) == 8);
static_assert(sizeof(const char *) == 4);
constexpr std::uint_fast32_t hash(std::string_view s) noexcept
{
std::uint_fast32_t val{};
for (size_t i = 0; i < s.size(); ++i)
val ^= static_cast<std::uint_fast32_t>(s[i])
<< ((i % sizeof(std::uint_fast32_t))
* std::numeric_limits<unsigned char>::digits);
return val;
}
const char *vizzu_errorMessage(APIHandles::Exception exceptionPtr,
const std::type_info *typeinfo)
{
std::string_view type_info = typeinfo->name();
if (type_info.starts_with("St")
|| type_info.starts_with("NSt")) { // std::
constexpr auto valid = "abcdefghijklmnopqrstuvwxyz_";
auto last = type_info.find_last_of(valid);
auto first = type_info.find_last_not_of(valid, last);
type_info = type_info.substr(first + 1, last - first);
const std::exception *realException = nullptr;
switch (hash(type_info)) {
case hash("exception"):
realException =
static_cast<const std::exception *>(exceptionPtr);
break;
case hash("bad_alloc"):
realException =
static_cast<const std::bad_alloc *>(exceptionPtr);
break;
case hash("bad_array_new_length"):
realException =
static_cast<const std::bad_array_new_length *>(
exceptionPtr);
break;
case hash("bad_optional_access"):
realException =
static_cast<const std::bad_optional_access *>(
exceptionPtr);
break;
case hash("logic_error"):
realException =
static_cast<const std::logic_error *>(exceptionPtr);
break;
case hash("invalid_argument"):
realException =
static_cast<const std::invalid_argument *>(
exceptionPtr);
break;
case hash("length_error"):
realException =
static_cast<const std::length_error *>(exceptionPtr);
break;
case hash("out_of_range"):
realException =
static_cast<const std::out_of_range *>(exceptionPtr);
break;
case hash("runtime_error"):
realException =
static_cast<const std::runtime_error *>(exceptionPtr);
break;
case hash("bad_cast"):
realException =
static_cast<const std::bad_cast *>(exceptionPtr);
break;
case hash("bad_function_call"):
realException =
static_cast<const std::bad_function_call *>(
exceptionPtr);
break;
case hash("bad_variant_access"):
realException =
static_cast<const std::bad_variant_access *>(
exceptionPtr);
break;
default: break;
}
if (realException) return realException->what();
}
thread_local std::string data;
data = "Cannot call '";
data += type_info;
data += "::what()'. Please add this exception to the ";
data += __func__;
data += " function.";
return data.c_str();
}
const char *vizzu_version() { return Interface::version(); }
void vizzu_setLogging(bool enable) { Interface::setLogging(enable); }
APIHandles::DataTable vizzu_createData()
{
return Interface::getInstance().createData();
}
APIHandles::DataTable vizzu_createExternalData(
void (*stringDeleter)(const char *),
bool (*seriesMeta)(const char *),
const char *(*seriesInfo)(const char *, const char *),
void (*aggregator)(APIHandles::DataTable,
bool (*)(const Vizzu::Data::RowWrapper *),
bool (*)(const Vizzu::Data::RowWrapper *),
std::uint32_t,
const char *const *,
std::uint32_t,
const char *const *,
const char *const *,
const char **),
void (*deleter)())
{
return Interface::getInstance().createExternalData(
{{stringDeleter, seriesMeta, seriesInfo, aggregator},
deleter});
}
APIHandles::Chart vizzu_createChart(APIHandles::DataTable data)
{
return Interface::getInstance().createChart(data);
}
APIHandles::Canvas vizzu_createCanvas()
{
return Interface::getInstance().createCanvas();
}
void vizzu_pointerMove(APIHandles::Chart chart,
APIHandles::Canvas canvas,
int pointerId,
double x,
double y)
{
Interface::getInstance().pointerMove(chart,
canvas,
pointerId,
x,
y);
}
void vizzu_pointerDown(APIHandles::Chart chart,
APIHandles::Canvas canvas,
int pointerId,
double x,
double y)
{
Interface::getInstance().pointerDown(chart,
canvas,
pointerId,
x,
y);
}
void vizzu_pointerUp(APIHandles::Chart chart,
APIHandles::Canvas canvas,
int pointerId,
double x,
double y)
{
Interface::getInstance().pointerUp(chart,
canvas,
pointerId,
x,
y);
}
void vizzu_pointerLeave(APIHandles::Chart chart,
APIHandles::Canvas canvas,
int pointerId)
{
Interface::getInstance().pointerLeave(chart, canvas, pointerId);
}
void vizzu_wheel(APIHandles::Chart chart,
APIHandles::Canvas canvas,
double delta)
{
Interface::getInstance().wheel(chart, canvas, delta);
}
void vizzu_update(APIHandles::Chart chart, double timeInMSecs)
{
Interface::getInstance().update(chart, timeInMSecs);
}
void vizzu_render(APIHandles::Chart chart,
APIHandles::Canvas canvas,
double width,
double height)
{
Interface::getInstance().render(chart, canvas, width, height);
}
void vizzu_setLineResolution(APIHandles::Canvas canvas,
double distanceMax,
double curveHeightMax)
{
Interface::getInstance().setLineResolution(canvas,
distanceMax,
curveHeightMax);
}
const char *style_getList() { return Interface::getStyleList(); }
void style_setValue(APIHandles::Chart chart,
const char *path,
const char *value)
{
Interface::getInstance().setStyleValue(chart, path, value);
}
const char *style_getValue(APIHandles::Chart chart,
const char *path,
bool computed)
{
return Interface::getInstance().getStyleValue(chart,
path,
computed);
}
APIHandles::Snapshot chart_store(APIHandles::Chart chart)
{
return Interface::getInstance().storeChart(chart);
}
void chart_restore(APIHandles::Chart chart,
APIHandles::Snapshot snapshot)
{
Interface::getInstance().restoreChart(chart, snapshot);
}
APIHandles::Animation chart_anim_store(APIHandles::Chart chart)
{
return Interface::getInstance().storeAnim(chart);
}
void chart_anim_restore(APIHandles::Chart chart,
APIHandles::Animation anim)
{
Interface::getInstance().restoreAnim(chart, anim);
}
void object_free(APIHandles::Any handle)
{
Interface::getInstance().freeObj(handle);
}
const char *chart_getList() { return Interface::getChartParamList(); }
const char *chart_getValue(APIHandles::Chart chart, const char *path)
{
return Interface::getInstance().getChartValue(chart, path);
}
void chart_setValue(APIHandles::Chart chart,
const char *path,
const char *value)
{
Interface::getInstance().setChartValue(chart, path, value);
}
void chart_setFilter(APIHandles::Chart chart,
bool (*filter)(const Vizzu::Data::RowWrapper *),
void (*deleter)(bool (*)(const Vizzu::Data::RowWrapper *)))
{
Interface::getInstance().setChartFilter(chart, {filter, deleter});
}
const Value *record_getValue(const Vizzu::Data::RowWrapper *record,
const char *column)
{
thread_local Value val{{}, {}};
if (auto &&cval = Interface::getRecordValue(*record, column);
cval.index()) {
val.dimension = true;
auto &&dim = *std::get_if<const std::string *>(&cval);
new (
&val.dimensionValue) // NOLINT(bugprone-multi-level-implicit-pointer-conversion)
const char *{dim ? dim->c_str() : nullptr};
}
else {
val.dimension = false;
new (&val.measureValue) double{*std::get_if<double>(&cval)};
}
return &val;
}
void data_addDimension(APIHandles::Chart chart,
const char *name,
const char *const *categories,
std::uint32_t categoriesCount,
const std::uint32_t *categoryIndices,
std::uint32_t categoryIndicesCount,
bool isContiguous)
{
Interface::getInstance().addDimension(chart,
name,
categories,
categoriesCount,
categoryIndices,
categoryIndicesCount,
isContiguous);
}
void data_addMeasure(APIHandles::Chart chart,
const char *name,
const char *unit,
const double *values,
std::uint32_t count)
{
Interface::getInstance().addMeasure(chart,
name,
unit,
values,
count);
}
void data_addRecord(APIHandles::Chart chart,
const char *const *cells,
std::uint32_t count)
{
Interface::getInstance().addRecord(chart, cells, count);
}
const char *data_metaInfo(APIHandles::Chart chart)
{
return Interface::getInstance().dataMetaInfo(chart);
}
void addEventListener(APIHandles::Chart chart,
const char *name,
void (*callback)(APIHandles::Event event, const char *))
{
Interface::getInstance().addEventListener(chart, name, callback);
}
void removeEventListener(APIHandles::Chart chart,
const char *name,
void (*callback)(APIHandles::Event event, const char *))
{
Interface::getInstance().removeEventListener(chart,
name,
callback);
}
void event_preventDefault(APIHandles::Event event)
{
Interface::preventDefaultEvent(event);
}
void chart_animate(APIHandles::Chart chart, void (*callback)(bool))
{
Interface::getInstance().animate(chart, callback);
}
const Point *
chart_relToCanvasCoords(APIHandles::Chart chart, double rx, double ry)
{
thread_local Point res;
Interface::getInstance().relToCanvasCoords(chart,
rx,
ry,
res.x,
res.y);
return &res;
}
const Point *
chart_canvasToRelCoords(APIHandles::Chart chart, double x, double y)
{
thread_local Point res;
Interface::getInstance().canvasToRelCoords(chart,
x,
y,
res.x,
res.y);
return &res;
}
void chart_setKeyframe(APIHandles::Chart chart)
{
Interface::getInstance().setKeyframe(chart);
}
void anim_control_setValue(APIHandles::Chart chart,
const char *path,
const char *value)
{
Interface::getInstance().setAnimControlValue(chart, path, value);
}
const char *anim_control_getValue(APIHandles::Chart chart,
const char *path)
{
return Interface::getInstance().getAnimControlValue(chart, path);
}
void anim_setValue(APIHandles::Chart chart,
const char *path,
const char *value)
{
Interface::getInstance().setAnimValue(chart, path, value);
}