Accessing scripted class member from C++


#1

heho!
how can i access a scripted class’s member from c++ without eval?
there has to be a nicer way than this:

engine.add(var(*s.type),"tmp");
auto m = engine.eval< map< std::string, Boxed_Value > >("tmp.handlers");
if(m.count("init")) {
	if(boxed_cast<function<bool(Entity & e)>>(m["init"])(e)) {
		s.initialized = true;
	}
}

where s.type is a

shared_ptr<Boxed_Value>

which points to an instance of a scripted class. that class has a Map member/attribute named ‘handlers’, which holds a scripted function at key ‘init’.
now i want to know if that key exists and call the function with params and return-val from and to C++.
kind of hoped something like

boxed_cast< map<string,Boxed_Value> >(s.type->get_attr("handlers"));

would work, but get_attr seems not to do what i thought it does (returns an undef in this case).


#2

i think i figured it out after running into chaiscript::dispatch::Dynamic_Object:

const chaiscript::dispatch::Dynamic_Object & o = chaiscript::boxed_cast< chaiscript::dispatch::Dynamic_Object >(*s.type);
const std::map<std::string,chaiscript::Boxed_Value> & m = chaiscript::boxed_cast< std::map<std::string,chaiscript::Boxed_Value> >(o.get_attrs()["handlers"]);

so first boxed_cast the Boxed_Value to Dynamic_Object to have the instance, then do get_attrs on that to get and boxed_cast the member/attribute.

edit:
while i can access s.type from c++ now, i cannot access it from script…
added a getType helper to s that dereferences the type-ptr and returns a const reference.
bound that to script as ‘type’. this does indeed work, a little complicated though.