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