Nasal cppbind: support more member function types

This commit is contained in:
Thomas Geymayer 2013-02-28 23:43:42 +01:00
parent 7dd83dd2d1
commit e20cc6a90f
4 changed files with 41 additions and 18 deletions

View File

@ -357,15 +357,15 @@ namespace nasal
* @param setter Setter for variable (Pass 0 to prevent write access)
*
*/
template<class Var>
template<class Ret, class Param>
Ghost& member( const std::string& field,
Var (raw_type::*getter)() const,
void (raw_type::*setter)(typename boost::call_traits<Var>::param_type) = 0 )
Ret (raw_type::*getter)() const,
void (raw_type::*setter)(Param) )
{
member_t m;
if( getter )
{
typedef typename boost::call_traits<Var>::param_type param_type;
typedef typename boost::call_traits<Ret>::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<Param>::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<class Ret>
Ghost& member( const std::string& field,
Ret (raw_type::*getter)() const )
{
return member<Ret, Ret>(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<Var>(field, 0, setter);
return member<Var, Var>(field, 0, setter);
}
/**

View File

@ -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<BasePtr>::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<DerivedPtr>::init("DerivedPtr")
.bases<BasePtr>()
.member("x", &Derived::getX, &Derived::setX)

View File

@ -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");

View File

@ -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>,
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<class T>
std::vector<T> from_nasal_helper(naContext c, naRef ref, std::vector<T>*)
std::vector<T>
from_nasal_helper(naContext c, naRef ref, const std::vector<T>*)
{
if( !naIsVector(ref) )
throw bad_nasal_cast("Not a vector");
@ -132,7 +134,7 @@ namespace nasal
*/
template<class Vec2>
typename boost::enable_if<is_vec2<Vec2>, Vec2>::type
from_nasal_helper(naContext c, naRef ref, Vec2*)
from_nasal_helper(naContext c, naRef ref, const Vec2*)
{
std::vector<double> vec =
from_nasal_helper(c, ref, static_cast<std::vector<double>*>(0));