added support for native methods & member function pointers.
fixed test cases arrays_dimensionless & cpp_basic. Added new example (functor). git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9472 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
7d7a9a387e
commit
2af7e4b447
11 changed files with 279 additions and 75 deletions
|
|
@ -54,7 +54,7 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* Overloaded operator support
|
||||
* ----------------------------------------------------------------------------- */
|
||||
// lua likes to call the + operator '__add'
|
||||
// lua calls the + operator '__add'
|
||||
// python likes to call it '__add__'
|
||||
// Assuming most SWIGers will probably use the __add__ if they extend their classes
|
||||
// we have two sets of renames
|
||||
|
|
@ -73,13 +73,13 @@
|
|||
%rename(__unm) *::operator-();
|
||||
%rename(__unm) *::operator-() const;
|
||||
|
||||
%rename(__eq) *::operator==; // note: Lua does not have a not equal
|
||||
%rename(__eq) *::operator==;
|
||||
%ignore *::operator!=; // note: Lua does not have a notequal operator
|
||||
// it just uses 'not (a==b)'
|
||||
%ignore *::operator!=;
|
||||
%rename(__lt) *::operator<; // ditto less than vs greater than
|
||||
%ignore *::operator>;
|
||||
%rename(__le) *::operator<=; // ditto less than vs greater than
|
||||
%ignore *::operator>=;
|
||||
%rename(__lt) *::operator<;
|
||||
%ignore *::operator>; // ditto less than vs greater than
|
||||
%rename(__le) *::operator<=;
|
||||
%ignore *::operator>=; // ditto less than vs greater than
|
||||
|
||||
%rename(__call) *::operator(); // the fn call operator
|
||||
|
||||
|
|
|
|||
|
|
@ -67,20 +67,32 @@ typedef struct swig_lua_class {
|
|||
struct swig_lua_class **bases;
|
||||
} swig_lua_class;
|
||||
|
||||
/* this is the struct for wrappering all pointers in SwigLua
|
||||
*/
|
||||
typedef struct {
|
||||
void *ptr;
|
||||
swig_type_info *type;
|
||||
int own; /* 1 if owned & must be destroyed */
|
||||
void *ptr;
|
||||
} swig_lua_userdata;
|
||||
|
||||
/* this is the struct for wrappering arbitary packed binary data
|
||||
(currently it is only used for member fn pointers)
|
||||
the data ordering is similar to swig_lua_userdata, but its currently not possible
|
||||
to tell the two structres apart within Swig, other than by looking at the type
|
||||
*/
|
||||
typedef struct {
|
||||
swig_type_info *type;
|
||||
int own; /* 1 if owned & must be destroyed */
|
||||
char data[1]; /* arbitary amount of data */
|
||||
} swig_lua_rawdata;
|
||||
|
||||
/* Common SWIG API */
|
||||
#define SWIG_NewPointerObj(L, ptr, type, owner) \
|
||||
SWIG_Lua_NewPointerObj(L, (void *)ptr, type, owner)
|
||||
#define SWIG_ConvertPtr(L,idx, ptr, type, flags) \
|
||||
SWIG_Lua_ConvertPtr(L,idx,ptr,type,flags)
|
||||
#define SWIG_MustGetPtr(L,idx, type,flags, argnum,fnname) \
|
||||
SWIG_Lua_MustGetPtr(L,idx, type,flags, argnum,fnname)
|
||||
#define SWIG_NewPointerObj(L, ptr, type, owner) SWIG_Lua_NewPointerObj(L, (void *)ptr, type, owner)
|
||||
#define SWIG_ConvertPtr(L,idx, ptr, type, flags) SWIG_Lua_ConvertPtr(L,idx,ptr,type,flags)
|
||||
#define SWIG_MustGetPtr(L,idx, type,flags, argnum,fnname) SWIG_Lua_MustGetPtr(L,idx, type,flags, argnum,fnname)
|
||||
/* for C++ member pointers, ie, member methods */
|
||||
#define SWIG_ConvertMember(L, idx, ptr, sz, ty) SWIG_Lua_ConvertPacked(L, idx, ptr, sz, ty)
|
||||
#define SWIG_NewMemberObj(L, ptr, sz, type) SWIG_Lua_NewPackedObj(L, ptr, sz, type)
|
||||
|
||||
/* Runtime API */
|
||||
#define SWIG_GetModule(clientdata) SWIG_Lua_GetModule((lua_State*)(clientdata))
|
||||
|
|
@ -111,9 +123,9 @@ typedef struct {
|
|||
#define SWIG_isptrtype(L,I) (lua_isuserdata(L,I) || lua_isnil(L,I))
|
||||
|
||||
#ifdef __cplusplus
|
||||
/* Special helper for member function pointers */
|
||||
#define SWIG_as_voidptr(a) const_cast< void * >(static_cast< const void * >(a))
|
||||
#define SWIG_as_voidptrptr(a) ((void)SWIG_as_voidptr(*a),reinterpret_cast< void** >(a))
|
||||
/* Special helper for member function pointers
|
||||
it gets the address, casts it, then dereferences it */
|
||||
#define SWIG_mem_fn_as_voidptr(a) (*((char**)&(a)))
|
||||
#endif
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
|
|
@ -482,18 +494,9 @@ SWIGINTERN void SWIG_Lua_class_register(lua_State* L,swig_lua_class* clss)
|
|||
* Class/structure conversion fns
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
/* pushes a new object into the lua stack */
|
||||
SWIGRUNTIME void SWIG_Lua_NewPointerObj(lua_State* L,void* ptr,swig_type_info *type, int own)
|
||||
/* helper to add metatable to new lua object */
|
||||
SWIGINTERN void _SWIG_Lua_AddMetatable(lua_State* L,swig_type_info *type)
|
||||
{
|
||||
swig_lua_userdata* usr;
|
||||
if (!ptr){
|
||||
lua_pushnil(L);
|
||||
return;
|
||||
}
|
||||
usr=(swig_lua_userdata*)lua_newuserdata(L,sizeof(swig_lua_userdata)); /* get data */
|
||||
usr->ptr=ptr; /* set the ptr */
|
||||
usr->type=type;
|
||||
usr->own=own;
|
||||
if (type->clientdata) /* there is clientdata: so add the metatable */
|
||||
{
|
||||
SWIG_Lua_get_class_metatable(L,((swig_lua_class*)(type->clientdata))->name);
|
||||
|
|
@ -508,6 +511,21 @@ SWIGRUNTIME void SWIG_Lua_NewPointerObj(lua_State* L,void* ptr,swig_type_info *t
|
|||
}
|
||||
}
|
||||
|
||||
/* pushes a new object into the lua stack */
|
||||
SWIGRUNTIME void SWIG_Lua_NewPointerObj(lua_State* L,void* ptr,swig_type_info *type, int own)
|
||||
{
|
||||
swig_lua_userdata* usr;
|
||||
if (!ptr){
|
||||
lua_pushnil(L);
|
||||
return;
|
||||
}
|
||||
usr=(swig_lua_userdata*)lua_newuserdata(L,sizeof(swig_lua_userdata)); /* get data */
|
||||
usr->ptr=ptr; /* set the ptr */
|
||||
usr->type=type;
|
||||
usr->own=own;
|
||||
_SWIG_Lua_AddMetatable(L,type); /* add metatable */
|
||||
}
|
||||
|
||||
/* takes a object from the lua stack & converts it into an object of the correct type
|
||||
(if possible) */
|
||||
SWIGRUNTIME int SWIG_Lua_ConvertPtr(lua_State* L,int index,void** ptr,swig_type_info *type,int flags)
|
||||
|
|
@ -544,6 +562,33 @@ SWIGRUNTIME void* SWIG_Lua_MustGetPtr(lua_State* L,int index,swig_type_info *typ
|
|||
return result;
|
||||
}
|
||||
|
||||
/* pushes a packed userdata. user for member fn pointers only */
|
||||
SWIGRUNTIME void SWIG_Lua_NewPackedObj(lua_State* L,void* ptr,size_t size,swig_type_info *type)
|
||||
{
|
||||
swig_lua_rawdata* raw;
|
||||
assert(ptr); /* not acceptable to pass in a NULL value */
|
||||
raw=(swig_lua_rawdata*)lua_newuserdata(L,sizeof(swig_lua_rawdata)-1+size); /* alloc data */
|
||||
raw->type=type;
|
||||
raw->own=0;
|
||||
memcpy(raw->data,ptr,size); /* copy the data */
|
||||
_SWIG_Lua_AddMetatable(L,type); /* add metatable */
|
||||
}
|
||||
|
||||
/* converts a packed userdata. user for member fn pointers only */
|
||||
SWIGRUNTIME int SWIG_Lua_ConvertPacked(lua_State* L,int index,void* ptr,size_t size,swig_type_info *type)
|
||||
{
|
||||
swig_lua_rawdata* raw;
|
||||
swig_cast_info *cast;
|
||||
raw=(swig_lua_rawdata*)lua_touserdata(L,index); /* get data */
|
||||
if (!raw) return SWIG_ERROR; /* error */
|
||||
if (type==0 || type==raw->type) /* void* or identical type */
|
||||
{
|
||||
memcpy(ptr,raw->data,size); /* copy it */
|
||||
return SWIG_OK; /* ok */
|
||||
}
|
||||
return SWIG_ERROR; /* error */
|
||||
}
|
||||
|
||||
/* lua callable function to get the userdata's type */
|
||||
SWIGRUNTIME int SWIG_Lua_type(lua_State* L)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -119,26 +119,15 @@
|
|||
|
||||
// passing objects by value
|
||||
// SWIG expects the object pointer (the $<ype argp)
|
||||
// then dereferences it
|
||||
// then dereferences it to get the object
|
||||
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE ($<ype argp)
|
||||
%{
|
||||
if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,0))){
|
||||
if (!SWIG_IsOK(SWIG_ConvertPtr(L,$input,(void**)&argp,$&descriptor,0))){
|
||||
SWIG_fail_ptr("$symname",$argnum,$descriptor);
|
||||
}
|
||||
$1 = *argp;
|
||||
%}
|
||||
|
||||
// member function pointer
|
||||
// currently does not work as C++ doesn't like casting member fn pointer to void*
|
||||
/*%typemap(in,checkfn="lua_isuserdata") SWIGTYPE (CLASS::*)
|
||||
%{
|
||||
if (SWIG_ConvertPtr(L,$input,(void**)(&$1),$descriptor,0) == -1)
|
||||
SWIG_fail_ptr("$symname",$argnum,$descriptor);
|
||||
%}
|
||||
|
||||
%typemap(out) SWIGTYPE (CLASS::*)
|
||||
%{SWIG_NewPointerObj(L,SWIG_as_voidptr($1),$descriptor,$owner); SWIG_arg++; %}*/
|
||||
|
||||
// Primitive types--return by value
|
||||
// must make a new object, copy the data & return the new object
|
||||
#ifdef __cplusplus
|
||||
|
|
@ -173,6 +162,24 @@
|
|||
%}
|
||||
#endif*/
|
||||
|
||||
// member function pointer
|
||||
// a member fn ptr is not 4 bytes like a normal pointer, but 8 bytes
|
||||
// so the standard wrappering cannot be done
|
||||
// nor can you cast a member function pointer to a void* (obviously)
|
||||
#ifdef __cplusplus
|
||||
%typemap(in,checkfn="lua_isuserdata") SWIGTYPE (CLASS::*)
|
||||
%{
|
||||
if (!SWIG_IsOK(SWIG_ConvertMember(L,$input,(void*)(&$1),sizeof($type),$descriptor)))
|
||||
SWIG_fail_ptr("$symname",$argnum,$descriptor);
|
||||
%}
|
||||
|
||||
%typemap(out) SWIGTYPE (CLASS::*)
|
||||
%{
|
||||
SWIG_NewMemberObj(L,(void*)(&$1),sizeof($type),$descriptor); SWIG_arg++;
|
||||
%}
|
||||
#endif
|
||||
|
||||
|
||||
// void (must be empty without the SWIG_arg++)
|
||||
%typemap(out) void "";
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue