]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/cppbind_test.cxx
Nasal String wrapper and allow adding methods to string objects.
[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 };
27 struct Derived:
28   public Base
29 {
30   int _x;
31   int getX() const { return _x; }
32   void setX(int x) { _x = x; }
33 };
34 struct DoubleDerived:
35   public Derived
36 {
37
38 };
39 struct DoubleDerived2:
40   public Derived
41 {
42
43 };
44
45 typedef boost::shared_ptr<Base> BasePtr;
46 typedef boost::shared_ptr<Derived> DerivedPtr;
47 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
48 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
49
50 naRef member(Derived&, const nasal::CallContext&) { return naNil(); }
51 naRef f_derivedGetX(naContext c, const Derived& d)
52 {
53   return nasal::to_nasal(c, d.getX());
54 }
55
56 int main(int argc, char* argv[])
57 {
58   naContext c = naNewContext();
59   naRef r;
60
61   using namespace nasal;
62
63   r = to_nasal(c, "Test");
64   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
65   VERIFY( from_nasal<std::string>(c, r) == "Test" );
66
67   r = to_nasal(c, std::string("Test"));
68   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
69   VERIFY( from_nasal<std::string>(c, r) == "Test" );
70
71   r = to_nasal(c, 42);
72   VERIFY( naNumValue(r).num == 42 );
73   VERIFY( from_nasal<int>(c, r) == 42 );
74
75   r = to_nasal(c, 4.2f);
76   VERIFY( naNumValue(r).num == 4.2f );
77   VERIFY( from_nasal<float>(c, r) == 4.2f );
78
79   float test_data[3] = {0, 4, 2};
80   r = to_nasal(c, test_data);
81
82   SGVec2f vec(0,2);
83   r = to_nasal(c, vec);
84   VERIFY( from_nasal<SGVec2f>(c, r) == vec );
85
86   std::vector<int> std_vec;
87   r = to_nasal(c, std_vec);
88
89   r = to_nasal(c, "string");
90   try
91   {
92     from_nasal<int>(c, r);
93
94     std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
95     return 1;
96   }
97   catch(nasal::bad_nasal_cast&)
98   {}
99
100   Hash hash(c);
101   hash.set("vec", r);
102   hash.set("vec2", vec);
103   hash.set("name", "my-name");
104   hash.set("string", std::string("blub"));
105
106   r = to_nasal(c, hash);
107   VERIFY( naIsHash(r) );
108
109   VERIFY( hash.get<std::string>("name") == "my-name" );
110   VERIFY( naIsString(hash.get("name")) );
111
112   Hash mod = hash.createHash("mod");
113   mod.set("parent", hash);
114
115   String string( to_nasal(c, "Test") );
116   VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
117   VERIFY( string.c_str() == std::string("Test") );
118   VERIFY( string.starts_with(string) );
119   VERIFY( string.starts_with(String(c, "T")) );
120   VERIFY( string.starts_with(String(c, "Te")) );
121   VERIFY( string.starts_with(String(c, "Tes")) );
122   VERIFY( string.starts_with(String(c, "Test")) );
123   VERIFY( !string.starts_with(String(c, "Test1")) );
124   VERIFY( !string.starts_with(String(c, "bb")) );
125   VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
126   VERIFY( string.find('e') == 1 );
127   VERIFY( string.find('9') == String::npos );
128   VERIFY( string.find_first_of(String(c, "st")) == 2 );
129   VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
130   VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
131   VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
132   VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
133   VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
134   VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );
135
136   Ghost<BasePtr>::init("BasePtr")
137     .method<&Base::member>("member")
138     .member("str", &Base::getString, &Base::setString);
139   Ghost<DerivedPtr>::init("DerivedPtr")
140     .bases<BasePtr>()
141     .member("x", &Derived::getX, &Derived::setX)
142     .member("x_alternate", &f_derivedGetX)
143     .method_func<&member>("free_member");
144   Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
145     .bases<DerivedPtr>();
146   Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
147     .bases< Ghost<DerivedPtr> >();
148
149   BasePtr d( new Derived );
150   naRef derived = Ghost<BasePtr>::create(c, d);
151   VERIFY( naIsGhost(derived) );
152   VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
153
154   BasePtr d2( new DoubleDerived );
155   derived = Ghost<BasePtr>::create(c, d2);
156   VERIFY( naIsGhost(derived) );
157   VERIFY( std::string("DoubleDerivedPtr") ==  naGhost_type(derived)->name );
158
159   BasePtr d3( new DoubleDerived2 );
160   derived = Ghost<BasePtr>::create(c, d3);
161   VERIFY( naIsGhost(derived) );
162   VERIFY( std::string("DoubleDerived2Ptr") ==  naGhost_type(derived)->name );
163
164   VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
165   VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
166   VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
167
168   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
169   VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
170   VERIFY(    Ghost<DerivedPtr>::fromNasal(c, derived)
171           == boost::dynamic_pointer_cast<Derived>(d3) );
172   VERIFY(    Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
173           == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
174   VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
175
176   // Check converting to Ghost if using Nasal hashes with actual ghost inside
177   // the hashes parents vector
178   std::vector<naRef> parents;
179   parents.push_back(hash.get_naRef());
180   parents.push_back(derived);
181
182   Hash obj(c);
183   obj.set("parents", parents);
184   VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
185
186   // Check recursive parents (aka parent-of-parent)
187   std::vector<naRef> parents2;
188   parents2.push_back(obj.get_naRef());
189   Hash derived_obj(c);
190   derived_obj.set("parents", parents2);
191   VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
192
193   naRef args[] = {
194     to_nasal(c, std::string("test-arg"))
195   };
196   CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
197   VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
198   VERIFY( cc.getArg<std::string>(0) == "test-arg" );
199   VERIFY( cc.getArg<std::string>(1) == "" );
200
201   naRef args_vec = nasal::to_nasal(c, args);
202   VERIFY( naIsVector(args_vec) );
203
204   // TODO actually do something with the ghosts...
205
206   naFreeContext(c);
207
208   return 0;
209 }