-
Notifications
You must be signed in to change notification settings - Fork 43
/
inheritance.cpp
148 lines (120 loc) · 3.62 KB
/
inheritance.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
#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 {
};
// 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;
};
}
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<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; };
}
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>());
}
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>();
}