]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/cppbind_test.cxx
May MSVC likes this more...
[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   size_t 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 size_t 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
54 typedef boost::shared_ptr<Base> BasePtr;
55
56 struct DoubleDerived2:
57   public Derived
58 {
59   const BasePtr& getBase() const{return _base;}
60   BasePtr _base;
61 };
62
63 typedef boost::shared_ptr<Derived> DerivedPtr;
64 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
65 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
66
67 naRef to_nasal_helper(naContext c, const BasePtr& base)
68 {
69   return nasal::Ghost<BasePtr>::create(c, base);
70 }
71
72 naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
73 naRef f_derivedGetX(naContext c, const Derived& d)
74 {
75   return nasal::to_nasal(c, d.getX());
76 }
77
78 int main(int argc, char* argv[])
79 {
80   naContext c = naNewContext();
81   naRef r;
82
83   using namespace nasal;
84
85   r = to_nasal(c, "Test");
86   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
87   VERIFY( from_nasal<std::string>(c, r) == "Test" );
88
89   r = to_nasal(c, std::string("Test"));
90   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
91   VERIFY( from_nasal<std::string>(c, r) == "Test" );
92
93   r = to_nasal(c, 42);
94   VERIFY( naNumValue(r).num == 42 );
95   VERIFY( from_nasal<int>(c, r) == 42 );
96
97   r = to_nasal(c, 4.2f);
98   VERIFY( naNumValue(r).num == 4.2f );
99   VERIFY( from_nasal<float>(c, r) == 4.2f );
100
101   float test_data[3] = {0, 4, 2};
102   r = to_nasal(c, test_data);
103
104   SGVec2f vec(0,2);
105   r = to_nasal(c, vec);
106   VERIFY( from_nasal<SGVec2f>(c, r) == vec );
107
108   std::vector<int> std_vec;
109   r = to_nasal(c, std_vec);
110
111   r = to_nasal(c, "string");
112   try
113   {
114     from_nasal<int>(c, r);
115
116     std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
117     return 1;
118   }
119   catch(nasal::bad_nasal_cast&)
120   {}
121
122   Hash hash(c);
123   hash.set("vec", r);
124   hash.set("vec2", vec);
125   hash.set("name", "my-name");
126   hash.set("string", std::string("blub"));
127
128   r = to_nasal(c, hash);
129   VERIFY( naIsHash(r) );
130
131   VERIFY( hash.get<std::string>("name") == "my-name" );
132   VERIFY( naIsString(hash.get("name")) );
133
134   Hash mod = hash.createHash("mod");
135   mod.set("parent", hash);
136
137   //----------------------------------------------------------------------------
138   // Test exposing classes to Nasal
139   //----------------------------------------------------------------------------
140
141   Ghost<BasePtr>::init("BasePtr")
142     .method("member", &Base::member)
143     .method("strlen", &Base::test1Arg)
144     .member("str", &Base::getString, &Base::setString)
145     .method("str_m", &Base::getString)
146     .method("void", &Base::constVoidFunc)
147     .member("var_r", &Base::getVar)
148     .member("var_w", &Base::setVar)
149     .member("var", &Base::getVar, &Base::setVar)
150     .method("void", &baseVoidFunc)
151     .method("void_c", &baseConstVoidFunc)
152     .method("int2args", &baseFunc2Args)
153     .method("bool2args", &Base::test2Args)
154     .method("str_ptr", &testPtr);
155   Ghost<DerivedPtr>::init("DerivedPtr")
156     .bases<BasePtr>()
157     .member("x", &Derived::getX, &Derived::setX)
158     .member("x_alternate", &f_derivedGetX)
159     .method("free_fn", &derivedFreeMember)
160     .method("free_member", &derivedFreeMember)
161     .method("baseDoIt", &baseFuncCallContext);
162   Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
163     .bases<DerivedPtr>();
164   Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
165     .bases< Ghost<DerivedPtr> >()
166     .member("base", &DoubleDerived2::getBase);
167
168   BasePtr d( new Derived );
169   naRef derived = Ghost<BasePtr>::create(c, d);
170   VERIFY( naIsGhost(derived) );
171   VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
172
173   BasePtr d2( new DoubleDerived );
174   derived = Ghost<BasePtr>::create(c, d2);
175   VERIFY( naIsGhost(derived) );
176   VERIFY( std::string("DoubleDerivedPtr") ==  naGhost_type(derived)->name );
177
178   BasePtr d3( new DoubleDerived2 );
179   derived = Ghost<BasePtr>::create(c, d3);
180   VERIFY( naIsGhost(derived) );
181   VERIFY( std::string("DoubleDerived2Ptr") ==  naGhost_type(derived)->name );
182
183   VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
184   VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
185   VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
186
187   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
188   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
189   VERIFY(    Ghost<DerivedPtr>::fromNasal(c, derived)
190           == boost::dynamic_pointer_cast<Derived>(d3) );
191   VERIFY(    Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
192           == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
193   VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
194
195   std::map<std::string, BasePtr> instances;
196   VERIFY( naIsHash(to_nasal(c, instances)) );
197
198   std::map<std::string, DerivedPtr> instances_d;
199   VERIFY( naIsHash(to_nasal(c, instances_d)) );
200
201   std::map<std::string, int> int_map;
202   VERIFY( naIsHash(to_nasal(c, int_map)) );
203
204   std::map<std::string, std::vector<int> > int_vector_map;
205   VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
206
207   // Check converting to Ghost if using Nasal hashes with actual ghost inside
208   // the hashes parents vector
209   std::vector<naRef> parents;
210   parents.push_back(hash.get_naRef());
211   parents.push_back(derived);
212
213   Hash obj(c);
214   obj.set("parents", parents);
215   VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
216
217   // Check recursive parents (aka parent-of-parent)
218   std::vector<naRef> parents2;
219   parents2.push_back(obj.get_naRef());
220   Hash derived_obj(c);
221   derived_obj.set("parents", parents2);
222   VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
223
224   // TODO actually do something with the ghosts...
225
226   //----------------------------------------------------------------------------
227   // Test nasal::CallContext
228   //----------------------------------------------------------------------------
229
230
231   int int_vec[] = {1,2,3};
232   std::map<std::string, std::string> map;
233   naRef args[] = {
234     to_nasal(c, std::string("test-arg")),
235     to_nasal(c, 4),
236     to_nasal(c, int_vec),
237     to_nasal(c, map)
238   };
239   CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
240   VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
241   VERIFY( cc.getArg<std::string>(0) == "test-arg" );
242   VERIFY( cc.getArg<std::string>(10) == "" );
243   VERIFY( cc.isString(0) );
244   VERIFY( !cc.isNumeric(0) );
245   VERIFY( !cc.isVector(0) );
246   VERIFY( !cc.isHash(0) );
247   VERIFY( !cc.isGhost(0) );
248   VERIFY( cc.isNumeric(1) );
249   VERIFY( cc.isVector(2) );
250   VERIFY( cc.isHash(3) );
251
252   naRef args_vec = nasal::to_nasal(c, args);
253   VERIFY( naIsVector(args_vec) );
254
255   //----------------------------------------------------------------------------
256   // Test nasal::String
257   //----------------------------------------------------------------------------
258
259   String string( to_nasal(c, "Test") );
260   VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
261   VERIFY( string.c_str() == std::string("Test") );
262   VERIFY( string.starts_with(string) );
263   VERIFY( string.starts_with(String(c, "T")) );
264   VERIFY( string.starts_with(String(c, "Te")) );
265   VERIFY( string.starts_with(String(c, "Tes")) );
266   VERIFY( string.starts_with(String(c, "Test")) );
267   VERIFY( !string.starts_with(String(c, "Test1")) );
268   VERIFY( !string.starts_with(String(c, "bb")) );
269   VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
270   VERIFY( string.find('e') == 1 );
271   VERIFY( string.find('9') == String::npos );
272   VERIFY( string.find_first_of(String(c, "st")) == 2 );
273   VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
274   VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
275   VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
276   VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
277   VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
278   VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );
279
280   naFreeContext(c);
281
282   return 0;
283 }