-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathinheritance.cpp
More file actions
279 lines (226 loc) · 7.07 KB
/
inheritance.cpp
File metadata and controls
279 lines (226 loc) · 7.07 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
#include <string>
#include <memory>
#include "jlcxx/jlcxx.hpp"
#include "jlcxx/functions.hpp"
// Dummy base class to test multiple inheritance.
// See https://stackoverflow.com/questions/5445105/conversion-from-void-to-the-pointer-of-the-base-class
struct FirstBase
{
int allyourbasebelongtous;
virtual ~FirstBase() {}
};
struct A
{
virtual std::string message() const = 0;
virtual ~A() {}
std::string data = "mydata";
};
struct B : FirstBase, A
{
virtual std::string message() const
{
return "B";
}
virtual ~B() {}
};
struct C : B
{
C() { this->data = "C"; }
virtual std::string message() const
{
return "C";
}
};
struct D : FirstBase, A
{
virtual std::string message() const
{
return "D";
}
};
B b;
A& create_abstract()
{
b = B();
return b;
}
std::string take_ref(A& a)
{
return a.message();
}
// Static inheritance test (issue #156)
struct StaticBase {
};
struct StaticDerived: public StaticBase {
};
// A virtual C++ class that we will extend from Julia
class VirtualCpp
{
public:
VirtualCpp(int size, double val) : m_data(size)
{
for (int i = 0; i < size; ++i)
{
m_data[i] = val;
}
}
virtual ~VirtualCpp() {}
virtual double virtualfunc() = 0;
protected:
std::vector<double> m_data;
};
class VirtualCppJuliaExtended : public VirtualCpp
{
public:
VirtualCppJuliaExtended(int size, double val) : VirtualCpp(size, val)
{
}
virtual ~VirtualCppJuliaExtended() {}
virtual double virtualfunc()
{
jlcxx::JuliaFunction cb(m_callback);
// Apply cb to each element of m_data and return the sum of the result
double sum = 0;
for (size_t i = 0; i < m_data.size(); ++i)
{
sum += jlcxx::unbox<double>(cb(static_cast<double>(m_data[i]))); // Without the static_cast<double> it would be a reference to a double
}
return sum;
}
void set_callback(jl_value_t* callback)
{
m_callback = callback;
}
private:
jl_value_t* m_callback = nullptr;
};
class VirtualCfunctionExtended : public VirtualCpp
{
using callback_t = double (*)(double);
public:
VirtualCfunctionExtended(int size, double val) : VirtualCpp(size, val)
{
}
virtual ~VirtualCfunctionExtended() {}
std::vector<double>& getData()
{
return m_data;
}
virtual double virtualfunc()
{
double sum = 0;
for (size_t i = 0; i < m_data.size(); ++i)
{
sum += m_callback(m_data[i]);
}
return sum;
}
void set_callback(jlcxx::SafeCFunction callback)
{
m_callback = jlcxx::make_function_pointer<double(double)>(callback);
}
private:
callback_t m_callback = nullptr;
};
// Example based on https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977
namespace virtualsolver
{
typedef double (*history_f) (double);
class Base
{
virtual double history(double) = 0;
public:
void solve(){
for (int i=0;i<3;i++) {
std::cout<<history((double) i)<<" \n";
}
}
virtual ~Base() {}
};
class E: public Base
{
double history(double x){return x;}
};
class F: public Base
{
public:
F(history_f h){f=h;}
double history(double x){return f(x);}
history_f f;
};
}
struct Parent {
virtual std::string name() const { return "Parent"; }
virtual ~Parent() = default;
};
struct DerivedA : public Parent {
std::string name() const override { return "DerivedA"; }
};
struct DerivedB : public Parent {
std::string name() const override { return "DerivedB"; }
};
Parent* make_a() { return new DerivedA(); }
Parent* make_b() { return new DerivedB(); }
const Parent* make_const_a() { return new const DerivedA(); }
const Parent* make_const_b() { return new const DerivedB(); }
namespace jlcxx
{
// Needed for upcasting
template<> struct SuperType<D> { typedef A type; };
template<> struct SuperType<C> { typedef B type; };
template<> struct SuperType<B> { typedef A type; };
template<> struct SuperType<VirtualCppJuliaExtended> { typedef VirtualCpp type; };
template<> struct SuperType<VirtualCfunctionExtended> { typedef VirtualCpp type; };
template<> struct SuperType<virtualsolver::E> { typedef virtualsolver::Base type; };
template<> struct SuperType<virtualsolver::F> { typedef virtualsolver::Base type; };
template<> struct IsMirroredType<StaticBase> : std::false_type { };
template<> struct IsMirroredType<StaticDerived> : std::false_type { };
template<> struct SuperType<StaticDerived> { typedef StaticBase type; };
template<> struct SuperType<DerivedA> { typedef Parent type; };
template<> struct SuperType<DerivedB> { typedef Parent type; };
}
JLCXX_MODULE define_types_module(jlcxx::Module& types)
{
types.add_type<A>("A").method("message", &A::message);
types.add_type<B>("B", jlcxx::julia_base_type<A>());
types.add_type<C>("C", jlcxx::julia_base_type<B>());
types.add_type<D>("D", jlcxx::julia_base_type<A>());
types.method("create_abstract", create_abstract);
types.method("shared_b", []() { return std::make_shared<B>(); });
types.method("shared_c", []() { return std::make_shared<C>(); });
types.method("shared_d", []() { return std::make_shared<const D>(); });
types.method("shared_ptr_message", [](const std::shared_ptr<const A>& x) { return x->message(); });
types.method("weak_ptr_message_a", [](const std::weak_ptr<const A>& x) { return x.lock()->message(); });
types.method("weak_ptr_message_b", [](const std::weak_ptr<B>& x) { return x.lock()->message(); });
types.method("dynamic_message_c", [](const A& c) { return dynamic_cast<const C*>(&c)->data; });
types.method("take_ref", take_ref);
types.add_type<StaticBase>("StaticBase");
types.add_type<StaticDerived>("StaticDerived", jlcxx::julia_base_type<StaticBase>());
types.add_type<VirtualCpp>("VirtualCpp")
.method("virtualfunc", &VirtualCpp::virtualfunc);
types.add_type<VirtualCppJuliaExtended>("VirtualCppJuliaExtended", jlcxx::julia_base_type<VirtualCpp>())
.constructor<int, double>()
.method("set_callback", &VirtualCppJuliaExtended::set_callback);
types.add_type<VirtualCfunctionExtended>("VirtualCfunctionExtended", jlcxx::julia_base_type<VirtualCpp>())
.constructor<int, double>()
.method("getData", &VirtualCfunctionExtended::getData)
.method("set_callback", &VirtualCfunctionExtended::set_callback);
types.add_type<Parent>("Parent")
.method("name", &Parent::name);
types.add_type<DerivedA>("DerivedA", jlcxx::julia_base_type<Parent>())
.method("name", &DerivedA::name);
types.add_type<DerivedB>("DerivedB", jlcxx::julia_base_type<Parent>())
.method("name", &DerivedB::name);
types.method("make_a", &make_a);
types.method("make_b", &make_b);
types.method("make_const_a", &make_const_a);
types.method("make_const_b", &make_const_b);
}
JLCXX_MODULE define_vsolver_module(jlcxx::Module& vsolver_mod)
{
vsolver_mod.add_type<virtualsolver::Base>("BaseV")
.method("solve", &virtualsolver::Base::solve);
vsolver_mod.add_type<virtualsolver::E>("E", jlcxx::julia_base_type<virtualsolver::Base>());
vsolver_mod.add_type<virtualsolver::F>("F", jlcxx::julia_base_type<virtualsolver::Base>())
.constructor<virtualsolver::history_f>();
}