Added typemap throws for std::string*, typemap for SWIGTYPE DYNAMIC, changed the existing throws typemap to throw a string instead of making a copy of the object
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9637 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
e545d7b957
commit
0cbba6842b
3 changed files with 43 additions and 28 deletions
|
|
@ -116,12 +116,10 @@
|
||||||
* Exceptions
|
* Exceptions
|
||||||
* ------------------------------------------------------------ */
|
* ------------------------------------------------------------ */
|
||||||
/* Confession: I dont really like C++ exceptions
|
/* Confession: I dont really like C++ exceptions
|
||||||
The python ones are great, but C++ ones I dont like
|
The python/lua ones are great, but C++ ones I dont like
|
||||||
(mainly because I cannot get the stack trace out of it)
|
(mainly because I cannot get the stack trace out of it)
|
||||||
Therefore I have not bothered to try doing much in this
|
Therefore I have not bothered to try doing much in this
|
||||||
|
|
||||||
On top of this I am not clear on how best to do this is Lua
|
|
||||||
|
|
||||||
Therefore currently its just enough to get a few test cases running ok
|
Therefore currently its just enough to get a few test cases running ok
|
||||||
|
|
||||||
note: if you wish to throw anything related to std::exception
|
note: if you wish to throw anything related to std::exception
|
||||||
|
|
@ -134,37 +132,41 @@ use %include <std_except.i> instead
|
||||||
long long,unsigned long long,
|
long long,unsigned long long,
|
||||||
char, unsigned char, signed char,
|
char, unsigned char, signed char,
|
||||||
enum SWIGTYPE
|
enum SWIGTYPE
|
||||||
%{lua_pushfstring(L,"numeric exception thrown of value %f",(double)$1);
|
%{lua_pushfstring(L,"numeric exception:%f",(double)$1);
|
||||||
SWIG_fail; %}
|
SWIG_fail; %}
|
||||||
|
|
||||||
// strings are just sent as errors
|
// strings are just sent as errors
|
||||||
%typemap(throws) char*, const char*
|
%typemap(throws) char*, const char*
|
||||||
%{lua_pushstring(L,$1);SWIG_fail;%}
|
%{lua_pushstring(L,$1);SWIG_fail;%}
|
||||||
|
|
||||||
// anything else is sent as an object
|
/*
|
||||||
#ifdef __cplusplus
|
Throwing object is a serious problem:
|
||||||
|
Assuming some code throws a 'FooBar'
|
||||||
|
There are a few options:
|
||||||
|
- return a pointer to it: but its unclear how long this will last for.
|
||||||
|
- return a copy of it: but not all objects are copyable
|
||||||
|
(see exception_partial_info in the test suite for a case where you cannot)
|
||||||
|
- convert to a string & throw that
|
||||||
|
its not so useful, but it works (this is more lua like).
|
||||||
|
For a more useful solution: see std_except for more details
|
||||||
|
*/
|
||||||
%typemap(throws) SWIGTYPE
|
%typemap(throws) SWIGTYPE
|
||||||
{
|
{
|
||||||
$&1_ltype resultptr;
|
lua_pushfstring(L,"object exception:%s",SWIG_TypePrettyName($1_descriptor));
|
||||||
resultptr = new $1_ltype(($1_ltype &) $1);
|
|
||||||
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
|
|
||||||
SWIG_fail;
|
SWIG_fail;
|
||||||
}
|
}
|
||||||
/* %typemap(throws) SWIGTYPE&
|
|
||||||
|
// old code: fails under exception_partial_info
|
||||||
|
// if you have a function which throws a FooBar & you want SWIG to throw the actual object
|
||||||
|
// then use
|
||||||
|
// %apply SWIGTYPE BY_VAL {FooBar};
|
||||||
|
%typemap(throws) SWIGTYPE BY_VAL
|
||||||
{
|
{
|
||||||
SWIG_NewPointerObj(L,(void *) &$1,$&1_descriptor,1);
|
SWIG_NewPointerObj(L,(void *)new $1_ltype(($1_ltype &) $1),$&1_descriptor,1);
|
||||||
SWIG_fail;
|
|
||||||
}*/
|
|
||||||
#else
|
|
||||||
%typemap(throws) SWIGTYPE
|
|
||||||
{
|
|
||||||
$&1_ltype resultptr;
|
|
||||||
resultptr = ($&1_ltype) malloc(sizeof($1_type));
|
|
||||||
memmove(resultptr, &$1, sizeof($1_type));
|
|
||||||
SWIG_NewPointerObj(L,(void *) resultptr,$&1_descriptor,1);
|
|
||||||
SWIG_fail;
|
SWIG_fail;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,15 @@
|
||||||
%typemap(out) SWIGTYPE*,SWIGTYPE&
|
%typemap(out) SWIGTYPE*,SWIGTYPE&
|
||||||
%{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
|
%{SWIG_NewPointerObj(L,$1,$descriptor,$owner); SWIG_arg++; %}
|
||||||
|
|
||||||
|
// dynamic casts
|
||||||
|
%typemap(out) SWIGTYPE *DYNAMIC,
|
||||||
|
SWIGTYPE &DYNAMIC
|
||||||
|
{
|
||||||
|
swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1);
|
||||||
|
SWIG_NewPointerObj(L,(void*)$1,ty,$owner); SWIG_arg++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// passing objects by value
|
// passing objects by value
|
||||||
// SWIG expects the object pointer (the $<ype argp)
|
// SWIG expects the object pointer (the $<ype argp)
|
||||||
// then dereferences it to get the object
|
// then dereferences it to get the object
|
||||||
|
|
|
||||||
|
|
@ -60,8 +60,12 @@ Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2
|
||||||
%typemap(out) const std::string&
|
%typemap(out) const std::string&
|
||||||
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
|
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}
|
||||||
|
|
||||||
|
// for throwing of any kind of string, string ref's and string pointers
|
||||||
|
// we convert all to lua strings
|
||||||
%typemap(throws) std::string,const std::string&
|
%typemap(throws) std::string,const std::string&
|
||||||
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%}
|
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_fail;%}
|
||||||
|
%typemap(throws) std::string*,const std::string*
|
||||||
|
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_fail;%}
|
||||||
|
|
||||||
// and the typechecks
|
// and the typechecks
|
||||||
%typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& {
|
%typecheck(SWIG_TYPECHECK_STRING) std::string,const std::string& {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue