From e20cc6a90fe39541b25e6b6f78c565e4dd668c7c Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Thu, 28 Feb 2013 23:43:42 +0100 Subject: [PATCH] Nasal cppbind: support more member function types --- simgear/nasal/cppbind/Ghost.hxx | 26 ++++++++++++++++----- simgear/nasal/cppbind/cppbind_test.cxx | 9 ++++++- simgear/nasal/cppbind/from_nasal.cxx | 6 ++--- simgear/nasal/cppbind/from_nasal_detail.hxx | 18 +++++++------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index 37cd749e..2d672097 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -357,15 +357,15 @@ namespace nasal * @param setter Setter for variable (Pass 0 to prevent write access) * */ - template + template Ghost& member( const std::string& field, - Var (raw_type::*getter)() const, - void (raw_type::*setter)(typename boost::call_traits::param_type) = 0 ) + Ret (raw_type::*getter)() const, + void (raw_type::*setter)(Param) ) { member_t m; if( getter ) { - typedef typename boost::call_traits::param_type param_type; + typedef typename boost::call_traits::param_type param_type; naRef (*to_nasal_)(naContext, param_type) = &nasal::to_nasal; // Getter signature: naRef(naContext, raw_type&) @@ -374,7 +374,8 @@ namespace nasal if( setter ) { - Var (*from_nasal_)(naContext, naRef) = &nasal::from_nasal; + typename boost::remove_reference::type + (*from_nasal_)(naContext, naRef) = &nasal::from_nasal; // Setter signature: void(naContext, raw_type&, naRef) m.setter = boost::bind(setter, _2, boost::bind(from_nasal_, _1, _3)); @@ -383,6 +384,19 @@ namespace nasal return member(field, m.getter, m.setter); } + /** + * Register a read only member variable. + * + * @param field Name of member + * @param getter Getter for variable + */ + template + Ghost& member( const std::string& field, + Ret (raw_type::*getter)() const ) + { + return member(field, getter, 0); + } + /** * Register a write only member variable. * @@ -393,7 +407,7 @@ namespace nasal Ghost& member( const std::string& field, void (raw_type::*setter)(Var) ) { - return member(field, 0, setter); + return member(field, 0, setter); } /** diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index 422f3d70..18a282ce 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -23,6 +23,10 @@ struct Base std::string getString() const { return ""; } void setString(const std::string&) {} + + std::string var; + const std::string& getVar() const { return var; } + void setVar(const std::string v) { var = v; } }; struct Derived: public Base @@ -135,7 +139,10 @@ int main(int argc, char* argv[]) Ghost::init("BasePtr") .method<&Base::member>("member") - .member("str", &Base::getString, &Base::setString); + .member("str", &Base::getString, &Base::setString) + .member("var_r", &Base::getVar) + .member("var_w", &Base::setVar) + .member("var", &Base::getVar, &Base::setVar); Ghost::init("DerivedPtr") .bases() .member("x", &Derived::getX, &Derived::setX) diff --git a/simgear/nasal/cppbind/from_nasal.cxx b/simgear/nasal/cppbind/from_nasal.cxx index 2eb1e1f4..d1e48eaa 100644 --- a/simgear/nasal/cppbind/from_nasal.cxx +++ b/simgear/nasal/cppbind/from_nasal.cxx @@ -50,7 +50,7 @@ namespace nasal } //---------------------------------------------------------------------------- - std::string from_nasal_helper(naContext c, naRef ref, std::string*) + std::string from_nasal_helper(naContext c, naRef ref, const std::string*) { naRef na_str = naStringValue(c, ref); return std::string(naStr_data(na_str), naStr_len(na_str)); @@ -63,7 +63,7 @@ namespace nasal } //---------------------------------------------------------------------------- - Hash from_nasal_helper(naContext c, naRef ref, Hash*) + Hash from_nasal_helper(naContext c, naRef ref, const Hash*) { if( !naIsHash(ref) ) throw bad_nasal_cast("Not a hash"); @@ -72,7 +72,7 @@ namespace nasal } //---------------------------------------------------------------------------- - String from_nasal_helper(naContext c, naRef ref, String*) + String from_nasal_helper(naContext c, naRef ref, const String*) { if( !naIsString(ref) ) throw bad_nasal_cast("Not a string"); diff --git a/simgear/nasal/cppbind/from_nasal_detail.hxx b/simgear/nasal/cppbind/from_nasal_detail.hxx index 7eb0c966..6ff72f25 100644 --- a/simgear/nasal/cppbind/from_nasal_detail.hxx +++ b/simgear/nasal/cppbind/from_nasal_detail.hxx @@ -71,27 +71,28 @@ namespace nasal /** * Simple pass through for unified handling also of naRef. */ - inline naRef from_nasal_helper(naContext, naRef ref, naRef*) { return ref; } + inline naRef from_nasal_helper(naContext, naRef ref, const naRef*) + { return ref; } /** * Convert Nasal string to std::string */ - std::string from_nasal_helper(naContext c, naRef ref, std::string*); + std::string from_nasal_helper(naContext c, naRef ref, const std::string*); /** * Convert a Nasal string to an SGPath */ - SGPath from_nasal_helper(naContext c, naRef ref, SGPath*); + SGPath from_nasal_helper(naContext c, naRef ref, const SGPath*); /** * Convert a Nasal hash to a nasal::Hash */ - Hash from_nasal_helper(naContext c, naRef ref, Hash*); + Hash from_nasal_helper(naContext c, naRef ref, const Hash*); /** * Convert a Nasal string to a nasal::String */ - String from_nasal_helper(naContext c, naRef ref, String*); + String from_nasal_helper(naContext c, naRef ref, const String*); /** * Convert a Nasal number to a C++ numeric type @@ -100,7 +101,7 @@ namespace nasal typename boost::enable_if< boost::is_arithmetic, T >::type - from_nasal_helper(naContext c, naRef ref, T*) + from_nasal_helper(naContext c, naRef ref, const T*) { naRef num = naNumValue(ref); if( !naIsNum(num) ) @@ -113,7 +114,8 @@ namespace nasal * Convert a Nasal vector to a std::vector */ template - std::vector from_nasal_helper(naContext c, naRef ref, std::vector*) + std::vector + from_nasal_helper(naContext c, naRef ref, const std::vector*) { if( !naIsVector(ref) ) throw bad_nasal_cast("Not a vector"); @@ -132,7 +134,7 @@ namespace nasal */ template typename boost::enable_if, Vec2>::type - from_nasal_helper(naContext c, naRef ref, Vec2*) + from_nasal_helper(naContext c, naRef ref, const Vec2*) { std::vector vec = from_nasal_helper(c, ref, static_cast*>(0)); -- 2.39.5