cppbind: Prepare for improved bindings.
- Improved Nasal/C++ bindings will follow. For now just test if all compilers are happy with intended approach. - Add to_nasal overload for std::map<std::string, T>.
This commit is contained in:
parent
5e45bdeeda
commit
530de4d809
@ -197,12 +197,14 @@ namespace nasal
|
||||
BOOST_STATIC_ASSERT( internal::has_element_type<T>::value );
|
||||
|
||||
public:
|
||||
typedef typename T::element_type raw_type;
|
||||
typedef T pointer;
|
||||
typedef typename T::element_type raw_type;
|
||||
typedef T pointer;
|
||||
typedef naRef (raw_type::*member_func_t)(const CallContext&);
|
||||
typedef naRef (*free_func_t)(raw_type&, const CallContext&);
|
||||
typedef boost::function<naRef(naContext, raw_type&)> getter_t;
|
||||
typedef boost::function<void(naContext, raw_type&, naRef)> setter_t;
|
||||
typedef boost::function<naRef(naContext, raw_type&)> getter_t;
|
||||
typedef boost::function<void(naContext, raw_type&, naRef)> setter_t;
|
||||
typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
|
||||
typedef boost::shared_ptr<method_t> method_ptr;
|
||||
|
||||
/**
|
||||
* A ghost member. Can consist either of getter and/or setter functions
|
||||
@ -466,6 +468,83 @@ namespace nasal
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke a method which returns a value and convert it to Nasal.
|
||||
*/
|
||||
template<class Ret>
|
||||
static
|
||||
typename boost::disable_if<boost::is_void<Ret>, naRef>::type
|
||||
method_invoker
|
||||
(
|
||||
const boost::function<Ret (raw_type&, const CallContext&)>& func,
|
||||
raw_type& obj,
|
||||
const CallContext& ctx
|
||||
)
|
||||
{
|
||||
typedef typename boost::call_traits<Ret>::param_type param_type;
|
||||
naRef (*to_nasal_)(naContext, param_type) = &nasal::to_nasal;
|
||||
|
||||
return to_nasal_(ctx.c, func(obj, ctx));
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoke a method which returns void and "convert" it to nil.
|
||||
*/
|
||||
template<class Ret>
|
||||
static
|
||||
typename boost::enable_if<boost::is_void<Ret>, naRef>::type
|
||||
method_invoker
|
||||
(
|
||||
const boost::function<void (raw_type&, const CallContext&)>& func,
|
||||
raw_type& obj,
|
||||
const CallContext& ctx
|
||||
)
|
||||
{
|
||||
func(obj, ctx);
|
||||
return naNil();
|
||||
};
|
||||
|
||||
/**
|
||||
* Bind any callable entity as method callable from Nasal
|
||||
*
|
||||
* Does not really register method yet!!!
|
||||
*/
|
||||
template<class Ret>
|
||||
Ghost& method
|
||||
(
|
||||
const std::string& name,
|
||||
const boost::function<Ret (raw_type&, const CallContext&)>& func
|
||||
)
|
||||
{
|
||||
// _members[name].func.reset
|
||||
// (
|
||||
new method_t( boost::bind(method_invoker<Ret>, func, _1, _2) );
|
||||
// );
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Ret>
|
||||
struct method_raw
|
||||
{
|
||||
typedef boost::function<Ret (raw_type&, const CallContext&)> type;
|
||||
};
|
||||
|
||||
/**
|
||||
* Bind member function as method callable from Nasal
|
||||
*
|
||||
* Does not really register method yet!!!
|
||||
*/
|
||||
template<class Ret>
|
||||
Ghost& method( const std::string& name,
|
||||
Ret (raw_type::*fn)() const )
|
||||
{
|
||||
return method<Ret>
|
||||
(
|
||||
name,
|
||||
typename method_raw<Ret>::type(boost::bind(fn, _1))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a free function as member function. The object instance is
|
||||
* passed as additional first argument.
|
||||
|
@ -23,11 +23,16 @@ struct Base
|
||||
|
||||
std::string getString() const { return ""; }
|
||||
void setString(const std::string&) {}
|
||||
void constVoidFunc() const {}
|
||||
|
||||
std::string var;
|
||||
const std::string& getVar() const { return var; }
|
||||
void setVar(const std::string v) { var = v; }
|
||||
};
|
||||
|
||||
void baseVoidFunc(Base& b) {}
|
||||
void baseConstVoidFunc(const Base& b) {}
|
||||
|
||||
struct Derived:
|
||||
public Base
|
||||
{
|
||||
@ -51,6 +56,11 @@ typedef boost::shared_ptr<Derived> DerivedPtr;
|
||||
typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
|
||||
typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
|
||||
|
||||
naRef to_nasal(naContext c, const BasePtr& base)
|
||||
{
|
||||
return nasal::Ghost<BasePtr>::create(c, base);
|
||||
}
|
||||
|
||||
naRef member(Derived&, const nasal::CallContext&) { return naNil(); }
|
||||
naRef f_derivedGetX(naContext c, const Derived& d)
|
||||
{
|
||||
@ -140,9 +150,12 @@ int main(int argc, char* argv[])
|
||||
Ghost<BasePtr>::init("BasePtr")
|
||||
.method<&Base::member>("member")
|
||||
.member("str", &Base::getString, &Base::setString)
|
||||
.method("str_m", &Base::getString)
|
||||
.method("void", &Base::constVoidFunc)
|
||||
.member("var_r", &Base::getVar)
|
||||
.member("var_w", &Base::setVar)
|
||||
.member("var", &Base::getVar, &Base::setVar);
|
||||
.member("var", &Base::getVar, &Base::setVar)
|
||||
/*.method("void", &baseVoidFunc)*/;
|
||||
Ghost<DerivedPtr>::init("DerivedPtr")
|
||||
.bases<BasePtr>()
|
||||
.member("x", &Derived::getX, &Derived::setX)
|
||||
@ -180,6 +193,18 @@ int main(int argc, char* argv[])
|
||||
== boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
|
||||
VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
|
||||
|
||||
std::map<std::string, BasePtr> instances;
|
||||
VERIFY( naIsHash(to_nasal(c, instances)) );
|
||||
|
||||
std::map<std::string, DerivedPtr> instances_d;
|
||||
VERIFY( naIsHash(to_nasal(c, instances_d)) );
|
||||
|
||||
std::map<std::string, int> int_map;
|
||||
VERIFY( naIsHash(to_nasal(c, int_map)) );
|
||||
|
||||
std::map<std::string, std::vector<int> > int_vector_map;
|
||||
VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
|
||||
|
||||
// Check converting to Ghost if using Nasal hashes with actual ghost inside
|
||||
// the hashes parents vector
|
||||
std::vector<naRef> parents;
|
||||
|
@ -25,8 +25,10 @@
|
||||
#include <simgear/nasal/nasal.h>
|
||||
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/call_traits.hpp>
|
||||
#include <boost/type_traits.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -74,18 +76,24 @@ namespace nasal
|
||||
return naNum(num);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a 2d vector to Nasal vector with 2 elements
|
||||
*/
|
||||
template<class Vec2>
|
||||
typename boost::enable_if<is_vec2<Vec2>, naRef>::type
|
||||
to_nasal(naContext c, const Vec2& vec);
|
||||
|
||||
/**
|
||||
* Convert a std::map to a Nasal Hash
|
||||
*/
|
||||
template<class Value>
|
||||
naRef to_nasal(naContext c, const std::map<std::string, Value>& map);
|
||||
|
||||
/**
|
||||
* Convert a fixed size array to a Nasal vector
|
||||
*/
|
||||
template<class T, size_t N>
|
||||
naRef to_nasal(naContext c, const T(&array)[N])
|
||||
{
|
||||
naRef ret = naNewVector(c);
|
||||
naVec_setsize(c, ret, N);
|
||||
for(size_t i = 0; i < N; ++i)
|
||||
naVec_set(ret, i, to_nasal(c, array[i]));
|
||||
return ret;
|
||||
}
|
||||
naRef to_nasal(naContext c, const T(&array)[N]);
|
||||
|
||||
/**
|
||||
* Convert std::vector to Nasal vector
|
||||
@ -108,9 +116,7 @@ namespace nasal
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a 2d vector to Nasal vector with 2 elements
|
||||
*/
|
||||
//----------------------------------------------------------------------------
|
||||
template<class Vec2>
|
||||
typename boost::enable_if<is_vec2<Vec2>, naRef>::type
|
||||
to_nasal(naContext c, const Vec2& vec)
|
||||
@ -121,6 +127,37 @@ namespace nasal
|
||||
return to_nasal(c, nasal_vec);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
template<class Value>
|
||||
naRef to_nasal(naContext c, const std::map<std::string, Value>& map)
|
||||
{
|
||||
naRef hash = naNewHash(c);
|
||||
|
||||
typedef typename boost::call_traits<Value>::param_type param_type;
|
||||
typedef typename std::map<std::string, Value>::const_iterator map_iterator;
|
||||
|
||||
for( map_iterator it = map.begin(); it != map.end(); ++it )
|
||||
naHash_set
|
||||
(
|
||||
hash,
|
||||
to_nasal(c, it->first),
|
||||
to_nasal(c, static_cast<param_type>(it->second))
|
||||
);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
template<class T, size_t N>
|
||||
naRef to_nasal(naContext c, const T(&array)[N])
|
||||
{
|
||||
naRef ret = naNewVector(c);
|
||||
naVec_setsize(c, ret, N);
|
||||
for(size_t i = 0; i < N; ++i)
|
||||
naVec_set(ret, i, to_nasal(c, array[i]));
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace nasal
|
||||
|
||||
#endif /* SG_TO_NASAL_HXX_ */
|
||||
|
Loading…
Reference in New Issue
Block a user