From: Thomas Geymayer Date: Sun, 18 May 2014 14:24:24 +0000 (+0200) Subject: cppbind.Ghost: clean up a bit X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c967fbc0a6173a254e93c82373bbe24b268c387d;p=simgear.git cppbind.Ghost: clean up a bit --- diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index e27eb066..43ed7d0a 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -217,15 +217,15 @@ namespace nasal typedef T pointer; typedef naRef (raw_type::*member_func_t)(const CallContext&); typedef naRef (*free_func_t)(raw_type&, const CallContext&); - typedef boost::function getter_t; - typedef boost::function setter_t; + typedef boost::function getter_t; + typedef boost::function setter_t; typedef boost::function method_t; - typedef boost::function fallback_getter_t; - typedef boost::function fallback_getter_t; + typedef boost::function fallback_setter_t; @@ -527,6 +527,20 @@ namespace nasal return *this; } + /** + * Register a function which is called upon retrieving an unknown member + * of this ghost, and convert it to the given @a Param type. + */ + template + Ghost& _get( const boost::function& getter ) + { + return _get(boost::bind( + convert_param_invoker, getter, _1, _2, _3, _4 + )); + } + /** * Register a method which is called upon retrieving an unknown member of * this ghost. @@ -546,9 +560,9 @@ namespace nasal template Ghost& _get(bool (raw_type::*getter)(const std::string&, Param&)) { - return _get(boost::bind( - convert_param_invoker, getter, _1, _2, _3, _4 - )); + return _get( + boost::function(getter) + ); } /** @@ -572,12 +586,7 @@ namespace nasal const std::string&, naRef& )) { - // Getter signature: bool( naContext, - // raw_type&, - // const std::string&, - // naRef& ) - - return _get( boost::bind(getter, _2, _1, _3, _4) ); + return _get( fallback_getter_t(getter) ); } /** @@ -590,6 +599,27 @@ namespace nasal return *this; } + /** + * Register a function which is called upon setting an unknown member of + * this ghost, and convert it to the given @a Param type. + */ + template + Ghost& _set(const boost::function& setter) + { + // Setter signature: bool( raw_type&, + // naContext, + // const std::string&, + // naRef ) + return _set(boost::bind( + setter, + _1, + _3, + boost::bind(from_nasal_ptr::get(), _2, _4) + )); + } + /** * Register a method which is called upon setting an unknown member of * this ghost. @@ -609,16 +639,9 @@ namespace nasal template Ghost& _set(bool (raw_type::*setter)(const std::string&, Param)) { - // Setter signature: bool( naContext, - // raw_type&, - // const std::string&, - // naRef ) - return _set(boost::bind( - setter, - _2, - _3, - boost::bind(from_nasal_ptr::get(), _1, _4) - )); + return _set( + boost::function(setter) + ); } /** @@ -642,11 +665,7 @@ namespace nasal const std::string&, naRef )) { - // Setter signature: bool( naContext, - // raw_type&, - // const std::string&, - // naRef ) - return _set( boost::bind(setter, _2, _1, _3, _4) ); + return _set( fallback_setter_t(setter) ); } /** @@ -909,24 +928,24 @@ namespace nasal typedef typename boost::call_traits::param_type param_type; naRef(*to_nasal_)(naContext, param_type) = &to_nasal; - // Getter signature: naRef(naContext, raw_type&) + // Getter signature: naRef(raw_type&, naContext) return boost::bind ( to_nasal_, - _1, - boost::bind(getter, _2) + _2, + boost::bind(getter, _1) ); } template setter_t to_setter(void (raw_type::*setter)(Param)) { - // Setter signature: void(naContext, raw_type&, naRef) + // Setter signature: void(raw_type&, naContext, naRef) return boost::bind ( setter, - _2, - boost::bind(from_nasal_ptr::get(), _1, _3) + _1, + boost::bind(from_nasal_ptr::get(), _2, _3) ); } @@ -940,8 +959,8 @@ namespace nasal const boost::function& func, - naContext c, raw_type& obj, + naContext c, const std::string& key, naRef& out ) @@ -1085,13 +1104,13 @@ namespace nasal { fallback_getter_t fallback_get = getSingletonPtr()->_fallback_getter; if( !fallback_get - || !fallback_get(c, *getRawPtr(g), key_str, *out) ) + || !fallback_get(*getRawPtr(g), c, key_str, *out) ) return 0; } else if( member->second.func ) *out = member->second.func->get_naRef(c); else if( !member->second.getter.empty() ) - *out = member->second.getter(c, *getRawPtr(g)); + *out = member->second.getter(*getRawPtr(g), c); else return "Read-protected member"; @@ -1112,7 +1131,7 @@ namespace nasal fallback_setter_t fallback_set = getSingletonPtr()->_fallback_setter; if( !fallback_set ) naRuntimeError(c, "ghost: No such member: %s", key.c_str()); - else if( !fallback_set(c, *getRawPtr(g), key, val) ) + else if( !fallback_set(*getRawPtr(g), c, key, val) ) naRuntimeError(c, "ghost: Failed to write (_set: %s)", key.c_str()); } else if( member->second.setter.empty() ) @@ -1120,7 +1139,7 @@ namespace nasal else if( member->second.func ) naRuntimeError(c, "ghost: Write to function: %s", key.c_str()); else - member->second.setter(c, *getRawPtr(g), val); + member->second.setter(*getRawPtr(g), c, val); } }; diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index 9dce3e1e..665cb0a9 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -105,7 +105,7 @@ typedef SGSharedPtr SGWeakRefBasedPtr; typedef boost::weak_ptr DerivedWeakPtr; naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); } -naRef f_derivedGetX(naContext c, const Derived& d) +naRef f_derivedGetX(const Derived& d, naContext c) { return nasal::to_nasal(c, d.getX()); }