struct Base
{
- naRef member(naContext, int, naRef*) { return naNil(); }
+ naRef member(const nasal::CallContext&) { return naNil(); }
virtual ~Base(){};
+
+ std::string getString() const { return ""; }
+ void setString(const std::string&) {}
};
struct Derived:
public Base
};
-naRef member(Derived&, naContext, int, naRef*) { return naNil(); }
+typedef boost::shared_ptr<Base> BasePtr;
+typedef boost::shared_ptr<Derived> DerivedPtr;
+typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
+typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
+
+naRef member(Derived&, const nasal::CallContext&) { return naNil(); }
int main(int argc, char* argv[])
{
r = to_nasal(c, hash);
VERIFY( naIsHash(r) );
+ VERIFY( hash.get<std::string>("name") == "my-name" );
+ VERIFY( naIsString(hash.get("name")) );
+
Hash mod = hash.createHash("mod");
mod.set("parent", hash);
Ghost<Base>::init("Base")
- .method<&Base::member>("member");
+ .method<&Base::member>("member")
+ .member("str", &Base::getString, &Base::setString);
Ghost<Derived>::init("Derived")
.bases<Base>()
.member("x", &Derived::getX, &Derived::setX)
VERIFY( naIsGhost(derived) );
VERIFY( std::string("Derived") == naGhost_type(derived)->name );
- typedef boost::shared_ptr<Base> BasePtr;
- typedef boost::shared_ptr<Derived> DerivedPtr;
- typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
- typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
-
Ghost<BasePtr>::init("BasePtr");
Ghost<DerivedPtr>::init("DerivedPtr")
- .bases<BasePtr>();
+ .bases<BasePtr>()
+ .member("x", &Derived::getX, &Derived::setX)
+ .method_func<&member>("free_member");
Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
.bases<DerivedPtr>();
Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
VERIFY( naIsGhost(derived) );
VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
- // TODO actuall do something with the ghosts...
+ VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
+ VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
+ VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
+
+ VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
+ VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
+ VERIFY( Ghost<DerivedPtr>::fromNasal(c, derived)
+ == boost::dynamic_pointer_cast<Derived>(d3) );
+ VERIFY( Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
+ == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
+ VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
+
+ // Check converting to Ghost if using Nasal hashes with actual ghost inside
+ // the hashes parents vector
+ std::vector<naRef> parents;
+ parents.push_back(hash.get_naRef());
+ parents.push_back(derived);
+
+ Hash obj(c);
+ obj.set("parents", parents);
+ VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
+
+ // Check recursive parents (aka parent-of-parent)
+ std::vector<naRef> parents2;
+ parents2.push_back(obj.get_naRef());
+ Hash derived_obj(c);
+ derived_obj.set("parents", parents2);
+ VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
+
+ naRef args[] = {
+ to_nasal(c, std::string("test-arg"))
+ };
+ CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
+ VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
+ VERIFY( cc.getArg<std::string>(0) == "test-arg" );
+ VERIFY( cc.getArg<std::string>(1) == "" );
+
+ // TODO actually do something with the ghosts...
naFreeContext(c);