-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathfunctions.cpp
358 lines (305 loc) · 10.4 KB
/
functions.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
#include <algorithm>
#include <sstream>
#include <cstddef>
#include "jlcxx/jlcxx.hpp"
#include "jlcxx/array.hpp"
#include "jlcxx/functions.hpp"
#ifdef _WIN32
#ifdef JLCXX_EXAMPLES_EXPORTS
#define JLCXX_EXAMPLES_API __declspec(dllexport)
#else
#define JLCXX_EXAMPLES_API __declspec(dllimport)
#endif
#else
#define JLCXX_EXAMPLES_API __attribute__ ((visibility("default")))
#endif
// C function for performance comparison
extern "C" JLCXX_EXAMPLES_API double half_c(const double d)
{
return 0.5 * d;
}
namespace functions
{
double half_function(const double d)
{
return 0.5 * d;
}
template<typename T>
T half_template (const T x)
{
return x / static_cast<T>(2);
}
bool test_int32_array(int32_t* f)
{
return f[0] == 1 && f[1] == 2;
}
bool test_int64_array(int64_t* f)
{
return f[0] == 1 && f[1] == 2;
}
bool test_float_array(float* f)
{
return f[0] == 1. && f[1] == 2.;
}
bool test_double_array(double* f)
{
return f[0] == 1. && f[1] == 2.;
}
std::size_t test_array_len(jlcxx::ArrayRef<double> a)
{
return a.size();
}
double test_array_get(jlcxx::ArrayRef<double> a, const int64_t i)
{
return a[i];
}
void test_array_set(jlcxx::ArrayRef<double> a, const int64_t i, const double v)
{
a[i] = v;
}
long long int test_long_long()
{
return 42;
}
short test_short()
{
return 43;
}
void test_exception()
{
throw std::runtime_error("This is an exception");
}
std::string test_type_name(const std::string& name)
{
return jlcxx::julia_type_name(jlcxx::julia_type(name));
}
JLCXX_MODULE init_half_module(jlcxx::Module& mod)
{
// register a standard C++ function
mod.method("half_d", half_function);
// register some template instantiations
mod.method("half_i", half_template<int>);
mod.method("half_u", half_template<unsigned int>);
// Register a lambda
mod.method("half_lambda", [](const double a) {return a*0.5;});
// Strict number typing
mod.method("strict_half", [](const jlcxx::StrictlyTypedNumber<double> a) {return a.value*0.5;});
// Looping function
mod.method("half_loop_cpp!",
[](jlcxx::ArrayRef<double> in, jlcxx::ArrayRef<double> out)
{
std::transform(in.begin(), in.end(), out.begin(), [](const double d) { return 0.5*d; });
});
// Looping function calling Julia
mod.method("half_loop_jlcall!",
[](jlcxx::ArrayRef<double> in, jlcxx::ArrayRef<double> out)
{
jlcxx::JuliaFunction f("half_julia");
std::transform(in.begin(), in.end(), out.begin(), [=](double d)
{
return jlcxx::unbox<double>(f(std::forward<double>(d)));
});
});
// Looping function calling Julia cfunction
mod.method("half_loop_cfunc!",
[](jlcxx::ArrayRef<double> in, jlcxx::ArrayRef<double> out, double(*f)(const double))
{
std::transform(in.begin(), in.end(), out.begin(), f);
});
}
// Test for string conversion. Pointer to this function is passed to Julia as-is.
std::string concatenate_numbers(int i, double d)
{
std::stringstream stream;
stream << i << d;
return stream.str();
}
std::string concatenate_strings(const int n, std::string s, const std::string& s2)
{
std::string result;
for(int i = 0; i != n; ++i)
{
result += s + s2;
}
return result;
}
double g_test_double = 0.0;
double& get_test_double_ref()
{
return g_test_double;
}
double get_test_double()
{
return g_test_double;
}
struct BoxedNumber
{
BoxedNumber(int n = 0) : m_number(n)
{
++BoxedNumber::m_nb_created;
}
BoxedNumber(const BoxedNumber& other) : m_number(other.m_number)
{
++BoxedNumber::m_nb_created;
}
~BoxedNumber()
{
++BoxedNumber::m_nb_deleted;
}
int getnumber() const
{
return m_number;
}
int m_number = 0;
static int m_nb_created;
static int m_nb_deleted;
};
int BoxedNumber::m_nb_created = 0;
int BoxedNumber::m_nb_deleted = 0;
JLCXX_MODULE init_test_module(jlcxx::Module& mod)
{
mod.add_type<BoxedNumber>("BoxedNumber")
.constructor<int>()
.method("getnumber", &BoxedNumber::getnumber);
mod.method("boxednumber_nb_created", [] () { return BoxedNumber::m_nb_created; });
mod.method("boxednumber_nb_deleted", [] () { return BoxedNumber::m_nb_deleted; });
mod.method("concatenate_numbers", &concatenate_numbers);
mod.method("concatenate_numbers_with_named_args", &concatenate_numbers, jlcxx::arg("i"), jlcxx::arg("d"));
// wrong number of arguments doesn't compile
// mod.method("concatenate_numbers_with_kwargs", &concatenate_numbers, jlcxx::kwarg("i"));
mod.method("concatenate_numbers_with_kwargs", &concatenate_numbers, jlcxx::kwarg("i"), jlcxx::kwarg("d"));
mod.method("concatenate_numbers_with_default_values", &concatenate_numbers, jlcxx::arg("i"), jlcxx::arg("d")=5.2);
mod.method("concatenate_numbers_with_default_values_of_different_type", &concatenate_numbers, jlcxx::arg("i"), jlcxx::arg("d")=5);
mod.method("concatenate_numbers_with_default_kwarg", &concatenate_numbers, jlcxx::arg("i"), jlcxx::kwarg("d")=5.2);
mod.method("concatenate_strings", &concatenate_strings);
mod.method("test_int32_array", test_int32_array);
mod.method("test_int64_array", test_int64_array);
mod.method("test_float_array", test_float_array);
mod.method("test_double_array", test_double_array);
mod.method("test_exception", test_exception, jlcxx::calling_policy::std_function);
mod.method("test_array_len", test_array_len);
mod.method("test_array_set", test_array_set);
mod.method("test_array_get", test_array_get);
mod.method("test_type_name", test_type_name);
mod.method("test_long_long", test_long_long);
mod.method("test_short", test_short);
mod.method("test_julia_call", [](double a, double b)
{
jlcxx::JuliaFunction julia_max("max");
return julia_max(std::forward<double>(a), std::forward<double>(b)); // std::forward here ensures a and b are passed by value
});
mod.method("test_julia_call_any", [](jl_value_t* x)
{
jlcxx::JuliaFunction identity("identity");
return identity(x);
});
mod.method("test_string_array", [](jlcxx::ArrayRef<std::string> arr)
{
return arr[0] == "first" && arr[1] == "second" && *(arr.begin()) == "first" && *(++arr.begin()) == "second";
});
mod.method("test_append_array!", [](jlcxx::ArrayRef<double> arr)
{
arr.push_back(3.);
});
// Typed callback
mod.method("test_safe_cfunction", [](jlcxx::SafeCFunction f_data)
{
auto f = jlcxx::make_function_pointer<double(double,double)>(f_data);
std::cout << "callback result for function " << f_data.fptr << " is " << f(1.,2.) << std::endl;
if(f(1.,2.) != 3.)
{
throw std::runtime_error("Incorrect callback result, expected 3");
}
});
// Typed callback, using a pointer
mod.method("test_safe_cfunction2", [](double(*f)(double,double))
{
std::cout << "callback result for function " << (void*)f << " is " << f(1.,2.) << std::endl;
if(f(1.,2.) != 3.)
{
throw std::runtime_error("Incorrect callback result, expected 3");
}
});
mod.method("test_safe_cfunction3", [](void (*f) (const double* const, jlcxx::cxxint_t))
{
static const double arr[] = {1.0, 2.0};
f(arr,2);
});
mod.method("fn_clb", [](double (*fnClb)(jl_value_t*, jl_value_t*))
{
std::vector<double> v{1., 2.};
auto ar = jlcxx::ArrayRef<double, 1>(v.data(), v.size());
jl_value_t* boxed_str = jlcxx::box<std::wstring>(std::wstring(L"calledFromCPP"));
JL_GC_PUSH1(&boxed_str);
fnClb((jl_value_t *)ar.wrapped(), boxed_str);
JL_GC_POP();
});
mod.method("fn_clb2", [] (jl_function_t* f)
{
std::vector<double> v{1., 2.};
auto ar = jlcxx::ArrayRef<double, 1>(v.data(), v.size());
jlcxx::JuliaFunction fnClb(f);
fnClb((jl_value_t*)ar.wrapped(), std::wstring(L"calledFromCPP"));
});
mod.method("callback_byval", [] (jl_function_t* f, int& result)
{
jlcxx::JuliaFunction juliafunc(f);
juliafunc(BoxedNumber(1), result);
});
mod.method("callback_byref", [] (jl_function_t* f, int& result)
{
jlcxx::JuliaFunction juliafunc(f);
BoxedNumber n(2);
juliafunc(n, result);
});
mod.method("callback_byptr", [] (jl_function_t* f, int& result)
{
jlcxx::JuliaFunction juliafunc(f);
BoxedNumber n(3);
juliafunc(&n, result);
});
// Write to reference
mod.method("test_double_ref", [](double& d) { d = 1.0; });
mod.method("get_test_double_ref", get_test_double_ref);
mod.method("get_test_double", get_test_double);
// Const string return
mod.method("test_const_string_return", []() -> const std::string { return "test"; });
mod.method("test_datatype_conversion", [] (jlcxx::SingletonType<double>) { return jl_float64_type; });
mod.method("test_double_pointer", [] () { return static_cast<double*>(nullptr); });
mod.method("test_double2_pointer", [] () { return static_cast<double**>(nullptr); });
mod.method("test_double3_pointer", [] () { return static_cast<double***>(nullptr); });
// Val
mod.method("test_val", [](jlcxx::Val<int, 1>) { return 1; });
mod.method("test_val", [](jlcxx::Val<int, 2>) { return jlcxx::Val<int, 2>::jl_value(); });
mod.method("test_val", [](jlcxx::Val<short, 3>) { return 3; });
mod.method("test_val", [](jlcxx::Val<int, 4>) { return jlcxx::Val<int, 4>(); });
JLCXX_VAL_SYM cst_sym_1 = "A";
JLCXX_VAL_SYM cst_sym_2 = "B";
JLCXX_VAL_SYM cst_sym_3 = "C";
mod.method("test_val", [](jlcxx::ValSym<cst_sym_1>) { return (jl_value_t*) jl_symbol("A"); });
mod.method("test_val", [](jlcxx::ValSym<cst_sym_2>) { return jlcxx::ValSym<cst_sym_2>::jl_value(); });
mod.method("test_val", [](jlcxx::ValSym<cst_sym_3>) { return jlcxx::ValSym<cst_sym_3>(); });
// wstring
mod.method("test_wstring_to_julia", [] () { return std::wstring(L"šČô_φ_привет_일보"); });
mod.method("test_wstring_to_cpp", [] (const std::wstring& ws) { return ws == L"šČô_φ_привет_일보"; });
// complex
mod.method("real_part", [](std::complex<double> c) { return c.real(); } );
mod.method("imag_part", [](const std::complex<double>& c) { return c.imag(); } );
mod.method("make_complex", [](const float a, const float b) { return std::complex<float>(a,b); });
// Irrational
mod.method("process_irrational", [] (const double irr, const double fact) { return irr*fact; });
static jl_value_t* marked_value;
mod.method("marked_boxed_value", [&] () -> jl_value_t*
{
marked_value = jlcxx::create<BoxedNumber>(43).value;
jlcxx::protect_from_gc(marked_value);
return marked_value;
});
mod.method("unmark_boxed", [&] () { jlcxx::unprotect_from_gc(marked_value); });
mod.method("open", [] (const std::string& s) { return s; });
mod.method("boolref", [] (bool& b)
{
b = !b;
});
}
}