A
Adam Dziendziel
Hi all!
I'm writing a luabind/boost:ython-like binding utility for a Squirrel
language to generating wrapper-functions for C++ classes and have a
problem with passing the pointer-to-member argument and deducing the
type of them.
I want to make a library which I can use like that:
class_<fs:ath, sq_filesystem::ttag>(v, "path", no_base)
.constructor<const char*>()
.def("native_file_string", &fs:ath::native_file_string)
;
I didn't have problems with contructor - I've just write a specialized
constructor for each type:
// base template
template<typename Class, int TypeTag, typename Sig>
struct sq_constructor {};
// const char* constructor template
template<class Class, int TypeTag>
struct sq_constructor<Class, TypeTag, const char*>
{
static int implementation(HSQUIRRELVM v)
{
if (sq_gettype(v, 2) == OT_STRING)
{
const char * sz;
sq_getstring(v, 2, &sz);
Class * o = new Class(sz);
sq_setinstanceup(v, 1, o);
return 0;
}
else
return sq_throwerror(v, "invalid param - string expected");
};
};
and implement a _class::method:
template<class Class, unsigned int TypeTag>
class class_
{
// adding a constructor
template<class Sig>
inline class_& constructor()
{
sq_register("constructor",
sq_constructor<Class, TypeTag, Sig>::implementation
}
(...)
};
But with creating method wrapper I have a problem. When I wrote:
template<typename Class, int TypeTag, typename Sig, Sig Method>
struct sq_method {};
template<class Class, int TypeTag, std::string (Class::*Method)() const>
struct sq_method<Class, TypeTag, std::string (Class::*)() const, Method>
{
SQUADD_FUNC_PARAMS(0, 1, ".")
static int implementation(HSQUIRRELVM v)
{
Class * o;
SQUADD_SETUP_POINTER(o, 1)
// sq_pushstring(v, o->*Method().c_str(), -1);
return 1;
};
};
template<class Class, unsigned int TypeTag>
class class_
{
// adding a method
template<class Sig>
inline class_& def(const SQChar * name, Sig func)
{
sq_register(name, sq_method<Class, TypeTag, Sig, func>::implementation);
);
};
I had error in class_::def() on template resolution:
`func' is not a valid template argument
error: it must be a pointer-to-member of
the form `&X::Y'
and "`<type error>' is not a class type"
What I'm doing wrong?
I'm writing a luabind/boost:ython-like binding utility for a Squirrel
language to generating wrapper-functions for C++ classes and have a
problem with passing the pointer-to-member argument and deducing the
type of them.
I want to make a library which I can use like that:
class_<fs:ath, sq_filesystem::ttag>(v, "path", no_base)
.constructor<const char*>()
.def("native_file_string", &fs:ath::native_file_string)
;
I didn't have problems with contructor - I've just write a specialized
constructor for each type:
// base template
template<typename Class, int TypeTag, typename Sig>
struct sq_constructor {};
// const char* constructor template
template<class Class, int TypeTag>
struct sq_constructor<Class, TypeTag, const char*>
{
static int implementation(HSQUIRRELVM v)
{
if (sq_gettype(v, 2) == OT_STRING)
{
const char * sz;
sq_getstring(v, 2, &sz);
Class * o = new Class(sz);
sq_setinstanceup(v, 1, o);
return 0;
}
else
return sq_throwerror(v, "invalid param - string expected");
};
};
and implement a _class::method:
template<class Class, unsigned int TypeTag>
class class_
{
// adding a constructor
template<class Sig>
inline class_& constructor()
{
sq_register("constructor",
sq_constructor<Class, TypeTag, Sig>::implementation
}
(...)
};
But with creating method wrapper I have a problem. When I wrote:
template<typename Class, int TypeTag, typename Sig, Sig Method>
struct sq_method {};
template<class Class, int TypeTag, std::string (Class::*Method)() const>
struct sq_method<Class, TypeTag, std::string (Class::*)() const, Method>
{
SQUADD_FUNC_PARAMS(0, 1, ".")
static int implementation(HSQUIRRELVM v)
{
Class * o;
SQUADD_SETUP_POINTER(o, 1)
// sq_pushstring(v, o->*Method().c_str(), -1);
return 1;
};
};
template<class Class, unsigned int TypeTag>
class class_
{
// adding a method
template<class Sig>
inline class_& def(const SQChar * name, Sig func)
{
sq_register(name, sq_method<Class, TypeTag, Sig, func>::implementation);
);
};
I had error in class_::def() on template resolution:
`func' is not a valid template argument
error: it must be a pointer-to-member of
the form `&X::Y'
and "`<type error>' is not a class type"
What I'm doing wrong?