From ebbae30d7253158d6ea978cbda0d166a703673eb Mon Sep 17 00:00:00 2001 From: Mark Gossage Date: Mon, 13 Feb 2006 02:49:55 +0000 Subject: [PATCH] added correct void* support git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8810 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- CHANGES.current | 2 + Lib/lua/_std_common.i | 95 +++++++++++++++++++++++++++++++++++++++++++ Lib/lua/lua.swg | 9 +++- Lib/lua/luarun.swg | 2 +- 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 Lib/lua/_std_common.i diff --git a/CHANGES.current b/CHANGES.current index f5cec8aee..16ef1651b 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -4,4 +4,6 @@ Version 1.3.29 (In progress) 02/13/2006: mgossage [Documents] updated the extending documents to give a skeleton swigging code with a few typemaps. + [Lua] added an extra typemap for void* [in], so a function which requires a void* + can take any kind of pointer diff --git a/Lib/lua/_std_common.i b/Lib/lua/_std_common.i new file mode 100644 index 000000000..3b30ae215 --- /dev/null +++ b/Lib/lua/_std_common.i @@ -0,0 +1,95 @@ +/*********************************************************************** + * _std_stl.i + * + * std::helpers for LUA + * + * Author : Mark Gossage (mark@gossage.cjb.net) + ************************************************************************/ + +%include // the general exepctions + +/* +The basic idea here, is instead of trying to feed SWIG all the +horribly templated STL code, to give it a neatened version. + +These %defines cover some of the more common methods +so the class declarations become just a set of %defines + +*/ + +/* #define for basic container features +note: I allow front(), back() & pop_back() to throw execptions +upon empty containers, rather than coredump +(as we have'nt defined the methods, we can use %extend to add with +new features) + +*/ +%define %STD_CONTAINER_METHODS(CLASS,T) +public: + CLASS(); + CLASS(const CLASS&); + unsigned int size() const; + unsigned int max_size() const; + bool empty() const; + void clear(); + %extend { // the extra stuff which must be checked + T front()const throw (std::out_of_range){ // only read front & back + if (self->empty()) + throw std::out_of_range("in "#CLASS"::front()"); + return self->front(); + } + T back()const throw (std::out_of_range){ // not write to them + if (self->empty()) + throw std::out_of_range("in "#CLASS"::back()"); + return self->back(); + } + } +%enddef + +/* push/pop for front/back +also note: front & back are read only methods, not used for writing +*/ +%define %STD_FRONT_ACCESS_METHODS(CLASS,T) +public: + void push_front(const T& val); + %extend { // must check this + void pop_front() throw (std::out_of_range){ + if (self->empty()) + throw std::out_of_range("in "#CLASS"::pop_front()"); + self->pop_back(); + } + } +%enddef + +%define %STD_BACK_ACCESS_METHODS(CLASS,T) +public: + void push_back(const T& val); + %extend { // must check this + void pop_back() throw (std::out_of_range){ + if (self->empty()) + throw std::out_of_range("in "#CLASS"::pop_back()"); + self->pop_back(); + } + } +%enddef + +/* +Random access methods +*/ +%define %STD_RANDOM_ACCESS_METHODS(CLASS,T) + %extend // this is a extra bit of SWIG code + { + // [] is replaced by __getitem__ & __setitem__ + // simply throws a string, which causes a lua error + T __getitem__(unsigned int idx) throw (std::out_of_range){ + if (idx>=self->size()) + throw std::out_of_range("in "#CLASS"::__getitem__()"); + return (*self)[idx]; + } + void __setitem__(unsigned int idx,const T& val) throw (std::out_of_range){ + if (idx>=self->size()) + throw std::out_of_range("in "#CLASS"::__setitem__()"); + (*self)[idx]=val; + } + }; +%enddef diff --git a/Lib/lua/lua.swg b/Lib/lua/lua.swg index 5ad280954..12cc55209 100644 --- a/Lib/lua/lua.swg +++ b/Lib/lua/lua.swg @@ -89,7 +89,6 @@ %typemap(out) const char& %{ lua_pushfstring(L,"%c",*$1); SWIG_arg++;%} - // pointers and references %typemap(in,checkfn="lua_isuserdata") SWIGTYPE*,SWIGTYPE&,SWIGTYPE[] %{$1=($1_ltype)SWIG_MustGetPtr(L,$input,$descriptor,0,$argnum,"$symname");%} @@ -129,6 +128,13 @@ // void (must be empty without the SWIG_arg++) %typemap(out) void ""; +/* void* is a special case +A function void fn(void*) should take any kind of pointer as a parameter (just like C/C++ does) +but if its an output, then it should be wrappered like any other SWIG object (using default typemap) +*/ +%typemap(in,checkfn="lua_isuserdata") void* +%{$1=((swig_lua_userdata*)(lua_touserdata(L,$input)))->ptr;%} + /* ----------------------------------------------------------------------------- * constants typemaps @@ -253,6 +259,7 @@ SWIG_NUMBER_BY_CONST_REF(float); SWIG_NUMBER_BY_CONST_REF(double); SWIG_NUMBER_BY_CONST_REF(enum SWIGTYPE); + // Also needed for object ptrs by const ref // eg const A* ref_pointer(A* const& a); // found in mixed_types.i diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg index a154d2970..dd156dad2 100644 --- a/Lib/lua/luarun.swg +++ b/Lib/lua/luarun.swg @@ -524,7 +524,7 @@ SWIGRUNTIME void* SWIG_Lua_MustGetPtr(lua_State* L,int index,swig_type_info *typ void* result; if (SWIG_ConvertPtr(L,index,&result,type,flags)){ lua_pushfstring(L,"Error in %s, expected a %s at argument number %d\n", - func_name,type->str?type->str:"void*",argnum); + func_name,(type && type->str)?type->str:"void*",argnum); lua_error(L); } return result;