]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/cppbind_test.cxx
cppbind: rework to allow binding nearly everything.
[simgear.git] / simgear / nasal / cppbind / cppbind_test.cxx
1 #include <simgear/math/SGMath.hxx>
2
3 #include "Ghost.hxx"
4 #include "NasalHash.hxx"
5 #include "NasalString.hxx"
6
7 #include <boost/shared_ptr.hpp>
8
9 #include <cstring>
10 #include <iostream>
11
12 #define VERIFY(a) \
13   if( !(a) ) \
14   { \
15     std::cerr << "failed: line " << __LINE__ << ": " << #a << std::endl; \
16     return 1; \
17   }
18
19 struct Base
20 {
21   naRef member(const nasal::CallContext&) { return naNil(); }
22   virtual ~Base(){};
23
24   std::string getString() const { return ""; }
25   void setString(const std::string&) {}
26   void constVoidFunc() const {}
27   int test1Arg(const std::string& str) const { return str.length(); }
28   bool test2Args(const std::string& s, bool c) { return c && s.empty(); }
29
30   std::string var;
31   const std::string& getVar() const { return var; }
32   void setVar(const std::string v) { var = v; }
33 };
34
35 void baseVoidFunc(Base& b) {}
36 void baseConstVoidFunc(const Base& b) {}
37 int baseFunc2Args(Base& b, int x, const std::string& s) { return x + s.size(); }
38 std::string testPtr(Base& b) { return b.getString(); }
39 void baseFuncCallContext(const Base&, const nasal::CallContext&) {}
40
41 struct Derived:
42   public Base
43 {
44   int _x;
45   int getX() const { return _x; }
46   void setX(int x) { _x = x; }
47 };
48 struct DoubleDerived:
49   public Derived
50 {
51
52 };
53 struct DoubleDerived2:
54   public Derived
55 {
56
57 };
58
59 typedef boost::shared_ptr<Base> BasePtr;
60 typedef boost::shared_ptr<Derived> DerivedPtr;
61 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
62 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
63
64 naRef to_nasal(naContext c, const BasePtr& base)
65 {
66   return nasal::Ghost<BasePtr>::create(c, base);
67 }
68
69 naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
70 naRef f_derivedGetX(naContext c, const Derived& d)
71 {
72   return nasal::to_nasal(c, d.getX());
73 }
74
75 int main(int argc, char* argv[])
76 {
77   naContext c = naNewContext();
78   naRef r;
79
80   using namespace nasal;
81
82   r = to_nasal(c, "Test");
83   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
84   VERIFY( from_nasal<std::string>(c, r) == "Test" );
85
86   r = to_nasal(c, std::string("Test"));
87   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
88   VERIFY( from_nasal<std::string>(c, r) == "Test" );
89
90   r = to_nasal(c, 42);
91   VERIFY( naNumValue(r).num == 42 );
92   VERIFY( from_nasal<int>(c, r) == 42 );
93
94   r = to_nasal(c, 4.2f);
95   VERIFY( naNumValue(r).num == 4.2f );
96   VERIFY( from_nasal<float>(c, r) == 4.2f );
97
98   float test_data[3] = {0, 4, 2};
99   r = to_nasal(c, test_data);
100
101   SGVec2f vec(0,2);
102   r = to_nasal(c, vec);
103   VERIFY( from_nasal<SGVec2f>(c, r) == vec );
104
105   std::vector<int> std_vec;
106   r = to_nasal(c, std_vec);
107
108   r = to_nasal(c, "string");
109   try
110   {
111     from_nasal<int>(c, r);
112
113     std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
114     return 1;
115   }
116   catch(nasal::bad_nasal_cast&)
117   {}
118
119   Hash hash(c);
120   hash.set("vec", r);
121   hash.set("vec2", vec);
122   hash.set("name", "my-name");
123   hash.set("string", std::string("blub"));
124
125   r = to_nasal(c, hash);
126   VERIFY( naIsHash(r) );
127
128   VERIFY( hash.get<std::string>("name") == "my-name" );
129   VERIFY( naIsString(hash.get("name")) );
130
131   Hash mod = hash.createHash("mod");
132   mod.set("parent", hash);
133
134   //----------------------------------------------------------------------------
135   // Test exposing classes to Nasal
136   //----------------------------------------------------------------------------
137
138   Ghost<BasePtr>::init("BasePtr")
139     .method("member", &Base::member)
140     .method("strlen", &Base::test1Arg)
141     .member("str", &Base::getString, &Base::setString)
142     .method("str_m", &Base::getString)
143     .method("void", &Base::constVoidFunc)
144     .member("var_r", &Base::getVar)
145     .member("var_w", &Base::setVar)
146     .member("var", &Base::getVar, &Base::setVar)
147     .method("void", &baseVoidFunc)
148     .method("void_c", &baseConstVoidFunc)
149     .method("int2args", &baseFunc2Args)
150     .method("bool2args", &Base::test2Args)
151     .method("str_ptr", &testPtr);
152   Ghost<DerivedPtr>::init("DerivedPtr")
153     .bases<BasePtr>()
154     .member("x", &Derived::getX, &Derived::setX)
155     .member("x_alternate", &f_derivedGetX)
156     .method("free_fn", &derivedFreeMember)
157     .method("free_member", &derivedFreeMember)
158     .method("baseDoIt", &baseFuncCallContext);
159   Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
160     .bases<DerivedPtr>();
161   Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
162     .bases< Ghost<DerivedPtr> >();
163
164   BasePtr d( new Derived );
165   naRef derived = Ghost<BasePtr>::create(c, d);
166   VERIFY( naIsGhost(derived) );
167   VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
168
169   BasePtr d2( new DoubleDerived );
170   derived = Ghost<BasePtr>::create(c, d2);
171   VERIFY( naIsGhost(derived) );
172   VERIFY( std::string("DoubleDerivedPtr") ==  naGhost_type(derived)->name );
173
174   BasePtr d3( new DoubleDerived2 );
175   derived = Ghost<BasePtr>::create(c, d3);
176   VERIFY( naIsGhost(derived) );
177   VERIFY( std::string("DoubleDerived2Ptr") ==  naGhost_type(derived)->name );
178
179   VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
180   VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
181   VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
182
183   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
184   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
185   VERIFY(    Ghost<DerivedPtr>::fromNasal(c, derived)
186           == boost::dynamic_pointer_cast<Derived>(d3) );
187   VERIFY(    Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
188           == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
189   VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
190
191   std::map<std::string, BasePtr> instances;
192   VERIFY( naIsHash(to_nasal(c, instances)) );
193
194   std::map<std::string, DerivedPtr> instances_d;
195   VERIFY( naIsHash(to_nasal(c, instances_d)) );
196
197   std::map<std::string, int> int_map;
198   VERIFY( naIsHash(to_nasal(c, int_map)) );
199
200   std::map<std::string, std::vector<int> > int_vector_map;
201   VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
202
203   // Check converting to Ghost if using Nasal hashes with actual ghost inside
204   // the hashes parents vector
205   std::vector<naRef> parents;
206   parents.push_back(hash.get_naRef());
207   parents.push_back(derived);
208
209   Hash obj(c);
210   obj.set("parents", parents);
211   VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
212
213   // Check recursive parents (aka parent-of-parent)
214   std::vector<naRef> parents2;
215   parents2.push_back(obj.get_naRef());
216   Hash derived_obj(c);
217   derived_obj.set("parents", parents2);
218   VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
219
220   // TODO actually do something with the ghosts...
221
222   //----------------------------------------------------------------------------
223   // Test nasal::CallContext
224   //----------------------------------------------------------------------------
225
226   naRef args[] = {
227     to_nasal(c, std::string("test-arg"))
228   };
229   CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
230   VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
231   VERIFY( cc.getArg<std::string>(0) == "test-arg" );
232   VERIFY( cc.getArg<std::string>(1) == "" );
233
234   naRef args_vec = nasal::to_nasal(c, args);
235   VERIFY( naIsVector(args_vec) );
236
237   //----------------------------------------------------------------------------
238   // Test nasal::String
239   //----------------------------------------------------------------------------
240
241   String string( to_nasal(c, "Test") );
242   VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
243   VERIFY( string.c_str() == std::string("Test") );
244   VERIFY( string.starts_with(string) );
245   VERIFY( string.starts_with(String(c, "T")) );
246   VERIFY( string.starts_with(String(c, "Te")) );
247   VERIFY( string.starts_with(String(c, "Tes")) );
248   VERIFY( string.starts_with(String(c, "Test")) );
249   VERIFY( !string.starts_with(String(c, "Test1")) );
250   VERIFY( !string.starts_with(String(c, "bb")) );
251   VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
252   VERIFY( string.find('e') == 1 );
253   VERIFY( string.find('9') == String::npos );
254   VERIFY( string.find_first_of(String(c, "st")) == 2 );
255   VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
256   VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
257   VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
258   VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
259   VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
260   VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );
261
262   naFreeContext(c);
263
264   return 0;
265 }