]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/cppbind_test.cxx
One more fix for old gcc
[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(naContext, int, naRef*) { 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 naRef member(Derived&, naContext, int, naRef*) { return naNil(); }
40
41 int main(int argc, char* argv[])
42 {
43   naContext c = naNewContext();
44   naRef r;
45
46   using namespace nasal;
47
48   r = to_nasal(c, "Test");
49   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
50   VERIFY( from_nasal<std::string>(c, r) == "Test" );
51
52   r = to_nasal(c, std::string("Test"));
53   VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
54   VERIFY( from_nasal<std::string>(c, r) == "Test" );
55
56   r = to_nasal(c, 42);
57   VERIFY( naNumValue(r).num == 42 );
58   VERIFY( from_nasal<int>(c, r) == 42 );
59
60   r = to_nasal(c, 4.2f);
61   VERIFY( naNumValue(r).num == 4.2f );
62   VERIFY( from_nasal<float>(c, r) == 4.2f );
63
64   std::vector<int> vec;
65   r = to_nasal(c, vec);
66
67   r = to_nasal(c, "string");
68   try
69   {
70     from_nasal<int>(c, r);
71
72     std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
73     return 1;
74   }
75   catch(nasal::bad_nasal_cast&)
76   {}
77
78   Hash hash(c);
79   hash.set("vec", r);
80   hash.set("vec2", vec);
81   hash.set("name", "my-name");
82   hash.set("string", std::string("blub"));
83
84   r = to_nasal(c, hash);
85   VERIFY( naIsHash(r) );
86
87   Hash mod = hash.createHash("mod");
88   mod.set("parent", hash);
89
90   Ghost<Base>::init("Base")
91     .method<&Base::member>("member");
92   Ghost<Derived>::init("Derived")
93     .bases<Base>()
94     .member("x", &Derived::getX, &Derived::setX)
95     .method_func<&member>("free_member");
96
97   naRef derived = Ghost<Derived>::create(c);
98   VERIFY( naIsGhost(derived) );
99   VERIFY( std::string("Derived") ==  naGhost_type(derived)->name );
100
101   typedef boost::shared_ptr<Base> BasePtr;
102   typedef boost::shared_ptr<Derived> DerivedPtr;
103   typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
104   typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
105
106   Ghost<BasePtr>::init("BasePtr");
107   Ghost<DerivedPtr>::init("DerivedPtr")
108     .bases<BasePtr>();
109   Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
110     .bases<DerivedPtr>();
111   Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
112     .bases<DerivedPtr>();
113
114   BasePtr d( new Derived );
115   derived = Ghost<BasePtr>::create(c, d);
116   VERIFY( naIsGhost(derived) );
117   VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
118
119   BasePtr d2( new DoubleDerived );
120   derived = Ghost<BasePtr>::create(c, d2);
121   VERIFY( naIsGhost(derived) );
122   VERIFY( std::string("DoubleDerivedPtr") ==  naGhost_type(derived)->name );
123
124   BasePtr d3( new DoubleDerived2 );
125   derived = Ghost<BasePtr>::create(c, d3);
126   VERIFY( naIsGhost(derived) );
127   VERIFY( std::string("DoubleDerived2Ptr") ==  naGhost_type(derived)->name );
128
129   // TODO actuall do something with the ghosts...
130
131   naFreeContext(c);
132
133   return 0;
134 }