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