2 #include "NasalHash.hxx"
4 #include <boost/shared_ptr.hpp>
12 std::cerr << "failed: line " << __LINE__ << ": " << #a << std::endl; \
18 naRef member(const nasal::CallContext&) { return naNil(); }
25 int getX() const { return _x; }
26 void setX(int x) { _x = x; }
33 struct DoubleDerived2:
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;
44 naRef member(Derived&, const nasal::CallContext&) { return naNil(); }
46 int main(int argc, char* argv[])
48 naContext c = naNewContext();
51 using namespace nasal;
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" );
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" );
62 VERIFY( naNumValue(r).num == 42 );
63 VERIFY( from_nasal<int>(c, r) == 42 );
65 r = to_nasal(c, 4.2f);
66 VERIFY( naNumValue(r).num == 4.2f );
67 VERIFY( from_nasal<float>(c, r) == 4.2f );
72 r = to_nasal(c, "string");
75 from_nasal<int>(c, r);
77 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
80 catch(nasal::bad_nasal_cast&)
85 hash.set("vec2", vec);
86 hash.set("name", "my-name");
87 hash.set("string", std::string("blub"));
89 r = to_nasal(c, hash);
90 VERIFY( naIsHash(r) );
92 VERIFY( hash.get<std::string>("name") == "my-name" );
93 VERIFY( naIsString(hash.get("name")) );
95 Hash mod = hash.createHash("mod");
96 mod.set("parent", hash);
98 Ghost<Base>::init("Base")
99 .method<&Base::member>("member");
100 Ghost<Derived>::init("Derived")
102 .member("x", &Derived::getX, &Derived::setX)
103 .method_func<&member>("free_member");
105 naRef derived = Ghost<Derived>::create(c);
106 VERIFY( naIsGhost(derived) );
107 VERIFY( std::string("Derived") == naGhost_type(derived)->name );
109 Ghost<BasePtr>::init("BasePtr");
110 Ghost<DerivedPtr>::init("DerivedPtr")
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>();
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 );
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 );
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 );
134 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
135 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
136 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
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) );
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);
153 obj.set("parents", parents);
154 VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
157 to_nasal(c, std::string("test-arg"))
159 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
160 VERIFY( cc.require<std::string>(0) == "test-arg" );
161 VERIFY( cc.get<std::string>(0) == "test-arg" );
162 VERIFY( cc.get<std::string>(1) == "" );
164 // TODO actually do something with the ghosts...