-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtypes.cpp
More file actions
502 lines (409 loc) · 14.7 KB
/
types.cpp
File metadata and controls
502 lines (409 loc) · 14.7 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include <type_traits>
#include <string>
#include <memory>
#include <iostream>
#include "jlcxx/jlcxx.hpp"
#include "jlcxx/functions.hpp"
#include "jlcxx/stl.hpp"
namespace cpp_types
{
// Custom minimal smart pointer type
template<typename T>
struct MySmartPointer
{
MySmartPointer(T* ptr) : m_ptr(ptr)
{
}
MySmartPointer(std::shared_ptr<T> ptr) : m_ptr(ptr.get())
{
}
T& operator*() const
{
return *m_ptr;
}
T* m_ptr;
};
struct DoubleData
{
double a[4];
};
struct World
{
World(const std::string& message = "default hello") : msg(message){}
World(jlcxx::cxxint_t) : msg("NumberedWorld") {}
void set(const std::string& msg) { this->msg = msg; }
const std::string& greet() const { return msg; }
std::string msg;
~World() { std::cout << "Destroying World with message " << msg << std::endl; }
};
std::string greet_overload(World& w) { return w.msg + "_byref"; }
std::string greet_overload(const World& w) { return w.msg + "_byconstref"; }
std::string greet_overload(World* w) { return w->msg + "_bypointer"; }
std::string greet_overload(const World* w) { return w->msg + "_byconstpointer"; }
std::string greet_overload(const std::shared_ptr<World> w) { return w->msg + "_bysharedptr"; }
struct Array { Array() {} };
struct NonCopyable
{
NonCopyable() {}
NonCopyable& operator=(const NonCopyable&) = delete;
NonCopyable(const NonCopyable&) = delete;
};
struct AConstRef
{
AConstRef() {}
int value() const
{
return 42;
}
};
struct ReturnConstRef
{
const AConstRef& operator()()
{
return m_val;
}
AConstRef m_val;
};
struct CallOperator
{
CallOperator() {}
int operator()() const
{
return 43;
}
};
struct ConstPtrConstruct
{
ConstPtrConstruct(const World* w) : m_w(w)
{
}
const std::string& greet() { return m_w->greet(); }
const World* m_w;
};
// Call a function on a type that is defined in Julia
struct JuliaTestType {
double a;
double b;
};
void call_testtype_function()
{
jlcxx::JuliaFunction("julia_test_func")(jlcxx::box<JuliaTestType>(JuliaTestType({2.0, 3.0}), jlcxx::julia_type("JuliaTestType")));
}
enum MyEnum
{
EnumValA,
EnumValB
};
enum MyEnumNew
{
EnumVal1,
EnumVal2
};
enum class EnumClass { red, green = 20, blue };
enum class BigEnumClass : uint64_t { zero = 0, verybig = 0xffffffffffffffff };
struct Foo
{
Foo(const std::wstring& n, jlcxx::ArrayRef<double,1> d) : name(n), data(d.begin(), d.end())
{
}
std::wstring name;
std::vector<double> data;
};
struct NeverEmpty
{
int data;
NeverEmpty() = delete;
NeverEmpty(int d) : data(d) {};
};
struct NullableStruct { NullableStruct() {} };
struct IntDerived
{
int val;
IntDerived() : val(42) { }
bool operator == (IntDerived &other){ return this->val == other.val; }
};
// See https://stackoverflow.com/questions/257288/templated-check-for-the-existence-of-a-class-member-function
template<class>
struct sfinae_true : std::true_type{};
namespace detail
{
template<class T>
static auto test_getImpl(int)
-> sfinae_true<decltype(std::declval<T>().getImpl())>;
template<class>
static auto test_getImpl(long) -> std::false_type;
}
template<class T>
struct has_getImpl : decltype(detail::test_getImpl<T>(0)){};
class UseCustomDelete
{
public:
UseCustomDelete() {}
void getImpl() const {}
static int nb_deleted;
};
int UseCustomDelete::nb_deleted = 0;
class UseCustomClassDelete
{
public:
UseCustomClassDelete() {}
static int nb_deleted;
};
int UseCustomClassDelete::nb_deleted = 0;
void int_vec_arg(std::vector<std::shared_ptr<int>>){}
void const_int_vec_arg(std::vector<std::shared_ptr<const int>>){}
} // namespace cpp_types
namespace jlcxx
{
template<> struct IsMirroredType<cpp_types::DoubleData> : std::false_type { };
template<> struct IsMirroredType<cpp_types::NeverEmpty> : std::false_type { };
template<typename T> struct IsSmartPointerType<cpp_types::MySmartPointer<T>> : std::true_type { };
template<typename T> struct ConstructorPointerType<cpp_types::MySmartPointer<T>> { typedef std::shared_ptr<T> type; };
}
class SingletonType
{
public:
static SingletonType& instance() {static SingletonType s; return s; }
int alive() { return 1; }
private:
SingletonType() {}
~SingletonType() {}
};
namespace jlcxx
{
template<typename T>
struct Finalizer<T, SpecializedFinalizer>
{
static void finalize(T* to_delete)
{
if constexpr(cpp_types::has_getImpl<T>::value) {
std::cout << "calling specialized delete" << std::endl;
delete to_delete;
T::nb_deleted += 1;
} else {
delete to_delete;
}
}
};
template<>
struct Finalizer<cpp_types::UseCustomClassDelete, SpecializedFinalizer>
{
static void finalize(cpp_types::UseCustomClassDelete* to_delete)
{
std::cout << "Class specific finalizer called" << std::endl;
cpp_types::UseCustomClassDelete::nb_deleted += 1;
delete to_delete;
}
};
}
JLCXX_MODULE define_julia_module(jlcxx::Module& types)
{
using namespace cpp_types;
types.method("call_testtype_function", call_testtype_function);
types.add_type<DoubleData>("DoubleData");
types.add_type<World>("World")
.constructor<const std::string&>()
.constructor<jlcxx::cxxint_t>(jlcxx::finalize_policy::no) // no finalizer
.constructor([] (const std::string& a, const std::string& b) { return new World(a + " " + b); })
.method("set", &World::set)
.method("greet_cref", &World::greet)
.method("greet_lambda", [] (const World& w) { return w.greet(); } )
.method("greet_byvalue", [] (World w) { return w.greet(); } );
types.method("greet_overload", static_cast<std::string (*) (World&)>(greet_overload));
types.method("greet_overload", static_cast<std::string (*) (const World&)>(greet_overload));
types.method("greet_overload", static_cast<std::string (*) (World*)>(greet_overload));
types.method("greet_overload", static_cast<std::string (*) (const World*)>(greet_overload));
types.method("greet_overload", static_cast<std::string (*) (std::shared_ptr<World>)>(greet_overload));
types.method("test_unbox", [] ()
{
std::vector<bool> results;
results.push_back(jlcxx::unbox<int>(jlcxx::JuliaFunction("return_int")()) == 3);
results.push_back(*jlcxx::unbox<double*>(jlcxx::JuliaFunction("return_ptr_double")()) == 4.0);
results.push_back(jlcxx::unbox<World>(jlcxx::JuliaFunction("return_world")()).greet() == "returned_world");
results.push_back(jlcxx::unbox<World*>(jlcxx::JuliaFunction("return_world")())->greet() == "returned_world");
results.push_back(jlcxx::unbox<World&>(jlcxx::JuliaFunction("return_world")()).greet() == "returned_world");
results.push_back(jlcxx::unbox<World*>(jlcxx::JuliaFunction("return_world_ptr")())->greet() == "returned_world_ptr");
results.push_back(jlcxx::unbox<World&>(jlcxx::JuliaFunction("return_world_ref")()).greet() == "returned_world_ref");
return results;
});
types.add_type<Array>("Array");
types.method("world_factory", []()
{
return new World("factory hello");
});
types.method("shared_world_factory", []() -> const std::shared_ptr<World>
{
return std::shared_ptr<World>(new World("shared factory hello"));
});
// Shared ptr overload for greet
types.method("greet_shared", [](const std::shared_ptr<World>& w)
{
return w->greet();
});
types.method("greet_shared_const", [](const std::shared_ptr<const World>& w)
{
return w->greet();
});
types.method("int_vec_arg", cpp_types::int_vec_arg);
types.method("const_int_vec_arg", cpp_types::const_int_vec_arg);
types.method("shared_world_ref", []() -> std::shared_ptr<World>&
{
static std::shared_ptr<World> refworld(new World("shared factory hello ref"));
return refworld;
});
types.method("reset_shared_world!", [](std::shared_ptr<World>& target, std::string message)
{
target.reset(new World(message));
});
jlcxx::add_smart_pointer<MySmartPointer>(types, "MySmartPointer");
types.method("smart_world_factory", []()
{
return MySmartPointer<World>(new World("smart factory hello"));
});
// smart ptr overload for greet
types.method("greet_smart", [](const MySmartPointer<World>& w)
{
return (*w).greet();
});
// weak ptr overload for greet
types.method("greet_weak", [](const std::weak_ptr<World>& w)
{
return w.lock()->greet();
});
types.method("unique_world_factory", []()
{
return std::unique_ptr<const World>(new World("unique factory hello"));
});
types.method("world_by_value", [] () -> World
{
return World("world by value hello");
});
types.method("boxed_world_factory", []()
{
static World w("boxed world");
return jlcxx::box<World&>(w);
});
types.method("boxed_world_pointer_factory", []()
{
static World w("boxed world pointer");
return jlcxx::box<World*>(&w);
});
types.method("world_ref_factory", []() -> World&
{
static World w("reffed world");
return w;
});
types.method("shared_vector_factory", []() -> std::vector<std::shared_ptr<World>>
{
return {std::shared_ptr<World>(new World("shared vector hello"))};
});
types.method("shared_const_vector_factory", []() -> std::vector<std::shared_ptr<const World>>
{
return {std::shared_ptr<const World>(new World("shared vector const hello"))};
});
types.method("world_ptr_vector", []() { static World w; return std::vector({&w}); });
types.method("get_shared_vector_msg", [](const std::vector<std::shared_ptr<World>>& v)
{
return v[0]->greet();
});
types.method("get_shared_vector_msg", [](const std::vector<std::shared_ptr<const World>>& v)
{
return v[0]->greet() + " from const overload";
});
types.add_type<NonCopyable>("NonCopyable");
types.add_type<AConstRef>("AConstRef").method("value", &AConstRef::value);
types.add_type<ReturnConstRef>("ReturnConstRef").method("value", &ReturnConstRef::operator());
types.add_type<CallOperator>("CallOperator").method(&CallOperator::operator())
.method([] (const CallOperator&, int i) { return i; } );
types.add_type<ConstPtrConstruct>("ConstPtrConstruct")
.constructor<const World*>()
.method("greet_cref", &ConstPtrConstruct::greet);
// Enum
types.add_bits<MyEnum>("MyEnum", jlcxx::julia_type("CppEnum"));
types.set_const("EnumValA", EnumValA);
types.set_const("EnumValB", EnumValB);
#if JULIA_VERSION_MAJOR == 1 && JULIA_VERSION_MINOR < 4
jl_gc_collect(1);
#else
jl_gc_collect(JL_GC_FULL);
#endif
types.method("enum_to_int", [] (const MyEnum e) { return static_cast<int>(e); });
types.method("get_enum_b", [] () { return EnumValB; });
types.add_bits<EnumClass>("EnumClass", jlcxx::julia_type("CppEnum"));
types.set_const("EnumClassRed", EnumClass::red);
types.set_const("EnumClassBlue", EnumClass::blue);
types.method("check_red", [] (const EnumClass c) { return c == EnumClass::red; });
static const EnumClass stored_blue = EnumClass::blue;
types.method("check_enum_byref", [] (const EnumClass& c) { return c == EnumClass::red; });
types.set_const("StoredBlue", stored_blue);
types.add_enum<MyEnumNew>("MyEnumNew", std::vector<const char*>({"EnumVal1", "EnumVal2"}), std::vector<int>({EnumVal1, EnumVal2}));
types.method("newenum_to_int", [] (const MyEnumNew e) { return static_cast<int>(e); });
types.method("newenum_from_int", [] (int i) { return static_cast<MyEnumNew>(i); });
types.method("newenum_byref", [] (const MyEnumNew& e) { return static_cast<int>(e); });
types.add_enum<BigEnumClass>("BigEnumClass", std::vector<const char*>({"zero", "verybig"}), std::vector<uint64_t>({static_cast<uint64_t>(BigEnumClass::zero), static_cast<uint64_t>(BigEnumClass::verybig)}));
types.add_type<Foo>("Foo")
.constructor<const std::wstring&, jlcxx::ArrayRef<double,1>>()
.method("name", [](Foo& f) { return f.name; })
.method("data", [](Foo& f) { return jlcxx::ArrayRef<double,1>(&(f.data[0]), f.data.size()); });
types.method("print_foo_array", [] (jlcxx::ArrayRef<jl_value_t*> farr)
{
for(jl_value_t* v : farr)
{
const Foo& f = jlcxx::unbox<Foo&>(v);
std::wcout << f.name << ":";
for(const double d : f.data)
{
std::wcout << " " << d;
}
std::wcout << std::endl;
}
});
types.add_type<NullableStruct>("NullableStruct");
types.method("return_ptr", [] () { return new NullableStruct; });
types.method("return_null", [] () { return static_cast<NullableStruct*>(nullptr); });
types.method("greet_vector", [] (const std::vector<World>& v)
{
std::stringstream messages;
for(const World& w : v)
{
messages << w.greet() << " ";
}
const std::string result = messages.str();
return result.substr(0,result.size()-1);
});
types.add_type<IntDerived>("IntDerived", jlcxx::julia_type("Integer", "Base"));
types.set_override_module(jl_base_module);
types.method("==", [](IntDerived& a, IntDerived& b) { return a == b; });
types.method("Int", [](IntDerived& a) { return int_t(a.val); });
types.unset_override_module();
types.add_type<SingletonType>("SingeltonType")
.method("alive", &SingletonType::alive);
types.method("singleton_instance", SingletonType::instance);
types.add_type<UseCustomDelete>("UseCustomDelete");
types.method("get_custom_nb_deletes", [] () { return UseCustomDelete::nb_deleted; });
types.add_type<UseCustomClassDelete>("UseCustomClassDelete");
types.method("get_custom_class_nb_deletes", [] () { return UseCustomClassDelete::nb_deleted; });
types.method("world_dequeue", []() { static World w; return std::deque({w}); });
types.method("world_list", []() { static World w; return std::list({w}); });
types.add_type<NeverEmpty>("NeverEmpty")
.constructor<int>()
.method("get_data", [](NeverEmpty& n) { return n.data; })
.method("set_data", [](NeverEmpty& n, int d) { n.data = d; });
types.method("neverempty_array", [] () { NeverEmpty n(1); return std::vector({n}); });
types.method("neverempty_deque", [] () { NeverEmpty n(1); return std::deque({n}); });
// Note that when mixing compilers (e.g. MSVC and GCC) the FILE* must be manipulated only from
// within functions compiled with the same compiler. Passing between CRT results in errors.
types.method("makefptr", [] (const std::string& filename) { return fopen(filename.c_str(), "w"); });
types.method("writefptr", [] (FILE* fp) { fprintf(fp, "Hello world!"); });
types.method("closefptr", [] (FILE* fp) { fclose(fp); });
}
JLCXX_MODULE define_types2_module(jlcxx::Module& types2)
{
types2.method("vecvec", [] (const std::vector<std::vector<int>>& v) { return v[0][0]; });
types2.method("vecvec", [] (const std::vector<std::vector<cpp_types::World>>& v) { return v[0][0]; });
}
JLCXX_MODULE define_types3_module(jlcxx::Module& types3)
{
types3.method("vecvec", [] (const std::vector<std::vector<int>>& v) { return 2*v[0][0]; });
types3.method("vecvec", [] (const std::vector<std::vector<cpp_types::World>>& v) { return v[0][0]; });
}