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