From 7e5e4fd1f9c8adfcd0eb1328e35143e110e115ff Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Tue, 18 Oct 2005 13:24:15 +0000 Subject: [PATCH] massive typemap unification git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7676 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- CHANGES.current | 60 +- Examples/python/multimap/Makefile | 2 +- Examples/python/varargs/Makefile | 2 +- Examples/ruby/mpointer/Makefile | 2 +- Examples/tcl/multimap/Makefile | 2 +- Examples/test-suite/class_scope_weird.i | 12 +- Examples/test-suite/common.mk | 1 + Examples/test-suite/director_wstring.i | 49 + Examples/test-suite/enum_thorough.i | 2 + Examples/test-suite/extern_declaration.i | 6 +- .../{python/implicittest.i => li_implicit.i} | 2 +- Examples/test-suite/primitive_types.i | 9 +- Examples/test-suite/python/Makefile.in | 3 +- .../test-suite/python/argcargvtest_runme.py | 1 + Examples/test-suite/python/empty_runme.py | 1 + Examples/test-suite/python/inout.i | 5 + .../test-suite/python/li_cstring_runme.py | 2 + ...icittest_runme.py => li_implicit_runme.py} | 2 +- .../test-suite/python/li_std_string_runme.py | 1 + .../test-suite/python/li_std_wstring_runme.py | 1 + .../python/primitive_types_runme.py | 19 +- Examples/test-suite/ruby/bools_runme.rb | 4 +- .../ruby/director_exception_runme.rb | 17 + .../test-suite/ruby/primitive_types_runme.rb | 43 + Examples/test-suite/tcl/Makefile.in | 5 +- Examples/test-suite/tcl/bools_runme.tcl | 4 +- .../test-suite/tcl/enum_thorough_runme.tcl | 1 + .../test-suite/tcl/overload_simple_runme.tcl | 2 +- .../test-suite/tcl/primitive_types_runme.tcl | 21 + Lib/cdata.i | 17 +- Lib/chicken/chickenrun.swg | 1 - Lib/exception.i | 119 --- Lib/lua/lua.swg | 4 +- Lib/lua/luarun.swg | 5 +- Lib/php4/php4run.swg | 2 - Lib/python/README | 17 +- Lib/python/argcargv.i | 40 +- Lib/python/attribute.i | 2 +- Lib/python/cdata.i | 1 + Lib/python/cstring.i | 4 +- Lib/python/cwstring.i | 4 +- Lib/python/director_h.swg | 55 +- Lib/python/file.i | 20 +- Lib/python/implicit.i | 217 +---- Lib/python/pyapi.swg | 57 +- Lib/python/pycomplex.swg | 57 +- Lib/python/pycontainer.swg | 33 +- Lib/python/pyenum.swg | 52 - Lib/python/pyerrors.swg | 81 ++ Lib/python/pyinit.swg | 23 +- Lib/python/pyinout.swg | 233 ----- Lib/python/pymacros.swg | 160 +--- Lib/python/pyobject.swg | 17 - Lib/python/pyprimtypes.swg | 898 ++++++------------ Lib/python/pyptrtypes.swg | 165 ---- Lib/python/pyrun.swg | 293 ++---- Lib/python/pyruntime.swg | 36 +- Lib/python/pystdcommon.swg | 31 +- Lib/python/pystrbase.swg | 330 ------- Lib/python/pystrings.swg | 151 +-- Lib/python/pyswigtype.swg | 404 +------- Lib/python/python.swg | 36 +- Lib/python/pytuplehlp.swg | 22 +- Lib/python/pytypemaps.swg | 56 ++ Lib/python/pyuserdir.swg | 5 +- Lib/python/pyvaltypes.swg | 208 ---- Lib/python/pyvoid.swg | 61 -- Lib/python/pywstrings.swg | 150 +-- Lib/python/std_basic_string.i | 66 +- Lib/python/std_pair.i | 4 +- Lib/python/std_string.i | 68 +- Lib/python/std_wstring.i | 70 +- Lib/python/typemaps.i | 188 +--- Lib/ruby/cdata.i | 1 + Lib/ruby/cstring.i | 137 +++ Lib/ruby/director.swg | 94 +- Lib/ruby/fragments.i | 17 - Lib/ruby/ruby.swg | 68 +- Lib/ruby/rubyapi.swg | 27 + Lib/ruby/rubydef.swg | 267 ------ Lib/ruby/rubyenum.swg | 64 -- Lib/ruby/rubyerrors.swg | 61 ++ Lib/ruby/rubyfragments.swg | 23 + Lib/ruby/rubyinit.swg | 1 + Lib/ruby/rubymacros.swg | 1 + Lib/ruby/rubymisctypes.swg | 6 - Lib/ruby/rubyobject.swg | 13 - Lib/ruby/rubyprimtypes.swg | 548 ++++++----- Lib/ruby/rubyrun.swg | 289 ++++++ Lib/ruby/{rubyhead.swg => rubyruntime.swg} | 42 +- Lib/ruby/rubystrings.swg | 161 +--- Lib/ruby/rubyswigtype.swg | 162 ---- Lib/ruby/rubytracking.swg | 141 +-- Lib/ruby/rubytypemaps.swg | 45 + Lib/ruby/rubyuserdir.swg | 6 + Lib/ruby/rubyvoid.swg | 33 - Lib/ruby/rubywstrings.swg | 74 ++ Lib/ruby/std_string.i | 98 +- Lib/ruby/typemaps.i | 442 +-------- Lib/std/std_common.i | 16 +- Lib/swig.swg | 8 +- Lib/swigrun.swg | 36 +- Lib/tcl/cdata.i | 1 + Lib/tcl/cstring.i | 213 +---- Lib/tcl/cwstring.i | 137 +++ Lib/tcl/std_string.i | 77 +- Lib/tcl/std_wstring.i | 37 + Lib/tcl/tcl8.swg | 743 +-------------- Lib/tcl/tclapi.swg | 107 +++ Lib/tcl/tclerrors.swg | 75 ++ Lib/tcl/tclfragments.swg | 23 + Lib/tcl/tclinit.swg | 106 +++ Lib/tcl/{tcl8kw.swg => tclkw.swg} | 0 Lib/tcl/tclmacros.swg | 26 + Lib/tcl/tclopers.swg | 24 + Lib/tcl/tclprimtypes.swg | 308 ++++++ Lib/tcl/{swigtcl8.swg => tclrun.swg} | 609 +++++------- Lib/tcl/tclruntime.swg | 13 + Lib/tcl/tclstrings.swg | 36 + Lib/tcl/tclswigtype.swg | 13 + Lib/tcl/tcltypemaps.swg | 42 + Lib/tcl/tcluserdir.swg | 5 + Lib/tcl/tclwstrings.swg | 36 + Lib/tcl/typemaps.i | 508 +--------- Lib/typemaps/README | 16 + Lib/typemaps/cdata.swg | 79 ++ .../cstrbase.swg => typemaps/cstring.swg} | 158 +-- Lib/typemaps/enumint.swg | 64 ++ Lib/typemaps/implicit.swg | 204 ++++ Lib/typemaps/inoutlist.swg | 278 ++++++ .../misctypes.swg} | 4 - Lib/typemaps/primtypes.swg | 132 +++ Lib/typemaps/ptrtypes.swg | 160 ++++ Lib/typemaps/std_string.swg | 69 ++ Lib/typemaps/strings.swg | 505 ++++++++++ Lib/typemaps/swigmacros.swg | 241 +++++ Lib/typemaps/swigobject.swg | 29 + Lib/typemaps/swigtype.swg | 426 +++++++++ Lib/typemaps/typemaps.swg | 167 ++++ Lib/typemaps/valtypes.swg | 189 ++++ Lib/typemaps/void.swg | 60 ++ Source/Modules/python.cxx | 5 +- Source/Modules/ruby.cxx | 33 +- Source/Modules/tcl8.cxx | 43 +- 144 files changed, 6378 insertions(+), 7248 deletions(-) create mode 100644 Examples/test-suite/director_wstring.i rename Examples/test-suite/{python/implicittest.i => li_implicit.i} (92%) create mode 100644 Examples/test-suite/python/empty_runme.py rename Examples/test-suite/python/{implicittest_runme.py => li_implicit_runme.py} (90%) create mode 100644 Examples/test-suite/ruby/primitive_types_runme.rb create mode 100644 Examples/test-suite/tcl/primitive_types_runme.tcl create mode 100644 Lib/python/cdata.i delete mode 100644 Lib/python/pyenum.swg create mode 100644 Lib/python/pyerrors.swg delete mode 100644 Lib/python/pyinout.swg delete mode 100644 Lib/python/pyobject.swg delete mode 100644 Lib/python/pyptrtypes.swg delete mode 100644 Lib/python/pystrbase.swg create mode 100644 Lib/python/pytypemaps.swg delete mode 100644 Lib/python/pyvaltypes.swg delete mode 100644 Lib/python/pyvoid.swg create mode 100644 Lib/ruby/cdata.i create mode 100644 Lib/ruby/cstring.i delete mode 100644 Lib/ruby/fragments.i create mode 100644 Lib/ruby/rubyapi.swg delete mode 100644 Lib/ruby/rubydef.swg delete mode 100644 Lib/ruby/rubyenum.swg create mode 100644 Lib/ruby/rubyerrors.swg create mode 100644 Lib/ruby/rubyfragments.swg create mode 100644 Lib/ruby/rubyinit.swg create mode 100644 Lib/ruby/rubymacros.swg delete mode 100644 Lib/ruby/rubymisctypes.swg delete mode 100644 Lib/ruby/rubyobject.swg create mode 100644 Lib/ruby/rubyrun.swg rename Lib/ruby/{rubyhead.swg => rubyruntime.swg} (72%) delete mode 100644 Lib/ruby/rubyswigtype.swg create mode 100644 Lib/ruby/rubytypemaps.swg create mode 100644 Lib/ruby/rubyuserdir.swg delete mode 100644 Lib/ruby/rubyvoid.swg create mode 100644 Lib/ruby/rubywstrings.swg create mode 100644 Lib/tcl/cdata.i create mode 100644 Lib/tcl/cwstring.i create mode 100644 Lib/tcl/std_wstring.i create mode 100644 Lib/tcl/tclapi.swg create mode 100644 Lib/tcl/tclerrors.swg create mode 100644 Lib/tcl/tclfragments.swg create mode 100644 Lib/tcl/tclinit.swg rename Lib/tcl/{tcl8kw.swg => tclkw.swg} (100%) create mode 100644 Lib/tcl/tclmacros.swg create mode 100644 Lib/tcl/tclopers.swg create mode 100644 Lib/tcl/tclprimtypes.swg rename Lib/tcl/{swigtcl8.swg => tclrun.swg} (69%) create mode 100644 Lib/tcl/tclruntime.swg create mode 100644 Lib/tcl/tclstrings.swg create mode 100644 Lib/tcl/tclswigtype.swg create mode 100644 Lib/tcl/tcltypemaps.swg create mode 100644 Lib/tcl/tcluserdir.swg create mode 100644 Lib/tcl/tclwstrings.swg create mode 100644 Lib/typemaps/README create mode 100644 Lib/typemaps/cdata.swg rename Lib/{python/cstrbase.swg => typemaps/cstring.swg} (52%) create mode 100644 Lib/typemaps/enumint.swg create mode 100644 Lib/typemaps/implicit.swg create mode 100644 Lib/typemaps/inoutlist.swg rename Lib/{python/pymisctypes.swg => typemaps/misctypes.swg} (89%) create mode 100644 Lib/typemaps/primtypes.swg create mode 100644 Lib/typemaps/ptrtypes.swg create mode 100644 Lib/typemaps/std_string.swg create mode 100644 Lib/typemaps/strings.swg create mode 100644 Lib/typemaps/swigmacros.swg create mode 100644 Lib/typemaps/swigobject.swg create mode 100644 Lib/typemaps/swigtype.swg create mode 100644 Lib/typemaps/typemaps.swg create mode 100644 Lib/typemaps/valtypes.swg create mode 100644 Lib/typemaps/void.swg diff --git a/CHANGES.current b/CHANGES.current index 58d7c5b64..e8c07172d 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,11 +1,67 @@ Version 1.3.27 (October 15, 2005) ================================= -07/15/2005: wsfulton +10/18/2005: mmatus + + Unifying the typemaps for + + python, ruby, tcl + + and in the process, fix several problems in three + languages to work in the "canonical" way now stablished in + the typemap library + + SWIG/Lib/typempas + + The current status of the unification is that everything + compiles and runs inside the test-suite and examples + directories. And for the first type we have three + languages than pass the primitive_types.i case. + + Also, we have uniform way to treat the errors, for example + if you do something like + + >>> from primitive_types import * + >>> print val_uchar(10) + 10 + >>> print val_uchar(1000) + Traceback (most recent call last): + File "", line 1, in ? + OverflowError: in argument 1 of type 'unsigned char' + + you get the same exception in all the three languages. + + And well, many more good things will come from this + unification, as proper support of the STL/STD classes + for all the languages, and hopefully, we can keep + adding other languages. + + The hardest part, writting a common typemap library + that suites the three different languages, is done, + and adding another language it is easy now. + + Still the global unification is not complete, the STL/STD + part is next, and probably adding one or two more + languages. + + If you are curious, look at the python, ruby and/or tcl + directories to see what is needed to support the new + common typemaps library. Still, the final way to + integrate a new language could change as we move to + integrate the STD/STL. + + *** POTENTIAL INCOMPATIBILITY *** + + Some missing typemaps could start working, and change + the old expected behavior, specially in ruby and tcl. + + + +10/15/2005: wsfulton [Java] Fix for typesafe enum wrapping so that it is possible to overload a method with 2 different enum types. -07/15/2005: wsfulton +10/15/2005: wsfulton Fix for %feature("immutable","0") attempting to generate setters for constants. diff --git a/Examples/python/multimap/Makefile b/Examples/python/multimap/Makefile index 754c4a029..4a1e1bb71 100644 --- a/Examples/python/multimap/Makefile +++ b/Examples/python/multimap/Makefile @@ -1,5 +1,5 @@ TOP = ../.. -SWIG = $(TOP)/../swig +SWIG = $(TOP)/../preinst-swig SRCS = example.c TARGET = example INTERFACE = example.i diff --git a/Examples/python/varargs/Makefile b/Examples/python/varargs/Makefile index 5b726c2b1..01d0f943a 100644 --- a/Examples/python/varargs/Makefile +++ b/Examples/python/varargs/Makefile @@ -1,5 +1,5 @@ TOP = ../.. -SWIG = $(TOP)/../swig +SWIG = $(TOP)/../preinst-swig SRCS = TARGET = example INTERFACE = example.i diff --git a/Examples/ruby/mpointer/Makefile b/Examples/ruby/mpointer/Makefile index 27cb6d6bd..2a8eb7b56 100644 --- a/Examples/ruby/mpointer/Makefile +++ b/Examples/ruby/mpointer/Makefile @@ -1,5 +1,5 @@ TOP = ../.. -SWIG = $(TOP)/../swig +SWIG = $(TOP)/../preinst-swig CXXSRCS = example.cxx TARGET = example INTERFACE = example.i diff --git a/Examples/tcl/multimap/Makefile b/Examples/tcl/multimap/Makefile index 6efb6fb84..bed049a36 100644 --- a/Examples/tcl/multimap/Makefile +++ b/Examples/tcl/multimap/Makefile @@ -1,5 +1,5 @@ TOP = ../.. -SWIG = $(TOP)/../swig +SWIG = $(TOP)/../preinst-swig SRCS = example.c TARGET = my_tclsh DLTARGET = example diff --git a/Examples/test-suite/class_scope_weird.i b/Examples/test-suite/class_scope_weird.i index 74155478c..a0a544d28 100644 --- a/Examples/test-suite/class_scope_weird.i +++ b/Examples/test-suite/class_scope_weird.i @@ -16,16 +16,16 @@ public: } }; -class quat; +class Quat; class matrix4; class tacka3; -class quat { +class Quat { public: - quat::quat(void){} - quat::quat(float in_w, float x, float y, float z){} - quat::quat(const tacka3& axis, float angle){} - quat::quat(const matrix4& m){} + Quat::Quat(void){} + Quat::Quat(float in_w, float x, float y, float z){} + Quat::Quat(const tacka3& axis, float angle){} + Quat::Quat(const matrix4& m){} }; %} diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 6f45b8811..6448d572a 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -346,6 +346,7 @@ C_TEST_CASES += \ sizeof_pointer \ sneaky1 \ struct_rename \ + typedef_struct \ typemap_subst \ unions diff --git a/Examples/test-suite/director_wstring.i b/Examples/test-suite/director_wstring.i new file mode 100644 index 000000000..f386b20ec --- /dev/null +++ b/Examples/test-suite/director_wstring.i @@ -0,0 +1,49 @@ +%module(directors="1") director_wstring; +%include stl.i +%include std_vector.i +%include std_wstring.i + +// Using thread unsafe wrapping +%warnfilter(470) A; + +%{ +#include +#include +%} + +%feature("director") A; +%inline %{ + +struct A +{ + A(const std::wstring& first) + : m_strings(1, first) + {} + + virtual ~A() {} + + virtual const std::wstring& get_first() const + { return get(0); } + + virtual const std::wstring& get(int n) const + { return m_strings[n]; } + + virtual const std::wstring& call_get_first() const + { return get_first(); } + + virtual const std::wstring& call_get(int n) const + { return get(n); } + + std::vector m_strings; + + + virtual void process_text(const char *text) + { + } + + void call_process_func() { process_text("hello"); } + }; + + %} + +%template(StringVector) std::vector; diff --git a/Examples/test-suite/enum_thorough.i b/Examples/test-suite/enum_thorough.i index 0a31b7d1c..3fe737d89 100644 --- a/Examples/test-suite/enum_thorough.i +++ b/Examples/test-suite/enum_thorough.i @@ -63,6 +63,7 @@ struct SpeedClass { enum speed { slow=10, medium=20, fast=30, lightning }; typedef enum speed speedtd1; + int speedTest0(int s) { return s; } speed speedTest1(speed s) { return s; } enum speed speedTest2(enum speed s) { return s; } const speed speedTest3(const speed s) { return s; } @@ -77,6 +78,7 @@ struct SpeedClass { SpeedClass() : myColour2(red), mySpeedtd1(slow) { } }; +int speedTest0(int s) { return s; } SpeedClass::speed speedTest1(SpeedClass::speed s) { return s; } enum SpeedClass::speed speedTest2(enum SpeedClass::speed s) { return s; } const SpeedClass::speed speedTest3(const SpeedClass::speed s) { return s; } diff --git a/Examples/test-suite/extern_declaration.i b/Examples/test-suite/extern_declaration.i index 24e214a2e..eca97b5b4 100644 --- a/Examples/test-suite/extern_declaration.i +++ b/Examples/test-suite/extern_declaration.i @@ -19,8 +19,10 @@ SWIGEXPORT extern int externexport(int); extern int SWIGSTDCALL externstdcall(int); %{ -// externimport ought to be using MYDLLIMPORT and compiled into another dll, but that is -// a bit tricky to do in the test framework +/* + externimport ought to be using MYDLLIMPORT and compiled into another dll, but that is + a bit tricky to do in the test framework +*/ SWIGEXPORT extern int externimport(int i) { return i; } SWIGEXPORT extern int externexport(int i) { return i; } extern int SWIGSTDCALL externstdcall(int i) { return i; } diff --git a/Examples/test-suite/python/implicittest.i b/Examples/test-suite/li_implicit.i similarity index 92% rename from Examples/test-suite/python/implicittest.i rename to Examples/test-suite/li_implicit.i index 617fa0a27..ce457be8b 100644 --- a/Examples/test-suite/python/implicittest.i +++ b/Examples/test-suite/li_implicit.i @@ -1,4 +1,4 @@ -%module implicittest +%module li_implicit %include implicit.i %inline diff --git a/Examples/test-suite/primitive_types.i b/Examples/test-suite/primitive_types.i index 964d82f65..01813ad8f 100644 --- a/Examples/test-suite/primitive_types.i +++ b/Examples/test-suite/primitive_types.i @@ -1,6 +1,9 @@ // Massive primitive datatype test. %module(directors="1") primitive_types +// Ruby constant names +#pragma SWIG nowarn=801 + // Using thread unsafe wrapping #pragma SWIG nowarn=470 /* @@ -217,8 +220,8 @@ macro(long, pfx, long) macro(unsigned long, pfx, ulong) macro(long long, pfx, llong) macro(unsigned long long, pfx, ullong) -//macro(float, pfx, float) -//macro(double, pfx, double) +macro(float, pfx, float) +macro(double, pfx, double) macro(char, pfx, char) %enddef @@ -345,9 +348,7 @@ macro(size_t, pfx, sizet) %enddef -#ifdef SWIGPYTHON %apply (char *STRING, int LENGTH) { (const char *str, size_t len) } -#endif %inline { struct Foo diff --git a/Examples/test-suite/python/Makefile.in b/Examples/test-suite/python/Makefile.in index e6faa2bbc..32122943a 100644 --- a/Examples/test-suite/python/Makefile.in +++ b/Examples/test-suite/python/Makefile.in @@ -18,14 +18,15 @@ CPP_TEST_CASES += \ complextest \ director_stl \ director_thread \ + director_wstring \ file_test \ - implicittest \ inout \ input \ inplaceadd \ kwargs \ li_cstring \ li_cwstring \ + li_implicit \ li_std_except \ li_std_vectora \ li_std_map \ diff --git a/Examples/test-suite/python/argcargvtest_runme.py b/Examples/test-suite/python/argcargvtest_runme.py index c1ca9386d..5ddb3df42 100644 --- a/Examples/test-suite/python/argcargvtest_runme.py +++ b/Examples/test-suite/python/argcargvtest_runme.py @@ -6,6 +6,7 @@ if mainc(largs) != 3: targs=('hi','hola') if mainv(targs,1) != 'hola': + print mainv(targs,1) raise RuntimeError, "bad main typemap" targs=('hi', 'hola') diff --git a/Examples/test-suite/python/empty_runme.py b/Examples/test-suite/python/empty_runme.py new file mode 100644 index 000000000..7de5d15e5 --- /dev/null +++ b/Examples/test-suite/python/empty_runme.py @@ -0,0 +1 @@ +import empty diff --git a/Examples/test-suite/python/inout.i b/Examples/test-suite/python/inout.i index f557b5c46..dc6db0ecb 100644 --- a/Examples/test-suite/python/inout.i +++ b/Examples/test-suite/python/inout.i @@ -32,6 +32,10 @@ p->second += 1; } + inline void AddOne1r(double& a) { + a += 1; + } + %} %template() std::pair; @@ -41,3 +45,4 @@ void AddOne3(double* INOUT, double* INOUT, double* INOUT); void AddOne1p(std::pair* INOUT); void AddOne2p(std::pair* INOUT, double* INOUT); void AddOne3p(double* INOUT, std::pair* INOUT, double* INOUT); +void AddOne1r(double& INOUT); diff --git a/Examples/test-suite/python/li_cstring_runme.py b/Examples/test-suite/python/li_cstring_runme.py index 5ac4d7e71..6503744bd 100644 --- a/Examples/test-suite/python/li_cstring_runme.py +++ b/Examples/test-suite/python/li_cstring_runme.py @@ -11,9 +11,11 @@ if test2() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ raise RuntimeError if test3("hello") != "hello-suffix": + print test3("hello") raise RuntimeError if test4("hello") != "hello-suffix": + print test4("hello") raise RuntimeError if test5(4) != 'xxxx': diff --git a/Examples/test-suite/python/implicittest_runme.py b/Examples/test-suite/python/li_implicit_runme.py similarity index 90% rename from Examples/test-suite/python/implicittest_runme.py rename to Examples/test-suite/python/li_implicit_runme.py index 5de43844f..a0672e016 100644 --- a/Examples/test-suite/python/implicittest_runme.py +++ b/Examples/test-suite/python/li_implicit_runme.py @@ -1,4 +1,4 @@ -from implicittest import * +from li_implicit import * b = B() ai = A(1) ad = A(2.0) diff --git a/Examples/test-suite/python/li_std_string_runme.py b/Examples/test-suite/python/li_std_string_runme.py index a30795ae6..7a04a3e6f 100644 --- a/Examples/test-suite/python/li_std_string_runme.py +++ b/Examples/test-suite/python/li_std_string_runme.py @@ -11,6 +11,7 @@ if li_std_string.test_cvalue(x) != x: raise RuntimeError, "bad string mapping" if li_std_string.test_value(x) != x: + print x, li_std_string.test_value(x) raise RuntimeError, "bad string mapping" if li_std_string.test_const_reference(x) != x: diff --git a/Examples/test-suite/python/li_std_wstring_runme.py b/Examples/test-suite/python/li_std_wstring_runme.py index 3e49033fd..1b2252ddb 100644 --- a/Examples/test-suite/python/li_std_wstring_runme.py +++ b/Examples/test-suite/python/li_std_wstring_runme.py @@ -3,6 +3,7 @@ import li_std_wstring x=u"h" if li_std_wstring.test_wcvalue(x) != x: + print li_std_wstring.test_wcvalue(x) raise RuntimeError, "bad string mapping" x=u"hello" diff --git a/Examples/test-suite/python/primitive_types_runme.py b/Examples/test-suite/python/primitive_types_runme.py index bd357b528..29d596ee8 100644 --- a/Examples/test-suite/python/primitive_types_runme.py +++ b/Examples/test-suite/python/primitive_types_runme.py @@ -14,8 +14,8 @@ cvar.var_long = sct_long cvar.var_ulong = sct_ulong cvar.var_llong = sct_llong cvar.var_ullong = sct_ullong -#cvar.var_float = sct_float -#cvar.var_double = sct_double +cvar.var_float = sct_float +cvar.var_double = sct_double cvar.var_char = sct_char cvar.var_pchar = sct_pchar cvar.var_pcharc = sct_pcharc @@ -46,8 +46,8 @@ if cvar.var_long != cct_long: pyerror("long", cvar.var_long, cct_long) if cvar.var_ulong != cct_ulong: pyerror("ulong", cvar.var_ulong, cct_ulong) if cvar.var_llong != cct_llong: pyerror("llong", cvar.var_llong, cct_llong) if cvar.var_ullong != cct_ullong: pyerror("ullong", cvar.var_ullong, cct_ullong) -#if cvar.var_float != cct_float: pyerror("float", cvar.var_float, cct_float) -#if cvar.var_double != cct_double: pyerror("double", cvar.var_double, cct_double) +if cvar.var_float != cct_float: pyerror("float", cvar.var_float, cct_float) +if cvar.var_double != cct_double: pyerror("double", cvar.var_double, cct_double) if cvar.var_char != cct_char: pyerror("char", cvar.var_char, cct_char) if cvar.var_pchar != cct_pchar: pyerror("pchar", cvar.var_pchar, cct_pchar) if cvar.var_pcharc != cct_pcharc: pyerror("pchar", cvar.var_pcharc, cct_pcharc) @@ -128,8 +128,8 @@ p.var_long = p.stc_long p.var_ulong = p.stc_ulong p.var_llong = p.stc_llong p.var_ullong = p.stc_ullong -#p.var_float = p.stc_float -#p.var_double = p.stc_double +p.var_float = p.stc_float +p.var_double = p.stc_double p.var_char = p.stc_char p.var_pchar = sct_pchar p.var_pcharc = sct_pcharc @@ -156,8 +156,8 @@ t.var_long = t.stc_long t.var_ulong = t.stc_ulong t.var_llong = t.stc_llong t.var_ullong = t.stc_ullong -#t.var_float = t.stc_float -#t.var_double = t.stc_double +t.var_float = t.stc_float +t.var_double = t.stc_double t.var_char = t.stc_char t.var_pchar = sct_pchar t.var_pcharc = sct_pcharc @@ -215,6 +215,7 @@ if cvar.var_char != '\0': cvar.var_namet = '\0' #if cvar.var_namet != '\0\0\0\0\0': if cvar.var_namet != '': + print 'hola', '', cvar.var_namet raise RuntimeError, "bad char '\0' case" cvar.var_namet = '' @@ -301,7 +302,7 @@ try: a = t.var_uint t.var_uint = -1 error = 1 -except TypeError: +except OverflowError: if a != t.var_uint: error = 1 pass diff --git a/Examples/test-suite/ruby/bools_runme.rb b/Examples/test-suite/ruby/bools_runme.rb index 2f6a49b19..ef118b269 100644 --- a/Examples/test-suite/ruby/bools_runme.rb +++ b/Examples/test-suite/ruby/bools_runme.rb @@ -33,7 +33,7 @@ if (Bools.value(Bools.const_pbool) != Bools.bool1) exit 1 end -if (Bools.value(Bools.const_rbool) != Bools.bool2) +if (Bools.const_rbool != Bools.bool2) print "Runtime test 7 failed\n" exit 1 end @@ -59,7 +59,7 @@ if (Bools.value(Bools.pbo(Bools.pbool)) != Bools.value(Bools.pbool)) exit 1 end -if (Bools.const_rbo(Bools.value(Bools.const_rbool)) != Bools.value(Bools.const_rbool)) +if (Bools.const_rbo(Bools.const_rbool) != Bools.const_rbool) print "Runtime test 12 failed\n" exit 1 end diff --git a/Examples/test-suite/ruby/director_exception_runme.rb b/Examples/test-suite/ruby/director_exception_runme.rb index 0d91a4f2e..ec6559b2a 100644 --- a/Examples/test-suite/ruby/director_exception_runme.rb +++ b/Examples/test-suite/ruby/director_exception_runme.rb @@ -14,6 +14,12 @@ class MyFoo2 < Foo end end +class MyFoo3 < Foo + def ping + 5 # error: should return a string + end +end + ok = false a = MyFoo.new @@ -38,5 +44,16 @@ rescue TypeError ok = true end + +a = MyFoo3.new +b = launder(a) + +begin + b.pong +rescue TypeError + ok = true +end + + raise RuntimeError unless ok diff --git a/Examples/test-suite/ruby/primitive_types_runme.rb b/Examples/test-suite/ruby/primitive_types_runme.rb new file mode 100644 index 000000000..35c380826 --- /dev/null +++ b/Examples/test-suite/ruby/primitive_types_runme.rb @@ -0,0 +1,43 @@ +require 'primitive_types' + +include Primitive_types + + +raise RuntimeError if val_uchar(255) != 255 + +raise RuntimeError if val_double(255.5) != 255.5 + + +fail = 0 +begin + val_uchar(-1) +rescue RangeError + fail = 1 +end + +fail = 0 +begin + val_uchar(256) +rescue RangeError + fail = 1 +end + +raise RuntimeError if fail != 1 + +fail = 0 +begin + val_uchar(256.0) +rescue TypeError + fail = 1 +end + +raise RuntimeError if fail != 1 + +fail = 0 +begin + val_uchar("caca") +rescue TypeError + fail = 1 +end + +raise RuntimeError if fail != 1 diff --git a/Examples/test-suite/tcl/Makefile.in b/Examples/test-suite/tcl/Makefile.in index c94f98195..37f65f164 100644 --- a/Examples/test-suite/tcl/Makefile.in +++ b/Examples/test-suite/tcl/Makefile.in @@ -9,6 +9,9 @@ srcdir = @srcdir@ top_srcdir = @top_srcdir@ top_builddir = @top_builddir@ +CPP_TEST_CASES += \ + primitive_types \ + include $(srcdir)/../common.mk # Overridden variables here @@ -39,6 +42,6 @@ run_testcase = \ # Clean %.clean: - + clean: $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile tcl_clean diff --git a/Examples/test-suite/tcl/bools_runme.tcl b/Examples/test-suite/tcl/bools_runme.tcl index 92aeb99ec..582b8121f 100644 --- a/Examples/test-suite/tcl/bools_runme.tcl +++ b/Examples/test-suite/tcl/bools_runme.tcl @@ -35,7 +35,7 @@ if { [ value $const_pbool ] != $bool1} { exit 1 } -if { [ value $const_rbool ] != $bool2} { +if { $const_rbool != $bool2} { puts stderr "Runtime test 7 failed" exit 1 } @@ -61,7 +61,7 @@ if { [ value [ pbo $pbool ] ] != [ value $pbool ]} { exit 1 } -if { [ const_rbo [ value $const_rbool ] ] != [ value $const_rbool ]} { +if { [ const_rbo $const_rbool ] != $const_rbool } { puts stderr "Runtime test 12 failed" exit 1 } diff --git a/Examples/test-suite/tcl/enum_thorough_runme.tcl b/Examples/test-suite/tcl/enum_thorough_runme.tcl index 3457bfe4b..d4cc1995a 100644 --- a/Examples/test-suite/tcl/enum_thorough_runme.tcl +++ b/Examples/test-suite/tcl/enum_thorough_runme.tcl @@ -3,6 +3,7 @@ if [ catch { load ./enum_thorough[info sharedlibextension] enum_thorough} err_ms puts stderr "Could not load shared object:\n$err_msg" } +if { [speedTest0 $SpeedClass_slow] != $SpeedClass_slow } { puts stderr "speedTest0 failed" } if { [speedTest4 $SpeedClass_slow] != $SpeedClass_slow } { puts stderr "speedTest4 failed" } if { [speedTest5 $SpeedClass_slow] != $SpeedClass_slow } { puts stderr "speedTest5 failed" } diff --git a/Examples/test-suite/tcl/overload_simple_runme.tcl b/Examples/test-suite/tcl/overload_simple_runme.tcl index e5e7b6f2d..38e6dd1dc 100644 --- a/Examples/test-suite/tcl/overload_simple_runme.tcl +++ b/Examples/test-suite/tcl/overload_simple_runme.tcl @@ -9,7 +9,7 @@ set v [malloc_void 32] set x [foo 3] if {$x != "foo:int"} { - puts stderr "foo(int) test failed" + puts stderr "foo(int) test failed $x" exit 1 } diff --git a/Examples/test-suite/tcl/primitive_types_runme.tcl b/Examples/test-suite/tcl/primitive_types_runme.tcl new file mode 100644 index 000000000..f10e1b0cb --- /dev/null +++ b/Examples/test-suite/tcl/primitive_types_runme.tcl @@ -0,0 +1,21 @@ + +if [ catch { load ./primitive_types[info sharedlibextension] primitive_types} err_msg ] { + puts stderr "Could not load shared object:\n$err_msg" +} + + +if {[val_int 10] != 10 } { error "bad int map" } +if {[val_schar 10] != 10 } { error "bad char map" } +if {[val_short 10] != 10 } { error "bad schar map" } + + +if [catch { val_schar 10000 } ] {} else { error "bad schar map" } +if [catch { val_uint -100 } ] {} else { error "bad uint map" } +if [catch { val_uchar -100 } ] {} else { error "bad uchar map" } + +if {[val_uint 10] != 10 } { error "bad uint map" } +if {[val_uchar 10] != 10 } { error "bad uchar map" } +if {[val_ushort 10] != 10 } { error "bad ushort map" } + + + diff --git a/Lib/cdata.i b/Lib/cdata.i index 37fb43455..310dd76a0 100644 --- a/Lib/cdata.i +++ b/Lib/cdata.i @@ -19,27 +19,12 @@ typedef struct SWIGCDATA { * Typemaps for returning binary data * ----------------------------------------------------------------------------- */ -#if SWIGPYTHON -%typemap(out) SWIGCDATA { - $result = PyString_FromStringAndSize($1.data,$1.len); -} -%typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH); -#elif SWIGPERL +if SWIGPERL %typemap(out) SWIGCDATA { ST(argvi) = sv_newmortal(); sv_setpvn((SV*)ST(argvi++),$1.data,$1.len); } %typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH); -#elif SWIGTCL -%typemap(out) SWIGCDATA { - Tcl_SetObjResult(interp,Tcl_NewStringObj($1.data,$1.len)); -} -%typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH); -#elif SWIGRUBY -%typemap(out) SWIGCDATA { - $result = rb_str_new($1.data,$1.len); -} -%typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH); #elif SWIGGUILE %typemap(out) SWIGCDATA { $result = gh_str2scm($1.data,$1.len); diff --git a/Lib/chicken/chickenrun.swg b/Lib/chicken/chickenrun.swg index e11d87baf..defccb998 100644 --- a/Lib/chicken/chickenrun.swg +++ b/Lib/chicken/chickenrun.swg @@ -84,7 +84,6 @@ extern "C" { func = C_SCHEME_FALSE; \ } -#define SWIG_POINTER_DISOWN 1 enum { SWIG_BARF1_BAD_ARGUMENT_TYPE /* 1 arg */, diff --git a/Lib/exception.i b/Lib/exception.i index e106bd0c7..877d6d28e 100644 --- a/Lib/exception.i +++ b/Lib/exception.i @@ -5,36 +5,6 @@ // // This SWIG library file provides language independent exception handling -%{ -#define SWIG_MemoryError 1 -#define SWIG_IOError 2 -#define SWIG_RuntimeError 3 -#define SWIG_IndexError 4 -#define SWIG_TypeError 5 -#define SWIG_DivisionByZero 6 -#define SWIG_OverflowError 7 -#define SWIG_SyntaxError 8 -#define SWIG_ValueError 9 -#define SWIG_SystemError 10 -#define SWIG_UnknownError 99 -%} - -#ifdef SWIGTCL8 -%{ -/* We cast from 'const char*' to 'char*' since TCL_VOLATILE ensure - that an internal copy of the strng will be stored. - - NOTE: Later use const_cast via SWIG_const_cast or so. -*/ -#define SWIG_exception(a,b) { Tcl_SetResult(interp,(char *)b,TCL_VOLATILE); SWIG_fail; } -%} -#else -#ifdef SWIGTCL -%{ -#define SWIG_exception(a,b) { Tcl_SetResult(interp,(char *)b,TCL_VOLATILE); return TCL_ERROR; } -%} -#endif -#endif #ifdef SWIGPERL5 %{ @@ -49,50 +19,6 @@ %} #endif -#ifdef SWIGPYTHON -%{ -SWIGINTERN void SWIG_exception_(int code, const char *msg) { - switch(code) { - case SWIG_MemoryError: - PyErr_SetString(PyExc_MemoryError,msg); - break; - case SWIG_IOError: - PyErr_SetString(PyExc_IOError,msg); - break; - case SWIG_RuntimeError: - PyErr_SetString(PyExc_RuntimeError,msg); - break; - case SWIG_IndexError: - PyErr_SetString(PyExc_IndexError,msg); - break; - case SWIG_TypeError: - PyErr_SetString(PyExc_TypeError,msg); - break; - case SWIG_DivisionByZero: - PyErr_SetString(PyExc_ZeroDivisionError,msg); - break; - case SWIG_OverflowError: - PyErr_SetString(PyExc_OverflowError,msg); - break; - case SWIG_SyntaxError: - PyErr_SetString(PyExc_SyntaxError,msg); - break; - case SWIG_ValueError: - PyErr_SetString(PyExc_ValueError,msg); - break; - case SWIG_SystemError: - PyErr_SetString(PyExc_SystemError,msg); - break; - default: - PyErr_SetString(PyExc_RuntimeError,msg); - break; - } -} - -#define SWIG_exception(a,b) { SWIG_exception_(a,b); SWIG_fail; } -%} -#endif - #ifdef SWIGGUILE %{ SWIGINTERN void SWIG_exception_ (int code, const char *msg, @@ -215,51 +141,6 @@ SWIGINTERN void SWIG_exception_(int code, const char *msg) { %} #endif -#ifdef SWIGRUBY -%{ -SWIGINTERN void SWIG_exception_(int code, const char *msg) { - switch (code) { - case SWIG_MemoryError: - rb_raise(rb_eNoMemError, msg); - break; - case SWIG_IOError: - rb_raise(rb_eIOError, msg); - break; - case SWIG_RuntimeError: - rb_raise(rb_eRuntimeError, msg); - break; - case SWIG_IndexError: - rb_raise(rb_eIndexError, msg); - break; - case SWIG_TypeError: - rb_raise(rb_eTypeError, msg); - break; - case SWIG_DivisionByZero: - rb_raise(rb_eZeroDivError, msg); - break; - case SWIG_OverflowError: - rb_raise(rb_eRangeError, msg); - break; - case SWIG_SyntaxError: - rb_raise(rb_eSyntaxError, msg); - break; - case SWIG_ValueError: - rb_raise(rb_eArgError, msg); - break; - case SWIG_SystemError: - rb_raise(rb_eFatal, msg); - break; - case SWIG_UnknownError: - rb_raise(rb_eRuntimeError, msg); - break; - default: - break; - } -} - -#define SWIG_exception(a, b) SWIG_exception_((a), (b)) -%} -#endif #ifdef SWIGCHICKEN %{ diff --git a/Lib/lua/lua.swg b/Lib/lua/lua.swg index 744be2d2c..4ac5d86e6 100644 --- a/Lib/lua/lua.swg +++ b/Lib/lua/lua.swg @@ -92,7 +92,7 @@ %typemap(in,checkfn="lua_isuserdata") SWIGTYPE { $<ype argp; - if(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,SWIG_POINTER_EXCEPTION) == -1) SWIG_fail; + if(SWIG_ConvertPtr(L,$input,(void**)(&argp),$&descriptor,0)) SWIG_fail; $1 = *argp; } @@ -462,4 +462,4 @@ SWIGEXPORT int SWIG_import(lua_State* L) /* Note: the initialization function is closed after all code is generated */ -/*************************** end lua.swg ******************************/ \ No newline at end of file +/*************************** end lua.swg ******************************/ diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg index f8489ef86..ec0876e8e 100644 --- a/Lib/lua/luarun.swg +++ b/Lib/lua/luarun.swg @@ -26,9 +26,6 @@ extern "C" { #define SWIG_LUA_POINTER 4 #define SWIG_LUA_BINARY 5 -/* Flags for pointer conversion */ -#define SWIG_POINTER_EXCEPTION 0x1 - /* type for all wrapper fns */ typedef int (*swig_lua_wrapper_func)(lua_State*); @@ -678,4 +675,4 @@ SWIG_Lua_SetModule(lua_State* L, swig_module_info *module) { } #endif -/*************************** end luarun.swg ******************************/ \ No newline at end of file +/*************************** end luarun.swg ******************************/ diff --git a/Lib/php4/php4run.swg b/Lib/php4/php4run.swg index 5ef54ad13..1620dea9e 100644 --- a/Lib/php4/php4run.swg +++ b/Lib/php4/php4run.swg @@ -36,8 +36,6 @@ extern "C" { make PHP type calls later as we handle php resources */ #define SWIG_ConvertPtr(obj,pp,type,flags) SWIG_ZTS_ConvertPtr(obj,pp,type,flags TSRMLS_CC) -/* Flags for SWIG_ConvertPtr */ -#define SWIG_POINTER_DISOWN 0x1 #define SWIG_fail goto fail diff --git a/Lib/python/README b/Lib/python/README index f618a0bbc..bdfe75908 100644 --- a/Lib/python/README +++ b/Lib/python/README @@ -48,12 +48,6 @@ std_wsstream.i wstring stream std_wstreambuf.i wstreambuf std_wstring.i wstring -/*----------------------------------------------------------------------------- - * Backward compatibility - * ----------------------------------------------------------------------------- */ - -std_vectora.i vector + allocator (allocators are now supported in STD/STL) -typemaps.i old in/out typemaps (doen't need to be included) /******************************************************************************** @@ -87,14 +81,9 @@ pyrun.swg Python run-time code * ----------------------------------------------------------------------------- */ pyswigtype.swg SWIGTYPE -pyvoid.swg void * -pyobject.swg PyObject -pystrbase.swg String base pystrings.swg Char strings (char *) pywstrings.swg Wchar Strings (wchar_t *) pyprimtypes.swg Primitive types (shot,int,double,etc) -pymisctypes.swg Miscellaneos types (size_t, ptrdiff_t, etc) -pyenum.swg enum especializations pycomplex.swg PyComplex and helper for C/C++ complex types pydocs.swg Typemaps documentation @@ -107,8 +96,10 @@ std_common.i general common code for the STD/STL implementation std_container.i general common code for the STD/STL containers -/* ----------------------------------------------------------------------------- - * deprecated +/*----------------------------------------------------------------------------- + * Backward compatibility and deprecated * ----------------------------------------------------------------------------- */ +std_vectora.i vector + allocator (allocators are now supported in STD/STL) +typemaps.i old in/out typemaps (doen't need to be included) defarg.swg for processing default arguments with shadow classes diff --git a/Lib/python/argcargv.i b/Lib/python/argcargv.i index c5c42e61a..2b26a3690 100644 --- a/Lib/python/argcargv.i +++ b/Lib/python/argcargv.i @@ -2,16 +2,15 @@ * --- Argc & Argv --- * ------------------------------------------------------------ */ -%fragment("SWIG_AsArgcArgv","header", - fragment="SWIG_AsCharPtr") { +%fragment("SWIG_AsArgcArgv","header",fragment="SWIG_AsCharPtrAndSize") { SWIGINTERN char** - SWIG_AsArgcArgv(PyObject* input, - swig_type_info* ppchar_info, - size_t* argc, int* owner) +SWIG_AsArgcArgv(PyObject* input, + swig_type_info* ppchar_info, + size_t* argc, int* owner) { char **argv = 0; size_t i = 0; - if (SWIG_ConvertPtr(input, (void **)&argv, ppchar_info, 0) == -1) { + if (SWIG_ConvertPtr(input, (void **)&argv, ppchar_info, 0) != SWIG_OK) { int list = 0; PyErr_Clear(); list = PyList_Check(input); @@ -21,10 +20,17 @@ SWIGINTERN char** *owner = 1; for (; i < *argc; ++i) { PyObject *obj = list ? PyList_GetItem(input,i) : PyTuple_GetItem(input,i); - if (!SWIG_AsCharPtr(obj, &(argv[i]))) { + char *cptr = 0; size_t size = 0; int alloc = 0; + if (SWIG_AsCharPtrAndSize(obj, &cptr, &size, &alloc) == SWIG_OK) { + if (cptr && size) { + argv[i] = (alloc == SWIG_NEWOBJ) ? cptr : SWIG_new_copy_array(cptr, size, char); + } else { + argv[i] = 0; + } + } else { PyErr_Clear(); PyErr_SetString(PyExc_TypeError,"list or tuple must contain strings only"); - } + } } argv[i] = 0; return argv; @@ -48,13 +54,11 @@ SWIGINTERN char** tuple */ -%typemap(in,fragment="SWIG_AsArgcArgv") (int ARGC, char **ARGV) - (int owner) { - size_t argc = 0; - char **argv = SWIG_AsArgcArgv($input, $descriptor(char**), &argc, &owner); - if (PyErr_Occurred()) { +%typemap(in,noblock=0,fragment="SWIG_AsArgcArgv") (int ARGC, char **ARGV) (char **argv = 0, size_t argc = 0, int owner= 0) { + argv = SWIG_AsArgcArgv($input, $descriptor(char**), &argc, &owner); + if (!argv) { $1 = 0; $2 = 0; - if (SWIG_arg_fail($argnum)) SWIG_fail; + SWIG_arg_fail(SWIG_TypeError, "int ARGC, char **ARGV", $argnum); } else { $1 = ($1_ltype) argc; $2 = ($2_ltype) argv; @@ -62,6 +66,12 @@ SWIGINTERN char** } %typemap(freearg) (int ARGC, char **ARGV) { - if (owner$argnum) SWIG_delete_array($2); + if (owner$argnum) { + size_t i = argc$argnum; + while (i) { + SWIG_delete_array(argv$argnum[--i]); + } + SWIG_delete_array(argv$argnum); + } } diff --git a/Lib/python/attribute.i b/Lib/python/attribute.i index 8bceaef7f..63833eaba 100644 --- a/Lib/python/attribute.i +++ b/Lib/python/attribute.i @@ -8,7 +8,7 @@ Attribute implementation using JOHN E LENZ ideas. The following macros convert a pair of set/get methods - into a "native" python attribute. + into a "native" attribute. Use %attribute when you have a pair of get/set methods like in: diff --git a/Lib/python/cdata.i b/Lib/python/cdata.i new file mode 100644 index 000000000..367965990 --- /dev/null +++ b/Lib/python/cdata.i @@ -0,0 +1 @@ +%include diff --git a/Lib/python/cstring.i b/Lib/python/cstring.i index 2a9a5be53..1ab035f5b 100644 --- a/Lib/python/cstring.i +++ b/Lib/python/cstring.i @@ -125,13 +125,13 @@ * } */ +%include %include -%include %typemap_cstrings(%cstring, char, SWIG_AsCharPtr, SWIG_AsCharPtrAndSize, SWIG_FromCharPtr, - SWIG_FromCharArray); + SWIG_FromCharPtrAndSize); diff --git a/Lib/python/cwstring.i b/Lib/python/cwstring.i index b9fe6cb04..7c31a64a1 100644 --- a/Lib/python/cwstring.i +++ b/Lib/python/cwstring.i @@ -125,13 +125,13 @@ */ +%include %include -%include %typemap_cstrings(%cwstring, wchar_t, SWIG_AsWCharPtr, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtr, - SWIG_FromWCharArray); + SWIG_FromWCharPtrAndSize); diff --git a/Lib/python/director_h.swg b/Lib/python/director_h.swg index 490edce67..19b83c10b 100644 --- a/Lib/python/director_h.swg +++ b/Lib/python/director_h.swg @@ -90,30 +90,40 @@ namespace Swig { protected: std::string swig_msg; public: - DirectorException(const char* hdr ="", const char* msg ="") - : swig_msg(hdr) { - swig_msg += msg; + DirectorException(PyObject *error, const char* hdr ="", const char* msg ="") + : swig_msg(hdr) + { + if (strlen(msg)) { + swig_msg += " "; + swig_msg += msg; + } if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, getMessage()); + SWIG_Python_SetErrorMsg(error, getMessage()); } else { - SWIG_Python_AddErrMesg(getMessage(), 1); + SWIG_Python_AddErrorMsg(getMessage()); } } - const char *getMessage() const { + const char *getMessage() const + { return swig_msg.c_str(); } - static void raise(const char* msg = "") + static void raise(PyObject *error, const char *msg) { - throw DirectorException(msg); + throw DirectorException(error, msg); + } + + static void raise(const char *msg) + { + raise(PyExc_RuntimeError, msg); } }; + /* unknown exception handler */ class UnknownExceptionHandler { - static void handler(); - + static void handler(); public: #ifdef SWIG_DIRECTOR_UEH @@ -133,11 +143,22 @@ namespace Swig { /* type mismatch in the return value from a python method call */ class DirectorTypeMismatchException : public Swig::DirectorException { public: - DirectorTypeMismatchException(const char* msg="") - : Swig::DirectorException("Swig director type mismatch: ", msg) { + DirectorTypeMismatchException(PyObject *error, const char* msg="") + : Swig::DirectorException(error, "Swig director type mismatch", msg) + { } - static void raise(const char* msg = "") + DirectorTypeMismatchException(const char* msg="") + : Swig::DirectorException(PyExc_TypeError, "Swig director type mismatch", msg) + { + } + + static void raise(PyObject *error, const char *msg) + { + throw DirectorTypeMismatchException(error, msg); + } + + static void raise(const char *msg) { throw DirectorTypeMismatchException(msg); } @@ -147,11 +168,11 @@ namespace Swig { class DirectorMethodException : public Swig::DirectorException { public: DirectorMethodException(const char* msg = "") - : DirectorException("Swig director python method error: ", msg) + : DirectorException(PyExc_RuntimeError, "Swig director method error", msg) { } - static void raise(const char* msg = "") + static void raise(const char *msg) { throw DirectorMethodException(msg); } @@ -162,11 +183,11 @@ namespace Swig { { public: DirectorPureVirtualException(const char* msg = "") - : DirectorException("Swig director pure virtal method called: ", msg) + : DirectorException(PyExc_RuntimeError, "Swig director pure virtal method called", msg) { } - static void raise(const char* msg = "") + static void raise(const char *msg) { throw DirectorPureVirtualException(msg); } diff --git a/Lib/python/file.i b/Lib/python/file.i index 03b03fe7f..25ea1dfdd 100644 --- a/Lib/python/file.i +++ b/Lib/python/file.i @@ -14,19 +14,19 @@ SWIG_AsValFilePtr(PyObject *obj, FILE **val) { static swig_type_info* desc = 0; FILE *ptr = 0; if (!desc) desc = SWIG_TypeQuery("FILE *"); - if ((SWIG_ConvertPtr(obj,(void **)(&ptr), desc, 0)) != -1) { + if ((SWIG_ConvertPtr(obj,(void **)(&ptr), desc, 0)) == SWIG_OK) { if (val) *val = ptr; - return 1; + return SWIG_OK; } if (PyFile_Check(obj)) { if (val) *val = PyFile_AsFile(obj); - return 1; + return SWIG_OK; } - if (val) PyErr_SetString(PyExc_TypeError, "a FILE* is expected"); - return 0; + return SWIG_TypeError; } } + %fragment("SWIG_AsFilePtr","header",fragment="SWIG_AsValFilePtr") { SWIGINTERNINLINE FILE* SWIG_AsFilePtr(PyObject *obj) { @@ -36,13 +36,5 @@ SWIG_AsFilePtr(PyObject *obj) { } } -%fragment("SWIG_CheckFilePtr","header",fragment="SWIG_AsValFilePtr") { -SWIGINTERNINLINE int -SWIG_CheckFilePtr(PyObject *obj) { - return SWIG_AsValFilePtr(obj, (FILE **)(0)); -} -} - /* defining the typemaps */ -%typemap_ascheck(SWIG_CCode(POINTER), SWIG_AsFilePtr, SWIG_CheckFilePtr, - "SWIG_AsFilePtr", "SWIG_CheckFilePtr", FILE*); +%typemap_asval(SWIG_CCode(POINTER), SWIG_AsValFilePtr, "SWIG_AsValFilePtr", FILE*); diff --git a/Lib/python/implicit.i b/Lib/python/implicit.i index 8ef687e31..aa4f9f983 100644 --- a/Lib/python/implicit.i +++ b/Lib/python/implicit.i @@ -1,217 +1,2 @@ %include - -/* - The %implict macro allows a SwigType to be accepted - as an input parameter and use its implicit constructors when needed. - - - %implicit(A, int, double, B); - - %inline - { - struct B { }; - struct A - { - int ii; - A(int i) { ii = 1; } - A(double d) { ii = 2; } - A(const B& b) { ii = 3; } - }; - - int get(A a) { return a.ii; } - } - - Here, you can call 'get' as - - get(1) ==> get(A(1)) - get(2.0) ==> get(A(2.0)) - get(B()) ==> get(A(B())) - - and swig will construct an 'A' temporal variable using the - corresponding implicit constructor. - - - The plain implicit macro takes care of simple type list. If it doesn't - work because you are passing template types with commas, then use - the %implicit_{1,2,3} versions, and the SWIG_arg macro. - -*/ - - -%define %implicit_type(...) -%traits_swigtype(__VA_ARGS__); -%enddef - -%define %implicit_frag(...) ,fragment=SWIG_Traits_frag(__VA_ARGS__) %enddef - -%define %implicit_code(...) - if (swig::check<__VA_ARGS__ >(obj)) { - if (val) *val = new value_type(swig::as<__VA_ARGS__ >(obj)); - return SWIG_NEWOBJ; - } -%enddef - -/* implicit */ - -%define %implicit(Type, ...) - -%formacro_1(%implicit_type,__VA_ARGS__); - -%fragment(SWIG_Traits_frag(Type),"header", - fragment="StdTraits" - %formacro_1(%implicit_frag,__VA_ARGS__)) %{ -namespace swig { - template <> struct traits { - typedef pointer_category category; - static const char* type_name() { return "Type"; } - }; - - template <> struct traits_asptr< Type > { - typedef Type value_type; - static int asptr(PyObject *obj, value_type **val) { - Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - if (PyErr_Occurred()) PyErr_Clear(); - %formacro_1(%implicit_code,__VA_ARGS__) - } - if (val) { - PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); - } - return 0; - } - }; -} -%} - -%typemap_traits_ptr(SWIG_CCode(POINTER),Type); -%enddef - -/* implicit_1 */ - - -%define %implicit_1(Type, Imp1) -%traits_swigtype(Imp1); - -%fragment(SWIG_Traits_frag(Type),"header", - fragment="StdTraits", - fragment=SWIG_Traits_frag(Imp1)) %{ -namespace swig { - template <> struct traits< Type > { - typedef pointer_category category; - static const char* type_name() { return "Type"; } - }; - - template <> struct traits_asptr< Type > { - typedef Type value_type; - static int asptr(PyObject *obj, value_type **val) { - Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - if (PyErr_Occurred()) PyErr_Clear(); - %implicit_code(Imp1); - } - if (val) { - PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); - } - return 0; - } - }; -} -%} - -%typemap_traits_ptr(SWIG_CCode(POINTER),Type); - -%enddef - -/* implicit_2 */ - -%define %implicit_2(Type, Imp1, Imp2) -%traits_swigtype(Imp1); -%traits_swigtype(Imp2); - -%fragment(SWIG_Traits_frag(Type),"header", - fragment="StdTraits", - fragment=SWIG_Traits_frag(Imp1), - fragment=SWIG_Traits_frag(Imp2)) %{ -namespace swig { - template <> struct traits< Type > { - typedef pointer_category category; - static const char* type_name() { return "Type"; } - }; - - template <> struct traits_asptr< Type > { - typedef Type value_type; - static int asptr(PyObject *obj, value_type **val) { - Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - if (PyErr_Occurred()) PyErr_Clear(); - %implicit_code(Imp1); - %implicit_code(Imp2); - } - if (val) { - PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); - } - return 0; - } - }; -} -%} - -%typemap_traits_ptr(SWIG_CCode(POINTER),Type); -%enddef - - -/* implicit_3 */ - -%define %implicit_3(Type, Imp1, Imp2, Imp3) -%traits_swigtype(Imp1); -%traits_swigtype(Imp2); -%traits_swigtype(Imp3); - -%fragment(SWIG_Traits_frag(Type),"header", - fragment="StdTraits", - fragment=SWIG_Traits_frag(Imp1), - fragment=SWIG_Traits_frag(Imp2), - fragment=SWIG_Traits_frag(Imp3)) %{ -namespace swig { - template <> struct traits< Type > { - typedef pointer_category category; - static const char* type_name() { return "Type"; } - }; - - template <> struct traits_asptr< Type > { - typedef Type value_type; - static int asptr(PyObject *obj, value_type **val) { - Type *vptr; - static swig_type_info* desc = SWIG_TypeQuery("Type *"); - if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) != -1)) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - if (PyErr_Occurred()) PyErr_Clear(); - %implicit_code(Imp1); - %implicit_code(Imp2); - %implicit_code(Imp3); - } - if (val) { - PyErr_SetString(PyExc_TypeError, "a " "Type" " is expected"); - } - return 0; - } - }; -} -%} - -%typemap_traits_ptr(SWIG_CCode(POINTER),Type); -%enddef +%include diff --git a/Lib/python/pyapi.swg b/Lib/python/pyapi.swg index 69ecf53e0..8c946297d 100644 --- a/Lib/python/pyapi.swg +++ b/Lib/python/pyapi.swg @@ -1,47 +1,16 @@ /* ----------------------------------------------------------------------------- - * SWIG API. Portion that goes into the runtime + * Python API portion that goes into the runtime * ----------------------------------------------------------------------------- */ #ifdef __cplusplus extern "C" { #endif -/* ----------------------------------------------------------------------------- - * for internal method declarations - * ----------------------------------------------------------------------------- */ - -#ifndef SWIGINTERN -# define SWIGINTERN static SWIGUNUSED -#endif - -#ifndef SWIGINTERNINLINE -# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE -#endif - -/* - Exception handling in wrappers -*/ -#define SWIG_fail goto fail -#define SWIG_arg_fail(arg) SWIG_Python_ArgFail(arg) -#define SWIG_append_errmsg(msg) SWIG_Python_AddErrMesg(msg,0) -#define SWIG_preppend_errmsg(msg) SWIG_Python_AddErrMesg(msg,1) -#define SWIG_type_error(type,obj) SWIG_Python_TypeError(type,obj) -#define SWIG_null_ref(type) SWIG_Python_NullRef(type) - -/* - Contract support -*/ -#define SWIG_contract_assert(expr, msg) \ - if (!(expr)) { PyErr_SetString(PyExc_RuntimeError, (char *) msg ); goto fail; } else - /* ----------------------------------------------------------------------------- * Constant declarations * ----------------------------------------------------------------------------- */ /* Constant Types */ -#define SWIG_PY_INT 1 -#define SWIG_PY_FLOAT 2 -#define SWIG_PY_STRING 3 #define SWIG_PY_POINTER 4 #define SWIG_PY_BINARY 5 @@ -56,12 +25,28 @@ typedef struct swig_const_info { } swig_const_info; + /* ----------------------------------------------------------------------------- - * Alloc. memory flags + * Append a value to the result obj * ----------------------------------------------------------------------------- */ -#define SWIG_OLDOBJ 1 -#define SWIG_NEWOBJ SWIG_OLDOBJ + 1 -#define SWIG_PYSTR SWIG_NEWOBJ + 1 +SWIGINTERN PyObject* +SWIG_Python_AppendResult(PyObject* result, PyObject* obj) { + if (!result) { + result = obj; + } else if (result == Py_None) { + Py_DECREF(result); + result = obj; + } else { + if (!PyList_Check(result)) { + PyObject *o2 = result; + result = PyList_New(1); + PyList_SetItem(result, 0, o2); + } + PyList_Append(result,obj); + Py_DECREF(obj); + } + return result; +} #ifdef __cplusplus } diff --git a/Lib/python/pycomplex.swg b/Lib/python/pycomplex.swg index 8b5d05906..ef7d90b46 100644 --- a/Lib/python/pycomplex.swg +++ b/Lib/python/pycomplex.swg @@ -10,42 +10,36 @@ /* the common from conversor */ %define %swig_fromcplx_conv(Type, Real, Imag) %fragment(SWIG_From_frag(Type),"header") -%{ +{ SWIGINTERNINLINE PyObject* - SWIG_From(Type)(SWIG_cplusplus(const Type&, Type) c) +SWIG_From(Type)(SWIG_cplusplus(const Type&, Type) c) { return PyComplex_FromDoubles(Real(c), Imag(c)); } -%} +} %enddef /* the double case */ %define %swig_cplxdbl_conv(Type, Constructor, Real, Imag) %fragment(SWIG_AsVal_frag(Type),"header", fragment=SWIG_AsVal_frag(double)) -%{ +{ SWIGINTERN int - SWIG_AsVal(Type) (PyObject *o, Type* val) +SWIG_AsVal(Type) (PyObject *o, Type* val) { if (PyComplex_Check(o)) { - if (val) *val = Constructor(PyComplex_RealAsDouble(o), - PyComplex_ImagAsDouble(o)); - return 1; + if (val) *val = Constructor(PyComplex_RealAsDouble(o), PyComplex_ImagAsDouble(o)); + return SWIG_OK; } else { double d; - if (SWIG_AsVal(double)(o, &d)) { + if (SWIG_AsVal(double)(o, &d) == SWIG_OK) { if (val) *val = Constructor(d, 0.0); - return 1; - } else { - PyErr_Clear(); + return SWIG_OK; } } - if (val) { - SWIG_type_error("Type", o); - } - return 0; + return SWIG_TypeError; +} } -%} %swig_fromcplx_conv(Type, Real, Imag); %enddef @@ -55,37 +49,26 @@ SWIGINTERN int fragment="SWIG_CheckDoubleInRange", fragment=SWIG_AsVal_frag(float)) { SWIGINTERN int - SWIG_AsVal(Type)(PyObject *o, Type *val) +SWIG_AsVal(Type)(PyObject *o, Type *val) { - const char* errmsg = val ? #Type : 0; if (PyComplex_Check(o)) { double re = PyComplex_RealAsDouble(o); double im = PyComplex_ImagAsDouble(o); - if (SWIG_CheckDoubleInRange(re, -FLT_MAX, FLT_MAX, errmsg) - && SWIG_CheckDoubleInRange(im, -FLT_MAX, FLT_MAX, errmsg)) { + if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) { if (val) *val = Constructor(SWIG_numeric_cast(re, float), SWIG_numeric_cast(im, float)); - return 1; + return SWIG_OK; } else { - return 0; + return SWIG_TypeError; } } else { - double re; - if (SWIG_AsVal(double)(o, &re)) { - if (SWIG_CheckDoubleInRange(re, -FLT_MAX, FLT_MAX, errmsg)) { - if (val) *val = Constructor(SWIG_numeric_cast(re,float), 0.0); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); + float re; + if (SWIG_AsVal(float)(o, &re) == SWIG_OK) { + if (val) *val = Constructor(re, 0.0); + return SWIG_OK; } } - if (val) { - SWIG_type_error("Type", o); - } - return 0; + return SWIG_TypeError; } } diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 210a8b92e..1da325cd6 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -23,7 +23,7 @@ %} %fragment("PySequence_Base","header") -%{ +{ namespace swig { inline size_t check_index(ptrdiff_t i, size_t size, bool insert = false) { @@ -115,13 +115,13 @@ namespace swig { } } } -%} +} %fragment("PySequence_Cont","header", fragment="StdTraits", fragment="PySequence_Base", fragment="PyObject_var") -%{ +{ #include namespace swig { @@ -140,12 +140,12 @@ namespace swig return swig::as(item, true); } catch (std::exception& e) { char msg[1024]; - PyOS_snprintf(msg, sizeof(msg), "in sequence element %d ", _index); + snprintf(msg, sizeof(msg), "in sequence element %d ", _index); if (!PyErr_Occurred()) { - SWIG_type_error(swig::type_name(), item); + SWIG_set_errmsg(SWIG_TypeError,swig::type_name()); } - SWIG_append_errmsg(msg); - SWIG_append_errmsg(e.what()); + SWIG_Python_AddErrorMsg(msg); + SWIG_Python_AddErrorMsg(e.what()); throw; } } @@ -335,9 +335,8 @@ namespace swig if (!swig::check(item)) { if (set_err) { char msg[1024]; - PyOS_snprintf(msg, sizeof(msg), "in sequence element %d", i); - SWIG_type_error(swig::type_name(), item); - SWIG_append_errmsg(msg); + snprintf(msg, sizeof(msg), "in sequence element %d of type %s", i, swig::type_name()); + SWIG_set_errmsg(SWIG_TypeError, msg); } return 0; } @@ -350,7 +349,7 @@ namespace swig }; } -%} +} /**** The python container methods ****/ @@ -444,20 +443,20 @@ namespace swig %fragment("StdSequenceTraits","header", fragment="StdTraits",fragment="PyObject_var", fragment="PySequence_Cont") -%{ +{ namespace swig { template inline void assign(const PySeq& pyseq, Seq* seq) { -#ifdef SWIG_STD_NOASSIGN_STL +%#ifdef SWIG_STD_NOASSIGN_STL typedef typename PySeq::value_type value_type; typename PySeq::const_iterator it = pyseq.begin(); for (;it != pyseq.end(); ++it) { seq->insert(seq->end(),(value_type)(*it)); } -#else +%#else seq->assign(pyseq.begin(), pyseq.end()); -#endif +%#endif } template @@ -487,7 +486,7 @@ namespace swig } else { sequence *p; if (SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0) != -1) { + swig::type_info(),0) == SWIG_OK) { if (seq) *seq = p; return 1; } @@ -525,4 +524,4 @@ namespace swig } }; } -%} +} diff --git a/Lib/python/pyenum.swg b/Lib/python/pyenum.swg deleted file mode 100644 index d95eebdf1..000000000 --- a/Lib/python/pyenum.swg +++ /dev/null @@ -1,52 +0,0 @@ -/* ------------------------------------------------------------ - * Enums - * ------------------------------------------------------------ */ -%apply int { enum SWIGTYPE }; -%apply const int& { const enum SWIGTYPE& }; - -%typemap(in,fragment=SWIG_As_frag(int)) const enum SWIGTYPE& ($basetype temp) { - temp = SWIG_static_cast(SWIG_As(int)($input),$basetype); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = &temp; -} - - -%typemap(varin,fragment=SWIG_AsVal_frag(int)) enum SWIGTYPE -{ - if (sizeof(int) != sizeof($1)) { - PyErr_SetString(PyExc_AttributeError, "enum variable '$name' can not be set"); - return 1; - } - if (!SWIG_AsVal(int)($input, (int*)(void*)(&$1))) { - return 1; - } -} - -/* - typemaps needed due to unnamed enums -*/ -%define PY_ENUM_OUT_TYPEMAPS(from_meth,pyfrag) - %typemap(out,fragment=pyfrag) enum SWIGTYPE - "$result = from_meth(($1));"; - %typemap(out,fragment=pyfrag) const enum SWIGTYPE& - "$result = from_meth((*$1));"; - %typemap(varout,fragment=pyfrag) enum SWIGTYPE, const enum SWIGTYPE& - "$result = from_meth($1);"; - %typemap(constcode,fragment=pyfrag) enum SWIGTYPE - "PyDict_SetItemString(d,\"$symname\", from_meth($value));"; - %typemap(directorin,fragment=pyfrag) enum SWIGTYPE *DIRECTORIN - "$input = from_meth(*$1_name);"; - %typemap(directorin,fragment=pyfrag) enum SWIGTYPE, const enum SWIGTYPE& - "$input = from_meth($1_name);"; - %typemap(throws,fragment=pyfrag) enum SWIGTYPE - "PyErr_SetObject(PyExc_RuntimeError, from_meth($1)); - SWIG_fail;"; -%enddef - -PY_ENUM_OUT_TYPEMAPS(SWIG_From(int),SWIG_From_frag(int)); - - - - - - diff --git a/Lib/python/pyerrors.swg b/Lib/python/pyerrors.swg new file mode 100644 index 000000000..e4f35195d --- /dev/null +++ b/Lib/python/pyerrors.swg @@ -0,0 +1,81 @@ +/* ----------------------------------------------------------------------------- + * error manipulation + * ----------------------------------------------------------------------------- */ + + +%insert("header") %{ + +SWIGINTERN PyObject* +SWIG_Python_ErrorType(int code) { + switch(code) { + case SWIG_MemoryError: + return PyExc_MemoryError; + break; + case SWIG_IOError: + return PyExc_IOError; + break; + case SWIG_RuntimeError: + return PyExc_RuntimeError; + break; + case SWIG_IndexError: + return PyExc_IndexError; + break; + case SWIG_TypeError: + return PyExc_TypeError; + break; + case SWIG_DivisionByZero: + return PyExc_ZeroDivisionError; + break; + case SWIG_OverflowError: + return PyExc_OverflowError; + break; + case SWIG_SyntaxError: + return PyExc_SyntaxError; + break; + case SWIG_ValueError: + return PyExc_ValueError; + break; + case SWIG_SystemError: + return PyExc_SystemError; + break; + case SWIG_AttributeError: + return PyExc_AttributeError; + break; + default: + return PyExc_RuntimeError; + break; + } +} + +SWIGINTERN void +SWIG_Python_SetErrorMsg(PyObject *type, const char *mesg) { + PyErr_SetString(type, mesg); +} + +SWIGINTERNINLINE void +SWIG_Python_SetExceptionObj(swig_type_info *desc, PyObject *obj) { + PyErr_SetObject((desc && desc->clientdata ? (PyObject *)(desc->clientdata) : PyExc_RuntimeError), obj); +} + + +SWIGINTERN void +SWIG_Python_AddErrorMsg(const char* mesg) +{ + PyObject *type = 0; + PyObject *value = 0; + PyObject *traceback = 0; + + if (PyErr_Occurred()) PyErr_Fetch(&type, &value, &traceback); + if (value) { + PyObject *old_str = PyObject_Str(value); + Py_XINCREF(type); + PyErr_Clear(); + PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg); + Py_DECREF(old_str); + } else { + PyErr_Format(PyExc_RuntimeError, mesg); + } +} + +%} + diff --git a/Lib/python/pyinit.swg b/Lib/python/pyinit.swg index d0fbdde16..3541cbafc 100644 --- a/Lib/python/pyinit.swg +++ b/Lib/python/pyinit.swg @@ -173,33 +173,18 @@ SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]) { size_t i; for (i = 0; constants[i].type; ++i) { switch(constants[i].type) { - case SWIG_PY_INT: - obj = PyInt_FromLong(constants[i].lvalue); - break; - case SWIG_PY_FLOAT: - obj = PyFloat_FromDouble(constants[i].dvalue); - break; - case SWIG_PY_STRING: - if (constants[i].pvalue) { - obj = PyString_FromString((char *) constants[i].pvalue); - } else { - Py_INCREF(Py_None); - obj = Py_None; - } - break; case SWIG_PY_POINTER: obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0); break; case SWIG_PY_BINARY: - obj = SWIG_NewPackedObj(constants[i].pvalue, constants[i].lvalue, *(constants[i].ptype)); + obj = SWIG_NewPackedObj(constants[i].pvalue, constants[i].lvalue, *(constants[i].ptype), 0); break; default: obj = 0; break; } if (obj) { - PyDict_SetItemString(d,constants[i].name,obj); - Py_DECREF(obj); + PyDict_SetItemString(d, constants[i].name, obj); } } } @@ -250,10 +235,6 @@ SWIG_Python_FixMethods(PyMethodDef *methods, } } -/* -----------------------------------------------------------------------------* - * Initialize type list - * -----------------------------------------------------------------------------*/ - #ifdef __cplusplus } #endif diff --git a/Lib/python/pyinout.swg b/Lib/python/pyinout.swg deleted file mode 100644 index 196d63918..000000000 --- a/Lib/python/pyinout.swg +++ /dev/null @@ -1,233 +0,0 @@ -// -// Uncomment the following definition if you don't want the in/out -// typemaps by default, ie, you prefer to use typemaps.i. -// -//#define SWIG_INOUT_NODEF - -// -// Use the following definition to enable the INPUT parameters to -// accept both 'by value' and 'pointer' objects. -// -#define SWIG_INPUT_ACCEPT_PTRS - -// ------------------------------------------------------------------------ -// Pointer handling -// -// These mappings provide support for input/output arguments and common -// uses for C/C++ pointers. -// ------------------------------------------------------------------------ - -// INPUT typemaps. -// These remap a C pointer to be an "INPUT" value which is passed by value -// instead of reference. - -/* -The following methods can be applied to turn a pointer into a simple -"input" value. That is, instead of passing a pointer to an object, -you would use a real value instead. - -To use these, suppose you had a C function like this : - - double fadd(double *a, double *b) { - return *a+*b; - } - -You could wrap it with SWIG as follows : - - double fadd(double *INPUT, double *INPUT); - -or you can use the %apply directive : - - %apply double *INPUT { double *a, double *b }; - double fadd(double *a, double *b); - -*/ -#ifdef SWIG_INPUT_ACCEPT_PTRS -#define SWIG_CheckInputPtr(input,arg,desc,disown) (SWIG_ConvertPtr(input,arg,desc,disown) != -1) -#else -#define SWIG_CheckInputPtr(input,arg,desc,disown) (0) -#endif - -%define _PYVAL_INPUT_TYPEMAP(code,as_meth,check_meth,as_frag,check_frag,Type) - %typemap(in,fragment=as_frag) Type *INPUT ($*1_ltype temp, int res = 0) { - if (!SWIG_CheckInputPtr($input,(void **)(&$1),$1_descriptor,$disown)) { - temp = as_meth($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = &temp; - res = SWIG_NEWOBJ; - } - } - %typemap(in,fragment=as_frag) Type &INPUT($*1_ltype temp, int res = 0) { - if (!SWIG_CheckInputPtr($input,(void **)(&$1),$1_descriptor,$disown)) { - temp = as_meth($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = &temp; - res = SWIG_NEWOBJ; - } - if (!$1) { - SWIG_null_ref("$basetype"); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; - } - %typemap(typecheck,precedence=code,fragment=check_frag) Type *INPUT, Type &INPUT { - void *ptr; - $1 = (check_meth($input) || (SWIG_CheckInputPtr($input,&ptr,$1_descriptor,0))); - } -%enddef - -%define _PYPTR_INPUT_TYPEMAP(code,asptr_meth,asptr_frag,Type) - %typemap(in,fragment=asptr_frag) Type *INPUT(int res = 0) { - res = asptr_meth($input, &$1); - if (!res) { - SWIG_type_error("$basetype", $input); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; - } - - %typemap(in,fragment=asptr_frag) Type &INPUT(int res = 0) { - res = asptr_meth($input, &$1); - if (!res) { - SWIG_type_error("$basetype", $input); - } else { - if (!$1) { - SWIG_null_ref("$basetype"); - } - } - if (SWIG_arg_fail($argnum)) SWIG_fail; - } - %typemap(freearg) Type *INPUT, Type &INPUT - "if (res$argnum == SWIG_NEWOBJ) delete $1;"; - %typemap(typecheck,precedence=code,fragment=asptr_frag) Type *INPUT, Type &INPUT - "$1 = asptr_meth($input, (Type**)0) != 0;" -%enddef - -// OUTPUT typemaps. These typemaps are used for parameters that -// are output only. The output value is appended to the result as -// a list element. - -/* -The following methods can be applied to turn a pointer into an "output" -value. When calling a function, no input value would be given for -a parameter, but an output value would be returned. In the case of -multiple output values, they are returned in the form of a Python tuple. - - -For example, suppose you were trying to wrap the modf() function in the -C math library which splits x into integral and fractional parts (and -returns the integer part in one of its parameters).K: - - double modf(double x, double *ip); - -You could wrap it with SWIG as follows : - - double modf(double x, double *OUTPUT); - -or you can use the %apply directive : - - %apply double *OUTPUT { double *ip }; - double modf(double x, double *ip); - -The Python output of the function would be a tuple containing both -output values. - -*/ - -// These typemaps contributed by Robin Dunn -//---------------------------------------------------------------------- -// -// T_OUTPUT typemap (and helper function) to return multiple argouts as -// a tuple instead of a list. -// -// Author: Robin Dunn -//---------------------------------------------------------------------- - -%include - -%define _PYVAL_OUTPUT_TYPEMAP(from_meth, from_frag, Type) - %typemap(in,numinputs=0) Type *OUTPUT ($*1_ltype temp, int res = 0), - Type &OUTPUT ($*1_ltype temp, int res = 0) - "$1 = &temp; res = SWIG_NEWOBJ;"; - %fragment("t_out_helper"{Type},"header", - fragment="t_output_helper",fragment=from_frag) {} - %typemap(argout,fragment="t_out_helper"{Type}) Type *OUTPUT, Type &OUTPUT - "$result = t_output_helper($result, ((res$argnum == SWIG_NEWOBJ) ? - from_meth((*$1)) : SWIG_NewPointerObj((void*)($1), $1_descriptor, 0)));"; - -%enddef - - -// INOUT -// Mappings for an argument that is both an input and output -// parameter - -/* -The following methods can be applied to make a function parameter both -an input and output value. This combines the behavior of both the -"INPUT" and "OUTPUT" methods described earlier. Output values are -returned in the form of a Python tuple. - -For example, suppose you were trying to wrap the following function : - - void neg(double *x) { - *x = -(*x); - } - -You could wrap it with SWIG as follows : - - void neg(double *INOUT); - -or you can use the %apply directive : - - %apply double *INOUT { double *x }; - void neg(double *x); - -Unlike C, this mapping does not directly modify the input value (since -this makes no sense in Python). Rather, the modified input value shows -up as the return value of the function. Thus, to apply this function -to a Python variable you might do this : - - x = neg(x) - -Note : previous versions of SWIG used the symbol 'BOTH' to mark -input/output arguments. This is still supported, but will be slowly -phased out in future releases. - -*/ - -%define _PYVAL_INOUT_TYPEMAP(Type) - %typemap(in) Type *INOUT = Type *INPUT; - %typemap(in) Type &INOUT = Type &INPUT; - %typemap(typecheck) Type *INOUT = Type *INPUT; - %typemap(typecheck) Type &INOUT = Type &INPUT; - %typemap(argout) Type *INOUT = Type *OUTPUT; - %typemap(argout) Type &INOUT = Type &OUTPUT; -%enddef - - -%define _PYPTR_INOUT_TYPEMAP(Type) - _PYVAL_INOUT_TYPEMAP(SWIG_arg(Type)) - %typemap(freearg) Type *INOUT = Type *INPUT; - %typemap(freearg) Type &INOUT = Type &INPUT; -%enddef - -#ifndef SWIG_INOUT_NODEF -#define PYVAL_INPUT_TYPEMAP(code,_a,_c,_af,_cf,...) \ - _PYVAL_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_c), \ - SWIG_arg(_af),SWIG_arg(_cf),SWIG_arg(__VA_ARGS__)) - -#define PYPTR_INPUT_TYPEMAP(code,_a,_af,...) \ - _PYPTR_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_af), \ - SWIG_arg(__VA_ARGS__)) - -#define PYVAL_OUTPUT_TYPEMAP(_f,_ff,...) \ - _PYVAL_OUTPUT_TYPEMAP(SWIG_arg(_f),SWIG_arg(_ff),SWIG_arg(__VA_ARGS__)) - -#define PYVAL_INOUT_TYPEMAP(...) _PYVAL_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__)) -#define PYPTR_INOUT_TYPEMAP(...) _PYPTR_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__)) -#else /* You need to include typemaps.i */ -#define PYVAL_OUTPUT_TYPEMAP(...) -#define PYVAL_INPUT_TYPEMAP(...) -#define PYVAL_INOUT_TYPEMAP(...) -#define PYPTR_INPUT_TYPEMAP(...) -#define PYPTR_INOUT_TYPEMAP(...) -#endif /* SWIG_INOUT_DEFAULT */ diff --git a/Lib/python/pymacros.swg b/Lib/python/pymacros.swg index fff615664..4fd0d1f90 100644 --- a/Lib/python/pymacros.swg +++ b/Lib/python/pymacros.swg @@ -1,159 +1 @@ -/* ----------------------------------------------------------------------------- - * SWIG API. Portion only visible from SWIG - * ----------------------------------------------------------------------------- */ - -#define SWIG_arg(Arg...) Arg - -#define SWIG_str(Type...) #Type - -#define SWIG_Mangle(Type...) #@Type -#define SWIG_Descriptor(Type...) SWIGTYPE_ ## #@Type - -#define SWIG_NameType(Name, Type...) SWIG_ ## Name ## _ ## #@Type -#define SWIG_StringType(Name, Type...) "SWIG_" #Name "_" {Type} - -#define SWIG_AsVal(Type...) SWIG_NameType(AsVal, Type) -#define SWIG_AsPtr(Type...) SWIG_NameType(AsPtr, Type) -#define SWIG_As(Type...) SWIG_NameType(As, Type) -#define SWIG_From(Type...) SWIG_NameType(From, Type) -#define SWIG_Check(Type...) SWIG_NameType(Check, Type) -#define SWIG_CCode(Type...) SWIG_NameType(TYPECHECK, Type) -#define SWIG_OrderType(Type...) SWIG_NameType(OrderType, Type) -#define SWIG_EqualType(Type...) SWIG_NameType(EqualType, Type) - -#define SWIG_Traits_frag(Type...) SWIG_StringType(Traits, Type) -#define SWIG_AsPtr_frag(Type...) SWIG_StringType(AsPtr, Type) -#define SWIG_AsVal_frag(Type...) SWIG_StringType(AsVal, Type) -#define SWIG_As_frag(Type...) SWIG_StringType(As, Type) -#define SWIG_From_frag(Type...) SWIG_StringType(From, Type) -#define SWIG_Check_frag(Type...) SWIG_StringType(Check, Type) -#define SWIG_CCode_frag(Type...) SWIG_StringType(TYPECHECK, Type) - -/* Internal C/C++ API */ -#ifdef SWIG_NO_CPLUSPLUS_CAST -/* ----------------------------------------------------------------------------- - * Disable 'modern' cplusplus casting operators - * ----------------------------------------------------------------------------- */ -#ifdef SWIG_CPLUSPLUS_CAST -#undef SWIG_CPLUSPLUS_CAST -#endif -#endif - -#if defined(__cplusplus) -#define SWIG_new(Type...) (new Type) -#define SWIG_new_copy(ptr,Type...) (new Type(*ptr)) -#define SWIG_new_array(size,Type...) (new Type[(size)]) -#define SWIG_delete(cptr) delete cptr -#define SWIG_delete_array(cptr) delete[] cptr -#else /* C case */ -#define SWIG_new(Type...) ((Type*)malloc(sizeof(Type))) -#define SWIG_new_copy(ptr,Type...) ((Type*)memcpy(malloc(sizeof(Type)),ptr,sizeof(Type))) -#define SWIG_new_array(size,Type...) ((Type*) malloc((size)*sizeof(Type))) -#define SWIG_delete(cptr) free((char*)cptr) -#define SWIG_delete_array(cptr) free((char*)cptr) -#endif /* __cplusplus */ - -#if defined(__cplusplus) && defined(SWIG_CPLUSPLUS_CAST) -#define SWIG_const_cast(a,Type...) const_cast(a) -#define SWIG_static_cast(a,Type...) static_cast(a) -#define SWIG_reinterpret_cast(a,Type...) reinterpret_cast(a) -#define SWIG_numeric_cast(a,Type...) static_cast(a) -#else /* C case */ -#define SWIG_const_cast(a,Type...) (Type)(a) -#define SWIG_static_cast(a,Type...) (Type)(a) -#define SWIG_reinterpret_cast(a,Type...) (Type)(a) -#define SWIG_numeric_cast(a,Type...) (Type)(a) -#endif /* __cplusplus */ - - -/* ----------------------------------------------------------------------------- - * Auxiliar swig macros used to write typemaps - * ----------------------------------------------------------------------------- */ - -/* define a new macro */ -%define SWIG_define(Def, Val) -%#define Def Val -%enddef - -/* include C++ or C value */ -%define SWIG_cplusplus(cppval, cval) -#ifdef __cplusplus -cppval -#else -cval -#endif -%enddef - -/* for loop for macro with one argument */ -%define %_formacro_1(macro, arg1,...) -macro(arg1) -#if #__VA_ARGS__ != "__fordone__" -%_formacro_1(macro, __VA_ARGS__) -#endif -%enddef - -/* for loop for macro with one argument */ -%define %formacro_1(macro,...) -%_formacro_1(macro,__VA_ARGS__,__fordone__) -%enddef - -/* for loop for macro with two arguments */ -%define %_formacro_2(macro, arg1, arg2, ...) -macro(arg1, arg2) -#if #__VA_ARGS__ != "__fordone__" -%_formacro_2(macro, __VA_ARGS__) -#endif -%enddef - -/* for loop for macro with two arguments */ -%define %formacro_2(macro,...) -%_formacro_2(macro, __VA_ARGS__, __fordone__) -%enddef - - -/* - mark a flag, ie, define a macro name but ignore it in - the interface. - - the flags latter can be used with %evalif -*/ - -%define %swig_mark_flag(x) -%ignore x; -#define x 1 -%enddef - -/* - %swig_equal_type and %swig_order_type flagged a type of having equal (==,!=) - and/or order methods (<=,>=,<,>). -*/ -#define %swig_equal_type(...) %swig_mark_flag(SWIG_EqualType(__VA_ARGS__)) -#define %swig_order_type(...) \ - %swig_mark_flag(SWIG_EqualType(__VA_ARGS__)) \ - %swig_mark_flag(SWIG_OrderType(__VA_ARGS__)) - -/* - %evalif and %evalif_2 are use to evaluate or process - an expression if the given predicate is 'true' (1). -*/ -%define %_evalif(_x,_expr) -#if _x == 1 -_expr -#endif -%enddef - -%define %_evalif_2(_x,_y,_expr) -#if _x == 1 && _y == 1 -_expr -#endif -%enddef - -%define %evalif(_x,...) - %_evalif(SWIG_arg(_x),SWIG_arg(__VA_ARGS__)) -%enddef - -%define %evalif_2(_x,_y,...) - %_evalif_2(SWIG_arg(_x),SWIG_arg(_y),SWIG_arg(__VA_ARGS__)) -%enddef - - +%include diff --git a/Lib/python/pyobject.swg b/Lib/python/pyobject.swg deleted file mode 100644 index 06048431f..000000000 --- a/Lib/python/pyobject.swg +++ /dev/null @@ -1,17 +0,0 @@ -/* ------------------------------------------------------------ - * PyObject * - Just pass straight through unmodified - * ------------------------------------------------------------ */ - -%typemap(in) PyObject * "$1 = $input;"; -%typemap(out) PyObject * "$result = $1;"; - -%typemap(constcode) PyObject * "PyDict_SetItemString(d,\"$symname\", $value);"; - -%typemap(directorin, parse="O") PyObject * ""; -%typemap(directorout) PyObject * "$result = $input;"; - -%typecheck(SWIG_TYPECHECK_POINTER) PyObject * "$1 = ($input != 0);"; - -%typemap(throws) PyObject * - "PyErr_SetObject(PyExc_RuntimeError, $1); - SWIG_fail;"; diff --git a/Lib/python/pyprimtypes.swg b/Lib/python/pyprimtypes.swg index df4b6d0bc..e0f05e832 100644 --- a/Lib/python/pyprimtypes.swg +++ b/Lib/python/pyprimtypes.swg @@ -1,561 +1,28 @@ +%include + +/* Macro for 'signed long' derived types */ + +%define %type_slong(Type, Frag, Min, Max) +%derived_type_from(long, Type) +%signed_derived_type_asval(long, Type, Frag, Min, Max) +%enddef + +/* Macro for 'unsigned long' derived types */ + +%define %type_ulong(Type, Frag, Max) +%derived_type_from(unsigned long, Type) +%unsigned_derived_type_asval(unsigned long, Type, Frag, Max) +%enddef + /* ------------------------------------------------------------ * Primitive Types * ------------------------------------------------------------ */ -/* - Define the SWIG_As/From methods for the basic types. In many - cases, these method are just aliases of the original python As/From - methods. In the other cases, some extra work is needed. -*/ - -%fragment(SWIG_From_frag(signed char),"header") { - SWIG_define(SWIG_From(signed char), PyInt_FromLong) -} - -%fragment(SWIG_From_frag(unsigned char),"header") { - SWIG_define(SWIG_From(unsigned char), PyInt_FromLong) -} - -%fragment(SWIG_From_frag(short),"header") { - SWIG_define(SWIG_From(short), PyInt_FromLong) -} - -%fragment(SWIG_From_frag(unsigned short),"header") { - SWIG_define(SWIG_From(unsigned short), PyInt_FromLong) -} - -%fragment(SWIG_From_frag(int),"header") { - SWIG_define(SWIG_From(int), PyInt_FromLong) -} - -%fragment(SWIG_From_frag(long),"header") { - SWIG_define(SWIG_From(long), PyInt_FromLong) -} - -%fragment(SWIG_From_frag(float),"header") { - SWIG_define(SWIG_From(float), PyFloat_FromDouble) -} - -%fragment(SWIG_From_frag(double),"header") { - SWIG_define(SWIG_From(double), PyFloat_FromDouble) -} - -/* - Here, we have all the complex AsVal/From methods -*/ - -%fragment("","header") %{ -#include -%} - -%fragment(SWIG_AsVal_frag(unsigned long),"header") { -SWIGINTERN int - SWIG_AsVal(unsigned long)(PyObject *obj, unsigned long *val) -{ - if (PyLong_Check(obj)) { - unsigned long v = PyLong_AsUnsignedLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return 1; - } else { - if (!val) PyErr_Clear(); - return 0; - } - } - if (PyInt_Check(obj)) { - long v = PyInt_AsLong(obj); - if (v >= 0) { - if (val) *val = v; - return 1; - } - } - if (val) { - SWIG_type_error("unsigned long", obj); - } - return 0; -} -} - -%fragment("SWIG_CheckLongInRange","header", - fragment="") { -SWIGINTERN int - SWIG_CheckLongInRange(long value, long min_value, long max_value, - const char *errmsg) -{ - if (value < min_value) { - if (errmsg) { - PyErr_Format(PyExc_OverflowError, - "value %ld is less than '%s' minimum %ld", - value, errmsg, min_value); - } - return 0; - } else if (value > max_value) { - if (errmsg) { - PyErr_Format(PyExc_OverflowError, - "value %ld is greater than '%s' maximum %ld", - value, errmsg, max_value); - } - return 0; - } - return 1; -} -} - -%fragment("SWIG_CheckUnsignedLongInRange","header", - fragment="") { -SWIGINTERNINLINE int - SWIG_CheckUnsignedLongInRange(unsigned long value, - unsigned long max_value, - const char *errmsg) -{ - if (value > max_value) { - if (errmsg) { - PyErr_Format(PyExc_OverflowError, - "value %lu is greater than '%s' minimum %lu", - value, errmsg, max_value); - } - return 0; - } - return 1; - } -} - - -%fragment(SWIG_AsVal_frag(double),"header") { -SWIGINTERN int - SWIG_AsVal(double)(PyObject *obj, double *val) -{ - if (PyFloat_Check(obj)) { - if (val) *val = PyFloat_AsDouble(obj); - return 1; - } - if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); - return 1; - } - if (PyLong_Check(obj)) { - double v = PyLong_AsDouble(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return 1; - } else { - if (!val) PyErr_Clear(); - return 0; - } - } - if (val) { - SWIG_type_error("double", obj); - } - return 0; -} -} - -%fragment(SWIG_AsVal_frag(long),"header") { -SWIGINTERN int - SWIG_AsVal(long)(PyObject * obj, long* val) -{ - if (PyLong_Check(obj)) { - long v = PyLong_AsLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return 1; - } else { - if (!val) PyErr_Clear(); - return 0; - } - } - if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); - return 1; - } - if (val) { - SWIG_type_error("long", obj); - } - return 0; - } -} - - -%fragment(SWIG_From_frag(long long),"header", - fragment="") { -SWIGINTERNINLINE PyObject* - SWIG_From(long long)(long long value) -{ - return ((value < LONG_MIN) || (value > LONG_MAX)) ? - PyLong_FromLongLong(value) - : PyInt_FromLong(SWIG_numeric_cast(value,long)); -} -} - - -%fragment(SWIG_From_frag(unsigned long long),"header", - fragment="") { -SWIGINTERNINLINE PyObject* - SWIG_From(unsigned long long)(unsigned long long value) -{ - return (value > LONG_MAX) ? - PyLong_FromUnsignedLongLong(value) : - PyInt_FromLong(SWIG_numeric_cast(value,long)); -} -} - -%fragment(SWIG_AsVal_frag(long long),"header") { -SWIGINTERN int - SWIG_AsVal(long long)(PyObject *obj, long long *val) -{ - if (PyLong_Check(obj)) { - long long v = PyLong_AsLongLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return 1; - } else { - if (!val) PyErr_Clear(); - return 0; - } - } - if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); - return 1; - } - if (val) { - SWIG_type_error("long long", obj); - } - return 0; -} -} - -%fragment(SWIG_AsVal_frag(unsigned long long),"header", - fragment=SWIG_AsVal_frag(unsigned long)) { -SWIGINTERN int - SWIG_AsVal(unsigned long long)(PyObject *obj, unsigned long long *val) -{ - unsigned long v; - if (PyLong_Check(obj)) { - unsigned long long v = PyLong_AsUnsignedLongLong(obj); - if (!PyErr_Occurred()) { - if (val) *val = v; - return 1; - } else { - if (!val) PyErr_Clear(); - return 0; - } - } - if (SWIG_AsVal(unsigned long)(obj,&v)) { - if (val) *val = v; - return 1; - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error("unsigned long long", obj); - } - return 0; -} -} - -%fragment(SWIG_From_frag(unsigned long),"header") { -SWIGINTERNINLINE PyObject* - SWIG_From(unsigned long)(unsigned long value) -{ - return (value > LONG_MAX) ? - PyLong_FromUnsignedLong(value) - : PyInt_FromLong(SWIG_numeric_cast(value,long)); -} -} - -%fragment(SWIG_AsVal_frag(signed char),"header", - fragment="SWIG_CheckLongInRange", - fragment=SWIG_AsVal_frag(long)) { -SWIGINTERN int - SWIG_AsVal(signed char)(PyObject *obj, signed char *val) -{ - const char* errmsg = val ? "signed char" : (char*) 0; - long v; - if (SWIG_AsVal(long)(obj, &v)) { - if (SWIG_CheckLongInRange(v, SCHAR_MIN, SCHAR_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, signed char); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -} - -%fragment(SWIG_AsVal_frag(short),"header", - fragment="SWIG_CheckLongInRange", - fragment=SWIG_AsVal_frag(long)) { -SWIGINTERN int - SWIG_AsVal(short)(PyObject *obj, short *val) -{ - const char* errmsg = val ? "short" : (char*)0; - long v; - if (SWIG_AsVal(long)(obj, &v)) { - if (SWIG_CheckLongInRange(v, SHRT_MIN, SHRT_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, short); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -} - -/* need range checks */ - -%fragment(SWIG_AsVal_frag(int),"header", - fragment="SWIG_CheckLongInRange", - fragment=SWIG_AsVal_frag(long)) { -%#if INT_MAX != LONG_MAX -SWIGINTERN int - SWIG_AsVal(int)(PyObject *obj, int *val) -{ - const char* errmsg = val ? "int" : (char*)0; - long v; - if (SWIG_AsVal(long)(obj, &v)) { - if (SWIG_CheckLongInRange(v, INT_MIN,INT_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, int); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -%#else -SWIGINTERNINLINE int - SWIG_AsVal(int)(PyObject *obj, int *val) -{ - return SWIG_AsVal(long)(obj,(long*)val); -} -%#endif -} - -%fragment(SWIG_AsVal_frag(unsigned int),"header", - fragment="SWIG_CheckUnsignedLongInRange", - fragment=SWIG_AsVal_frag(unsigned long)) { -%#if UINT_MAX != ULONG_MAX -SWIGINTERN int - SWIG_AsVal(unsigned int)(PyObject *obj, unsigned int *val) -{ - const char* errmsg = val ? "unsigned int" : (char*)0; - unsigned long v; - if (SWIG_AsVal(unsigned long)(obj, &v)) { - if (SWIG_CheckUnsignedLongInRange(v, INT_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, unsigned int); - return 1; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -%#else -SWIGINTERNINLINE unsigned int - SWIG_AsVal(unsigned int)(PyObject *obj, unsigned int *val) -{ - return SWIG_AsVal(unsigned long)(obj,(unsigned long *)val); -} -%#endif -} - -%fragment(SWIG_From_frag(unsigned int),"header", - fragment=SWIG_From_frag(long), - fragment=SWIG_From_frag(unsigned long)) { -%#if UINT_MAX < LONG_MAX -SWIG_define(SWIG_From(unsigned int), SWIG_From(long)) -%#else -SWIG_define(SWIG_From(unsigned int), SWIG_From(unsigned long)) -%#endif -} - -%fragment(SWIG_AsVal_frag(unsigned char),"header", - fragment=SWIG_AsVal_frag(unsigned long), - fragment="SWIG_CheckUnsignedLongInRange") { -SWIGINTERN int - SWIG_AsVal(unsigned char)(PyObject *obj, unsigned char *val) -{ - const char* errmsg = val ? "unsigned char" : (char*)0; - unsigned long v; - if (SWIG_AsVal(unsigned long)(obj, &v)) { - if (SWIG_CheckUnsignedLongInRange(v, UCHAR_MAX,errmsg)) { - if (val) *val = SWIG_numeric_cast(v, unsigned char); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -} - -%fragment(SWIG_AsVal_frag(unsigned short),"header", - fragment="SWIG_CheckUnsignedLongInRange", - fragment=SWIG_AsVal_frag(unsigned long)) { -SWIGINTERN int - SWIG_AsVal(unsigned short)(PyObject *obj, unsigned short *val) -{ - const char* errmsg = val ? "unsigned short" : (char*)0; - unsigned long v; - if (SWIG_AsVal(unsigned long)(obj, &v)) { - if (SWIG_CheckUnsignedLongInRange(v, USHRT_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, unsigned short); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -} - - -%fragment("SWIG_CheckDoubleInRange","header") { -%#include -SWIGINTERN int - SWIG_CheckDoubleInRange(double value, double min_value, - double max_value, const char* errmsg) -{ - if (value < min_value) { - if (errmsg) { - PyErr_Format(PyExc_OverflowError, - "value %g is less than %s minimum %g", - value, errmsg, min_value); - } - return 0; - } else if (value > max_value) { - if (errmsg) { - PyErr_Format(PyExc_OverflowError, - "value %g is greater than %s maximum %g", - value, errmsg, max_value); - } - return 0; - } - return 1; -} -} - -%fragment(SWIG_AsVal_frag(float),"header", - fragment="SWIG_CheckDoubleInRange", - fragment=SWIG_AsVal_frag(double)) { -SWIGINTERN int - SWIG_AsVal(float)(PyObject *obj, float *val) -{ - const char* errmsg = val ? "float" : (char*)0; - double v; - if (SWIG_AsVal(double)(obj, &v)) { - if (SWIG_CheckDoubleInRange(v, -FLT_MAX, FLT_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, float); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error(errmsg, obj); - } - return 0; -} -} - -%fragment(SWIG_From_frag(char),"header") { -SWIGINTERNINLINE PyObject* - SWIG_From(char)(char c) -{ - return PyString_FromStringAndSize(&c,1); -} -} - -%fragment(SWIG_AsVal_frag(char),"header", - fragment="SWIG_AsCharArray", - fragment="SWIG_CheckLongInRange", - fragment=SWIG_AsVal_frag(long)) { -SWIGINTERN int - SWIG_AsVal(char)(PyObject *obj, char *val) -{ - const char* errmsg = val ? "char" : (char*)0; - long v; - if (SWIG_AsVal(long)(obj, &v)) { - if (SWIG_CheckLongInRange(v, CHAR_MIN,CHAR_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, char); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - return SWIG_AsCharArray(obj, val, 1); - } - } -} - -%fragment(SWIG_From_frag(wchar_t),"header") { -SWIGINTERNINLINE PyObject* - SWIG_From(wchar_t)(wchar_t c) -{ - return PyUnicode_FromWideChar(&c, 1); -} -} - -%fragment(SWIG_AsVal_frag(wchar_t),"header", - fragment="SWIG_AsWCharArray", - fragment="SWIG_CheckLongInRange", - fragment=SWIG_AsVal_frag(long)) { -%#include -SWIGINTERN int - SWIG_AsVal(wchar_t)(PyObject *obj, wchar_t *val) -{ - const char* errmsg = val ? "wchar_t" : (char*)0; - long v; - if (SWIG_AsVal(long)(obj, &v)) { - if (SWIG_CheckLongInRange(v, WCHAR_MIN, WCHAR_MAX, errmsg)) { - if (val) *val = SWIG_numeric_cast(v, wchar_t); - return 1; - } else { - return 0; - } - } else { - PyErr_Clear(); - return SWIG_AsWCharArray(obj, val, 1); - } - } -} +/* boolean */ %fragment(SWIG_From_frag(bool),"header") { SWIGINTERNINLINE PyObject* - SWIG_From(bool)(bool value) + SWIG_From_dec(bool)(bool value) { PyObject *obj = value ? Py_True : Py_False; Py_INCREF(obj); @@ -564,111 +31,278 @@ SWIGINTERNINLINE PyObject* } %fragment(SWIG_AsVal_frag(bool),"header", - fragment=SWIG_AsVal_frag(int)) { + fragment=SWIG_AsVal_frag(long)) { SWIGINTERN int - SWIG_AsVal(bool)(PyObject *obj, bool *val) +SWIG_AsVal_dec(bool)(PyObject *obj, bool *val) { if (obj == Py_True) { if (val) *val = true; - return 1; - } - if (obj == Py_False) { + return SWIG_OK; + } else if (obj == Py_False) { if (val) *val = false; - return 1; - } - int res = 0; - if (SWIG_AsVal(int)(obj, &res)) { - if (val) *val = res ? true : false; - return 1; + return SWIG_OK; } else { - PyErr_Clear(); - } - if (val) { - SWIG_type_error("bool", obj); + long v = 0; + int res = SWIG_AsVal(long)(obj, &v); + if (res == SWIG_OK && val) *val = v ? true : false; + return res; } - return 0; } } +/* signed/unsigned char */ -/* ------------------------------------------------------------ - * typemap for primitive type with no pointer representation - * ------------------------------------------------------------ */ +%type_slong(signed char, "", SCHAR_MIN, SCHAR_MAX) +%type_ulong(unsigned char, "", UCHAR_MAX) -%define %typemap_primitive(Code, ...) -%typemap_asvalfromn(SWIG_arg(Code), __VA_ARGS__); -%enddef - +/* short/unsigned short */ +%type_slong(short, "", SHRT_MIN, SHRT_MAX) +%type_ulong(unsigned short, "", USHRT_MAX) -/* ------------------------------------------------------------ - * Primitive Type Macros - * ------------------------------------------------------------ */ +/* int/unsigned int */ -/* useful macros to derive typemap declarations from primitive types */ +%type_slong(int, "", INT_MIN, INT_MAX) +%type_ulong(unsigned int, "", UINT_MAX) -%define _apply_macro(macro, arg, ...) -#if #__VA_ARGS__ != "" - macro(__VA_ARGS__,arg); -#else - macro(arg); +/* signed/unsigned wchar_t */ + +#ifdef __cplusplus +%type_slong(signed wchar_t, "", WCHAR_MIN, WCHAR_MAX) +%type_ulong(unsigned wchar_t, "", UWCHAR_MAX) #endif -%enddef -/* Apply macro to the order types */ -%define %apply_ctypes(Macro,...) -_apply_macro(Macro, bool , __VA_ARGS__); -_apply_macro(Macro, signed char , __VA_ARGS__); -_apply_macro(Macro, unsigned char , __VA_ARGS__); -_apply_macro(Macro, short , __VA_ARGS__); -_apply_macro(Macro, unsigned short , __VA_ARGS__); -_apply_macro(Macro, int , __VA_ARGS__); -_apply_macro(Macro, unsigned int , __VA_ARGS__); -_apply_macro(Macro, long , __VA_ARGS__); -_apply_macro(Macro, unsigned long , __VA_ARGS__); -_apply_macro(Macro, long long , __VA_ARGS__); -_apply_macro(Macro, unsigned long long , __VA_ARGS__); -_apply_macro(Macro, float , __VA_ARGS__); -_apply_macro(Macro, double , __VA_ARGS__); -_apply_macro(Macro, char , __VA_ARGS__); -_apply_macro(Macro, wchar_t , __VA_ARGS__); -%enddef +/* long */ -/* apply the Macro(Type) to all the C++ types */ -%define %apply_cpptypes(Macro,...) -%apply_ctypes(Macro, __VA_ARGS__) -_apply_macro(Macro, std::string, __VA_ARGS__); -_apply_macro(Macro, std::complex , __VA_ARGS__); -_apply_macro(Macro, std::complex , __VA_ARGS__); -%enddef +%fragment(SWIG_From_frag(long),"header", + fragment="") { + SWIG_define(SWIG_From_dec(long), PyInt_FromLong) +} -/* apply the Macro2(Type1, Type2) to all the C++ types */ -%define %apply_cpptypes_2(Macro2) -%apply_cpptypes(%apply_cpptypes, Macro2) -%enddef +%fragment(SWIG_AsVal_frag(long),"header") { +SWIGINTERN int +SWIG_AsVal_dec(long)(PyObject *obj, long* val) +{ + if (PyInt_Check(obj)) { + if (val) *val = PyInt_AsLong(obj); + return SWIG_OK; + } else if (PyLong_Check(obj)) { + long v = PyLong_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } + return SWIG_TypeError; +} +} -%define %apply_checkctypes(Macro) -Macro(SWIG_CCode(BOOL), bool); -Macro(SWIG_CCode(INT8), signed char); -Macro(SWIG_CCode(UINT8), unsigned char); -Macro(SWIG_CCode(INT16), short); -Macro(SWIG_CCode(UINT16), unsigned short); -Macro(SWIG_CCode(INT32), int); -Macro(SWIG_CCode(UINT32), unsigned int); -Macro(SWIG_CCode(INT64), long); -Macro(SWIG_CCode(UINT64), unsigned long); -Macro(SWIG_CCode(INT128), long long); -Macro(SWIG_CCode(UINT128), unsigned long long); -Macro(SWIG_CCode(FLOAT), float); -Macro(SWIG_CCode(DOUBLE), double); -Macro(SWIG_CCode(CHAR), char); -Macro(SWIG_CCode(UNICHAR), wchar_t); -%enddef +/* unsigned long */ + +%fragment(SWIG_From_frag(unsigned long),"header", + fragment=SWIG_From_frag(long)) { +SWIGINTERNINLINE PyObject* +SWIG_From_dec(unsigned long)(unsigned long value) +{ + return (value > LONG_MAX) ? + PyLong_FromUnsignedLong(value) : PyInt_FromLong(SWIG_numeric_cast(value,long)); +} +} + +%fragment(SWIG_AsVal_frag(unsigned long),"header") { +SWIGINTERN int +SWIG_AsVal_dec(unsigned long)(PyObject *obj, unsigned long *val) +{ + if (PyInt_Check(obj)) { + long v = PyInt_AsLong(obj); + if (v >= 0) { + if (val) *val = v; + return SWIG_OK; + } else { + return SWIG_OverflowError; + } + } else if (PyLong_Check(obj)) { + unsigned long v = PyLong_AsUnsignedLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } + return SWIG_TypeError; +} +} + +/* long long */ + +%fragment(SWIG_From_frag(long long),"header", + fragment=SWIG_From_frag(long), + fragment="") { +SWIGINTERNINLINE PyObject* +SWIG_From_dec(long long)(long long value) +{ + return ((value < LONG_MIN) || (value > LONG_MAX)) ? + PyLong_FromLongLong(value) : PyInt_FromLong(SWIG_numeric_cast(value,long)); +} +} + +%fragment(SWIG_AsVal_frag(long long),"header") { +SWIGINTERN int +SWIG_AsVal_dec(long long)(PyObject *obj, long long *val) +{ + if (PyInt_Check(obj)) { + if (val) *val = PyInt_AsLong(obj); + return SWIG_OK; + } else if (PyLong_Check(obj)) { + long long v = PyLong_AsLongLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } + return SWIG_TypeError; +} +} + +/* unsigned long long */ + +%fragment(SWIG_From_frag(unsigned long long),"header", + fragment=SWIG_From_frag(long long), + fragment="") { +SWIGINTERNINLINE PyObject* +SWIG_From_dec(unsigned long long)(unsigned long long value) +{ + return (value > LONG_MAX) ? + PyLong_FromUnsignedLongLong(value) : PyInt_FromLong(SWIG_numeric_cast(value,long)); +} +} + +%fragment(SWIG_AsVal_frag(unsigned long long),"header", + fragment=SWIG_AsVal_frag(unsigned long)) { +SWIGINTERN int +SWIG_AsVal_dec(unsigned long long)(PyObject *obj, unsigned long long *val) +{ + if (PyLong_Check(obj)) { + unsigned long long v = PyLong_AsUnsignedLongLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } else { + unsigned long v; + int res = SWIG_AsVal(unsigned long)(obj,&v); + if (res == SWIG_OK && val) *val = v; + return res; + } + return SWIG_TypeError; +} +} + +/* float */ + +%derived_type_from(double, float) +%signed_derived_type_asval(double, float, "", -FLT_MAX, FLT_MAX) + +/* double */ + +%fragment(SWIG_From_frag(double),"header") { + SWIG_define(SWIG_From_dec(double), PyFloat_FromDouble) +} + +%fragment(SWIG_AsVal_frag(double),"header") { +SWIGINTERN int +SWIG_AsVal_dec(double)(PyObject *obj, double *val) +{ + if (PyFloat_Check(obj)) { + if (val) *val = PyFloat_AsDouble(obj); + return SWIG_OK; + } else if (PyInt_Check(obj)) { + if (val) *val = PyInt_AsLong(obj); + return SWIG_OK; + } else if (PyLong_Check(obj)) { + double v = PyLong_AsDouble(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } + return SWIG_TypeError; +} +} + +/* char */ + +%fragment(SWIG_From_frag(char),"header") { +SWIGINTERNINLINE PyObject* +SWIG_From_dec(char)(char c) +{ + return PyString_FromStringAndSize(&c,1); +} +} + +%fragment(SWIG_AsVal_frag(char),"header", + fragment="SWIG_AsCharArray", + fragment=SWIG_AsVal_frag(signed char)) { +SWIGINTERN int +SWIG_AsVal_dec(char)(PyObject *obj, char *val) +{ + if (SWIG_AsCharArray(obj, val, 1) == SWIG_OK) { + return SWIG_OK; + } else { + signed char v; + int res = SWIG_AsVal(signed char)(obj, &v); + if (res == SWIG_OK && val) *val = (char)(v); + return res; + } + } +} + +/* wchar_t */ + + +%fragment(SWIG_From_frag(wchar_t),"header") { +SWIGINTERNINLINE PyObject* +SWIG_From_dec(wchar_t)(wchar_t c) +{ + return PyUnicode_FromWideChar(&c, 1); +} +} + +%fragment(SWIG_AsVal_frag(wchar_t),"header", + fragment="SWIG_AsWCharArray", + fragment=SWIG_AsVal_frag(long)) { +SWIGINTERN int +SWIG_AsVal_dec(wchar_t)(PyObject *obj, wchar_t *val) +{ + if (SWIG_AsWCharArray(obj, val, 1) == SWIG_OK) { + return SWIG_OK; + } else { + long v; + int res = SWIG_AsVal(long)(obj, &v); + if (res == SWIG_OK) { + if (WCHAR_MIN <= v && v <= WCHAR_MAX) { + if (val) *val = (wchar_t)(v); + return SWIG_OK; + } else { + return SWIG_OverflowError; + } + } + return res; + } +} +} /* ------------------------------------------------------------ * Apply the primitive typemap for all the types with checkcode * ------------------------------------------------------------ */ %apply_checkctypes(%typemap_primitive) - - diff --git a/Lib/python/pyptrtypes.swg b/Lib/python/pyptrtypes.swg deleted file mode 100644 index 1e7ad613c..000000000 --- a/Lib/python/pyptrtypes.swg +++ /dev/null @@ -1,165 +0,0 @@ -/* - Value typemaps (Type, const Type&) for "Ptr" types, such as swig - wrapped classes, that define the AsPtr/From methods -*/ - -/* in */ - -%define PYPTR_IN_TYPEMAP(asptr_meth,pyfrag,Type...) - %typemap(in,fragment=pyfrag) Type { - Type *ptr = (Type *)0; - int res = asptr_meth($input, &ptr); - if (!res) { - if (!PyErr_Occurred()) - SWIG_type_error("$basetype", $input); - } else if (!ptr) { - SWIG_null_ref("$basetype"); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = *ptr; - if (res == SWIG_NEWOBJ) delete ptr; - } - %typemap(in,fragment=pyfrag) const Type & (int res = 0) { - Type *ptr = (Type *)0; - res = asptr_meth($input, &ptr); - if (!res) { - if (!PyErr_Occurred()) - SWIG_type_error("$basetype", $input); - } else if (!ptr) { - SWIG_null_ref("$basetype"); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = ptr; - } - - %typemap(freearg) const Type & - "if (res$argnum == SWIG_NEWOBJ) delete $1;"; -%enddef - -/* varin */ - -%define PYPTR_VARIN_TYPEMAP(asptr_meth,pyfrag,Type...) - %typemap(varin,fragment=pyfrag) Type { - Type *ptr = (Type *)0; - int res = asptr_meth($input, &ptr); - if (!res) { - if (!PyErr_Occurred()) { - SWIG_type_error("$basetype", $input); - } - SWIG_append_errmsg(" C/C++ variable '$name'"); - return 1; - } else if (!ptr) { - SWIG_null_ref("$basetype"); - SWIG_append_errmsg(" C/C++ variable '$name'"); - return 1; - } - $1 = *ptr; - if (res == SWIG_NEWOBJ) delete ptr; - } -%enddef - -/* directorout */ - -%define PYPTR_DIRECTOROUT_TYPEMAP(asptr_meth,pyfrag,Type...) - %typemap(directorargout,fragment=pyfrag) Type *DIRECTOROUT ($*1_ltype temp) { - Type *ptr = 0; - int res = $input ? asptr_meth($input, &ptr) : 0; - if (!res || !ptr) - Swig::DirectorTypeMismatchException::raise("Error converting Python object when using asptr_meth."); - temp = *ptr; - $result = &temp; - if (res == SWIG_NEWOBJ) delete ptr; - } - - %typemap(directorout,fragment=pyfrag) Type { - Type *ptr = 0; - int res = $input ? asptr_meth($input, &ptr) : 0; - if (!res || !ptr) - Swig::DirectorTypeMismatchException::raise("Error converting Python object when using asptr_meth."); - $result = *ptr; - if (res == SWIG_NEWOBJ) delete ptr; - } - - %typemap(directorout,fragment=pyfrag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& { - Type *ptr = 0; - int res = $input ? asptr_meth($input, &ptr) : 0; - if (!res || !ptr) - Swig::DirectorTypeMismatchException::raise("Error converting Python object when using asptr_meth."); - $result = ptr; - if (res == SWIG_NEWOBJ) { - /* Possible thread/reentrant problem here! */ - static $*ltype temp = *ptr; - $result = &temp; - delete ptr; - } else { - $result = ptr; - } - } - - %typemap(directorout,fragment=pyfrag) Type &DIRECTOROUT = Type - -%enddef - -/* typecheck */ - -%define PYPTR_TYPECHECK_TYPEMAP(check,asptr_meth,pyfrag,Type...) -%typemap(typecheck,precedence=check,fragment=pyfrag) - Type, const Type& - "$1 = asptr_meth($input, (Type**)(0));"; -%enddef - -/* - typemap definition for types with AsPtr/From methods - */ - -%define %typemap_asptrfrom(CheckCode, AsPtrMeth, FromMeth, AsPtrFrag, FromFrag, Type...) - %fragment(SWIG_AsVal_frag(Type),"header", - fragment=SWIG_AsPtr_frag(Type)) %{ - SWIGINTERNINLINE int - SWIG_AsVal(Type)(PyObject* obj, Type *val) - { - Type *v = (Type *)0; - int res = SWIG_AsPtr(Type)(obj, &v); - if (!res || !v) return 0; - if (val) { - *val = *v; - if (res == SWIG_NEWOBJ) delete v; - } - return 1; - } - %} - %fragment(SWIG_As_frag(Type),"header", - fragment=SWIG_AsVal_frag(Type)) %{ - SWIGINTERNINLINE Type - SWIG_As(Type)(PyObject* obj) - { - Type v; - SWIG_AsVal(Type)(obj, &v); - return v; - } - %} - PYPTR_IN_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type); - PYPTR_VARIN_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type); - PYPTR_DIRECTOROUT_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type); - PYPTR_TYPECHECK_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(AsPtrMeth), - SWIG_arg(AsPtrFrag), Type); - %typemap_from(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - - PYPTR_INPUT_TYPEMAP(SWIG_arg(CheckCode),SWIG_arg(AsPtrMeth), - SWIG_arg(AsPtrFrag),Type); - PYVAL_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - PYPTR_INOUT_TYPEMAP(Type); -%enddef - -/* - typemap for simple swig types with only AsPtr/From methods -*/ - -%define %typemap_asptrfromn(CheckCode, Type...) -%typemap_asptrfrom(SWIG_arg(CheckCode), - SWIG_arg(SWIG_AsPtr(Type)), - SWIG_arg(SWIG_From(Type)), - SWIG_arg(SWIG_AsPtr_frag(Type)), - SWIG_arg(SWIG_From_frag(Type)), - Type); -%enddef diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 3fac91e5d..b87e39255 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -9,75 +9,60 @@ ************************************************************************/ /* Common SWIG API */ -#define SWIG_ConvertPtr(obj, pp, type, flags) SWIG_Python_ConvertPtr(obj, pp, type, flags) -#define SWIG_NewPointerObj(p, type, flags) SWIG_Python_NewPointerObj(p, type, flags) -#define SWIG_MustGetPtr(p, type, argnum, flags) SWIG_Python_MustGetPtr(p, type, argnum, flags) - -/* Python-specific SWIG API */ -#define SWIG_ConvertPacked(obj, ptr, sz, ty, flags) SWIG_Python_ConvertPacked(obj, ptr, sz, ty, flags) -#define SWIG_NewPackedObj(ptr, sz, type) SWIG_Python_NewPackedObj(ptr, sz, type) +/* for raw pointers */ +#define SWIG_ConvertPtr(obj, pptr, type, flags) SWIG_Python_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewPointerObj(ptr, type, flags) SWIG_Python_NewPointerObj(ptr, type, flags) + +/* for raw packed data */ +#define SWIG_ConvertPacked(obj, ptr, sz, ty, flags) SWIG_Python_ConvertPacked(obj, ptr, sz, ty, flags) +#define SWIG_NewPackedObj(ptr, sz, type, flags) SWIG_Python_NewPackedObj(ptr, sz, type) + +/* for class or struct pointers */ +#define SWIG_ConvertInstance(obj, pptr, type, flags) SWIG_Python_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewInstanceObj(ptr, type, flags) SWIG_Python_NewPointerObj(ptr, type, flags) + +/* for C or C++ function pointers */ +#define SWIG_ConvertFunctionPtr(obj, pptr, type, flags) SWIG_Python_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewFunctionPtrObj(ptr, type) SWIG_Python_NewPointerObj(ptr, type, 0) + +/* for C++ member pointers, ie, member methods */ +#define SWIG_ConvertMember(obj, ptr, sz, ty, flags) SWIG_Python_ConvertPacked(obj, ptr, sz, ty, flags) +#define SWIG_NewMemberObj(ptr, sz, type) SWIG_Python_NewPackedObj(ptr, sz, type) + /* Runtime API */ #define SWIG_GetModule(clientdata) SWIG_Python_GetModule() #define SWIG_SetModule(clientdata, pointer) SWIG_Python_SetModule(pointer) +/* Error manipulation */ +#define SWIG_ERROR -1 +#define SWIG_fail goto fail +#define SWIG_var_fail return 1 + +#define SWIG_error(code, msg) SWIG_Python_SetErrorMsg(SWIG_Python_ErrorType(code), msg) +#define SWIG_exception(code, msg) do { SWIG_error(code, msg); SWIG_fail; } while (0) +#define SWIG_contract_assert(expr, msg) if (!(expr)) { SWIG_error(SWIG_RuntimeError, msg); SWIG_fail; } else + /* ----------------------------------------------------------------------------- * Pointer declarations * ----------------------------------------------------------------------------- */ -/* - Use SWIG_NO_COBJECT_TYPES to force the use of strings to represent - C/C++ pointers in the python side. Very useful for debugging, but - not always safe. -*/ -#if !defined(SWIG_NO_COBJECT_TYPES) && !defined(SWIG_COBJECT_TYPES) -# define SWIG_COBJECT_TYPES -#endif -/* Flags for pointer conversion */ -#define SWIG_POINTER_EXCEPTION 0x1 -#define SWIG_POINTER_DISOWN 0x2 +SWIGRUNTIMEINLINE PyObject * +SWIG_Python_NoneObject() { + Py_INCREF(Py_None); + return Py_None; +} - -/* Add PyOS_snprintf for old Pythons */ -#if PY_VERSION_HEX < 0x02020000 -#define PyOS_snprintf snprintf -#endif +/* ----------------------------------------------------------------------------- + * Create a new pointer object + * ----------------------------------------------------------------------------- */ #ifdef __cplusplus extern "C" { #endif -/* ----------------------------------------------------------------------------- - * Create a new pointer string - * ----------------------------------------------------------------------------- */ -#ifndef SWIG_BUFFER_SIZE -#define SWIG_BUFFER_SIZE 1024 -#endif - -/* A crude PyString_FromFormat implementation for old Pythons */ -#if PY_VERSION_HEX < 0x02020000 -static PyObject * -PyString_FromFormat(const char *fmt, ...) { - va_list ap; - char buf[SWIG_BUFFER_SIZE * 2]; - int res; - va_start(ap, fmt); - res = vsnprintf(buf, sizeof(buf), fmt, ap); - va_end(ap); - return (res < 0 || res >= sizeof(buf)) ? 0 : PyString_FromString(buf); -} -#endif - -#if PY_VERSION_HEX < 0x01060000 -#define PyObject_Del(op) PyMem_DEL((op)) -#endif - -#if defined(SWIG_COBJECT_TYPES) -#if !defined(SWIG_COBJECT_PYTHON) -/* ----------------------------------------------------------------------------- - * Implements a simple Swig Object type, and use it instead of PyCObject - * ----------------------------------------------------------------------------- */ +/* PySwigObject */ typedef struct { PyObject_HEAD @@ -85,7 +70,6 @@ typedef struct { const char *desc; } PySwigObject; -/* Declarations for objects of type PySwigObject */ SWIGRUNTIME int PySwigObject_print(PySwigObject *v, FILE *fp, int flags) @@ -457,108 +441,6 @@ PySwigPacked_Check(PyObject *op) { || (strcmp((op)->ob_type->tp_name,"PySwigPacked") == 0); } -#else -/* ----------------------------------------------------------------------------- - * Use the old Python PyCObject instead of PySwigObject - * ----------------------------------------------------------------------------- */ - -#define PySwigObject_GetDesc(obj) PyCObject_GetDesc(obj) -#define PySwigObject_Check(obj) PyCObject_Check(obj) -#define PySwigObject_AsVoidPtr(obj) PyCObject_AsVoidPtr(obj) -#define PySwigObject_FromVoidPtrAndDesc(p, d) PyCObject_FromVoidPtrAndDesc(p, d, NULL) - -#endif - -#endif - -/* ----------------------------------------------------------------------------- - * errors manipulation - * ----------------------------------------------------------------------------- */ - -SWIGRUNTIME void -SWIG_Python_TypeError(const char *type, PyObject *obj) -{ - if (type) { -#if defined(SWIG_COBJECT_TYPES) - if (obj && PySwigObject_Check(obj)) { - const char *otype = (const char *) PySwigObject_GetDesc(obj); - if (otype) { - PyErr_Format(PyExc_TypeError, "a '%s' is expected, 'PySwigObject(%s)' is received", - type, otype); - return; - } - } else -#endif - { - const char *otype = (obj ? obj->ob_type->tp_name : 0); - if (otype) { - PyObject *str = PyObject_Str(obj); - const char *cstr = str ? PyString_AsString(str) : 0; - if (cstr) { - PyErr_Format(PyExc_TypeError, "a '%s' is expected, '%s(%s)' is received", - type, otype, cstr); - } else { - PyErr_Format(PyExc_TypeError, "a '%s' is expected, '%s' is received", - type, otype); - } - Py_XDECREF(str); - return; - } - } - PyErr_Format(PyExc_TypeError, "a '%s' is expected", type); - } else { - PyErr_Format(PyExc_TypeError, "unexpected type is received"); - } -} - -SWIGRUNTIMEINLINE void -SWIG_Python_NullRef(const char *type) -{ - if (type) { - PyErr_Format(PyExc_TypeError, "null reference of type '%s' was received",type); - } else { - PyErr_Format(PyExc_TypeError, "null reference was received"); - } -} - -SWIGRUNTIME int -SWIG_Python_AddErrMesg(const char* mesg, int infront) -{ - if (PyErr_Occurred()) { - PyObject *type = 0; - PyObject *value = 0; - PyObject *traceback = 0; - PyErr_Fetch(&type, &value, &traceback); - if (value) { - PyObject *old_str = PyObject_Str(value); - Py_XINCREF(type); - PyErr_Clear(); - if (infront) { - PyErr_Format(type, "%s %s", mesg, PyString_AsString(old_str)); - } else { - PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg); - } - Py_DECREF(old_str); - } - return 1; - } else { - return 0; - } -} - -SWIGRUNTIME int -SWIG_Python_ArgFail(int argnum) -{ - if (PyErr_Occurred()) { - /* add information about failing argument */ - char mesg[256]; - PyOS_snprintf(mesg, sizeof(mesg), "argument number %d:", argnum); - return SWIG_Python_AddErrMesg(mesg, 1); - } else { - return 0; - } -} - /* ----------------------------------------------------------------------------- * pointers/data manipulation @@ -574,13 +456,12 @@ SWIG_Python_ConvertPtr(PyObject *obj, void **ptr, swig_type_info *ty, int flags) PyObject *pyobj = 0; void *vptr; - if (!obj) return 0; + if (!obj) return SWIG_ERROR; if (obj == Py_None) { *ptr = 0; - return 0; + return SWIG_OK; } -#ifdef SWIG_COBJECT_TYPES if (!(PySwigObject_Check(obj))) { if (!SWIG_this) SWIG_this = PyString_FromString("this"); @@ -597,25 +478,6 @@ SWIG_Python_ConvertPtr(PyObject *obj, void **ptr, swig_type_info *ty, int flags) c = (const char *) PySwigObject_GetDesc(obj); if (newref) { Py_DECREF(obj); } goto type_check; -#else - if (!(PyString_Check(obj))) { - if (!SWIG_this) - SWIG_this = PyString_FromString("this"); - pyobj = obj; - obj = PyObject_GetAttr(obj,SWIG_this); - newref = 1; - if (!obj) goto type_error; - if (!PyString_Check(obj)) { - Py_DECREF(obj); - goto type_error; - } - } - c = PyString_AsString(obj); - /* Pointer values must start with leading underscore */ - c = SWIG_UnpackVoidPtr(c, &vptr, ty->name); - if (newref) { Py_DECREF(obj); } - if (!c) goto type_error; -#endif type_check: if (ty) { @@ -628,7 +490,7 @@ type_check: if ((pyobj) && (flags & SWIG_POINTER_DISOWN)) { PyObject_SetAttrString(pyobj,(char*)"thisown",Py_False); } - return 0; + return SWIG_OK; type_error: PyErr_Clear(); @@ -645,28 +507,7 @@ type_error: } } } - if (flags & SWIG_POINTER_EXCEPTION) { - if (ty) { - SWIG_Python_TypeError(SWIG_TypePrettyName(ty), obj); - } else { - SWIG_Python_TypeError("C/C++ pointer", obj); - } - } - return -1; -} - -/* Convert a pointer value, signal an exception on a type mismatch */ -SWIGRUNTIME void * -SWIG_Python_MustGetPtr(PyObject *obj, swig_type_info *ty, int argnum, int flags) { - void *result; - if (SWIG_Python_ConvertPtr(obj, &result, ty, flags) == -1) { - PyErr_Clear(); - if (flags & SWIG_POINTER_EXCEPTION) { - SWIG_Python_TypeError(SWIG_TypePrettyName(ty), obj); - SWIG_Python_ArgFail(argnum); - } - } - return result; + return SWIG_ERROR; } /* Convert a packed value value */ @@ -675,40 +516,27 @@ SWIG_Python_ConvertPacked(PyObject *obj, void *ptr, size_t sz, swig_type_info *t swig_cast_info *tc; const char *c = 0; -#if defined(SWIG_COBJECT_TYPES) && !defined(SWIG_COBJECT_PYTHON) c = PySwigPacked_UnpackData(obj, ptr, sz); -#else - if ((!obj) || (!PyString_Check(obj))) goto type_error; - c = PyString_AsString(obj); - /* Pointer values must start with leading underscore */ - c = SWIG_UnpackDataName(c, ptr, sz, ty->name); -#endif if (!c) goto type_error; if (ty) { tc = SWIG_TypeCheck(c,ty); if (!tc) goto type_error; } - return 0; + return SWIG_OK; type_error: PyErr_Clear(); - if (flags & SWIG_POINTER_EXCEPTION) { - if (ty) { - SWIG_Python_TypeError(SWIG_TypePrettyName(ty), obj); - } else { - SWIG_Python_TypeError("C/C++ packed data", obj); - } - } - return -1; + return SWIG_ERROR; } /* Create a new array object */ SWIGRUNTIME PyObject * -SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int own) { +SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int flags) { PyObject *robj = 0; + int own = flags & SWIG_POINTER_OWN; if (!type) { if (!PyErr_Occurred()) { - PyErr_Format(PyExc_TypeError, "Swig: null type passed to NewPointerObj"); + PyErr_SetString(PyExc_TypeError, "Swig: null type passed to NewPointerObj"); } return robj; } @@ -716,15 +544,7 @@ SWIG_Python_NewPointerObj(void *ptr, swig_type_info *type, int own) { Py_INCREF(Py_None); return Py_None; } -#ifdef SWIG_COBJECT_TYPES robj = PySwigObject_FromVoidPtrAndDesc((void *) ptr, (char *)type->name); -#else - { - char result[SWIG_BUFFER_SIZE]; - robj = SWIG_PackVoidPtr(result, ptr, type->name, sizeof(result)) ? - PyString_FromString(result) : 0; - } -#endif if (!robj || (robj == Py_None)) return robj; if (type->clientdata) { PyObject *inst; @@ -749,15 +569,8 @@ SWIG_Python_NewPackedObj(void *ptr, size_t sz, swig_type_info *type) { Py_INCREF(Py_None); return Py_None; } -#if defined(SWIG_COBJECT_TYPES) && !defined(SWIG_COBJECT_PYTHON) + robj = PySwigPacked_FromDataAndDesc((void *) ptr, sz, (char *)type->name); -#else - { - char result[SWIG_BUFFER_SIZE]; - robj = SWIG_PackDataName(result, ptr, sz, type->name, sizeof(result)) ? - PyString_FromString(result) : 0; - } -#endif return robj; } @@ -798,12 +611,12 @@ PyModule_AddObject(PyObject *m, char *name, PyObject *o) if (!PyModule_Check(m)) { PyErr_SetString(PyExc_TypeError, "PyModule_AddObject() needs module as first arg"); - return -1; + return SWIG_ERROR; } if (!o) { PyErr_SetString(PyExc_TypeError, "PyModule_AddObject() needs non-NULL value"); - return -1; + return SWIG_ERROR; } dict = PyModule_GetDict(m); @@ -811,12 +624,12 @@ PyModule_AddObject(PyObject *m, char *name, PyObject *o) /* Internal error -- modules must have a dict! */ PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", PyModule_GetName(m)); - return -1; + return SWIG_ERROR; } if (PyDict_SetItemString(dict, name, o)) - return -1; + return SWIG_ERROR; Py_DECREF(o); - return 0; + return SWIG_OK; } #endif diff --git a/Lib/python/pyruntime.swg b/Lib/python/pyruntime.swg index b9d83a7f1..1f16cb630 100644 --- a/Lib/python/pyruntime.swg +++ b/Lib/python/pyruntime.swg @@ -1,13 +1,41 @@ /* Python.h has to appear first */ %insert(runtime) %{ #include + +/* Add PyOS_snprintf for old Pythons */ +#if PY_VERSION_HEX < 0x02020000 +#define PyOS_snprintf snprintf +#endif + +/* A crude PyString_FromFormat implementation for old Pythons */ +#if PY_VERSION_HEX < 0x02020000 + +#ifndef SWIG_PYBUFFER_SIZE +#define SWIG_PYBUFFER_SIZE 1024 +#endif + +static PyObject * +PyString_FromFormat(const char *fmt, ...) { + va_list ap; + char buf[SWIG_PYBUFFER_SIZE * 2]; + int res; + va_start(ap, fmt); + res = vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + return (res < 0 || res >= sizeof(buf)) ? 0 : PyString_FromString(buf); +} +#endif + +/* Add PyObject_Del for old Pythons */ +#if PY_VERSION_HEX < 0x01060000 +#define PyObject_Del(op) PyMem_DEL((op)) +#endif + %} - -%insert(runtime) "swigrun.swg"; /* Common C API type-checking code */ -%insert(runtime) "pyapi.swg"; /* SWIG/Pyton API */ +%insert(runtime) "swigrun.swg"; /* SWIG API */ +%insert(runtime) "pyapi.swg"; /* Pyton API */ %insert(runtime) "pyrun.swg"; /* Python run-time code */ - /* When using -nortti, tell directors to avoid RTTI */ #ifdef SWIG_NORTTI %insert("runtime") %{ diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index 12c370e59..5bb4f6c98 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -1,7 +1,5 @@ -%include - %fragment("PyObject_var","header") -%{ +{ namespace swig { struct PyObject_var { PyObject* ptr; @@ -11,10 +9,10 @@ PyObject* operator->() const { return ptr; } }; } -%} +} %fragment("StdTraits","header",fragment="StdTraitsCommon") -%{ +{ namespace swig { /* Traits that provides the from method @@ -54,14 +52,9 @@ namespace swig { struct traits_asptr { static int asptr(PyObject *obj, Type **val) { Type *p; - int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0) != -1) - ? SWIG_OLDOBJ : 0; + int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info(), 0) == SWIG_OK) ? SWIG_OLDOBJ : 0; if (res) { - if (val) { - *val = p; - } - } else { - SWIG_type_error(type_name(), obj); + if (val) *val = p; } return res; } @@ -81,7 +74,7 @@ namespace swig { if ((res != 0) && p) { typedef typename noconst_traits::noconst_type noconst_type; *(const_cast(val)) = *p; - if (res == SWIG_NEWOBJ) delete p; + if (res == SWIG_NEWOBJ) SWIG_delete(p); return true; } else { return false; @@ -93,7 +86,7 @@ namespace swig { }; template struct traits_asval { - static bool asval(PyObject *obj, Type **val) { + static int asval(PyObject *obj, Type **val) { if (val) { typedef typename noconst_traits::noconst_type noconst_type; noconst_type *p = 0; @@ -121,7 +114,7 @@ namespace swig { Type v; if (!obj || !asval(obj, &v)) { if (!PyErr_Occurred()) { - SWIG_type_error(swig::type_name(), obj); + SWIG_Python_SetErrorMsg(SWIG_Python_ErrorType(SWIG_TypeError), swig::type_name()); } if (throw_error) throw std::invalid_argument("bad type"); } @@ -137,7 +130,7 @@ namespace swig { if (res && v) { if (res == SWIG_NEWOBJ) { Type r(*v); - delete v; + SWIG_delete(v); return r; } else { return *v; @@ -146,7 +139,7 @@ namespace swig { // Uninitialized return value, no Type() constructor required. static Type *v_def = (Type*) malloc(sizeof(Type)); if (!PyErr_Occurred()) { - SWIG_type_error(swig::type_name(), obj); + SWIG_Python_SetErrorMsg(SWIG_Python_ErrorType(SWIG_TypeError), swig::type_name()); } if (throw_error) throw std::invalid_argument("bad type"); memset(v_def,0,sizeof(Type)); @@ -164,7 +157,7 @@ namespace swig { return v; } else { if (!PyErr_Occurred()) { - SWIG_type_error(swig::type_name(), obj); + SWIG_Python_SetErrorMsg(SWIG_Python_ErrorType(SWIG_TypeError),swig::type_name()); } if (throw_error) throw std::invalid_argument("bad type"); return 0; @@ -196,7 +189,7 @@ namespace swig { return traits_check::category>::check(obj); } } -%} +} // // Backward compatibility diff --git a/Lib/python/pystrbase.swg b/Lib/python/pystrbase.swg deleted file mode 100644 index c4f057401..000000000 --- a/Lib/python/pystrbase.swg +++ /dev/null @@ -1,330 +0,0 @@ -// -// Use the macro SWIG_PRESERVE_CARRAY_SIZE if you prefer to preserve -// the size of char arrays, ie -// ------------------------------------------ -// C Side => Python Side -// ------------------------------------------ -// char name[5] = "hola" => 'hola\0' -// -// the default behaviour is -// -// char name[5] = "hola" => 'hola' -// -// -//#define SWIG_PRESERVE_CARRAY_SIZE - -/* ------------------------------------------------------------ - * String typemaps for type Char (char or wchar_t) - * ------------------------------------------------------------ */ - -%define %typemap_pystring(Char, - SWIG_AsCharPtr, - SWIG_AsCharPtrAndSize, - SWIG_FromCharPtr, - SWIG_AsNewCharPtr, - SWIG_AsCharArray, - SWIG_FromCharArray) -/* in */ - -%typemap(in,fragment=#SWIG_AsCharPtr) - Char *, Char const*, Char *const, Char const *const - "if (!SWIG_AsCharPtr($input, (Char**)&$1)) {SWIG_arg_fail($argnum);SWIG_fail;}"; - -%typemap(in,fragment=#SWIG_AsCharPtr) - Char const*&, Char *const&, Char const *const & -{ - $*ltype temp; - if (!SWIG_AsCharPtr($input, (Char**)&temp)) {SWIG_arg_fail($argnum);SWIG_fail;} - $1 = &temp; -} - -/* out */ - -%typemap(out,fragment=#SWIG_FromCharPtr) - Char *, Char const*, Char *const, Char const *const - "$result = SWIG_FromCharPtr($1);"; - -%typemap(out,fragment=#SWIG_FromCharPtr) - Char *const &, Char const* &, Char const *const & - "$result = SWIG_FromCharPtr(*$1);"; - -/* varin */ - -%typemap(varin,fragment=#SWIG_AsNewCharPtr) Char * -{ - Char *cptr = 0; - if (!SWIG_AsNewCharPtr($input, &cptr)) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - if ($1) SWIG_delete_array($1); - $1 = cptr; -} - -%typemap(varin,fragment=#SWIG_AsNewCharPtr, - warning="451:Setting const Char * variable may leak memory") - const Char * -{ - Char *cptr; - if (!SWIG_AsNewCharPtr($input, &cptr)) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - $1 = cptr; -} - -/* varout */ - -%typemap(varout,fragment=#SWIG_FromCharPtr) - Char*, Char const*, Char *const, Char const *const - "$result = SWIG_FromCharPtr($1);"; - -/* constant */ - -%typemap(constcode,fragment=#SWIG_FromCharPtr) - Char *, Char const*, Char * const, Char const* const - "PyDict_SetItemString(d,\"$symname\", SWIG_FromCharPtr($value));"; - -/* directorin */ - -%typemap(directorin,fragment=#SWIG_FromCharPtr) - Char *, Char const*, Char *const, Char const *const, - Char const *&, Char *const &, Char const *const & - "$input = SWIG_FromCharPtr($1_name);"; - -/* directorout */ - -%typemap(directorout,fragment=#SWIG_AsCharPtr) - Char *, Char const*, Char *const, Char const* const - "if (!$input || !SWIG_AsCharPtr($input, (Char**) &$result)) { - Swig::DirectorTypeMismatchException(\"Error converting Python object into Char*\"); - }"; - -%typemap(directorout,fragment=#SWIG_AsCharPtr) - Char const *&, Char *const &, Char const *const & -{ - Char* temp; - if (!$input || !SWIG_AsCharPtr($input, &temp)) { - Swig::DirectorTypeMismatchException("Error converting Python object into Char*"); - } - $result = ($1_ltype) &temp; -} - -/* typecheck */ - -%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, - fragment=#SWIG_AsCharPtr) - Char *, Char const*, Char *const, Char const *const, - Char const*&, Char *const&, Char const *const & - "$1 = SWIG_AsCharPtr($input, (Char **)(0));"; - -/* throws */ - -%typemap(throws,fragment=#SWIG_FromCharPtr) - Char *, Char const*, Char * const, Char const* const -{ - PyErr_SetObject(PyExc_RuntimeError, SWIG_FromCharPtr($1)); - SWIG_fail; -} - - -/* ------------------------------------------------------------ - * Unknown size const Character array Char[ANY] handling - * ------------------------------------------------------------ */ - -%apply Char* { Char [] }; -%apply const Char* { const Char [] }; - -%typemap(varin,warning="462:Unable to set variable of type Char []") Char [] -{ - PyErr_SetString(PyExc_AttributeError, "C/C++ variable '$name' is read-only"); - return 1; -} - - -/* ------------------------------------------------------------ - * Fix size Character array Char[ANY] handling - * ------------------------------------------------------------ */ - -/* memberin and globalin typemaps */ - -%typemap(memberin) Char [ANY] -{ - if ($input) memcpy($1,$input,$1_dim0*sizeof(Char)); - else memset($1,0,$1_dim0*sizeof(Char)); -} - -%typemap(globalin) Char [ANY] -{ - if ($input) memcpy($1,$input,$1_dim0*sizeof(Char)); - else memset($1,0,$1_dim0*sizeof(Char)); -} - -/* in */ - -%typemap(in,fragment=#SWIG_AsCharArray) - Char [ANY] (Char temp[$1_dim0]), - const Char [ANY](Char temp[$1_dim0]) -{ - if (!SWIG_AsCharArray($input, temp, $1_dim0)) {SWIG_arg_fail($argnum);SWIG_fail;} - $1 = temp; -} - -%typemap(in,fragment=#SWIG_AsCharArray) const Char (&)[ANY] (Char temp[$1_dim0]) -{ - if (!SWIG_AsCharArray($input, temp, $1_dim0)) {SWIG_arg_fail($argnum);SWIG_fail;} - $1 = &temp; -} - -%typemap(out,fragment=#SWIG_FromCharArray) - Char [ANY], const Char[ANY] -{ - size_t size = $1_dim0; -%#ifndef SWIG_PRESERVE_CARRAY_SIZE - while (size && ($1[size - 1] == '\0')) --size; -%#endif - $result = SWIG_FromCharArray($1, size); -} - -/* varin */ - -%typemap(varin,fragment=#SWIG_AsCharArray) Char [ANY] -{ - if (!SWIG_AsCharArray($input, $1, $1_dim0)) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } -} - -/* varout */ - -%typemap(varout,fragment=#SWIG_FromCharArray) - Char [ANY], const Char [ANY] -{ - size_t size = $1_dim0; -%#ifndef SWIG_PRESERVE_CARRAY_SIZE - while (size && ($1[size - 1] == '\0')) --size; -%#endif - $result = SWIG_FromCharArray($1, size); -} - -/* constants */ - -%typemap(constcode,fragment=#SWIG_FromCharArray) - Char [ANY], const Char [ANY] -{ - size_t size = $value_dim0; -%#ifndef SWIG_PRESERVE_CARRAY_SIZE - while (size && ($value[size - 1] == '\0')) --size; -%#endif - PyDict_SetItemString(d,"$symname", SWIG_FromCharArray($value,size)); -} - -/* directorin */ - -%typemap(directorin,fragment=#SWIG_FromCharArray) - Char [ANY], const Char [ANY] -{ - size_t size = $1_dim0; -%#ifndef SWIG_PRESERVE_CARRAY_SIZE - while (size && ($1_name[size - 1] == '\0')) --size; -%#endif - $input = SWIG_FromCharArray($1_name, size); -} - -/* directorout */ - -%typemap(directorout,fragment=#SWIG_AsCharArray) - Char [ANY], const Char [ANY] (Char temp[$result_dim0]) -{ - if (!$input || !SWIG_AsCharArray($input, temp, $result_dim0)) { - Swig::DirectorTypeMismatchException("Error converting Python object into Char[$result_dim0]"); - } - $result = temp; -} - -/* typecheck */ - -%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, - fragment=#SWIG_AsCharArray) - Char [ANY], const Char[ANY] - "$1 = SWIG_AsCharArray($input, (Char *)0, $1_dim0);"; - -/* throws */ - -%typemap(throws,fragment=#SWIG_FromCharArray) - Char [ANY], const Char[ANY] -{ - size_t size = $1_dim0; -%#ifndef SWIG_PRESERVE_CARRAY_SIZE - while (size && ($1[size - 1] == '\0')) --size; -%#endif - PyErr_SetObject(PyExc_RuntimeError, SWIG_FromCharArray($1, size)); - SWIG_fail; -} - -/* ------------------------------------------------------------------- - * --- Really fix size Char arrays, including '\0'chars at the end --- - * ------------------------------------------------------------------- */ - -%typemap(varout,fragment=#SWIG_FromCharArray) - Char FIXSIZE[ANY], const Char FIXSIZE[ANY] -{ - size_t size = $1_dim0; - $result = SWIG_FromCharArray($1, size); -} - -%typemap(out,fragment=#SWIG_FromCharArray) - Char FIXSIZE[ANY], const Char FIXSIZE[ANY] -{ - size_t size = $1_dim0; - $result = SWIG_FromCharArray($1, size); -} - -%typemap(constcode,fragment=#SWIG_FromCharArray) - Char FIXSIZE[ANY], const Char FIXSIZE[ANY] -{ - size_t size = $value_dim0; - PyDict_SetItemString(d,"$symname", SWIG_FromCharArray($value,size)); -} - -%typemap(directorin,fragment=#SWIG_FromCharArray) - Char FIXSIZE[ANY], const Char FIXSIZE[ANY] -{ - size_t size = $1_dim0; - $input = SWIG_FromCharArray($1_name, size); -} - -%typemap(throws,fragment=#SWIG_FromCharArray) - Char FIXSIZE[ANY], const Char FIXSIZE[ANY] -{ - size_t size = $1_dim0; - PyErr_SetObject(PyExc_RuntimeError, SWIG_FromCharArray($1, size)); - SWIG_fail; -} - -/* ------------------------------------------------------------ - * --- String & length --- - * ------------------------------------------------------------ */ - -/* Here len doesn't include the '0' terminator */ -%typemap(in, fragment=#SWIG_AsCharPtrAndSize) - (Char *STRING, int LENGTH) (Char *buf, size_t size) -{ - SWIG_AsCharPtrAndSize($input, &buf, &size); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = ($1_ltype) buf; - $2 = ($2_ltype) size - 1; -} - -/* Here size includes the '0' terminator */ -%typemap(in,fragment=#SWIG_AsCharPtrAndSize) - (Char *STRING, int SIZE) (Char *buf, size_t size) -{ - SWIG_AsCharPtrAndSize($input, &buf, &size); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = ($1_ltype) buf; - $2 = ($2_ltype) size; -} - -%enddef diff --git a/Lib/python/pystrings.swg b/Lib/python/pystrings.swg index 3c1806194..cf4dbd362 100644 --- a/Lib/python/pystrings.swg +++ b/Lib/python/pystrings.swg @@ -1,147 +1,55 @@ - /* ------------------------------------------------------------ * utility methods for char strings * ------------------------------------------------------------ */ -%types(char *); %fragment("SWIG_AsCharPtrAndSize","header") { -/* returns SWIG_OLDOBJ if the input is a raw char*, SWIG_PYSTR if is a PyString */ SWIGINTERN int -SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize) +SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) { static swig_type_info* pchar_info = 0; char* vptr = 0; if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) != -1) { + if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) { if (cptr) *cptr = vptr; if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; - return SWIG_OLDOBJ; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; } else { - PyErr_Clear(); if (PyString_Check(obj)) { - if (cptr) { - *cptr = PyString_AS_STRING(obj); - if (psize) { - *psize = PyString_GET_SIZE(obj) + 1; + char *cstr; int len; + PyString_AsStringAndSize(obj, &cstr, &len); + if (cptr) { + if (alloc) { + if (*alloc == SWIG_NEWOBJ) { + *cptr = SWIG_new_copy_array(cstr, len + 1, char); + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; + } } } - return SWIG_PYSTR; + if (psize) *psize = len + 1; + return SWIG_OK; } } - if (cptr) { - SWIG_type_error("char *", obj); - } - return 0; + return SWIG_TypeError; } } -%fragment("SWIG_AsCharPtr","header", - fragment="SWIG_AsCharPtrAndSize") { -SWIGINTERNINLINE int -SWIG_AsCharPtr(PyObject *obj, char **val) +%fragment("SWIG_FromCharPtrAndSize","header") { +SWIGINTERNINLINE PyObject * +SWIG_FromCharPtrAndSize(const char* carray, size_t size) { - if (SWIG_AsCharPtrAndSize(obj, val, (size_t*)(0))) { - return 1; - } - if (val) { - PyErr_Clear(); - SWIG_type_error("char *", obj); - } - return 0; -} -} - -%fragment("SWIG_FromCharPtr","header") { -SWIGINTERN PyObject * -SWIG_FromCharPtr(const char* cptr) -{ - if (cptr) { - size_t size = strlen(cptr); + if (carray) { if (size > INT_MAX) { - return SWIG_NewPointerObj(SWIG_const_cast(cptr,char*), + return SWIG_NewPointerObj(SWIG_const_cast(carray,char *), SWIG_TypeQuery("char *"), 0); } else { - if (size != 0) { - return PyString_FromStringAndSize(cptr, size); - } else { - return PyString_FromString(cptr); - } + return PyString_FromStringAndSize(carray, SWIG_numeric_cast(size,int)); } - } - Py_INCREF(Py_None); - return Py_None; -} -} - -%fragment("SWIG_AsNewCharPtr","header", - fragment="SWIG_AsCharPtrAndSize") { -SWIGINTERN int -SWIG_AsNewCharPtr(PyObject *obj, char **val) -{ - char* cptr = 0; size_t csize = 0; - int res = SWIG_AsCharPtrAndSize(obj, &cptr, &csize); - if (res) { - if (val) { - if (csize) { - *val = SWIG_new_array(csize, char); - memcpy(*val, cptr, --csize); - (*val)[csize] = 0; - } else if (cptr) { - *val = SWIG_new_array(1, char); - (*val)[0] = 0; - } else { - *val = 0; - } - } - return SWIG_NEWOBJ; - } - if (val) { - SWIG_type_error("char *", obj); - } - return 0; -} -} - -%fragment("SWIG_AsCharArray","header", - fragment="SWIG_AsCharPtrAndSize") { -SWIGINTERN int -SWIG_AsCharArray(PyObject *obj, char *val, size_t size) -{ - char* cptr; size_t csize; - if (SWIG_AsCharPtrAndSize(obj, &cptr, &csize)) { - /* in C you can do: - - char x[5] = "hello"; - - ie, assing the array using an extra '0' char. - */ - if ((csize == size + 1) && !(cptr[csize-1])) --csize; - if (csize <= size) { - if (val) { - if (csize) memcpy(val, cptr, csize); - if (csize < size) memset(val + csize, 0, size - csize); - } - return 1; - } - } - if (val) { - PyErr_Format(PyExc_TypeError, - "a char array of maximum size %lu is expected", - (unsigned long) size); - } - return 0; -} -} - -%fragment("SWIG_FromCharArray","header") { -SWIGINTERNINLINE PyObject * -SWIG_FromCharArray(const char* carray, size_t size) -{ - if (size > INT_MAX) { - return SWIG_NewPointerObj(SWIG_const_cast(carray,char *), - SWIG_TypeQuery("char *"), 0); } else { - return PyString_FromStringAndSize(carray, SWIG_numeric_cast(size,int)); + Py_INCREF(Py_None); + return Py_None; } } } @@ -150,10 +58,5 @@ SWIG_FromCharArray(const char* carray, size_t size) * The plain char * handling * ------------------------------------------------------------ */ -%typemap_pystring(char, - SWIG_AsCharPtr, - SWIG_AsCharPtrAndSize, - SWIG_FromCharPtr, - SWIG_AsNewCharPtr, - SWIG_AsCharArray, - SWIG_FromCharArray) +%include +%typemap_string(char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize, strlen) diff --git a/Lib/python/pyswigtype.swg b/Lib/python/pyswigtype.swg index 85b9cebf4..c21f24d4e 100644 --- a/Lib/python/pyswigtype.swg +++ b/Lib/python/pyswigtype.swg @@ -1,405 +1,11 @@ -/* ----------------------------------------------------------------------------- - * --- Input arguments --- - * ----------------------------------------------------------------------------- */ - -/* Pointers, references, and arrays */ - -%typemap(in) SWIGTYPE *, SWIGTYPE [] - "SWIG_Python_ConvertPtr($input, (void **)&$1, $descriptor, SWIG_POINTER_EXCEPTION | $disown); - if (SWIG_arg_fail($argnum)) SWIG_fail;"; - -%typemap(in) SWIGTYPE* const& ($*ltype temp) - "SWIG_Python_ConvertPtr($input, (void **)&temp, $*descriptor, SWIG_POINTER_EXCEPTION | $disown); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = &temp; - "; - -%typemap(in) SWIGTYPE *DISOWN - "SWIG_Python_ConvertPtr($input, (void **)&$1, $descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN); - if (SWIG_arg_fail($argnum)) SWIG_fail;"; - -/* Additional check for null references */ -%typemap(in) SWIGTYPE & { - SWIG_Python_ConvertPtr($input, (void **)&$1, $descriptor, SWIG_POINTER_EXCEPTION | $disown); - if (SWIG_arg_fail($argnum)) SWIG_fail; - if ($1 == NULL) { - SWIG_null_ref("$basetype"); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; -} - -/* Object passed by value. Convert to a pointer */ -%typemap(in) SWIGTYPE { - $<ype argp; - SWIG_Python_ConvertPtr($input, (void **)&argp, $&descriptor, SWIG_POINTER_EXCEPTION); - if (SWIG_arg_fail($argnum)) SWIG_fail; - if (argp == NULL) { - SWIG_null_ref("$basetype"); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = *argp; -} - -/* Pointer to a class member */ -%typemap(in) SWIGTYPE (CLASS::*) { - if ((SWIG_ConvertPacked($input,(void *)(&$1),sizeof($type),$descriptor,0)) == -1) { - SWIG_type_error("$type",$input); - } - if (SWIG_arg_fail($argnum)) SWIG_fail; -} - -/* ----------------------------------------------------------------------------- - * --- Output arguments --- - * ----------------------------------------------------------------------------- */ - -/* Pointers, references, and arrays */ -%typemap(out) SWIGTYPE *, SWIGTYPE & - "$result = SWIG_NewPointerObj((void*)($1), $descriptor, $owner);"; - - -%typemap(out) SWIGTYPE* const& - "$result = SWIG_NewPointerObj((void*)(*$1), $*descriptor, $owner);"; - -/* Dynamic casts */ - -%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { - swig_type_info *ty = SWIG_TypeDynamicCast($descriptor, (void **) &$1); - $result = SWIG_NewPointerObj((void *) $1, ty, $owner); -} - -/* Member pointer */ -%typemap(out) SWIGTYPE (CLASS::*) - "$result = SWIG_NewPackedObj((void*)(&$1), sizeof($type), $descriptor);"; - -/* Primitive types--return by value */ -#ifdef __cplusplus -%typemap(out) SWIGTYPE -{ - $<ype resultptr; - resultptr = new $ltype(SWIG_static_cast($1,$type &)); - $result = SWIG_NewPointerObj((void *)(resultptr), $&descriptor, 1); -} -#else -%typemap(out /* warning="452:Default return typemap could be unsafe" */) SWIGTYPE -{ - $<ype resultptr; - resultptr = ($<ype) malloc(sizeof($type)); - if (resultptr) memcpy(resultptr, &$1, sizeof($type)); - $result = SWIG_NewPointerObj((void *)(resultptr), $&descriptor, 1); -} -#endif - - -/* ----------------------------------------------------------------------------- - * --- Variable input --- - * ----------------------------------------------------------------------------- */ - -/* memberin/globalin/varin, for fix arrays. */ - -%typemap(memberin) SWIGTYPE [ANY] { - $basetype *inp = SWIG_static_cast($input, $basetype *); - if (inp) { - $basetype *dest = SWIG_static_cast($1, $basetype *); - size_t ii = 0; - for (; ii < $dim0; ++ii) dest[ii] = inp[ii]; - } else { - SWIG_null_ref("$basetype"); - } -} - -%typemap(globalin) SWIGTYPE [ANY] { - $basetype *inp = SWIG_static_cast($input, $basetype *); - if (inp) { - $basetype *dest = SWIG_static_cast($1, $basetype *); - size_t ii = 0; - for (; ii < $dim0; ++ii) dest[ii] = inp[ii]; - } else { - SWIG_null_ref("$basetype"); - } -} - -%typemap(varin) SWIGTYPE [ANY] { - $basetype *inp = 0; - if ((SWIG_ConvertPtr($input, (void **)&inp, $descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } else if (inp) { - size_t ii = 0; - $basetype *dest = SWIG_static_cast($1, $basetype *); - for (; ii < $dim0; ++ii) dest[ii] = inp[ii]; - } else { - SWIG_null_ref("$basetype"); - return 1; - } -} - -/* memberin/globalin/varin, for fix double arrays. */ - -%typemap(memberin) SWIGTYPE [ANY][ANY] { - $basetype (*inp)[$dim1] = SWIG_static_cast($input, $basetype (*)[$dim1]); - if (inp) { - $basetype (*dest)[$dim1] = SWIG_static_cast($1, $basetype (*)[$dim1]); - size_t ii = 0; - for (; ii < $dim0; ++ii) { - $basetype *ip = inp[ii]; - if (ip) { - $basetype *dp = dest[ii]; - size_t jj = 0; - for (; jj < $dim1; ++jj) dp[jj] = ip[jj]; - } else { - SWIG_null_ref("$basetype"); - } - } - } else { - SWIG_null_ref("$basetype[$dim1]"); - } -} - -%typemap(globalin) SWIGTYPE [ANY][ANY] { - $basetype (*inp)[$dim1] = SWIG_static_cast($input, $basetype (*)[$dim1]); - if (inp) { - $basetype (*dest)[$dim1] = SWIG_static_cast($1, $basetype (*)[$dim1]); - size_t ii = 0; - for (; ii < $dim0; ++ii) { - $basetype *ip = inp[ii]; - if (ip) { - $basetype *dp = dest[ii]; - size_t jj = 0; - for (; jj < $dim1; ++jj) dp[jj] = ip[jj]; - } else { - SWIG_null_ref("$basetype"); - } - } - } else { - SWIG_null_ref("$basetype[$dim1]"); - } -} - -%typemap(varin) SWIGTYPE [ANY][ANY] { - $basetype (*inp)[$dim1] = 0; - if ((SWIG_ConvertPtr($input, (void **)&inp, $descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } else if (inp) { - $basetype (*dest)[$dim1] = SWIG_static_cast($1, $basetype (*)[$dim1]); - size_t ii = 0; - for (; ii < $dim0; ++ii) { - $basetype *ip = inp[ii]; - if (ip) { - $basetype *dp = dest[ii]; - size_t jj = 0; - for (; jj < $dim1; ++jj) dp[jj] = ip[jj]; - } else { - SWIG_null_ref("$basetype"); - return 1; - } - } - } else { - SWIG_null_ref("$basetype[$dim1]"); - return 1; - } -} - -/* Pointers, references, and variable size arrays */ - -%typemap(varin) SWIGTYPE * { - void *temp; - if ((SWIG_ConvertPtr($input, &temp, $descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - $1 = ($ltype) temp; -} - -%typemap(varin,warning="462:Unable to set dimensionless array variable") SWIGTYPE [] -{ - PyErr_SetString(PyExc_AttributeError, "C/C++ variable '$name' is read-only"); - return 1; -} - -%typemap(varin) SWIGTYPE & { - void *temp; - if ((SWIG_ConvertPtr($input, &temp, $descriptor, SWIG_POINTER_EXCEPTION)) == -1 || temp == NULL) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - $1 = *($ltype) temp; -} - -%typemap(varin) SWIGTYPE (CLASS::*) { - char temp[sizeof($type)]; - if ((SWIG_ConvertPacked($input,(void *) temp, sizeof($type), $descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - memmove((void *) &$1,temp,sizeof($type)); -} - -%typemap(varin) SWIGTYPE { - $<ype temp; - if ((SWIG_ConvertPtr($input, (void **)(&temp), $&descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - $1 = *(($&type) temp); -} - -/* ----------------------------------------------------------------------------- - * --- Variable output --- - * ----------------------------------------------------------------------------- */ - -/* Pointers and arrays */ -%typemap(varout) SWIGTYPE *, SWIGTYPE [] - "$result = SWIG_NewPointerObj((void *)($1), $descriptor, 0);"; - -/* References */ -%typemap(varout) SWIGTYPE & - "$result = SWIG_NewPointerObj((void *)(&$1), $descriptor, 0);"; - -/* Member pointer */ -%typemap(varout) SWIGTYPE (CLASS::*) - "$result = SWIG_NewPackedObj((void *)(&$1), sizeof($type), $descriptor);"; - -%typemap(varout) SWIGTYPE - "$result = SWIG_NewPointerObj((void *)(&$1), $&descriptor, 0);"; - -/* ----------------------------------------------------------------------------- - * --- Constants --- * - * ----------------------------------------------------------------------------- */ - - -/* Pointers, arrays, objects */ - -%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] -{ SWIG_PY_POINTER, (char*)"$symname", 0, 0, (void *)$value, &$descriptor} - -%typemap(consttab) SWIGTYPE (CLASS::*) -{ SWIG_PY_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$descriptor} - - -/* ----------------------------------------------------------------------------- - * --- Director typemaps --- * - * ----------------------------------------------------------------------------- */ - -/* directorin */ - -%typemap(directorin) SWIGTYPE* { - $input = SWIG_NewPointerObj(SWIG_static_cast(SWIG_static_cast($1_name, $1_ltype), void*), $descriptor, 0); -} - -%typemap(directorin) SWIGTYPE { - $input = SWIG_NewPointerObj(SWIG_static_cast(SWIG_static_cast(&$1_name, $&1_ltype), void*), $&descriptor, 0); -} - -%typemap(directorin) SWIGTYPE& { - $input = SWIG_NewPointerObj(SWIG_static_cast(SWIG_static_cast(&$1_name, $1_ltype), void*), $descriptor, 0); -} - -/* the const cases */ -%typemap(directorin) SWIGTYPE const& { - $input = SWIG_NewPointerObj(SWIG_static_cast(SWIG_const_cast(&$1_name, $1_ltype), void*), $descriptor, 0); -} - -%typemap(directorin) SWIGTYPE const* { - $input = SWIG_NewPointerObj(SWIG_static_cast(SWIG_const_cast($1_name, $1_ltype), void*), $descriptor, 0); -} - - -/* directorout */ - -%typemap(directorout) SWIGTYPE ($<ype argp) - "if (!$input || (SWIG_ConvertPtr($input, (void **)(&argp), - $&descriptor, SWIG_POINTER_EXCEPTION | $disown)) == -1) - Swig::DirectorTypeMismatchException::raise(\"Pointer conversion failed.\"); - $result = *argp;"; - -%typemap(directorout) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] - "if (!$input || (SWIG_ConvertPtr($input,(void **)(&$result), - $descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) - Swig::DirectorTypeMismatchException::raise(\"Pointer conversion failed.\");"; - +%include /* ------------------------------------------------------------ - * --- Typechecking rules --- + * --- Consttab --- needed for callbacks, it should be removed later. * ------------------------------------------------------------ */ -%typecheck(SWIG_TYPECHECK_POINTER) - SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] -{ - void *ptr; - if (SWIG_ConvertPtr($input, &ptr, $descriptor, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} +%typemap(consttab) SWIGTYPE ((*)(ANY)) +{ SWIG_PY_POINTER, (char*)"$symname", 0, 0, (void *)($value), &$descriptor } -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE & -{ - void *ptr = 0; - if (SWIG_ConvertPtr($input, &ptr, $descriptor, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = (ptr != 0); - } -} +%typemap(constcode) SWIGTYPE ((*)(ANY)) ""; -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE -{ - void *ptr = 0; - if (SWIG_ConvertPtr($input, &ptr, $&descriptor, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = (ptr != 0); - } -} - - -/* ------------------------------------------------------------ - * --- Exception handling --- - * ------------------------------------------------------------ */ - -%typemap(throws) SWIGTYPE { - $<ype temp = new $ltype($1); - if ($&descriptor->clientdata) { - PyErr_SetObject((PyObject *) ($&descriptor->clientdata), SWIG_NewPointerObj(temp,$&descriptor,1)); - } else { - PyErr_SetString(PyExc_RuntimeError,"$type"); - } - SWIG_fail; -} - - -%typemap(throws) SWIGTYPE * { - if ($descriptor->clientdata) { - PyErr_SetObject((PyObject *) ($descriptor->clientdata), - SWIG_NewPointerObj((void *) $1,$descriptor,1)); - } else { - PyErr_SetString(PyExc_RuntimeError,"$type"); - } - SWIG_fail; -} - - -%typemap(throws) SWIGTYPE [ANY] { - if ($descriptor->clientdata) { - PyErr_SetObject((PyObject *) ($descriptor->clientdata), - SWIG_NewPointerObj((void *)$1,$descriptor,1)); - } else { - PyErr_SetString(PyExc_RuntimeError,"$type"); - } - SWIG_fail; -} - -%typemap(throws) SWIGTYPE & { - if ($descriptor->clientdata) { - PyErr_SetObject((PyObject *) ($descriptor->clientdata), - SWIG_NewPointerObj((void *)&($1),$descriptor,1)); - } else { - PyErr_SetString(PyExc_RuntimeError,"$type"); - } - SWIG_fail; -} diff --git a/Lib/python/python.swg b/Lib/python/python.swg index 02bacb1f3..7c10c4802 100644 --- a/Lib/python/python.swg +++ b/Lib/python/python.swg @@ -9,9 +9,16 @@ resolution. */ -#undef SWIG_TYPECHECK_BOOL -%define SWIG_TYPECHECK_BOOL 10000 %enddef +/* ----------------------------------------------------------------------------- + * Inner macros + * ----------------------------------------------------------------------------- */ +%include +/* ----------------------------------------------------------------------------- + * Error manipulation + * ----------------------------------------------------------------------------- */ + +%include /* ----------------------------------------------------------------------------- * The runtime part @@ -23,30 +30,11 @@ * ----------------------------------------------------------------------------- */ %include -/* ----------------------------------------------------------------------------- - * Inner macros (ugly ones) - * ----------------------------------------------------------------------------- */ -%include - -/* ----------------------------------------------------------------------------- - * Look for user fragments file. If not found, include empty system one. - * ----------------------------------------------------------------------------- */ -%include "pyfragments.swg" - /* ----------------------------------------------------------------------------- * Typemap specializations - * ----------------------------------------------------------------------------- */ -%include -%include -%include -%include -%include -%include -%include -%include -%include -%include -%include + * ----------------------------------------------------------------------------- */ + +%include /* ------------------------------------------------------------ * Overloaded operator support diff --git a/Lib/python/pytuplehlp.swg b/Lib/python/pytuplehlp.swg index ad96bfd32..8b4b419eb 100644 --- a/Lib/python/pytuplehlp.swg +++ b/Lib/python/pytuplehlp.swg @@ -4,23 +4,5 @@ (std::pair,std::vector,std::list,etc) return tuples. */ -%fragment("t_output_helper","header") %{ -SWIGINTERN PyObject* -t_output_helper(PyObject* target, PyObject* o) { - if (!target) { - target = o; - } else if (target == Py_None) { - Py_DECREF(target); - target = o; - } else { - if (!PyList_Check(target)) { - PyObject *o2 = target; - target = PyList_New(1); - PyList_SetItem(target, 0, o2); - } - PyList_Append(target,o); - Py_DECREF(o); - } - return target; -} -%} +#warning "Deprecated file: Don't use t_output_helper anymore," +#warning "use SWIG_Python_AppendResult or SWIG_append_result instead." diff --git a/Lib/python/pytypemaps.swg b/Lib/python/pytypemaps.swg new file mode 100644 index 000000000..324e33801 --- /dev/null +++ b/Lib/python/pytypemaps.swg @@ -0,0 +1,56 @@ +/* ----------------------------------------------------------------------------- + * Typemap specializations + * ----------------------------------------------------------------------------- */ + +/* directors are supported in Python */ +#ifndef SWIG_DIRECTOR_TYPEMAPS +#define SWIG_DIRECTOR_TYPEMAPS +#endif + +/* bool is dangerous in Python -> C++, change precedence */ +#undef SWIG_TYPECHECK_BOOL +%define SWIG_TYPECHECK_BOOL 10000 %enddef + + +/* ----------------------------------------------------------------------------- + * Basic definitions + * ----------------------------------------------------------------------------- */ + +%define_swig_object(PyObject *) + +#define SWIG_SetResultObj(obj) $result = obj +#define SWIG_AppendResultObj(obj) $result = SWIG_Python_AppendResult($result, obj) +#define SWIG_SetConstantObj(name, obj) PyDict_SetItemString(d, name, obj); +#define SWIG_NoneObject() SWIG_Python_NoneObject() + +/* error manipulation */ +#define SWIG_ErrorType(code) SWIG_Python_ErrorType(code) +#define SWIG_SetErrorObj(code, obj) PyErr_SetObject(SWIG_ErrorType(code), obj) +#define SWIG_SetErrorMsg(code, msg) SWIG_Python_SetErrorMsg(SWIG_ErrorType(code),msg) +#define SWIG_ExceptionObj(desc, type, obj) SWIG_Python_SetExceptionObj(desc, obj) +#define SWIG_DirOutFail(code, msg) Swig::DirectorTypeMismatchException::raise(SWIG_ErrorType(code), msg) + + +/* ----------------------------------------------------------------------------- + * All the typemaps + * ----------------------------------------------------------------------------- */ + + +%fragment("t_output_helper","header") %{ +#define t_output_helper SWIG_Python_AppendResult +%} + + +%include "pyfragments.swg" + +%include +%include +%include +%include +%include +%include +%include +%include +%include +%include + diff --git a/Lib/python/pyuserdir.swg b/Lib/python/pyuserdir.swg index d3ec314cf..849d6dbd1 100644 --- a/Lib/python/pyuserdir.swg +++ b/Lib/python/pyuserdir.swg @@ -105,8 +105,9 @@ These methods "may be called" if needed. #define %nopythoncallback %feature("python:callback","0") #define %clearpythoncallback %feature("python:callback","") - -/* Support for the old %callback directive name */ +/* + Support for the old %callback directive name +*/ #ifdef %callback #undef %callback #endif diff --git a/Lib/python/pyvaltypes.swg b/Lib/python/pyvaltypes.swg deleted file mode 100644 index 76225eced..000000000 --- a/Lib/python/pyvaltypes.swg +++ /dev/null @@ -1,208 +0,0 @@ -/*--------------------------------------------------------------------- - * Value typemaps (Type, const Type&) for value types, such as - * fundamental types (int, double), that define the As/AsVal/From - * methods. - *---------------------------------------------------------------------*/ - - -/* in */ - -%define PYVAL_IN_TYPEMAP(as_meth,pyfrag,Type...) - %typemap(in,fragment=pyfrag) Type { - $1 = SWIG_static_cast(SWIG_arg(as_meth($input)),$type); - if (SWIG_arg_fail($argnum)) SWIG_fail; - } - %typemap(in,fragment=pyfrag) const Type & ($*ltype temp) { - temp = SWIG_static_cast(SWIG_arg(as_meth($input)),$basetype); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = &temp; - } -%enddef - -/* out */ - -%define PYVAL_OUT_TYPEMAP(from_meth,pyfrag,Type...) - %typemap(out,fragment=pyfrag) Type, const Type - { $result = from_meth(SWIG_static_cast($1,Type)); } - - %typemap(out,fragment=pyfrag) const Type& - { $result = from_meth(SWIG_static_cast(*$1,Type)); } -%enddef - -/* varin */ - -%define PYVAL_VARIN_TYPEMAP(as_meth,pyfrag,Type...) - %typemap(varin,fragment=pyfrag) Type { - $1_type temp = SWIG_static_cast(SWIG_arg(as_meth($input)),$1_type); - if (PyErr_Occurred()) { - SWIG_append_errmsg("C/C++ variable '$name ($1_ltype)'"); - return 1; - } - $1 = temp; - } -%enddef - -/* varout */ - -%define PYVAL_VAROUT_TYPEMAP(from_meth,pyfrag,Type...) - %typemap(varout,fragment=pyfrag) Type, const Type& - { $result = from_meth(SWIG_static_cast($1,$basetype)); } -%enddef - -/* constant installation code */ - -%define PYVAL_CONSTCODE_TYPEMAP(from_meth,pyfrag,Type...) - %typemap(constcode,fragment=pyfrag) Type - { PyDict_SetItemString(d,"$symname", from_meth(SWIG_static_cast($value,$basetype))); } -%enddef - -/* directorin */ - -%define PYVAL_DIRECTORIN_TYPEMAP(from_meth,pyfrag,Type...) - %typemap(directorin,fragment=pyfrag) Type *DIRECTORIN - { $input = from_meth(SWIG_static_cast(*$1_name,$basetype)); } - %typemap(directorin,fragment=pyfrag) Type, const Type& - { $input = from_meth(SWIG_static_cast($1_name,$basetype)); } -%enddef - -/* directorout */ - -%define PYVAL_DIRECTOROUT_TYPEMAP(as_meth,pyfrag,Type...) - %typemap(directorargout,fragment=pyfrag) Type *DIRECTOROUT { - if ($input) *$result = SWIG_static_cast(SWIG_arg(as_meth($input)),$type); - if (!$input || PyErr_Occurred()) - Swig::DirectorTypeMismatchException::raise("Error converting Python object when using as_meth."); - } - %typemap(directorout,fragment=pyfrag) Type { - if ($input) $result = SWIG_static_cast(SWIG_arg(as_meth($input)),$type); - if (!$input || PyErr_Occurred()) - Swig::DirectorTypeMismatchException::raise("Error converting Python object when using as_meth."); - } - %typemap(directorout,fragment=pyfrag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& { - if ($input) { - static $basetype temp = SWIG_static_cast(SWIG_arg(as_meth($input)),$basetype); - $result = &temp; - } - if (!$input || PyErr_Occurred()) - Swig::DirectorTypeMismatchException::raise("Error converting Python object when using as_meth."); - } - %typemap(directorout,fragment=pyfrag) Type &DIRECTOROUT = Type -%enddef - -/* throws */ - -%define PYVAL_THROWS_TYPEMAP(from_meth,pyfrag,Type...) - %typemap(throws,fragment=pyfrag) Type { - PyErr_SetObject(PyExc_RuntimeError, from_meth(SWIG_static_cast($1,$basetype))); - SWIG_fail; - } -%enddef - -/* typecheck */ - -%define PYVAL_TYPECHECK_TYPEMAP(check,pyobj_check,pyfrag,Type...) -%typemap(typecheck,precedence=check,fragment=pyfrag) - Type, const Type& - "$1 = pyobj_check($input);"; -%enddef - -/*--------------------------------------------------------------------- - * typemap definition for types with As/Check methods - *---------------------------------------------------------------------*/ -%define %typemap_ascheck(CheckCode, AsMeth, CheckMeth, - AsFrag, CheckFrag, Type...) - PYVAL_IN_TYPEMAP(SWIG_arg(AsMeth), SWIG_arg(AsFrag), Type); - PYVAL_VARIN_TYPEMAP(SWIG_arg(AsMeth), SWIG_arg(AsFrag), Type); - PYVAL_DIRECTOROUT_TYPEMAP(SWIG_arg(AsMeth), SWIG_arg(AsFrag), Type); - PYVAL_TYPECHECK_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(CheckMeth), - SWIG_arg(CheckFrag), Type); - - PYVAL_INPUT_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(AsMeth), SWIG_arg(CheckMeth), - SWIG_arg(AsFrag), SWIG_arg(CheckFrag), Type); -%enddef - -/*--------------------------------------------------------------------- - * typemap definition for types with AsVal method - *---------------------------------------------------------------------*/ -%define %typemap_asvaln(CheckCode, Type...) -%fragment(SWIG_As_frag(Type),"header", - fragment=SWIG_AsVal_frag(Type)) %{ -SWIGINTERNINLINE Type -SWIG_As(Type)(PyObject* obj) -{ - Type v; - if (!SWIG_AsVal(Type)(obj, &v)) { - /* - this is needed to make valgrind/purify happier. - */ - memset((void*)&v, 0, sizeof(Type)); - } - return v; -} -%} -%fragment(SWIG_Check_frag(Type),"header", - fragment=SWIG_AsVal_frag(Type)) %{ -SWIGINTERNINLINE int -SWIG_Check(Type)(PyObject* obj) -{ - return SWIG_AsVal(Type)(obj, (Type*)0); -} -%} -%typemap_ascheck(SWIG_arg(CheckCode), - SWIG_As(Type), - SWIG_Check(Type), - SWIG_arg(SWIG_As_frag(Type)), - SWIG_arg(SWIG_Check_frag(Type)), - Type); -%enddef - -/*--------------------------------------------------------------------- - * typemap definition for types with from method - *---------------------------------------------------------------------*/ -%define %typemap_from(FromMeth, FromFrag, Type...) - PYVAL_OUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - PYVAL_VAROUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - PYVAL_CONSTCODE_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - PYVAL_DIRECTORIN_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - PYVAL_THROWS_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - PYVAL_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - -%enddef - - -%define %typemap_ascheckfrom(CheckCode, AsMeth, CheckMeth, FromMeth, - AsFrag, CheckFrag, FromFrag, Type...) - %typemap_ascheck(SWIG_arg(CheckCode), SWIG_arg(AsMeth), SWIG_arg(CheckMeth), - SWIG_arg(AsFrag), SWIG_arg(CheckFrag), Type); - %typemap_from(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); - - PYVAL_INOUT_TYPEMAP(Type); - -%enddef - -/*--------------------------------------------------------------------- - * typemap definition for types with asval/from method - *---------------------------------------------------------------------*/ -%define %typemap_asvalfromn(CheckCode, Type...) - %typemap_asvaln(SWIG_arg(CheckCode), Type); - %typemap_from(SWIG_arg(SWIG_From(Type)), - SWIG_arg(SWIG_From_frag(Type)), - Type); - - PYVAL_INOUT_TYPEMAP(Type); - -%enddef - -/*--------------------------------------------------------------------- - * typemap definition for types with as/check/from method - *---------------------------------------------------------------------*/ -%define %typemap_ascheckfromn(CheckCode, Type...) - %typemap_ascheckfrom(SWIG_arg(CheckCode), - SWIG_As(Type), - SWIG_From(Type), - SWIG_Check(Type), - SWIG_arg(SWIG_As_frag(Type)), - SWIG_arg(SWIG_From_frag(Type)), - SWIG_arg(SWIG_Check_frag(Type)), - Type); -%enddef diff --git a/Lib/python/pyvoid.swg b/Lib/python/pyvoid.swg deleted file mode 100644 index 572cb1316..000000000 --- a/Lib/python/pyvoid.swg +++ /dev/null @@ -1,61 +0,0 @@ -/* ------------------------------------------------------------ - * Void * - Accepts any kind of pointer - * ------------------------------------------------------------ */ - -/* in */ - -%typemap(in) void * { - if ((SWIG_ConvertPtr($input,SWIG_reinterpret_cast(&$1,void **),0,SWIG_POINTER_EXCEPTION|$disown))== -1) { - SWIG_arg_fail($argnum);SWIG_fail; - } -} - -%typemap(in) void * const& ($*ltype temp) { - SWIG_ConvertPtr($input,(void **)&temp,0,SWIG_POINTER_EXCEPTION|$disown); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = &temp; -} - - -/* out */ - -%typemap(out) void "Py_INCREF(Py_None); $result = Py_None;"; - -/* varin */ - -%typemap(varin) void * { - void * temp; - if ((SWIG_ConvertPtr($input, SWIG_static_cast(&temp,void **), 0, - SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) { - SWIG_append_errmsg("C/C++ variable '$name'"); - return 1; - } - $1 = ($1_ltype) temp; -} - -/* varout */ - -%typemap(varout) void "Py_INCREF(Py_None); $result = Py_None;"; - -/* directorout */ - -%typemap(directorout) void * { - if (!$input || (SWIG_ConvertPtr($input,(void **)(&$result), - 0, SWIG_POINTER_EXCEPTION | $disown )) == -1) - Swig::DirectorTypeMismatchException::raise("Pointer conversion failed."); -} - - - -/* typecheck */ - -%typecheck(SWIG_TYPECHECK_VOIDPTR) void * -{ - void *ptr; - if (SWIG_ConvertPtr($input, &ptr, 0, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} diff --git a/Lib/python/pywstrings.swg b/Lib/python/pywstrings.swg index b2a0b1169..bd0dea188 100644 --- a/Lib/python/pywstrings.swg +++ b/Lib/python/pywstrings.swg @@ -1,141 +1,58 @@ - /* ------------------------------------------------------------ * utility methods for wchar_t strings * ------------------------------------------------------------ */ -%fragment("SWIG_AsWCharPtrAndSize","header") { +%fragment("SWIG_AsWCharPtrAndSize","header",fragment="") { SWIGINTERN int -SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize) +SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) { static swig_type_info* pwchar_info = 0; wchar_t * vptr = 0; if (!pwchar_info) pwchar_info = SWIG_TypeQuery("wchar_t *"); - if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) != -1) { + if (SWIG_ConvertPtr(obj, (void**)&vptr, pwchar_info, 0) == SWIG_OK) { if (cptr) *cptr = vptr; if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0; - return SWIG_OLDOBJ; + return SWIG_OK; } else { + PyObject *tmp = 0; int isunicode = PyUnicode_Check(obj); - if (isunicode || PyString_Check(obj)) { + if (!isunicode && PyString_Check(obj)) { if (cptr) { - int size = isunicode ? PyUnicode_GetSize(obj) : PyString_Size(obj); - wchar_t *nptr = SWIG_new_array(size + 1, wchar_t); - PyUnicodeObject *uni = (PyUnicodeObject *)PyUnicode_FromObject(obj); - PyUnicode_AsWideChar(uni, nptr, size); - nptr[size] = 0; - *cptr = nptr; - if (psize) { - *psize = (size_t) size + 1; - } - Py_DECREF(uni); + obj = tmp = PyUnicode_FromObject(obj); } - return SWIG_NEWOBJ; + isunicode = 1; + } + if (isunicode) { + int len = PyUnicode_GetSize(obj); + if (cptr) { + *cptr = SWIG_new_array(len + 1, wchar_t); + PyUnicode_AsWideChar((PyUnicodeObject *)obj, *cptr, len); + (*cptr)[len] = 0; + } + if (psize) *psize = (size_t) len + 1; + if (alloc) *alloc = cptr ? SWIG_NEWOBJ : 0; + if (tmp) Py_DECREF(tmp); + return SWIG_OK; } } - if (cptr) { - SWIG_type_error("wchar_t *", obj); - } - return 0; + return SWIG_TypeError; } } -%fragment("SWIG_AsWCharPtr","header", - fragment="SWIG_AsWCharPtrAndSize") { -SWIGINTERN int -SWIG_AsWCharPtr(PyObject *obj, wchar_t **val) +%fragment("SWIG_FromWCharPtrAndSize","header",fragment="") { +SWIGINTERNINLINE PyObject * +SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size) { - if (SWIG_AsWCharPtrAndSize(obj, val, (size_t*)(0))) { - return 1; - } - if (val) { - PyErr_Clear(); - SWIG_type_error("wchar_t *", obj); - } - return 0; -} -} - -%fragment("SWIG_FromWCharPtr","header") { -SWIGINTERN PyObject * -SWIG_FromWCharPtr(const wchar_t * cptr) -{ - if (cptr) { - size_t size = wcslen(cptr); + if (carray) { if (size > INT_MAX) { - return SWIG_NewPointerObj(SWIG_const_cast(cptr,wchar_t *), + return SWIG_NewPointerObj(SWIG_const_cast(carray,wchar_t *), SWIG_TypeQuery("wchar_t *"), 0); } else { - return PyUnicode_FromWideChar(cptr, size); + return PyUnicode_FromWideChar(carray, SWIG_numeric_cast(size,int)); } - } - Py_INCREF(Py_None); - return Py_None; -} -} - -%fragment("SWIG_AsNewWCharPtr","header", - fragment="SWIG_AsWCharPtrAndSize") { -SWIGINTERN int -SWIG_AsNewWCharPtr(PyObject *obj, wchar_t **val) -{ - wchar_t * cptr = 0; size_t csize = 0; - int res = SWIG_AsWCharPtrAndSize(obj, &cptr, &csize); - if (res) { - if (val) { - if (csize) { - *val = SWIG_new_array(csize, wchar_t); - memcpy(*val, cptr, (--csize)*sizeof(wchar_t)); - (*val)[csize] = 0; - } else if (cptr) { - *val = SWIG_new_array(1, wchar_t); - (*val)[0] = 0; - } else { - *val = 0; - } - } - return SWIG_NEWOBJ; - } - if (val) { - SWIG_type_error("wchar_t *", obj); - } - return 0; -} -} - -%fragment("SWIG_AsWCharArray","header", - fragment="SWIG_AsWCharPtrAndSize") { -SWIGINTERN int -SWIG_AsWCharArray(PyObject *obj, wchar_t *val, size_t size) -{ - wchar_t * cptr; size_t csize; - if (SWIG_AsWCharPtrAndSize(obj, &cptr, &csize)) { - if ((csize == size + 1) && !(cptr[csize-1])) --csize; - if (csize <= size) { - if (val) { - if (csize) memcpy(val, cptr, csize*sizeof(wchar_t)); - if (csize < size) memset(val+csize, 0, (size-csize)*sizeof(wchar_t)); - } - return 1; - } - } - if (val) { - PyErr_Format(PyExc_ValueError, - "a wchar_t array of maximum size %lu is expected", - (unsigned long) size); - } - return 0; -} -} - -%fragment("SWIG_FromWCharArray","header") { -SWIGINTERNINLINE PyObject * -SWIG_FromWCharArray(const wchar_t * carray, size_t size) -{ - if (size > INT_MAX) { - return SWIG_NewPointerObj(SWIG_const_cast(carray,wchar_t *), - SWIG_TypeQuery("wchar_t *"), 0); } else { - return PyUnicode_FromWideChar(carray, SWIG_numeric_cast(size,int)); + Py_INCREF(Py_None); + return Py_None; } } } @@ -144,11 +61,6 @@ SWIG_FromWCharArray(const wchar_t * carray, size_t size) * The plain wchar_t * handling * ------------------------------------------------------------ */ -%typemap_pystring(wchar_t, - SWIG_AsWCharPtr, - SWIG_AsWCharPtrAndSize, - SWIG_FromWCharPtr, - SWIG_AsNewWCharPtr, - SWIG_AsWCharArray, - SWIG_FromWCharArray); +%include +%typemap_string(wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize, wcslen); diff --git a/Lib/python/std_basic_string.i b/Lib/python/std_basic_string.i index 9268223c7..d0db4c5c4 100644 --- a/Lib/python/std_basic_string.i +++ b/Lib/python/std_basic_string.i @@ -8,40 +8,41 @@ %fragment(SWIG_AsPtr_frag(std::basic_string),"header", fragment="SWIG_AsCharPtrAndSize") { - SWIGINTERN int - SWIG_AsPtr(std::basic_string)(PyObject* obj, std::string **val) - { - static swig_type_info* string_info = - SWIG_TypeQuery("std::basic_string *"); - std::string *vptr; - if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) != -1) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - PyErr_Clear(); - char* buf = 0 ; size_t size = 0; - if (SWIG_AsCharPtrAndSize(obj, &buf, &size)) { - if (buf) { - if (val) *val = new std::string(buf, size - 1); - return SWIG_NEWOBJ; - } - } else { - PyErr_Clear(); - } - if (val) { - PyErr_SetString(PyExc_TypeError,"a string is expected"); - } - return 0; +SWIGINTERN int +SWIG_AsPtr(std::basic_string)(PyObject* obj, std::string **val) +{ + static swig_type_info* string_info = + SWIG_TypeQuery("std::basic_string *"); + std::string *vptr; + if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) == SWIG_OK) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + PyErr_Clear(); + char* buf = 0 ; size_t size = 0; int alloc = 0; + if (SWIG_AsCharPtrAndSize(obj, &buf, &size, &alloc) == SWIG_OK) { + if (buf) { + if (val) *val = new std::string(buf, size - 1); + if (alloc == SWIG_NEWOBJ) SWIG_delete_array(buf); + return SWIG_NEWOBJ; } + } else { + PyErr_Clear(); } + if (val) { + PyErr_SetString(PyExc_TypeError,"a string is expected"); + } + return 0; + } +} } %fragment(SWIG_From_frag(std::basic_string),"header", - fragment="SWIG_FromCharArray") { + fragment="SWIG_FromCharPtrAndSize") { SWIGINTERNINLINE PyObject* SWIG_From(std::basic_string)(const std::string& s) { - return SWIG_FromCharArray(s.data(), s.size()); + return SWIG_FromCharPtrAndSize(s.data(), s.size()); } } @@ -61,17 +62,16 @@ SWIGINTERN int static swig_type_info* string_info = SWIG_TypeQuery("std::basic_string *"); std::wstring *vptr; - if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) != -1) { + if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) == SWIG_OK) { if (val) *val = vptr; return SWIG_OLDOBJ; } else { PyErr_Clear(); - wchar_t *buf = 0 ; size_t size = 0; - int res = 0; - if ((res = SWIG_AsWCharPtrAndSize(obj, &buf, &size))) { + wchar_t *buf = 0 ; size_t size = 0; int alloc = 0; + if (SWIG_AsWCharPtrAndSize(obj, &buf, &size, &alloc) == SWIG_OK) { if (buf) { if (val) *val = new std::wstring(buf, size - 1); - if (res == SWIG_NEWOBJ) SWIG_delete_array(buf); + if (alloc == SWIG_NEWOBJ) SWIG_delete_array(buf); return SWIG_NEWOBJ; } } else { @@ -86,11 +86,11 @@ SWIGINTERN int } %fragment(SWIG_From_frag(std::basic_string),"header", - fragment="SWIG_FromWCharArray") { + fragment="SWIG_FromWCharPtrAndSize") { SWIGINTERNINLINE PyObject* SWIG_From(std::basic_string)(const std::wstring& s) { - return SWIG_FromWCharArray(s.data(), s.size()); + return SWIG_FromWCharPtrAndSize(s.data(), s.size()); } } diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index f12a15fd1..d0505bf0a 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -16,14 +16,14 @@ T *pfirst = 0; U *psecond = 0; if (val) { - *val = new std::pair; + *val = SWIG_new(std::pair); pfirst = &((*val)->first); psecond = &((*val)->second); } if (swig::asval(first,pfirst) && swig::asval(second,psecond)) { return SWIG_NEWOBJ; } else { - delete *val; + SWIG_delete(*val); } } else { value_type *p; diff --git a/Lib/python/std_string.i b/Lib/python/std_string.i index 875b2e6a9..2c5fcf31b 100644 --- a/Lib/python/std_string.i +++ b/Lib/python/std_string.i @@ -8,74 +8,18 @@ %{ #include %} - -%include namespace std { class string; } + +%include +%include - -/* defining the std::string asptr/from methods */ - - -%fragment(SWIG_AsPtr_frag(std::string),"header", - fragment="SWIG_AsCharPtrAndSize") { - SWIGINTERN int - SWIG_AsPtr(std::string)(PyObject* obj, std::string **val) - { - static swig_type_info* string_info = SWIG_TypeQuery("std::string *"); - std::string *vptr; - if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) != -1) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - PyErr_Clear(); - char* buf = 0 ; size_t size = 0; - if (SWIG_AsCharPtrAndSize(obj, &buf, &size)) { - if (buf) { - if (val) *val = new std::string(buf, size - 1); - return SWIG_NEWOBJ; - } - } else { - PyErr_Clear(); - } - if (val) { - PyErr_SetString(PyExc_TypeError,"a string is expected"); - } - return 0; - } - } -} - -%fragment(SWIG_From_frag(std::string),"header", - fragment="SWIG_FromCharArray") { -SWIGINTERNINLINE PyObject* - SWIG_From(std::string)(const std::string& s) - { - return SWIG_FromCharArray(s.data(), s.size()); - } -} - -%fragment(SWIG_AsVal_frag(std::string),"header", - fragment=SWIG_AsPtr_frag(std::string)) { -SWIGINTERN int - SWIG_AsVal(std::string)(PyObject* obj, std::string *val) - { - std::string* s; - int res = SWIG_AsPtr(std::string)(obj, &s); - if ((res != 0) && s) { - if (val) *val = *s; - if (res == SWIG_NEWOBJ) delete s; - return res; - } - if (val) { - PyErr_SetString(PyExc_TypeError,"a string is expected"); - } - return 0; - } -} +%std_string_asptr(std::string, char, SWIG_AsCharPtrAndSize) +%std_string_from(std::string, SWIG_FromCharPtrAndSize) +%std_string_asval(std::string) %typemap_asptrfromn(SWIG_CCode(STRING), std::string); diff --git a/Lib/python/std_wstring.i b/Lib/python/std_wstring.i index 6ab6d06ce..1569d3b7e 100644 --- a/Lib/python/std_wstring.i +++ b/Lib/python/std_wstring.i @@ -7,78 +7,20 @@ %{ #include +#include %} -%include - namespace std { - %feature("novaluewrapper") wstring; class wstring; } - -/* defining the std::string asptr/from methods */ - -%fragment(SWIG_AsPtr_frag(std::wstring),"header", - fragment="SWIG_AsWCharPtrAndSize") { -SWIGINTERN int - SWIG_AsPtr(std::wstring)(PyObject* obj, std::wstring **val) - { - static swig_type_info* string_info = SWIG_TypeQuery("std::wstring *"); - std::wstring *vptr; - if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) != -1) { - if (val) *val = vptr; - return SWIG_OLDOBJ; - } else { - PyErr_Clear(); - wchar_t *buf = 0 ; size_t size = 0; - int res = 0; - if ((res = SWIG_AsWCharPtrAndSize(obj, &buf, &size))) { - if (buf) { - if (val) *val = new std::wstring(buf, size - 1); - if (res == SWIG_NEWOBJ) SWIG_delete_array(buf); - return SWIG_NEWOBJ; - } - } else { - PyErr_Clear(); - } - if (val) { - PyErr_SetString(PyExc_TypeError,"a wstring is expected"); - } - return 0; - } - } -} +%include +%include -%fragment(SWIG_From_frag(std::wstring),"header", - fragment="SWIG_FromWCharArray") { -SWIGINTERNINLINE PyObject* - SWIG_From(std::wstring)(const std::wstring& s) - { - return SWIG_FromWCharArray(s.data(), s.size()); - } -} - - -%fragment(SWIG_AsVal_frag(std::wstring),"header", - fragment=SWIG_AsPtr_frag(std::wstring)) { -SWIGINTERN int - SWIG_AsVal(std::wstring)(PyObject* obj, std::wstring *val) - { - std::wstring *s; - int res = SWIG_AsPtr(std::wstring)(obj, &s); - if ((res != 0) && s) { - if (val) *val = *s; - if (res == SWIG_NEWOBJ) delete s; - return res; - } - if (val) { - PyErr_SetString(PyExc_TypeError,"a wstring is expected"); - } - return 0; - } -} +%std_string_asptr(std::wstring, wchar_t, SWIG_AsWCharPtrAndSize) +%std_string_from(std::wstring, SWIG_FromWCharPtrAndSize) +%std_string_asval(std::wstring) %typemap_asptrfromn(SWIG_CCode(UNISTRING), std::wstring); diff --git a/Lib/python/typemaps.i b/Lib/python/typemaps.i index fa0f36b72..b367ddcda 100644 --- a/Lib/python/typemaps.i +++ b/Lib/python/typemaps.i @@ -1,190 +1,4 @@ -// -// SWIG Typemap library -// Dave Beazley -// May 5, 1997 -// -// Python implementation -// -// This library provides standard typemaps for modifying SWIG's behavior. -// With enough entries in this file, I hope that very few people actually -// ever need to write a typemap. -// -// Disclaimer : Unless you really understand how typemaps work, this file -// probably isn't going to make much sense. -// - -// ------------------------------------------------------------------------ -// Pointer handling -// -// These mappings provide support for input/output arguments and common -// uses for C/C++ pointers. -// ------------------------------------------------------------------------ - -// INPUT typemaps. -// These remap a C pointer to be an "INPUT" value which is passed by value -// instead of reference. - -/* -The following methods can be applied to turn a pointer into a simple -"input" value. That is, instead of passing a pointer to an object, -you would use a real value instead. - - int *INPUT - short *INPUT - long *INPUT - long long *INPUT - unsigned int *INPUT - unsigned short *INPUT - unsigned long *INPUT - unsigned long long *INPUT - unsigned char *INPUT - bool *INPUT - float *INPUT - double *INPUT - -To use these, suppose you had a C function like this : - - double fadd(double *a, double *b) { - return *a+*b; - } - -You could wrap it with SWIG as follows : - - %include - double fadd(double *INPUT, double *INPUT); - -or you can use the %apply directive : - - %include - %apply double *INPUT { double *a, double *b }; - double fadd(double *a, double *b); - -*/ - -// OUTPUT typemaps. These typemaps are used for parameters that -// are output only. The output value is appended to the result as -// a list element. - -/* -The following methods can be applied to turn a pointer into an "output" -value. When calling a function, no input value would be given for -a parameter, but an output value would be returned. In the case of -multiple output values, they are returned in the form of a Python tuple. - - int *OUTPUT - short *OUTPUT - long *OUTPUT - long long *OUTPUT - unsigned int *OUTPUT - unsigned short *OUTPUT - unsigned long *OUTPUT - unsigned long long *OUTPUT - unsigned char *OUTPUT - bool *OUTPUT - float *OUTPUT - double *OUTPUT - -For example, suppose you were trying to wrap the modf() function in the -C math library which splits x into integral and fractional parts (and -returns the integer part in one of its parameters).K: - - double modf(double x, double *ip); - -You could wrap it with SWIG as follows : - - %include - double modf(double x, double *OUTPUT); - -or you can use the %apply directive : - - %include - %apply double *OUTPUT { double *ip }; - double modf(double x, double *ip); - -The Python output of the function would be a tuple containing both -output values. - -*/ - -// INOUT -// Mappings for an argument that is both an input and output -// parameter - -/* -The following methods can be applied to make a function parameter both -an input and output value. This combines the behavior of both the -"INPUT" and "OUTPUT" methods described earlier. Output values are -returned in the form of a Python tuple. - - int *INOUT - short *INOUT - long *INOUT - long long *INOUT - unsigned int *INOUT - unsigned short *INOUT - unsigned long *INOUT - unsigned long long *INOUT - unsigned char *INOUT - bool *INOUT - float *INOUT - double *INOUT - -For example, suppose you were trying to wrap the following function : - - void neg(double *x) { - *x = -(*x); - } - -You could wrap it with SWIG as follows : - - %include - void neg(double *INOUT); - -or you can use the %apply directive : - - %include - %apply double *INOUT { double *x }; - void neg(double *x); - -Unlike C, this mapping does not directly modify the input value (since -this makes no sense in Python). Rather, the modified input value shows -up as the return value of the function. Thus, to apply this function -to a Python variable you might do this : - - x = neg(x) - -Note : previous versions of SWIG used the symbol 'BOTH' to mark -input/output arguments. This is still supported, but will be slowly -phased out in future releases. - -*/ +%include -%include -#ifdef SWIG_INOUT_NODEF -/* - Apply the INPUT/OUTPUT typemaps to all the C types (int, double, ...) if - not already defined. -*/ -%define %typemap_inout(Code,AsMeth, CheckMeth, FromMeth, AsFrag, CheckFrag, FromFrag, Type...) - _PYVAL_INPUT_TYPEMAP(SWIG_arg(Code), SWIG_arg(AsMeth), SWIG_arg(CheckMeth), - SWIG_arg(AsFrag), SWIG_arg(CheckFrag), SWIG_arg(Type)); - _PYVAL_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), SWIG_arg(Type)); - _PYVAL_INOUT_TYPEMAP(SWIG_arg(Type)); -%enddef - -%define %typemap_inoutn(Code,Type...) - %typemap_inout(SWIG_arg(Code), - SWIG_arg(SWIG_As(Type)), - SWIG_arg(SWIG_Check(Type)), - SWIG_arg(SWIG_From(Type)), - SWIG_arg(SWIG_As_frag(Type)), - SWIG_arg(SWIG_Check_frag(Type)), - SWIG_arg(SWIG_From_frag(Type)), - SWIG_arg(Type)); -%enddef - -%apply_checkctypes(%typemap_inoutn) - -#endif diff --git a/Lib/ruby/cdata.i b/Lib/ruby/cdata.i new file mode 100644 index 000000000..367965990 --- /dev/null +++ b/Lib/ruby/cdata.i @@ -0,0 +1 @@ +%include diff --git a/Lib/ruby/cstring.i b/Lib/ruby/cstring.i new file mode 100644 index 000000000..f64761c43 --- /dev/null +++ b/Lib/ruby/cstring.i @@ -0,0 +1,137 @@ +/* + * cstring.i + * $Header$ + * + * Author(s): David Beazley (beazley@cs.uchicago.edu) + * + * This file provides typemaps and macros for dealing with various forms + * of C character string handling. The primary use of this module + * is in returning character data that has been allocated or changed in + * some way. + */ + +/* + * %cstring_input_binary(TYPEMAP, SIZE) + * + * Macro makes a function accept binary string data along with + * a size. + */ + +/* + * %cstring_bounded_output(TYPEMAP, MAX) + * + * This macro is used to return a NULL-terminated output string of + * some maximum length. For example: + * + * %cstring_bounded_output(Char *outx, 512); + * void foo(Char *outx) { + * sprintf(outx,"blah blah\n"); + * } + * + */ + +/* + * %cstring_chunk_output(TYPEMAP, SIZE) + * + * This macro is used to return a chunk of binary string data. + * Embedded NULLs are okay. For example: + * + * %cstring_chunk_output(Char *outx, 512); + * void foo(Char *outx) { + * memmove(outx, somedata, 512); + * } + * + */ + +/* + * %cstring_bounded_mutable(TYPEMAP, SIZE) + * + * This macro is used to wrap a string that's going to mutate. + * + * %cstring_bounded_mutable(Char *in, 512); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +/* + * %cstring_mutable(TYPEMAP [, expansion]) + * + * This macro is used to wrap a string that will mutate in place. + * It may change size up to a user-defined expansion. + * + * %cstring_mutable(Char *in); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +/* + * %cstring_output_maxsize(TYPEMAP, SIZE) + * + * This macro returns data in a string of some user-defined size. + * + * %cstring_output_maxsize(Char *outx, int max) { + * void foo(Char *outx, int max) { + * sprintf(outx,"blah blah\n"); + * } + */ + +/* + * %cstring_output_withsize(TYPEMAP, SIZE) + * + * This macro is used to return Character data along with a size + * parameter. + * + * %cstring_output_maxsize(Char *outx, int *max) { + * void foo(Char *outx, int *max) { + * sprintf(outx,"blah blah\n"); + * *max = strlen(outx); + * } + */ + +/* + * %cstring_output_allocate(TYPEMAP, RELEASE) + * + * This macro is used to return Character data that was + * allocated with new or malloc. + * + * %cstring_output_allocated(Char **outx, free($1)); + * void foo(Char **outx) { + * *outx = (Char *) malloc(512); + * sprintf(outx,"blah blah\n"); + * } + */ + +/* + * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) + * + * This macro is used to return Character data that was + * allocated with new or malloc. + * + * %cstring_output_allocated(Char **outx, int *sz, free($1)); + * void foo(Char **outx, int *sz) { + * *outx = (Char *) malloc(512); + * sprintf(outx,"blah blah\n"); + * *sz = strlen(outx); + * } + */ + +%include +%include + +%typemap_cstrings(%cstring, + char, + SWIG_AsCharPtr, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtr, + SWIG_FromCharPtrAndSize); + diff --git a/Lib/ruby/director.swg b/Lib/ruby/director.swg index 6f1d7f1b0..2ae2c6990 100644 --- a/Lib/ruby/director.swg +++ b/Lib/ruby/director.swg @@ -20,42 +20,94 @@ namespace Swig { int argc; VALUE *argv; }; - + /* Base class for director exceptions */ class DirectorException { - protected: - VALUE swig_error; - protected: - DirectorException(VALUE error=Qnil) : swig_error(error) {} - public: - VALUE getType() const { - return CLASS_OF(swig_error); + protected: + VALUE swig_error; + std::string swig_msg; + protected: + DirectorException(VALUE error) + : swig_error(error) + { + } + + DirectorException(VALUE error, const char* hdr, const char* msg ="") + : swig_error(error), swig_msg(hdr) { + if (strlen(msg)) { + swig_msg += " "; + swig_msg += msg; } - VALUE getError() const { - return swig_error; + if (swig_msg.size()) { + VALUE str = rb_str_new2(swig_msg.c_str()); + swig_error = rb_exc_new3(error, str); + } else { + swig_error = error; } - virtual ~DirectorException() {} + } + public: + VALUE getType() const { + return CLASS_OF(swig_error); + } + VALUE getError() const { + return swig_error; + } + virtual ~DirectorException() {} }; - + /* Type mismatch in the return value from a Ruby method call */ class DirectorTypeMismatchException : public Swig::DirectorException { - public: - DirectorTypeMismatchException(const char *msg="") { - VALUE str = rb_str_new2("Swig director type mismatch: "); - rb_str_concat(str, rb_str_new2(msg)); - swig_error = rb_exc_new3(rb_eTypeError, str); - } + public: + DirectorTypeMismatchException(VALUE error, const char *msg="") + : Swig::DirectorException(error, "Swig director type mismatch", msg) + { + } + + DirectorTypeMismatchException(const char *msg="") + : Swig::DirectorException(rb_eTypeError, "Swig director type mismatch", msg) + { + } + + static void raise(VALUE error, const char *msg) { + throw DirectorTypeMismatchException(error, msg); + } + + static void raise(const char *msg) { + throw DirectorTypeMismatchException(msg); + } }; /* Any Ruby exception that occurs during a director method call */ class DirectorMethodException : public Swig::DirectorException { - public: - DirectorMethodException(VALUE error) : Swig::DirectorException(error) {} + public: + DirectorMethodException(VALUE error) + : Swig::DirectorException(error) { + } + + DirectorMethodException(const char* msg = "") + : Swig::DirectorException(rb_eRuntimeError, "Swig director method error", msg) { + } + + static void raise(VALUE error) + { + throw DirectorMethodException(error); + } }; /* Attempted to call a pure virtual method via a director method */ - class DirectorPureVirtualException : public Swig::DirectorException {}; + class DirectorPureVirtualException : public Swig::DirectorException + { + public: + DirectorPureVirtualException(const char* msg = "") + : DirectorException(rb_eRuntimeError, "Swig director pure virtal method called", msg) + { + } + static void raise(const char *msg) + { + throw DirectorPureVirtualException(msg); + } + }; /* Simple thread abstraction for pthreads on win32 */ #ifdef __THREAD__ diff --git a/Lib/ruby/fragments.i b/Lib/ruby/fragments.i deleted file mode 100644 index 8d4629005..000000000 --- a/Lib/ruby/fragments.i +++ /dev/null @@ -1,17 +0,0 @@ -// Helper function for Array output - -%fragment("output_helper", "header") %{ -static VALUE output_helper(VALUE target, VALUE o) { - if (NIL_P(target)) { - target = o; - } else { - if (TYPE(target) != T_ARRAY) { - VALUE o2 = target; - target = rb_ary_new(); - rb_ary_push(target, o2); - } - rb_ary_push(target, o); - } - return target; -} -%} diff --git a/Lib/ruby/ruby.swg b/Lib/ruby/ruby.swg index 3351dd0bf..911962a24 100644 --- a/Lib/ruby/ruby.swg +++ b/Lib/ruby/ruby.swg @@ -1,47 +1,53 @@ -/* ---------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- * ruby.swg * - * Ruby configuation file. - * ---------------------------------------------------------------------- */ - -%runtime "rubyhead.swg" -%runtime "swigrun.swg" /* Common C API type-checking code */ -%runtime "rubytracking.swg" /* API for tracking C++ classes to Ruby objects */ -%runtime "rubydef.swg" - -%insert(initbeforefunc) "swiginit.swg" - -#define %alias %feature("alias") -#define %freefunc %feature("freefunc") -#define %markfunc %feature("markfunc") -#define %mixin %feature("mixin") -#define %predicate %feature("predicate", "1") -#define %trackobjects %feature("trackobjects") - -/* ----------------------------------------------------------------------------- - * SWIGTYPE typemaps + * Ruby configuration module. * ----------------------------------------------------------------------------- */ -%include "rubyswigtype.swg" +/* ----------------------------------------------------------------------------- + * The runtime part + * ----------------------------------------------------------------------------- */ +%include + +/* ----------------------------------------------------------------------------- + * Special user directives + * ----------------------------------------------------------------------------- */ +%include + +/* ----------------------------------------------------------------------------- + * Inner macros + * ----------------------------------------------------------------------------- */ +%include + +/* ----------------------------------------------------------------------------- + * Error manipulation + * ----------------------------------------------------------------------------- */ + +%include + +/* ----------------------------------------------------------------------------- + * Look for user fragments file. If not found, include empty system one. + * ----------------------------------------------------------------------------- */ +%include "rubyfragments.swg" /* ----------------------------------------------------------------------------- * Typemap specializations * ----------------------------------------------------------------------------- */ - -%include "rubyvoid.swg" -%include "rubyobject.swg" -%include "rubystrings.swg" -%include "rubyprimtypes.swg" -%include "rubymisctypes.swg" -%include "rubyenum.swg" +%include /* ------------------------------------------------------------ * Overloaded operator support * ------------------------------------------------------------ */ -%include "rubyopers.swg" +%include /* ------------------------------------------------------------ - * Warnings for Ruby keywords + * Warnings for Python keywords * ------------------------------------------------------------ */ -%include "rubykw.swg" +%include + +/* ------------------------------------------------------------ + * The Python initialization function + * ------------------------------------------------------------ */ +%include + diff --git a/Lib/ruby/rubyapi.swg b/Lib/ruby/rubyapi.swg new file mode 100644 index 000000000..0563fd130 --- /dev/null +++ b/Lib/ruby/rubyapi.swg @@ -0,0 +1,27 @@ +/* ----------------------------------------------------------------------------- + * Ruby API portion that goes into the runtime + * ----------------------------------------------------------------------------- */ + +#ifdef __cplusplus +extern "C" { +#endif + +SWIGINTERN VALUE +SWIG_Ruby_AppendResult(VALUE target, VALUE o) { + if (NIL_P(target)) { + target = o; + } else { + if (TYPE(target) != T_ARRAY) { + VALUE o2 = target; + target = rb_ary_new(); + rb_ary_push(target, o2); + } + rb_ary_push(target, o); + } + return target; +} + +#ifdef __cplusplus +} +#endif + diff --git a/Lib/ruby/rubydef.swg b/Lib/ruby/rubydef.swg deleted file mode 100644 index e00f9247d..000000000 --- a/Lib/ruby/rubydef.swg +++ /dev/null @@ -1,267 +0,0 @@ -/* Common SWIG API */ -#define SWIG_ConvertPtr(obj, pp, type, flags) \ - SWIG_Ruby_ConvertPtr(obj, pp, type, flags) -#define SWIG_NewPointerObj(p, type, flags) \ - SWIG_Ruby_NewPointerObj(p, type, flags) -#define SWIG_MustGetPtr(p, type, argnum, flags) \ - SWIG_Ruby_MustGetPtr(p, type, argnum, flags) -#define SWIG_GetModule(clientdata) \ - SWIG_Ruby_GetModule() -#define SWIG_SetModule(clientdata, pointer) \ - SWIG_Ruby_SetModule(pointer) - -/* Ruby-specific SWIG API */ - -#define SWIG_InitRuntime() \ - SWIG_Ruby_InitRuntime() -#define SWIG_define_class(ty) \ - SWIG_Ruby_define_class(ty) -#define SWIG_NewClassInstance(value, ty) \ - SWIG_Ruby_NewClassInstance(value, ty) -#define SWIG_MangleStr(value) \ - SWIG_Ruby_MangleStr(value) -#define SWIG_CheckConvert(value, ty) \ - SWIG_Ruby_CheckConvert(value, ty) -#define SWIG_NewPackedObj(ptr, sz, ty) \ - SWIG_Ruby_NewPackedObj(ptr, sz, ty) -#define SWIG_ConvertPacked(obj, ptr, sz, ty, flags) \ - SWIG_Ruby_ConvertPacked(obj, ptr, sz, ty, flags) - -/* rubydef.swg */ -#ifdef __cplusplus -extern "C" { -#endif - -static VALUE _mSWIG = Qnil; -static VALUE _cSWIG_Pointer = Qnil; -static VALUE swig_runtime_data_type_pointer = Qnil; - -/* Initialize Ruby runtime support */ -static void -SWIG_Ruby_InitRuntime(void) -{ - if (_mSWIG == Qnil) { - _mSWIG = rb_define_module("SWIG"); - } -} - -/* Define Ruby class for C type */ -static void -SWIG_Ruby_define_class(swig_type_info *type) -{ - VALUE klass; - char *klass_name = (char *) malloc(4 + strlen(type->name) + 1); - sprintf(klass_name, "TYPE%s", type->name); - if (NIL_P(_cSWIG_Pointer)) { - _cSWIG_Pointer = rb_define_class_under(_mSWIG, "Pointer", rb_cObject); - rb_undef_method(CLASS_OF(_cSWIG_Pointer), "new"); - } - klass = rb_define_class_under(_mSWIG, klass_name, _cSWIG_Pointer); - free((void *) klass_name); -} - -/* Create a new pointer object */ -static VALUE -SWIG_Ruby_NewPointerObj(void *ptr, swig_type_info *type, int flags) -{ - int own = flags & SWIG_POINTER_OWN; - int track = flags & SWIG_TRACK_OBJECTS; - - char *klass_name; - swig_class *sklass; - VALUE klass; - VALUE obj; - - if (!ptr) - return Qnil; - - /* Have we already wrapped this pointer? */ - if (track) { - obj = SWIG_RubyInstanceFor(ptr); - if (obj != Qnil) { - return obj; - } - } - - if (type->clientdata) { - sklass = (swig_class *) type->clientdata; - obj = Data_Wrap_Struct(sklass->klass, VOIDFUNC(sklass->mark), (own ? VOIDFUNC(sklass->destroy) : 0), ptr); - } else { - klass_name = (char *) malloc(4 + strlen(type->name) + 1); - sprintf(klass_name, "TYPE%s", type->name); - klass = rb_const_get(_mSWIG, rb_intern(klass_name)); - free((void *) klass_name); - obj = Data_Wrap_Struct(klass, 0, 0, ptr); - } - rb_iv_set(obj, "__swigtype__", rb_str_new2(type->name)); - - /* Keep track of this object if necessary */ - if (track) { - SWIG_RubyAddTracking(ptr, obj); - } - - return obj; -} - -/* Create a new class instance (always owned) */ -static VALUE -SWIG_Ruby_NewClassInstance(VALUE klass, swig_type_info *type) -{ - VALUE obj; - swig_class *sklass = (swig_class *) type->clientdata; - obj = Data_Wrap_Struct(klass, VOIDFUNC(sklass->mark), VOIDFUNC(sklass->destroy), 0); - rb_iv_set(obj, "__swigtype__", rb_str_new2(type->name)); - return obj; -} - -/* Get type mangle from class name */ -static SWIGINLINE char * -SWIG_Ruby_MangleStr(VALUE obj) -{ - VALUE stype = rb_iv_get(obj, "__swigtype__"); - return StringValuePtr(stype); -} - -/* Convert a pointer value */ -static int -SWIG_Ruby_ConvertPtr(VALUE obj, void **ptr, swig_type_info *ty, int flags) -{ - char *c; - swig_cast_info *tc; - - /* Grab the pointer */ - if (NIL_P(obj)) { - *ptr = 0; - return 0; - } else { - Data_Get_Struct(obj, void, *ptr); - } - - /* Check to see if the input object is giving up ownership - of the underlying C struct or C++ object. If so then we - need to reset the destructor since the Ruby object no - longer owns the underlying C++ object.*/ - if (flags & SWIG_POINTER_DISOWN) { - if (flags & SWIG_TRACK_OBJECTS) { - /* We are tracking objects. Thus we change the destructor - * to SWIG_RubyRemoveTracking. This allows us to - * remove the mapping from the C++ to Ruby object - * when the Ruby object is garbage collected. If we don't - * do this, then it is possible we will return a reference - * to a Ruby object that no longer exists thereby crashing Ruby. */ - RDATA(obj)->dfree = SWIG_RubyRemoveTracking; - } else { - RDATA(obj)->dfree = 0; - } - } - - /* Do type-checking if type info was provided */ - if (ty) { - if (ty->clientdata) { - if (rb_obj_is_kind_of(obj, ((swig_class *) (ty->clientdata))->klass)) { - if (*ptr == 0) - rb_raise(rb_eRuntimeError, "This %s already released", ty->str); - return 0; - } - } - if ((c = SWIG_MangleStr(obj)) == NULL) { - if (flags & SWIG_POINTER_EXCEPTION) - rb_raise(rb_eTypeError, "Expected %s", ty->str); - else - return -1; - } - tc = SWIG_TypeCheck(c, ty); - if (!tc) { - if (flags & SWIG_POINTER_EXCEPTION) - rb_raise(rb_eTypeError, "Expected %s", ty->str); - else - return -1; - } - *ptr = SWIG_TypeCast(tc, *ptr); - } - return 0; -} - -/* Convert a pointer value, signal an exception on a type mismatch */ -static SWIGINLINE void * -SWIG_Ruby_MustGetPtr(VALUE obj, swig_type_info *ty, int argnum, int flags) -{ - void *result; - SWIG_ConvertPtr(obj, &result, ty, flags | SWIG_POINTER_EXCEPTION); - return result; -} - -/* Check convert */ -static SWIGINLINE int -SWIG_Ruby_CheckConvert(VALUE obj, swig_type_info *ty) -{ - char *c = SWIG_MangleStr(obj); - if (!c) - return 0; - return SWIG_TypeCheck(c,ty) != 0; -} - -static VALUE -SWIG_Ruby_NewPackedObj(void *ptr, int sz, swig_type_info *type) { - char result[1024]; - char *r = result; - if ((2*sz + 1 + strlen(type->name)) > 1000) return 0; - *(r++) = '_'; - r = SWIG_PackData(r, ptr, sz); - strcpy(r, type->name); - return rb_str_new2(result); -} - -/* Convert a packed value value */ -static void -SWIG_Ruby_ConvertPacked(VALUE obj, void *ptr, int sz, swig_type_info *ty, int flags) { - swig_cast_info *tc; - const char *c; - - if (TYPE(obj) != T_STRING) goto type_error; - c = StringValuePtr(obj); - /* Pointer values must start with leading underscore */ - if (*c != '_') goto type_error; - c++; - c = SWIG_UnpackData(c, ptr, sz); - if (ty) { - tc = SWIG_TypeCheck(c, ty); - if (!tc) goto type_error; - } - return; - -type_error: - - if (flags) { - if (ty) { - rb_raise(rb_eTypeError, "Type error. Expected %s", ty->name); - } else { - rb_raise(rb_eTypeError, "Expected a pointer"); - } - } -} - -static swig_module_info *SWIG_Ruby_GetModule() { - VALUE pointer; - swig_module_info *ret = 0; - - /* first check if pointer already created */ - pointer = rb_gv_get("$swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME); - if (pointer != Qnil) { - Data_Get_Struct(pointer, swig_module_info, ret); - } - return ret; -} - -static void SWIG_Ruby_SetModule(swig_module_info *pointer) { - /* register a new class */ - VALUE cl = rb_define_class("swig_runtime_data", rb_cObject); - /* create and store the structure pointer to a global variable */ - swig_runtime_data_type_pointer = Data_Wrap_Struct(cl, 0, 0, pointer); - rb_define_readonly_variable("$swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, &swig_runtime_data_type_pointer); -} - -#ifdef __cplusplus -} -#endif - diff --git a/Lib/ruby/rubyenum.swg b/Lib/ruby/rubyenum.swg deleted file mode 100644 index e817654b4..000000000 --- a/Lib/ruby/rubyenum.swg +++ /dev/null @@ -1,64 +0,0 @@ -/* ------------------------------------------------------------ - * Enums - * ------------------------------------------------------------ */ - -/* --- Input typemaps --- */ -%typemap(in) enum SWIGTYPE "$1 = ($1_ltype) NUM2INT($input);"; - -%typemap(in) const enum SWIGTYPE & ($*1_ltype temp) - "temp = ($*1_ltype) NUM2INT($input); - $1 = &temp;"; - - -%typemap(directorout) enum SWIGTYPE "$result = ($1_ltype) NUM2INT($input);"; -%typemap(directorout,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const enum SWIGTYPE & -%{ static $*1_ltype temp = ($*1_ltype) NUM2INT($input); - $result = &temp; %} - -/* --- Output typemaps --- */ -%typemap(out) enum SWIGTYPE "$result = INT2NUM($1);"; - -%typemap(out) const enum SWIGTYPE & "$result = INT2NUM((long) *($1));"; - -%typemap(directorin) enum SWIGTYPE "$input = INT2NUM($1);"; -%typemap(directorin) const enum SWIGTYPE& "$input = INT2NUM($1);"; - -/* --- Variable Input --- */ - -%{ -static void SWIG_AsVal(VALUE obj, int *val) -{ - *val = (int) NUM2INT(obj); -} -%} - -%typemap(varin) enum SWIGTYPE { - if (sizeof(int) != sizeof($1)) { - rb_raise(rb_eTypeError, "enum variable '$name' can not be set."); - } - SWIG_AsVal($input, (int *)(void *) &$1); -} - -/* --- Variable Output --- */ - -%typemap(varout) enum SWIGTYPE "$result = INT2NUM($1);"; - -/* --- Constants --- */ -%typemap(constant) enum SWIGTYPE "rb_define_const($module,\"$symname\", INT2NUM($1));"; - -/* ------------------------------------------------------------ - * Typechecking rules - * ------------------------------------------------------------ */ - -%typecheck(SWIG_TYPECHECK_INTEGER) enum SWIGTYPE, const enum SWIGTYPE & -{ - $1 = ((TYPE($input) == T_FIXNUM) || (TYPE($input) == T_BIGNUM)) ? 1 : 0; -} - -/* ------------------------------------------------------------ - * Exception handling - * ------------------------------------------------------------ */ - -%typemap(throws) enum SWIGTYPE - "(void)$1; rb_exc_raise(rb_exc_new3(rb_eRuntimeError, rb_obj_as_string(INT2NUM($1))));" - diff --git a/Lib/ruby/rubyerrors.swg b/Lib/ruby/rubyerrors.swg new file mode 100644 index 000000000..ac76a289d --- /dev/null +++ b/Lib/ruby/rubyerrors.swg @@ -0,0 +1,61 @@ +/* ----------------------------------------------------------------------------- + * error manipulation + * ----------------------------------------------------------------------------- */ + + +%insert("header") %{ + +SWIGINTERN VALUE +SWIG_Ruby_ErrorType(int SWIG_code) { + switch (SWIG_code) { + case SWIG_MemoryError: + return rb_eNoMemError; + break; + case SWIG_IOError: + return rb_eIOError; + break; + case SWIG_RuntimeError: + return rb_eRuntimeError; + break; + case SWIG_IndexError: + return rb_eIndexError; + break; + case SWIG_TypeError: + return rb_eTypeError; + break; + case SWIG_DivisionByZero: + return rb_eZeroDivError; + break; + case SWIG_OverflowError: + return rb_eRangeError; + break; + case SWIG_SyntaxError: + return rb_eSyntaxError; + break; + case SWIG_ValueError: + return rb_eArgError; + break; + case SWIG_SystemError: + return rb_eFatal; + break; + case SWIG_AttributeError: + return rb_eRuntimeError; + break; + case SWIG_UnknownError: + return rb_eRuntimeError; + break; + default: + return rb_eRuntimeError; + break; + } +} + + +SWIGINTERN void +SWIG_Ruby_SetErrorMsg(VALUE type, const char *msg) { + rb_raise(type, msg); +} + +%} + + diff --git a/Lib/ruby/rubyfragments.swg b/Lib/ruby/rubyfragments.swg new file mode 100644 index 000000000..8b63ee3a2 --- /dev/null +++ b/Lib/ruby/rubyfragments.swg @@ -0,0 +1,23 @@ +/* + + Create a file with this name, 'fragments.i', in your working + directory and add all the %fragments you want to take precedence + over the ones defined by default by swig. + + For example, if you add: + + %fragment(SWIG_AsVal_frag(int),"header") { + SWIGINTERNINLINE int + SWIG_AsVal(int)(PyObject *obj, int *val) + { + ; + } + } + + this will replace the code used to retreive an integer value for all + the typemaps that need it, including: + + int, std::vector, std::list >, etc. + + +*/ diff --git a/Lib/ruby/rubyinit.swg b/Lib/ruby/rubyinit.swg new file mode 100644 index 000000000..fc6e039b1 --- /dev/null +++ b/Lib/ruby/rubyinit.swg @@ -0,0 +1 @@ +%insert(initbeforefunc) "swiginit.swg" diff --git a/Lib/ruby/rubymacros.swg b/Lib/ruby/rubymacros.swg new file mode 100644 index 000000000..4fd0d1f90 --- /dev/null +++ b/Lib/ruby/rubymacros.swg @@ -0,0 +1 @@ +%include diff --git a/Lib/ruby/rubymisctypes.swg b/Lib/ruby/rubymisctypes.swg deleted file mode 100644 index 2dd9d1e8b..000000000 --- a/Lib/ruby/rubymisctypes.swg +++ /dev/null @@ -1,6 +0,0 @@ -/* ------------------------------------------------------------ - * --- ANSI/Posix C/C++ types --- - * ------------------------------------------------------------ */ - -%apply unsigned long { size_t }; - diff --git a/Lib/ruby/rubyobject.swg b/Lib/ruby/rubyobject.swg deleted file mode 100644 index cfed7028f..000000000 --- a/Lib/ruby/rubyobject.swg +++ /dev/null @@ -1,13 +0,0 @@ -/* ------------------------------------------------------------ - * VALUE - Just pass straight through unmodified - * ------------------------------------------------------------ */ - -typedef unsigned long VALUE; - -%typemap(in) VALUE "$1 = $input;"; -%typemap(out) VALUE "$result = $1;"; - -%typemap(directorin) VALUE "$input = $1;"; -%typemap(directorout) VALUE "$result = $input;"; - -%typecheck(SWIG_TYPECHECK_POINTER) VALUE "$1 = ($input != T_NONE);"; diff --git a/Lib/ruby/rubyprimtypes.swg b/Lib/ruby/rubyprimtypes.swg index 8c5e9e3bf..41e0eb9b2 100644 --- a/Lib/ruby/rubyprimtypes.swg +++ b/Lib/ruby/rubyprimtypes.swg @@ -1,257 +1,317 @@ +%include + +/* Macro for 'signed long' derived types */ + +%define %type_slong(Type, Frag, Min, Max) +%derived_type_from(long, Type) +%signed_derived_type_asval(long, Type, Frag, Min, Max) +%enddef + +/* Macro for 'unsigned long' derived types */ + +%define %type_ulong(Type, Frag, Max) +%derived_type_from(unsigned long, Type) +%unsigned_derived_type_asval(unsigned long, Type, Frag, Max) +%enddef + /* ------------------------------------------------------------ * Primitive Types * ------------------------------------------------------------ */ -/* --- Input Values --- */ - -%typemap(in) int "$1 = NUM2INT($input);"; -%typemap(in) unsigned int "$1 = NUM2UINT($input);"; -%typemap(in) short "$1 = NUM2SHRT($input);"; -%typemap(in) unsigned short "$1 = NUM2USHRT($input);"; -%typemap(in) long "$1 = NUM2LONG($input);"; -%typemap(in) unsigned long "$1 = NUM2ULONG($input);"; -%typemap(in) signed char "$1 = ($1_ltype) NUM2INT($input);"; -%typemap(in) unsigned char "$1 = ($1_ltype) NUM2INT($input);"; -%typemap(in) char "$1 = NUM2CHR($input);"; -%typemap(in) float, double "$1 = ($1_ltype) NUM2DBL($input);"; -%typemap(in) bool "$1 = RTEST($input);"; - -/* Long long */ - -%typemap(in) long long "$1 = ($1_ltype) NUM2LL($input);"; -%typemap(in) unsigned long long "$1 = ($1_ltype) NUM2ULL($input);"; - -/* Const primitive references. Passed by value */ - -%typemap(in) const int & (int temp), - const signed char & (signed char temp), - const unsigned char & (unsigned char temp) - "temp = ($*1_ltype) NUM2INT($input); - $1 = &temp;"; - -%typemap(in) const short & (short temp) - "temp = ($*1_ltype) NUM2SHRT($input); - $1 = &temp;"; - -%typemap(in) const long & (long temp) - "temp = ($*1_ltype) NUM2LONG($input); - $1 = &temp;"; - -%typemap(in) const unsigned int & (unsigned int temp) - "temp = ($*1_ltype) NUM2UINT($input); - $1 = &temp;"; - -%typemap(in) const unsigned short & (unsigned short temp) - "temp = ($*1_ltype) NUM2USHRT($input); - $1 = &temp;"; - -%typemap(in) const unsigned long & (unsigned long temp) - "temp = ($*1_ltype) NUM2ULONG($input); - $1 = &temp;"; - -%typemap(in) const bool & (bool temp) - "temp = ($*1_ltype) RTEST($input); - $1 = &temp;"; - -%typemap(in) const float & (float temp), - const double & (double temp) - "temp = ($*1_ltype) NUM2DBL($input); - $1 = &temp;"; - -%typemap(in) const long long & ($*1_ltype temp) - "temp = ($*1_ltype) NUM2LL($input); - $1 = &temp;"; - -%typemap(in) const unsigned long long & ($*1_ltype temp) - "temp = ($*1_ltype) NUM2ULL($input); - $1 = &temp;"; - -%typemap(in) const char &(char temp) { - char *stemp = StringValuePtr($input); - temp = *stemp; - $1 = &temp; -} - -/* --- Output typemaps --- */ - -%typemap(out) int, short, long, signed char - "$result = INT2NUM($1);"; - -%typemap(out) unsigned int, unsigned short, unsigned long, unsigned char - "$result = UINT2NUM($1);"; - -/* Long long */ - -%typemap(out) long long "$result = LL2NUM($1);"; -%typemap(out) unsigned long long "$result = ULL2NUM($1);"; - -/* Floating point output values */ -%typemap(out) double, float - "$result = rb_float_new($1);"; - -/* Boolean */ -%typemap(out) bool - "$result = $1 ? Qtrue : Qfalse;"; - -/* References to primitive types. Return by value */ - -%typemap(out) const int &, - const short &, - const long &, - const signed char & - "$result = INT2NUM((long) *($1));"; - -%typemap(out) const unsigned int &, - const unsigned short &, - const unsigned long &, - const unsigned char & - "$result = UINT2NUM((unsigned long) *($1));"; - -%typemap(out) const bool & - "$result = *($1) ? Qtrue : Qfalse;"; - -%typemap(out) const float &, const double & - "$result = rb_float_new((double) *($1));"; - -%typemap(out) const long long & - "$result = LL2NUM(*($1));"; - -%typemap(out) const unsigned long long & - "$result = ULL2NUM(*($1));"; - -/* --- Variable Input --- */ - -%typemap(varin) int "$1 = NUM2INT($input);"; -%typemap(varin) unsigned int "$1 = NUM2UINT($input);"; -%typemap(varin) short "$1 = NUM2SHRT($input);"; -%typemap(varin) unsigned short "$1 = NUM2USHRT($input);"; -%typemap(varin) long "$1 = NUM2LONG($input);"; -%typemap(varin) unsigned long "$1 = NUM2ULONG($input);"; -%typemap(varin) signed char "$1 = (signed char) NUM2INT($input);"; -%typemap(varin) unsigned char "$1 = (unsigned char) NUM2INT($input);"; -%typemap(varin) char "$1 = NUM2CHR($input);"; -%typemap(varin) float, double "$1 = ($1_ltype) NUM2DBL($input);"; -%typemap(varin) bool "$1 = RTEST($input);"; - -%typemap(varin) long long "$1 = NUM2LL($input);"; -%typemap(varin) unsigned long long "$1 = NUM2ULL($input);"; - -/* --- Variable Output --- */ - -%typemap(varout) int, short, long, signed char - "$result = INT2NUM($1);"; - -%typemap(varout) unsigned int, unsigned short, unsigned long, unsigned char - "$result = UINT2NUM($1);"; - -%typemap(varout) long long "$result = LL2NUM($1);"; -%typemap(varout) unsigned long long "$result = ULL2NUM($1);"; - -/* Floats and doubles */ -%typemap(varout) double, float - "$result = rb_float_new($1);"; - -/* Boolean */ -%typemap(varout) bool - "$result = $1 ? Qtrue : Qfalse;"; - -/* --- Constants --- */ - -%typemap(constant) int, short, long, signed char - "rb_define_const($module,\"$symname\", INT2NUM($1));"; - -%typemap(constant) unsigned int, unsigned short, unsigned long, unsigned char - "rb_define_const($module,\"$symname\", UINT2NUM($1));"; - -%typemap(constant) long long - "rb_define_const($module,\"$symname\", LL2NUM($1));"; - -%typemap(constant) unsigned long long - "rb_define_const($module,\"$symname\", ULL2NUM($1));"; - -%typemap(constant) double, float - "rb_define_const($module,\"$symname\", rb_float_new($1));"; - -%typemap(constant) bool - "rb_define_const($module,\"$symname\", ($1 ? Qtrue : Qfalse));"; - -/* directorin typemaps */ - -%typemap(directorin) int , const int& "$input = INT2NUM($1);"; -%typemap(directorin) short , const short& "$input = INT2NUM($1);"; -%typemap(directorin) long , const long& "$input = LONG2NUM($1);"; -%typemap(directorin) signed char , const signed char& "$input = INT2NUM($1);"; -%typemap(directorin) float , const float& "$input = rb_float_new($1);"; -%typemap(directorin) double , const double& "$input = rb_float_new($1);"; -%typemap(directorin) bool , const bool& "$input = $1 ? Qtrue : Qfalse;"; -%typemap(directorin) unsigned int , const unsigned int& "$input = UINT2NUM($1);"; -%typemap(directorin) unsigned short, const unsigned short& "$input = UINT2NUM($1);"; -%typemap(directorin) unsigned long , const unsigned long& "$input = ULONG2NUM($1);"; -%typemap(directorin) unsigned char , const unsigned char& "$input = UINT2NUM($1);"; - -/* --- directorout typemaps --- */ - -%define DIRECTOROUT_TYPEMAP(type, converter) -%typemap(directorargout) type *DIRECTOROUT "*$result = (type) converter($input);"; -%typemap(directorout) type "$result = (type) converter($input);"; -%typemap(directorout) const type& { - $basetype temp = converter($input); - $result = &temp; -} -%typemap(directorout) type &DIRECTOROUT = type -%enddef - -DIRECTOROUT_TYPEMAP(char, NUM2INT); -DIRECTOROUT_TYPEMAP(unsigned char, NUM2UINT); -DIRECTOROUT_TYPEMAP(short, NUM2INT); -DIRECTOROUT_TYPEMAP(unsigned short, NUM2INT); -DIRECTOROUT_TYPEMAP(int, NUM2INT); -DIRECTOROUT_TYPEMAP(unsigned int, NUM2INT); -DIRECTOROUT_TYPEMAP(long, NUM2INT); -DIRECTOROUT_TYPEMAP(unsigned long, NUM2INT); -DIRECTOROUT_TYPEMAP(long long, NUM2INT); -DIRECTOROUT_TYPEMAP(unsigned long long, NUM2INT); -DIRECTOROUT_TYPEMAP(float, NUM2DBL); -DIRECTOROUT_TYPEMAP(double, NUM2DBL); -DIRECTOROUT_TYPEMAP(bool, RTEST); - -/* ------------------------------------------------------------ - * Typechecking rules - * ------------------------------------------------------------ */ - -%typecheck(SWIG_TYPECHECK_BOOL) bool { - $1 = ($input == Qtrue || $input == Qfalse) ? 1 : 0; -} - -%typecheck(SWIG_TYPECHECK_INTEGER) - int, short, long, - unsigned int, unsigned short, unsigned long, - signed char, unsigned char, - long long, unsigned long long, - const int &, const short &, const long &, - const unsigned int &, const unsigned short &, const unsigned long &, - const long long &, const unsigned long long & +%fragment("SWIG_ruby_failed","header") { - $1 = ((TYPE($input) == T_FIXNUM) || (TYPE($input) == T_BIGNUM)) ? 1 : 0; +SWIGINTERN VALUE +SWIG_ruby_failed() +{ + return Qnil; +} + } -%typecheck(SWIG_TYPECHECK_DOUBLE) - float, double, - const float &, const double & + +/* boolean */ + +%fragment(SWIG_From_frag(bool),"header") { +SWIGINTERNINLINE VALUE + SWIG_From_dec(bool)(bool value) { - $1 = ((TYPE($input) == T_FLOAT) || (TYPE($input) == T_FIXNUM) || (TYPE($input) == T_BIGNUM)) ? 1 : 0; + return value ? Qtrue : Qfalse; +} +} + +%fragment(SWIG_AsVal_frag(bool),"header", + fragment=SWIG_AsVal_frag(int)) { +SWIGINTERN int +SWIG_AsVal_dec(bool)(VALUE obj, bool *val) +{ + if (obj == Qtrue) { + if (val) *val = true; + return SWIG_OK; + } else if (obj == Qfalse) { + if (val) *val = false; + return SWIG_OK; + } else { + int res = 0; + if (SWIG_AsVal(int)(obj, &res) == SWIG_OK) { + if (val) *val = res ? true : false; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +/* signed/unsigned char */ + +%type_slong(signed char, "", SCHAR_MIN, SCHAR_MAX) +%type_ulong(unsigned char, "", UCHAR_MAX) + +/* short/unsigned short */ + +%type_slong(short, "", SHRT_MIN, SHRT_MAX) +%type_ulong(unsigned short, "", USHRT_MAX) + +/* int/unsigned int */ + +%type_slong(int, "", INT_MIN, INT_MAX) +%type_ulong(unsigned int, "", UINT_MAX) + +/* signed/unsigned wchar_t */ + +#ifdef __cplusplus +%type_slong(signed wchar_t, "", WCHAR_MIN, WCHAR_MAX) +%type_ulong(unsigned wchar_t, "", UWCHAR_MAX) +#endif + +/* long */ + +%fragment(SWIG_From_frag(long),"header", + fragment="") { + SWIG_define(SWIG_From_dec(long), LONG2NUM) +} + +%fragment(SWIG_AsVal_frag(long),"header",fragment="SWIG_ruby_failed") { +SWIGINTERN VALUE SWIG_num2long(VALUE *args) +{ + *((long *)(args[1])) = NUM2LONG(args[0]); + return args[0]; +} +SWIGINTERN int +SWIG_AsVal_dec(long)(VALUE obj, long* val) +{ + if (obj != Qnil && ((TYPE(obj) == T_FIXNUM) || (TYPE(obj) == T_BIGNUM))) { + long v; + VALUE a[2] = { obj, (VALUE)(&v) }; + if (rb_rescue(RUBY_METHOD_FUNC(SWIG_num2long), (VALUE)a, RUBY_METHOD_FUNC(SWIG_ruby_failed), 0) != Qnil) { + if (val) *val = v; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +/* unsigned long */ + +%fragment(SWIG_From_frag(unsigned long),"header", + fragment=SWIG_From_frag(long)) { +SWIGINTERNINLINE VALUE +SWIG_From_dec(unsigned long)(unsigned long value) +{ + return ULONG2NUM(value); +} +} + +%fragment(SWIG_AsVal_frag(unsigned long),"header",fragment="SWIG_ruby_failed") { +SWIGINTERN VALUE SWIG_num2ulong(VALUE *args) +{ + *((unsigned long *)(args[1])) = NUM2ULONG(args[0]); + return args[0]; +} +SWIGINTERN int +SWIG_AsVal_dec(unsigned long)(VALUE obj, unsigned long *val) +{ + if (obj != Qnil && ((TYPE(obj) == T_FIXNUM) || (TYPE(obj) == T_BIGNUM))) { + unsigned long v; + VALUE a[2] = { obj, (VALUE)(&v) }; + if (rb_rescue(RUBY_METHOD_FUNC(SWIG_num2ulong), (VALUE)a, RUBY_METHOD_FUNC(SWIG_ruby_failed), 0) != Qnil) { + if (val) *val = v; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +/* long long */ + +%fragment(SWIG_From_frag(long long),"header", + fragment=SWIG_From_frag(long), + fragment="") { +SWIGINTERNINLINE VALUE +SWIG_From_dec(long long)(long long value) +{ + return LL2NUM(value); +} +} + +%fragment(SWIG_AsVal_frag(long long),"header",fragment="SWIG_ruby_failed") { +SWIGINTERN VALUE SWIG_num2longlong(VALUE *args) +{ + *((long long *)(args[1])) = NUM2LL(args[0]); + return args[0]; +} +SWIGINTERN int +SWIG_AsVal_dec(long long)(VALUE obj, long long *val) +{ + if (obj != Qnil && ((TYPE(obj) == T_FIXNUM) || (TYPE(obj) == T_BIGNUM))) { + long long v; + VALUE a[2] = { obj, (VALUE)(&v) }; + if (rb_rescue(RUBY_METHOD_FUNC(SWIG_num2longlong), (VALUE)a, RUBY_METHOD_FUNC(SWIG_ruby_failed), 0) != Qnil) { + if (val) *val = v; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +/* unsigned long long */ + +%fragment(SWIG_From_frag(unsigned long long),"header", + fragment=SWIG_From_frag(long long), + fragment="") { +SWIGINTERNINLINE VALUE +SWIG_From_dec(unsigned long long)(unsigned long long value) +{ + return ULL2NUM(value); +} +} + +%fragment(SWIG_AsVal_frag(unsigned long long),"header",fragment="SWIG_ruby_failed") { +SWIGINTERN VALUE SWIG_num2ulonglong(VALUE *args) +{ + *((unsigned long long *)(args[1])) = NUM2ULL(args[0]); + return args[0]; +} +SWIGINTERN int +SWIG_AsVal_dec(unsigned long long)(VALUE obj, unsigned long long *val) +{ + if (obj != Qnil && ((TYPE(obj) == T_FIXNUM) || (TYPE(obj) == T_BIGNUM))) { + unsigned long long v; + VALUE a[2] = { obj, (VALUE)(&v) }; + if (rb_rescue(RUBY_METHOD_FUNC(SWIG_num2ulonglong), (VALUE)a, RUBY_METHOD_FUNC(SWIG_ruby_failed), 0) != Qnil) { + if (val) *val = v; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +/* float */ + +%derived_type_from(double, float) +%signed_derived_type_asval(double, float, "", -FLT_MAX, FLT_MAX) + +/* double */ + +%fragment(SWIG_From_frag(double),"header") { + SWIG_define(SWIG_From_dec(double), rb_float_new) +} + +%fragment(SWIG_AsVal_frag(double),"header",fragment="SWIG_ruby_failed") { +SWIGINTERN VALUE SWIG_num2dbl(VALUE *args) +{ + *((double *)(args[1])) = NUM2DBL(args[0]); + return args[0]; +} +SWIGINTERN int +SWIG_AsVal_dec(double)(VALUE obj, double *val) +{ + if (obj != Qnil &&((TYPE(obj) == T_FLOAT) || (TYPE(obj) == T_FIXNUM) || (TYPE(obj) == T_BIGNUM))) { + double v; + VALUE a[2] = { obj, (VALUE)(&v) }; + if (rb_rescue(RUBY_METHOD_FUNC(SWIG_num2dbl), (VALUE)a, RUBY_METHOD_FUNC(SWIG_ruby_failed), 0) != Qnil) { + if (val) *val = v; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +/* char */ + +%fragment(SWIG_From_frag(char),"header") { +SWIGINTERNINLINE VALUE +SWIG_From_dec(char)(char c) +{ + return rb_str_new(&c,1); +} +} + +%fragment(SWIG_AsVal_frag(char),"header", + fragment="SWIG_AsCharArray", + fragment=SWIG_AsVal_frag(signed char)) { +SWIGINTERN int +SWIG_AsVal_dec(char)(VALUE obj, char *val) +{ + signed char v; + if (SWIG_AsVal(signed char)(obj, &v) == SWIG_OK) { + if (val) *val = (char)(v); + return SWIG_OK; + } else { + if (SWIG_AsCharArray(obj, val, 1) == SWIG_OK) { + return SWIG_OK; + } + } + return SWIG_TypeError; + } +} + +/* wchar_t */ + + +%fragment(SWIG_From_frag(wchar_t),"header", + fragment=SWIG_From_frag(char), + fragment=SWIG_From_frag(long)) { +SWIGINTERNINLINE VALUE +SWIG_From_dec(wchar_t)(wchar_t c) +{ + if (CHAR_MIN <= v && v <= CHAR_MAX) { + return SWIG_From(char)((char)c); + } else { + return SWIG_From(long)((long)c); + } +} +} + +%fragment(SWIG_AsVal_frag(wchar_t),"header", + fragment="SWIG_AsWCharArray", + fragment=SWIG_AsVal_frag(long)) { +SWIGINTERN int +SWIG_AsVal_dec(wchar_t)(VALUE obj, wchar_t *val) +{ + char v; + if (SWIG_AsVal(char)(obj, &v) == SWIG_OK) { + if (val) *val = (wchar_t)(v); + return SWIG_OK; + } else { + long v; + if (SWIG_AsVal(long)(obj, &v) == SWIG_OK) { + if (WCHAR_MIN <= v && v <= WCHAR_MAX) { + if (val) *val = (wchar_t)(v); + return SWIG_OK; + } + } + } + return SWIG_TypeError; +} } /* ------------------------------------------------------------ - * Exception handling + * Apply the primitive typemap for all the types with checkcode * ------------------------------------------------------------ */ -%typemap(throws) int, - long, - short, - unsigned int, - unsigned long, - unsigned short { - rb_exc_raise(rb_exc_new3(rb_eRuntimeError, rb_obj_as_string(INT2NUM($1)))); -} - +%apply_checkctypes(%typemap_primitive) diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg new file mode 100644 index 000000000..227edae17 --- /dev/null +++ b/Lib/ruby/rubyrun.swg @@ -0,0 +1,289 @@ +/*********************************************************************** + * rubyrun.swg + * + * This file contains the runtime support for Ruby modules + * and includes code for managing global variables and pointer + * type checking. + * + ************************************************************************/ + +/* for raw pointers */ +#define SWIG_ConvertPtr(obj, pptr, type, flags) SWIG_Ruby_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewPointerObj(ptr, type, flags) SWIG_Ruby_NewPointerObj(ptr, type, flags) + +/* for raw packed data */ +#define SWIG_ConvertPacked(obj, ptr, sz, ty, flags) SWIG_Ruby_ConvertPacked(obj, ptr, sz, ty, flags) +#define SWIG_NewPackedObj(ptr, sz, type, flags) SWIG_Ruby_NewPackedObj(ptr, sz, type) + +/* for class or struct pointers */ +#define SWIG_ConvertInstance(obj, pptr, type, flags) SWIG_Ruby_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewInstanceObj(ptr, type, flags) SWIG_Ruby_NewPointerObj(ptr, type, flags) + +/* for C or C++ function pointers */ +#define SWIG_ConvertFunctionPtr(obj, pptr, type, flags) SWIG_Ruby_ConvertPtr(obj, pptr, type, flags) +#define SWIG_NewFunctionPtrObj(ptr, type) SWIG_Ruby_NewPointerObj(ptr, type, 0) + +/* for C++ member pointers, ie, member methods */ +#define SWIG_ConvertMember(obj, ptr, sz, ty, flags) SWIG_Ruby_ConvertPacked(obj, ptr, sz, ty, flags) +#define SWIG_NewMemberObj(ptr, sz, type) SWIG_Ruby_NewPackedObj(ptr, sz, type) + + +/* Runtime API */ +#define SWIG_GetModule(clientdata) SWIG_Ruby_GetModule() +#define SWIG_SetModule(clientdata, pointer) SWIG_Ruby_SetModule(pointer) + +/* Ruby-specific SWIG API */ + +#define SWIG_InitRuntime() SWIG_Ruby_InitRuntime() +#define SWIG_define_class(ty) SWIG_Ruby_define_class(ty) +#define SWIG_NewClassInstance(value, ty) SWIG_Ruby_NewClassInstance(value, ty) +#define SWIG_MangleStr(value) SWIG_Ruby_MangleStr(value) +#define SWIG_CheckConvert(value, ty) SWIG_Ruby_CheckConvert(value, ty) + +/* Error manipulation */ +#define SWIG_ERROR -1 +#define SWIG_fail return Qnil +#define SWIG_var_fail return Qnil + +#define SWIG_error(code, msg) SWIG_Ruby_SetErrorMsg(SWIG_Ruby_ErrorType(code), msg) +#define SWIG_exception(code, msg) do { SWIG_error(code, msg); SWIG_fail; } while (0) +#define SWIG_contract_assert(expr, msg) if (!(expr)) { SWIG_error(SWIG_RuntimeError, msg); SWIG_fail; } else + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Flags for new pointer objects */ +#define SWIG_TRACK_OBJECTS 0x4 + +/* ----------------------------------------------------------------------------- + * pointers/data manipulation + * ----------------------------------------------------------------------------- */ + +typedef struct { + VALUE klass; + VALUE mImpl; + void (*mark)(void *); + void (*destroy)(void *); +} swig_class; + + +static VALUE _mSWIG = Qnil; +static VALUE _cSWIG_Pointer = Qnil; +static VALUE swig_runtime_data_type_pointer = Qnil; + +/* Initialize Ruby runtime support */ +SWIGRUNTIME void +SWIG_Ruby_InitRuntime(void) +{ + if (_mSWIG == Qnil) { + _mSWIG = rb_define_module("SWIG"); + } +} + +/* Define Ruby class for C type */ +SWIGRUNTIME void +SWIG_Ruby_define_class(swig_type_info *type) +{ + VALUE klass; + char *klass_name = (char *) malloc(4 + strlen(type->name) + 1); + sprintf(klass_name, "TYPE%s", type->name); + if (NIL_P(_cSWIG_Pointer)) { + _cSWIG_Pointer = rb_define_class_under(_mSWIG, "Pointer", rb_cObject); + rb_undef_method(CLASS_OF(_cSWIG_Pointer), "new"); + } + klass = rb_define_class_under(_mSWIG, klass_name, _cSWIG_Pointer); + free((void *) klass_name); +} + +/* Create a new pointer object */ +SWIGRUNTIME VALUE +SWIG_Ruby_NewPointerObj(void *ptr, swig_type_info *type, int flags) +{ + int own = flags & SWIG_POINTER_OWN; + int track = flags & SWIG_TRACK_OBJECTS; + + char *klass_name; + swig_class *sklass; + VALUE klass; + VALUE obj; + + if (!ptr) + return Qnil; + + /* Have we already wrapped this pointer? */ + if (track) { + obj = SWIG_RubyInstanceFor(ptr); + if (obj != Qnil) { + return obj; + } + } + + if (type->clientdata) { + sklass = (swig_class *) type->clientdata; + obj = Data_Wrap_Struct(sklass->klass, VOIDFUNC(sklass->mark), (own ? VOIDFUNC(sklass->destroy) : 0), ptr); + } else { + klass_name = (char *) malloc(4 + strlen(type->name) + 1); + sprintf(klass_name, "TYPE%s", type->name); + klass = rb_const_get(_mSWIG, rb_intern(klass_name)); + free((void *) klass_name); + obj = Data_Wrap_Struct(klass, 0, 0, ptr); + } + rb_iv_set(obj, "__swigtype__", rb_str_new2(type->name)); + + /* Keep track of this object if necessary */ + if (track) { + SWIG_RubyAddTracking(ptr, obj); + } + + return obj; +} + +/* Create a new class instance (always owned) */ +SWIGRUNTIME VALUE +SWIG_Ruby_NewClassInstance(VALUE klass, swig_type_info *type) +{ + VALUE obj; + swig_class *sklass = (swig_class *) type->clientdata; + obj = Data_Wrap_Struct(klass, VOIDFUNC(sklass->mark), VOIDFUNC(sklass->destroy), 0); + rb_iv_set(obj, "__swigtype__", rb_str_new2(type->name)); + return obj; +} + +/* Get type mangle from class name */ +SWIGRUNTIMEINLINE char * +SWIG_Ruby_MangleStr(VALUE obj) +{ + VALUE stype = rb_iv_get(obj, "__swigtype__"); + return StringValuePtr(stype); +} + +/* Convert a pointer value */ +SWIGRUNTIME int +SWIG_Ruby_ConvertPtr(VALUE obj, void **ptr, swig_type_info *ty, int flags) +{ + char *c; + swig_cast_info *tc; + + /* Grab the pointer */ + if (NIL_P(obj)) { + *ptr = 0; + return SWIG_OK; + } else { + if (TYPE(obj) != T_DATA) { + return SWIG_ERROR; + } + Data_Get_Struct(obj, void, *ptr); + } + + + /* Check to see if the input object is giving up ownership + of the underlying C struct or C++ object. If so then we + need to reset the destructor since the Ruby object no + longer owns the underlying C++ object.*/ + if (flags & SWIG_POINTER_DISOWN) { + if (flags & SWIG_TRACK_OBJECTS) { + /* We are tracking objects. Thus we change the destructor + * to SWIG_RubyRemoveTracking. This allows us to + * remove the mapping from the C++ to Ruby object + * when the Ruby object is garbage collected. If we don't + * do this, then it is possible we will return a reference + * to a Ruby object that no longer exists thereby crashing Ruby. */ + RDATA(obj)->dfree = SWIG_RubyRemoveTracking; + } else { + RDATA(obj)->dfree = 0; + } + } + + /* Do type-checking if type info was provided */ + if (ty) { + if (ty->clientdata) { + if (rb_obj_is_kind_of(obj, ((swig_class *) (ty->clientdata))->klass)) { + if (*ptr == 0) { + return SWIG_ERROR; + } + return SWIG_OK; + } + } + if ((c = SWIG_MangleStr(obj)) == NULL) { + return SWIG_ERROR; + } + tc = SWIG_TypeCheck(c, ty); + if (!tc) { + return SWIG_ERROR; + } + *ptr = SWIG_TypeCast(tc, *ptr); + } + return SWIG_OK; +} + +/* Check convert */ +SWIGRUNTIMEINLINE int +SWIG_Ruby_CheckConvert(VALUE obj, swig_type_info *ty) +{ + char *c = SWIG_MangleStr(obj); + if (!c) return 0; + return SWIG_TypeCheck(c,ty) != 0; +} + +SWIGRUNTIME VALUE +SWIG_Ruby_NewPackedObj(void *ptr, int sz, swig_type_info *type) { + char result[1024]; + char *r = result; + if ((2*sz + 1 + strlen(type->name)) > 1000) return 0; + *(r++) = '_'; + r = SWIG_PackData(r, ptr, sz); + strcpy(r, type->name); + return rb_str_new2(result); +} + +/* Convert a packed value value */ +SWIGRUNTIME int +SWIG_Ruby_ConvertPacked(VALUE obj, void *ptr, int sz, swig_type_info *ty, int flags) { + swig_cast_info *tc; + const char *c; + + if (TYPE(obj) != T_STRING) goto type_error; + c = StringValuePtr(obj); + /* Pointer values must start with leading underscore */ + if (*c != '_') goto type_error; + c++; + c = SWIG_UnpackData(c, ptr, sz); + if (ty) { + tc = SWIG_TypeCheck(c, ty); + if (!tc) goto type_error; + } + return SWIG_OK; + +type_error: + return SWIG_ERROR; +} + +SWIGRUNTIME swig_module_info * +SWIG_Ruby_GetModule() +{ + VALUE pointer; + swig_module_info *ret = 0; + + /* first check if pointer already created */ + pointer = rb_gv_get("$swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME); + if (pointer != Qnil) { + Data_Get_Struct(pointer, swig_module_info, ret); + } + return ret; +} + +SWIGRUNTIME void +SWIG_Ruby_SetModule(swig_module_info *pointer) +{ + /* register a new class */ + VALUE cl = rb_define_class("swig_runtime_data", rb_cObject); + /* create and store the structure pointer to a global variable */ + swig_runtime_data_type_pointer = Data_Wrap_Struct(cl, 0, 0, pointer); + rb_define_readonly_variable("$swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, &swig_runtime_data_type_pointer); +} + +#ifdef __cplusplus +} +#endif diff --git a/Lib/ruby/rubyhead.swg b/Lib/ruby/rubyruntime.swg similarity index 72% rename from Lib/ruby/rubyhead.swg rename to Lib/ruby/rubyruntime.swg index 220f437b8..cc5799c6a 100644 --- a/Lib/ruby/rubyhead.swg +++ b/Lib/ruby/rubyruntime.swg @@ -1,28 +1,5 @@ -/* ruby.swg */ -/* Implementation : RUBY */ -#define SWIGRUBY 1 - -#include "ruby.h" - -/* Flags for pointer conversion */ -#define SWIG_POINTER_EXCEPTION 0x1 -#define SWIG_POINTER_OWN 0x1 -#define SWIG_POINTER_DISOWN 0x2 -#define SWIG_TRACK_OBJECTS 0x4 - -#define NUM2USHRT(n) (\ - (0 <= NUM2UINT(n) && NUM2UINT(n) <= USHRT_MAX)\ - ? (unsigned short) NUM2UINT(n) \ - : (rb_raise(rb_eArgError, "integer %d out of range of `unsigned short'",\ - NUM2UINT(n)), (short)0)\ -) - -#define NUM2SHRT(n) (\ - (SHRT_MIN <= NUM2INT(n) && NUM2INT(n) <= SHRT_MAX)\ - ? (short)NUM2INT(n)\ - : (rb_raise(rb_eArgError, "integer %d out of range of `short'",\ - NUM2INT(n)), (short)0)\ -) +%insert(runtime) %{ +#include /* Ruby 1.7 defines NUM2LL(), LL2NUM() and ULL2NUM() macros */ #ifndef NUM2LL @@ -79,13 +56,6 @@ # define VOIDFUNC(f) (f) #endif -typedef struct { - VALUE klass; - VALUE mImpl; - void (*mark)(void *); - void (*destroy)(void *); -} swig_class; - /* Don't use for expressions have side effect */ #ifndef RB_STRING_VALUE #define RB_STRING_VALUE(s) (TYPE(s) == T_STRING ? (s) : (*(volatile VALUE *)&(s) = rb_str_to_str(s))) @@ -111,7 +81,9 @@ typedef struct { #define rb_undef_alloc_func(klass) rb_undef_method(CLASS_OF((klass)), "new") #endif -/* Contract support */ - -#define SWIG_contract_assert(expr, msg) if (!(expr)) { rb_raise(rb_eRuntimeError, (char *) msg ); } else +%} +%runtime "swigrun.swg" /* Common C API type-checking code */ +%runtime "rubytracking.swg" /* API for tracking C++ classes to Ruby objects */ +%runtime "rubyrun.swg" +%runtime "rubyapi.swg" diff --git a/Lib/ruby/rubystrings.swg b/Lib/ruby/rubystrings.swg index f46aa1d2d..53c587b86 100644 --- a/Lib/ruby/rubystrings.swg +++ b/Lib/ruby/rubystrings.swg @@ -1,125 +1,60 @@ -/* --- Input Values --- */ - -%typemap(in) char * "$1 = StringValuePtr($input);"; -%typemap(in) char [ANY] "$1 = StringValuePtr($input);"; - /* ------------------------------------------------------------ - * String & length + * utility methods for char strings * ------------------------------------------------------------ */ -%typemap(in) (int LENGTH, char *STRING) { - $1 = ($1_ltype) StringValueLen($input); - $2 = ($2_ltype) StringValuePtr($input); -} - -%typemap(in) (char *STRING, int LENGTH) { - $1 = ($1_ltype) StringValuePtr($input); - $2 = ($2_ltype) StringValueLen($input); -} - -/* --- Output typemaps --- */ - -/* Single char */ -%typemap(out) char "$result = rb_str_new(&$1,1);"; -%typemap(out) const char & "$result = rb_str_new($1, 1);"; - -/* C string */ -%typemap(out) char * "$result = rb_str_new2($1);"; - -/* Special typemap for character array return values */ -%typemap(out) char [ANY], const char [ANY] "$result = rb_str_new2($1);"; - -/* --- Variable Input --- */ - -/* A string */ -#ifdef __cplusplus - -%typemap(varin) char * { - char *temp = (char *) StringValuePtr($input); - if ($1) delete [] $1; - $1 = ($type) new char[strlen(temp)+1]; - strcpy((char*)$1,temp); -} - -%typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { - char *temp = (char *) StringValuePtr($input); - $1 = ($type) new char[strlen(temp)+1]; - strcpy((char*)$1,temp); -} - -#else - -%typemap(varin) char * { - char *temp = (char *) StringValuePtr($input); - if ($1) free((char*) $1); - $1 = ($type) malloc(strlen(temp)+1); - strcpy((char*)$1,temp); -} - -%typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { - char *temp = (char *) StringValuePtr($input); - $1 = ($type) malloc(strlen(temp)+1); - strcpy((char*)$1,temp); -} - -#endif /* __cplusplus */ - -/* Special case for string array variables */ -%typemap(varin, - warning="462:Unable to set variable of type char []") char[] +%fragment("SWIG_AsCharPtrAndSize","header") { +SWIGINTERN int +SWIG_AsCharPtrAndSize(VALUE obj, char** cptr, size_t* psize, int *alloc) { - rb_raise(rb_eTypeError, "C/C++ variable '$name' is read-only."); + static swig_type_info* pchar_info = 0; + char* vptr = 0; + if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); + if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) == SWIG_OK) { + if (cptr) *cptr = vptr; + if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; + } else { + if (TYPE(obj) == T_STRING) { + char *cstr = rb_string_value_ptr(&(obj)); + size_t size = RSTRING(obj)->len + 1; + if (cptr) { + if (alloc) { + if (*alloc == SWIG_NEWOBJ) { + *cptr = SWIG_new_copy_array(cstr, size, char); + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; + } + } + } + if (psize) *psize = size; + return SWIG_OK; + } + } + return SWIG_TypeError; +} } -%typemap(varin) char[ANY] "strncpy($1,StringValuePtr($input),$1_dim0);"; - -/* --- Variable Output --- */ - -/* Character */ -%typemap(varout) char "$result = rb_str_new(&$1,1);"; - -/* C string */ -%typemap(varout) char * "$result = rb_str_new2($1);"; - -/* Special typemap for character array return values */ -%typemap(varout) char [ANY], const char [ANY] "$result = rb_str_new2($1);"; - -/* --- Constants --- */ - -%typemap(constant) char { - char temp = $1; - rb_define_const($module,"$symname", rb_str_new(&temp,1)); +%fragment("SWIG_FromCharPtrAndSize","header") { +SWIGINTERNINLINE VALUE +SWIG_FromCharPtrAndSize(const char* carray, size_t size) +{ + if (carray) { + if (size > LONG_MAX) { + return SWIG_NewPointerObj(SWIG_const_cast(carray,char *), SWIG_TypeQuery("char *"), 0); + } else { + return rb_str_new(carray, SWIG_numeric_cast(size,long)); + } + } else { + return Qnil; + } } - -%typemap(constant) char * - "rb_define_const($module,\"$symname\", rb_str_new2($1));"; - -/* directorin typemaps */ - -%typemap(directorin) char * "$input = rb_str_new2($1);"; - -/* directorout typemaps */ - -%typemap(directorout) char * "$result = STR2CSTR($1);"; -%typemap(directorout) const char * "$result = STR2CSTR($1);"; - -/* ------------------------------------------------------------ - * Typechecking rules - * ------------------------------------------------------------ */ - -%typecheck(SWIG_TYPECHECK_CHAR) char { - $1 = (TYPE($input) == T_STRING && (RSTRING($input)->len == 1)) ? 1 : 0; -} - -%typecheck(SWIG_TYPECHECK_STRING) char * { - $1 = (TYPE($input) == T_STRING) ? 1 : 0; } /* ------------------------------------------------------------ - * Exception handling + * The plain char * handling * ------------------------------------------------------------ */ -%typemap(throws) char * { - rb_raise(rb_eRuntimeError, $1); -} - +%include +%typemap_string(char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize, strlen) diff --git a/Lib/ruby/rubyswigtype.swg b/Lib/ruby/rubyswigtype.swg deleted file mode 100644 index 8a5457471..000000000 --- a/Lib/ruby/rubyswigtype.swg +++ /dev/null @@ -1,162 +0,0 @@ -/* --- Input typemaps --- */ - -%typemap(in) SWIGTYPE *, - SWIGTYPE [] - "SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, $disown);" - -%typemap(in) SWIGTYPE *DISOWN - "SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, SWIG_POINTER_DISOWN);" - -/* Additional check for null references */ -%typemap(in) SWIGTYPE & - "SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, $disown); if ($1 == NULL) rb_raise(rb_eTypeError, \"null reference\");" - -/* Object passed by value. Convert to a pointer */ -%typemap(in) SWIGTYPE { - $&1_ltype ptr; - SWIG_ConvertPtr($input, (void **) &ptr, $&1_descriptor, $disown); - if (ptr) $1 = *ptr; -} - -/* Pointer to a class member */ -%typemap(in) SWIGTYPE (CLASS::*) "SWIG_ConvertPacked($input, (void *) &$1, sizeof($1_type), $1_descriptor, $disown);"; - -/* --- Output typemaps --- */ - -/* Pointers, references, and arrays */ -%typemap(out) SWIGTYPE*, SWIGTYPE &, SWIGTYPE [] - "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor,$owner);"; - -/* Dynamic casts */ - -%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { - swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1); - $result = SWIG_NewPointerObj((void *) $1, ty,$owner); -} - -/* Member pointer */ -%typemap(out) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);"; - -/* Primitive types--return by value */ -%typemap(out) SWIGTYPE -#ifdef __cplusplus -{ - $&1_ltype resultptr; - resultptr = new $1_ltype(($1_ltype &)$1); - $result = SWIG_NewPointerObj((void *) resultptr, $&1_descriptor, 1); -} -#else -{ - $&1_ltype resultptr; - resultptr = ($&1_ltype) malloc(sizeof($1_type)); - memmove(resultptr, &$1, sizeof($1_type)); - $result = SWIG_NewPointerObj((void *) resultptr, $&1_descriptor, 1); -} -#endif - -/* --- Variable Input --- */ - -%typemap(varin) SWIGTYPE [ANY] { - void *temp; - int ii; - $1_basetype *b = 0; - if ((SWIG_ConvertPtr($input,(void **) &temp, $1_descriptor, 0)) == -1) { - rb_raise(rb_eTypeError, "C variable '$name ($1_ltype)'"); - } - b = ($1_basetype *) $1; - for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) temp + ii); -} - -%typemap(varin,warning="462: Unable to set dimensionless array variable") SWIGTYPE [] { - rb_raise(rb_eTypeError, "C/C++ variable '$name' is readonly"); -} - -/* Typemaps for pointers. Note: the SWIG run-time type checker works - even if a pointer happens to be mapped to a Ruby class */ - -%typemap(varin) SWIGTYPE * - "SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);" - -%typemap(varin) SWIGTYPE & { - void *temp; - SWIG_ConvertPtr($input, (void **) &temp, $1_descriptor, 1); - $1 = *($1_ltype) temp; -} - -%typemap(varin) SWIGTYPE { - $&1_ltype ptr; - SWIG_ConvertPtr($input, (void **) &ptr, $&1_descriptor, 1); - if (ptr) $1 = *ptr; -} - -%typemap(varin) SWIGTYPE (CLASS::*) { - char temp[sizeof($1_type)]; - SWIG_ConvertPacked($input, (void *) temp, sizeof($1_type), $1_descriptor, 1); - memmove((void *) &$1, temp, sizeof($1_type)); -} - -/* --- Output typemaps --- */ - -/* Pointers, references, and arrays */ -%typemap(varout) SWIGTYPE*, SWIGTYPE [] - "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor,0);"; - -%typemap(varout) SWIGTYPE & - "$result = SWIG_NewPointerObj((void *) &$1, $1_descriptor,0);"; - -/* Copy by value */ -%typemap(varout) SWIGTYPE "$result = SWIG_NewPointerObj((void *) &$1, $&1_descriptor, 0);"; - -/* Member pointer */ -%typemap(varout) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);"; - -/* --- Constants --- */ - -%typemap(constant) SWIGTYPE*, SWIGTYPE &, SWIGTYPE [] - "rb_define_const($module,\"$symname\", SWIG_NewPointerObj((void *) $1, $1_descriptor,0));"; - -%typemap(constant) SWIGTYPE "rb_define_const($module,\"$symname\", SWIG_NewPointerObj((void *) &$1, $&1_descriptor, 0));"; - -%typemap(constant) SWIGTYPE (CLASS::*) "rb_define_const($module, \"$symname\", SWIG_NewPackedObj((void *) &$1, sizeof($type), $1_descriptor));"; - -/* --- directorin typemaps --- */ - -%typemap(directorin) SWIGTYPE* - "$input = SWIG_NewPointerObj((void *) $1, $1_descriptor,0);"; - -%typemap(directorin) SWIGTYPE - "$input = SWIG_NewPointerObj((void *) &$1, $1_descriptor,0);"; - -%typemap(directorin) SWIGTYPE& - "$input = SWIG_NewPointerObj((void *) &$1, $1_descriptor,0);"; - - -/* --- directorout typemaps --- */ -%typemap(directorout) SWIGTYPE *, - SWIGTYPE &, - SWIGTYPE [] - "if ((SWIG_ConvertPtr($input,(void **) &$result, $descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\");"; - -/* ------------------------------------------------------------ - * Typechecking rules - * ------------------------------------------------------------ */ - -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE { - void *ptr; - $1 = (NIL_P($input) || (TYPE($input) == T_DATA && SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 0) != -1)) ? 1 : 0; -} - -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] { - void *ptr; - $1 = (NIL_P($input) || (TYPE($input) == T_DATA && SWIG_ConvertPtr($input, &ptr, $1_descriptor, 0) != -1)) ? 1 : 0; -} - -/* ------------------------------------------------------------ - * Exception handling. - * Note that in Ruby, we can only raise an exception class and - * not some arbitrary object as in Python. - * ------------------------------------------------------------ */ - -%typemap(throws) SWIGTYPE, SWIGTYPE *, SWIGTYPE [ANY], SWIGTYPE & - "(void)$1; rb_raise(rb_eRuntimeError, \"$1_type\");"; - diff --git a/Lib/ruby/rubytracking.swg b/Lib/ruby/rubytracking.swg index 4a62d32a9..84cfb7003 100644 --- a/Lib/ruby/rubytracking.swg +++ b/Lib/ruby/rubytracking.swg @@ -7,109 +7,120 @@ * garbage collector. ************************************************************************/ + +#ifdef __cplusplus +extern "C" { +#endif + + /* Global Ruby hash table to store Trackings from C/C++ structs to Ruby Objects. */ static VALUE swig_ruby_trackings; /* Setup a Ruby hash table to store Trackings */ -static void SWIG_RubyInitializeTrackings() { - /* Create a ruby hash table to store Trackings from C++ - objects to Ruby objects. Also make sure to tell - the garabage collector about the hash table. */ - swig_ruby_trackings = rb_hash_new(); - rb_gc_register_address(&swig_ruby_trackings); +SWIGRUNTIME void SWIG_RubyInitializeTrackings() { + /* Create a ruby hash table to store Trackings from C++ + objects to Ruby objects. Also make sure to tell + the garabage collector about the hash table. */ + swig_ruby_trackings = rb_hash_new(); + rb_gc_register_address(&swig_ruby_trackings); } /* Get a Ruby number to reference a pointer */ -static VALUE SWIG_RubyPtrToReference(void* ptr) { - /* We cast the pointer to an unsigned long - and then store a reference to it using - a Ruby number object. */ +SWIGRUNTIME VALUE SWIG_RubyPtrToReference(void* ptr) { + /* We cast the pointer to an unsigned long + and then store a reference to it using + a Ruby number object. */ - /* Convert the pointer to a Ruby number */ - unsigned long value = (unsigned long) ptr; - return LONG2NUM(value); + /* Convert the pointer to a Ruby number */ + unsigned long value = (unsigned long) ptr; + return LONG2NUM(value); } /* Get a Ruby number to reference an object */ -static VALUE SWIG_RubyObjectToReference(VALUE object) { - /* We cast the object to an unsigned long - and then store a reference to it using - a Ruby number object. */ +SWIGRUNTIME VALUE SWIG_RubyObjectToReference(VALUE object) { + /* We cast the object to an unsigned long + and then store a reference to it using + a Ruby number object. */ - /* Convert the Object to a Ruby number */ - unsigned long value = (unsigned long) object; - return LONG2NUM(value); + /* Convert the Object to a Ruby number */ + unsigned long value = (unsigned long) object; + return LONG2NUM(value); } /* Get a Ruby object from a previously stored reference */ -static VALUE SWIG_RubyReferenceToObject(VALUE reference) { - /* The provided Ruby number object is a reference - to the Ruby object we want.*/ +SWIGRUNTIME VALUE SWIG_RubyReferenceToObject(VALUE reference) { + /* The provided Ruby number object is a reference + to the Ruby object we want.*/ - /* First convert the Ruby number to a C number */ - unsigned long value = NUM2LONG(reference); - return (VALUE) value; + /* First convert the Ruby number to a C number */ + unsigned long value = NUM2LONG(reference); + return (VALUE) value; } /* Add a Tracking from a C/C++ struct to a Ruby object */ -static void SWIG_RubyAddTracking(void* ptr, VALUE object) { - /* In a Ruby hash table we store the pointer and - the associated Ruby object. The trick here is - that we cannot store the Ruby object directly - if - we do then it cannot be garbage collected. So - instead we typecast it as a unsigned long and - convert it to a Ruby number object.*/ +SWIGRUNTIME void SWIG_RubyAddTracking(void* ptr, VALUE object) { + /* In a Ruby hash table we store the pointer and + the associated Ruby object. The trick here is + that we cannot store the Ruby object directly - if + we do then it cannot be garbage collected. So + instead we typecast it as a unsigned long and + convert it to a Ruby number object.*/ - /* Get a reference to the pointer as a Ruby number */ - VALUE key = SWIG_RubyPtrToReference(ptr); + /* Get a reference to the pointer as a Ruby number */ + VALUE key = SWIG_RubyPtrToReference(ptr); - /* Get a reference to the Ruby object as a Ruby number */ - VALUE value = SWIG_RubyObjectToReference(object); + /* Get a reference to the Ruby object as a Ruby number */ + VALUE value = SWIG_RubyObjectToReference(object); /* Store the mapping to the global hash table. */ - rb_hash_aset(swig_ruby_trackings, key, value); + rb_hash_aset(swig_ruby_trackings, key, value); } /* Get the Ruby object that owns the specified C/C++ struct */ -static VALUE SWIG_RubyInstanceFor(void* ptr) { - /* Get a reference to the pointer as a Ruby number */ - VALUE key = SWIG_RubyPtrToReference(ptr); +SWIGRUNTIME VALUE SWIG_RubyInstanceFor(void* ptr) { + /* Get a reference to the pointer as a Ruby number */ + VALUE key = SWIG_RubyPtrToReference(ptr); - /* Now lookup the value stored in the global hash table */ - VALUE value = rb_hash_aref(swig_ruby_trackings, key); + /* Now lookup the value stored in the global hash table */ + VALUE value = rb_hash_aref(swig_ruby_trackings, key); - if (value == Qnil) { - /* No object exists - return nil. */ - return Qnil; - } - else { - /* Convert this value to Ruby object */ - return SWIG_RubyReferenceToObject(value); - } + if (value == Qnil) { + /* No object exists - return nil. */ + return Qnil; + } + else { + /* Convert this value to Ruby object */ + return SWIG_RubyReferenceToObject(value); + } } /* Remove a Tracking from a C/C++ struct to a Ruby object */ -static void SWIG_RubyRemoveTracking(void* ptr) { - /* Get a reference to the pointer as a Ruby number */ - VALUE key = SWIG_RubyPtrToReference(ptr); +SWIGRUNTIME void SWIG_RubyRemoveTracking(void* ptr) { + /* Get a reference to the pointer as a Ruby number */ + VALUE key = SWIG_RubyPtrToReference(ptr); - /* Define delete method - in C++ this could be marked as - static but unfortunately not in C. */ - VALUE delete_function = rb_intern("delete"); + /* Define delete method - in C++ this could be marked as + static but unfortunately not in C. */ + VALUE delete_function = rb_intern("delete"); - /* Delete the object from the hash table by calling Ruby's - do this we need to call the Hash.delete method.*/ - rb_funcall(swig_ruby_trackings, delete_function, 1, key); + /* Delete the object from the hash table by calling Ruby's + do this we need to call the Hash.delete method.*/ + rb_funcall(swig_ruby_trackings, delete_function, 1, key); } /* This is a helper method that unlinks a Ruby object from its underlying C++ object. This is needed if the lifetime of the Ruby object is longer than the C++ object */ -static void SWIG_RubyUnlinkObjects(void* ptr) { - VALUE object = SWIG_RubyInstanceFor(ptr); +SWIGRUNTIME void SWIG_RubyUnlinkObjects(void* ptr) { + VALUE object = SWIG_RubyInstanceFor(ptr); - if (object != Qnil) { - DATA_PTR(object) = 0; - } + if (object != Qnil) { + DATA_PTR(object) = 0; + } } + + +#ifdef __cplusplus +} +#endif diff --git a/Lib/ruby/rubytypemaps.swg b/Lib/ruby/rubytypemaps.swg new file mode 100644 index 000000000..9734c1281 --- /dev/null +++ b/Lib/ruby/rubytypemaps.swg @@ -0,0 +1,45 @@ +/* ----------------------------------------------------------------------------- + * Typemap specializations + * ----------------------------------------------------------------------------- */ + +/* directors are supported in Python */ +#ifndef SWIG_DIRECTOR_TYPEMAPS +#define SWIG_DIRECTOR_TYPEMAPS +#endif + +/* ----------------------------------------------------------------------------- + * Basic definitions + * ----------------------------------------------------------------------------- */ + +%define_swig_object(VALUE) + +#define SWIG_SetResultObj(obj) $result = obj +#define SWIG_AppendResultObj(obj) $result = SWIG_Ruby_AppendResult($result, obj) +#define SWIG_SetConstantObj(name, obj) rb_define_const($module, name, obj); +#define SWIG_NoneObject() Qnil + +/* error manipulation */ +#define SWIG_ErrorType(code) SWIG_Ruby_ErrorType(code) +#define SWIG_SetErrorObj(code, obj) SWIG_Ruby_SetErrorMsg(SWIG_ErrorType(code),obj) +#define SWIG_SetErrorMsg(code, msg) SWIG_Ruby_SetErrorMsg(SWIG_ErrorType(code),msg) +#define SWIG_ExceptionObj(d, type, obj) rb_exc_raise(rb_exc_new3(rb_eRuntimeError, rb_obj_as_string(obj))) +#define SWIG_DirOutFail(code, msg) Swig::DirectorTypeMismatchException::raise(SWIG_ErrorType(code), msg) + + +/* ----------------------------------------------------------------------------- + * All the typemaps + * ----------------------------------------------------------------------------- */ + +%include "rubyfragments.swg" + +%include +%include +%include +%include +%include +%include +%include +%include +%include +%include + diff --git a/Lib/ruby/rubyuserdir.swg b/Lib/ruby/rubyuserdir.swg new file mode 100644 index 000000000..b7074eebf --- /dev/null +++ b/Lib/ruby/rubyuserdir.swg @@ -0,0 +1,6 @@ +#define %alias %feature("alias") +#define %freefunc %feature("freefunc") +#define %markfunc %feature("markfunc") +#define %mixin %feature("mixin") +#define %predicate %feature("predicate", "1") +#define %trackobjects %feature("trackobjects") diff --git a/Lib/ruby/rubyvoid.swg b/Lib/ruby/rubyvoid.swg deleted file mode 100644 index 7cbf75664..000000000 --- a/Lib/ruby/rubyvoid.swg +++ /dev/null @@ -1,33 +0,0 @@ -/* ------------------------------------------------------------ - * Void * - Accepts any kind of pointer - * ------------------------------------------------------------ */ - -/* in */ - -%typemap(in) void * - "SWIG_ConvertPtr($input, (void **) &$1, 0, SWIG_POINTER_EXCEPTION|$disown);"; - -/* out */ - -%typemap(out) void "$result = Qnil;"; - -/* varin */ - -%typemap(varin) void * - "SWIG_ConvertPtr($input, (void **) &$1, 0, 1);"; - -/* varout */ - -%typemap(varout) void "$result = Qnil;"; - -/* directorout */ - -%typemap(directorout) void * "if ((SWIG_ConvertPtr($input,(void **) &$result, 0, SWIG_POINTER_EXCEPTION | $disown )) == -1) throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\");"; - -/* typecheck */ - -%typecheck(SWIG_TYPECHECK_VOIDPTR) void * { - void *ptr; - $1 = (NIL_P($input) || (TYPE($input) == T_DATA && SWIG_ConvertPtr($input, &ptr, 0, 0) != -1)) ? 1 : 0; -} - diff --git a/Lib/ruby/rubywstrings.swg b/Lib/ruby/rubywstrings.swg new file mode 100644 index 000000000..4f4959844 --- /dev/null +++ b/Lib/ruby/rubywstrings.swg @@ -0,0 +1,74 @@ +/* ------------------------------------------------------------ + * utility methods for wchar_t strings + * ------------------------------------------------------------ */ + +/* + Ruby doesn't support the wchar_t, so, we need to use an 'opaque' type. + */ + +%types(ruby_wchar_array *); + +%fragment("ruby_wchar_array","header",fragment="") { + struct ruby_wchar_array { + wchar_t *cptr; + size_t size; + }; + + SWIGINTERN get_ruby_wchar_array_info() { + static swig_type_info* ruby_wchar_array_info = 0; + if (!ruby_wchar_array_info) ruby_wchar_array_info = SWIG_TypeQuery("ruby_wchar_array *"); + return ruby_wchar_array_info; + } +} + +%fragment("SWIG_AsWCharPtrAndSize","header",fragment="ruby_wchar_array") { +SWIGINTERN int +SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) +{ + static swig_type_info* ptr_wchar_info = 0; + wchar_t * vptr = 0; + if (!ptr_wchar_info) ptr_wchar_info = SWIG_TypeQuery("wchar_t *"); + if (SWIG_ConvertPtr(obj, (void**)&vptr, ptr_wchar_info, 0) != SWIG_OK) { + if (cptr) *cptr = vptr; + if (psize) *psize = vptr ? (wcslen(vptr) + 1) : 0; + return SWIG_OK; + } else { + ruby_wchar_array *vptr = 0; + if (SWIG_ConvertPtr(obj, (void**)&vptr, get_ruby_wchar_array_info(), 0) != SWIG_OK) { + if (cptr) *cptr = vptr->cptr; + if (psize) *psize = vprtr->size; + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +%fragment("SWIG_FromWCharPtrAndSize","header",fragment="ruby_wchar_array") { +SWIGINTERNINLINE PyObject * +SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size) +{ + ruby_wchar_array *vptr = SWIG_new(ruby_wchar_array); + vptr->cptr = carray; + vptr->size = size; + return SWIG_NewPointerObj(SWIG_const_cast(carray,wchar_t *), get_ruby_wchar_array_info(), 1); +} +} + +%fragment("SWIG_FromWCharPtr","header",fragment="") { +SWIGINTERNINLINE PyObject * +SWIG_FromWCharPtr(const wchar_t * carray) +{ + static swig_type_info* wchar_ptr_info = 0; + if (!wchar_array_info) wchar_ptr_info = SWIG_TypeQuery("wchar_t *"); + return SWIG_NewPointerObj(SWIG_const_cast(carray,wchar_t *), wchar_ptr_info, 0); +} +} + +/* ------------------------------------------------------------ + * The plain wchar_t * handling + * ------------------------------------------------------------ */ + +%include +%typemap_string(wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize, wcslen); + diff --git a/Lib/ruby/std_string.i b/Lib/ruby/std_string.i index 245f06da7..a0ec522bc 100644 --- a/Lib/ruby/std_string.i +++ b/Lib/ruby/std_string.i @@ -1,86 +1,30 @@ // -// SWIG typemaps for std::string -// Luigi Ballabio -// Apr 8, 2002 +// std::string // -// Ruby implementation - -// ------------------------------------------------------------------------ -// std::string is typemapped by value -// This can prevent exporting methods which return a string -// in order for the user to modify it. -// However, I think I'll wait until someone asks for it... -// ------------------------------------------------------------------------ - -%include exception.i +#ifndef SWIG_STD_BASIC_STRING +#define SWIG_STD_STRING %{ #include %} -namespace std { - - // Ruby wants class names to start with a capital letter - %rename(String) string; - class string; - - /* Overloading check */ - %typemap(typecheck) string = char *; - %typemap(typecheck) const string & = char *; - - %typemap(in) string { - if (TYPE($input) == T_STRING) { - $1 = std::string(StringValuePtr($input)); - } else { - SWIG_exception(SWIG_TypeError, "not a string"); - } - } - - %typemap(in) const string & (std::string temp) { - if (TYPE($input) == T_STRING) { - temp = std::string(StringValuePtr($input)); - $1 = &temp; - } else { - SWIG_exception(SWIG_TypeError, "not a string"); - } - } - - %typemap(out) string { - $result = rb_str_new2($1.c_str()); - } - - %typemap(out) const string & { - $result = rb_str_new2($1->c_str()); - } - - %typemap(directorin) string, const string &, string & { - $input = rb_str_new2($1_name.c_str()); - } - - %typemap(directorin) string *, const string * { - $input = rb_str_new2($1_name->c_str()); - } - - %typemap(directorout) string { - if (TYPE($input) == T_STRING) - $result = std::string(StringValuePtr($input)); - else - throw Swig::DirectorTypeMismatchException("string expected"); - } - - %typemap(directorout,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const string & { - if (TYPE($input) == T_STRING) { - static std::string temp = std::string(StringValuePtr($input)); - $result = &temp; - } else { - throw Swig::DirectorTypeMismatchException("string expected"); - } - } - - %typemap(throws) string, const string & - "rb_raise(rb_eRuntimeError, $1.c_str());"; - - %typemap(throws) string *, const string * - "rb_raise(rb_eRuntimeError, $1->c_str());"; +namespace std +{ + class string; } + +%include +%include + +%std_string_asptr(std::string, char, SWIG_AsCharPtrAndSize) +%std_string_from(std::string, SWIG_FromCharPtrAndSize) +%std_string_asval(std::string) + +%typemap_asptrfromn(SWIG_CCode(STRING), std::string); + +#else + +%include + +#endif diff --git a/Lib/ruby/typemaps.i b/Lib/ruby/typemaps.i index 7a44b5c47..1f9b9c432 100644 --- a/Lib/ruby/typemaps.i +++ b/Lib/ruby/typemaps.i @@ -1,441 +1 @@ -// -// typemaps for Ruby -// -// $Header$ -// -// Copyright (C) 2000 Network Applied Communication Laboratory, Inc. -// Copyright (C) 2000 Information-technology Promotion Agency, Japan -// -// Masaki Fukushima -// - -/* -The SWIG typemap library provides a language independent mechanism for -supporting output arguments, input values, and other C function -calling mechanisms. The primary use of the library is to provide a -better interface to certain C function--especially those involving -pointers. -*/ - -// ------------------------------------------------------------------------ -// Pointer handling -// -// These mappings provide support for input/output arguments and common -// uses for C/C++ pointers. -// ------------------------------------------------------------------------ - -// INPUT typemaps. -// These remap a C pointer to be an "INPUT" value which is passed by value -// instead of reference. - -/* -The following methods can be applied to turn a pointer into a simple -"input" value. That is, instead of passing a pointer to an object, -you would use a real value instead. - - int *INPUT - short *INPUT - long *INPUT - long long *INPUT - unsigned int *INPUT - unsigned short *INPUT - unsigned long *INPUT - unsigned long long *INPUT - unsigned char *INPUT - bool *INPUT - float *INPUT - double *INPUT - -To use these, suppose you had a C function like this : - - double fadd(double *a, double *b) { - return *a+*b; - } - -You could wrap it with SWIG as follows : - - %include typemaps.i - double fadd(double *INPUT, double *INPUT); - -or you can use the %apply directive : - - %include typemaps.i - %apply double *INPUT { double *a, double *b }; - double fadd(double *a, double *b); - -*/ - -%define INPUT_TYPEMAP(type, converter) -%typemap(in) type *INPUT($*1_ltype temp), type &INPUT($*1_ltype temp) -{ - temp = ($*1_ltype) converter($input); - $1 = &temp; -} -%typemap(typecheck) type *INPUT = type; -%typemap(typecheck) type &INPUT = type; -%enddef - -INPUT_TYPEMAP(float, NUM2DBL); -INPUT_TYPEMAP(double, NUM2DBL); -INPUT_TYPEMAP(int, NUM2INT); -INPUT_TYPEMAP(short, NUM2SHRT); -INPUT_TYPEMAP(long, NUM2LONG); -INPUT_TYPEMAP(long long, NUM2LL); -INPUT_TYPEMAP(unsigned int, NUM2UINT); -INPUT_TYPEMAP(unsigned short, NUM2USHRT); -INPUT_TYPEMAP(unsigned long, NUM2ULONG); -INPUT_TYPEMAP(unsigned long long, NUM2ULL); -INPUT_TYPEMAP(unsigned char, NUM2UINT); -INPUT_TYPEMAP(signed char, NUM2INT); -INPUT_TYPEMAP(bool, RTEST); - -#undef INPUT_TYPEMAP - -// OUTPUT typemaps. These typemaps are used for parameters that -// are output only. The output value is appended to the result as -// a array element. - -/* -The following methods can be applied to turn a pointer into an "output" -value. When calling a function, no input value would be given for -a parameter, but an output value would be returned. In the case of -multiple output values, they are returned in the form of a Ruby Array. - - int *OUTPUT - short *OUTPUT - long *OUTPUT - long long *OUTPUT - unsigned int *OUTPUT - unsigned short *OUTPUT - unsigned long *OUTPUT - unsigned long long *OUTPUT - unsigned char *OUTPUT - bool *OUTPUT - float *OUTPUT - double *OUTPUT - -For example, suppose you were trying to wrap the modf() function in the -C math library which splits x into integral and fractional parts (and -returns the integer part in one of its parameters).K: - - double modf(double x, double *ip); - -You could wrap it with SWIG as follows : - - %include typemaps.i - double modf(double x, double *OUTPUT); - -or you can use the %apply directive : - - %include typemaps.i - %apply double *OUTPUT { double *ip }; - double modf(double x, double *ip); - -The Ruby output of the function would be a Array containing both -output values. -*/ - -%include "fragments.i" - -%define OUTPUT_TYPEMAP(type, converter, convtype) -%typemap(in,numinputs=0) type *OUTPUT($*1_ltype temp), type &OUTPUT($*1_ltype temp) "$1 = &temp;"; -%typemap(argout, fragment="output_helper") type *OUTPUT, type &OUTPUT { - VALUE o = converter(convtype (*$1)); - $result = output_helper($result, o); -} -%enddef - -OUTPUT_TYPEMAP(int, INT2NUM, (int)); -OUTPUT_TYPEMAP(short, INT2NUM, (int)); -OUTPUT_TYPEMAP(long, INT2NUM, (long)); -OUTPUT_TYPEMAP(long long, LL2NUM, (long long)); -OUTPUT_TYPEMAP(unsigned int, UINT2NUM, (unsigned int)); -OUTPUT_TYPEMAP(unsigned short, UINT2NUM, (unsigned int)); -OUTPUT_TYPEMAP(unsigned long, UINT2NUM, (unsigned long)); -OUTPUT_TYPEMAP(unsigned long long, ULL2NUM, (unsigned long long)); -OUTPUT_TYPEMAP(unsigned char, UINT2NUM, (unsigned int)); -OUTPUT_TYPEMAP(signed char, INT2NUM, (int)); -OUTPUT_TYPEMAP(float, rb_float_new, (double)); -OUTPUT_TYPEMAP(double, rb_float_new, (double)); - -#undef OUTPUT_TYPEMAP - -%typemap(in,numinputs=0) bool *OUTPUT(bool temp), bool &OUTPUT(bool temp) "$1 = &temp;"; -%typemap(argout, fragment="output_helper") bool *OUTPUT, bool &OUTPUT { - VALUE o = (*$1) ? Qtrue : Qfalse; - $result = output_helper($result, o); -} - -// INOUT -// Mappings for an argument that is both an input and output -// parameter - -/* -The following methods can be applied to make a function parameter both -an input and output value. This combines the behavior of both the -"INPUT" and "OUTPUT" methods described earlier. Output values are -returned in the form of a Ruby array. - - int *INOUT - short *INOUT - long *INOUT - long long *INOUT - unsigned int *INOUT - unsigned short *INOUT - unsigned long *INOUT - unsigned long long *INOUT - unsigned char *INOUT - bool *INOUT - float *INOUT - double *INOUT - -For example, suppose you were trying to wrap the following function : - - void neg(double *x) { - *x = -(*x); - } - -You could wrap it with SWIG as follows : - - %include typemaps.i - void neg(double *INOUT); - -or you can use the %apply directive : - - %include typemaps.i - %apply double *INOUT { double *x }; - void neg(double *x); - -Unlike C, this mapping does not directly modify the input value (since -this makes no sense in Ruby). Rather, the modified input value shows -up as the return value of the function. Thus, to apply this function -to a Ruby variable you might do this : - - x = neg(x) - -Note : previous versions of SWIG used the symbol 'BOTH' to mark -input/output arguments. This is still supported, but will be slowly -phased out in future releases. - -*/ - -%typemap(in) int *INOUT = int *INPUT; -%typemap(in) short *INOUT = short *INPUT; -%typemap(in) long *INOUT = long *INPUT; -%typemap(in) long long *INOUT = long long *INPUT; -%typemap(in) unsigned *INOUT = unsigned *INPUT; -%typemap(in) unsigned short *INOUT = unsigned short *INPUT; -%typemap(in) unsigned long *INOUT = unsigned long *INPUT; -%typemap(in) unsigned long long *INOUT = unsigned long long *INPUT; -%typemap(in) unsigned char *INOUT = unsigned char *INPUT; -%typemap(in) signed char *INOUT = signed char *INPUT; -%typemap(in) bool *INOUT = bool *INPUT; -%typemap(in) float *INOUT = float *INPUT; -%typemap(in) double *INOUT = double *INPUT; - -%typemap(in) int &INOUT = int &INPUT; -%typemap(in) short &INOUT = short &INPUT; -%typemap(in) long &INOUT = long &INPUT; -%typemap(in) long long &INOUT = long long &INPUT; -%typemap(in) unsigned &INOUT = unsigned &INPUT; -%typemap(in) unsigned short &INOUT = unsigned short &INPUT; -%typemap(in) unsigned long &INOUT = unsigned long &INPUT; -%typemap(in) unsigned long long &INOUT = unsigned long long &INPUT; -%typemap(in) unsigned char &INOUT = unsigned char &INPUT; -%typemap(in) signed char &INOUT = signed char &INPUT; -%typemap(in) bool &INOUT = bool &INPUT; -%typemap(in) float &INOUT = float &INPUT; -%typemap(in) double &INOUT = double &INPUT; - -%typemap(argout) int *INOUT = int *OUTPUT; -%typemap(argout) short *INOUT = short *OUTPUT; -%typemap(argout) long *INOUT = long *OUTPUT; -%typemap(argout) long long *INOUT = long long *OUTPUT; -%typemap(argout) unsigned *INOUT = unsigned *OUTPUT; -%typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT; -%typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT; -%typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT; -%typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT; -%typemap(argout) signed char *INOUT = signed char *OUTPUT; -%typemap(argout) bool *INOUT = bool *OUTPUT; -%typemap(argout) float *INOUT = float *OUTPUT; -%typemap(argout) double *INOUT = double *OUTPUT; - -%typemap(argout) int &INOUT = int &OUTPUT; -%typemap(argout) short &INOUT = short &OUTPUT; -%typemap(argout) long &INOUT = long &OUTPUT; -%typemap(argout) long long &INOUT = long long &OUTPUT; -%typemap(argout) unsigned &INOUT = unsigned &OUTPUT; -%typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT; -%typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT; -%typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT; -%typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT; -%typemap(argout) signed char &INOUT = signed char &OUTPUT; -%typemap(argout) bool &INOUT = bool &OUTPUT; -%typemap(argout) float &INOUT = float &OUTPUT; -%typemap(argout) double &INOUT = double &OUTPUT; - -// -------------------------------------------------------------------- -// Special types -// -------------------------------------------------------------------- - -/* -The typemaps.i library also provides the following mappings : - -struct timeval * -time_t - - Ruby has builtin class Time. INPUT/OUTPUT typemap for timeval and - time_t is provided. - -int PROG_ARGC -char **PROG_ARGV - - Some C function receive argc and argv from C main function. - This typemap provides ignore typemap which pass Ruby ARGV contents - as argc and argv to C function. -*/ - - -// struct timeval * -%{ -#ifdef __cplusplus -extern "C" { -#endif -#ifdef HAVE_SYS_TIME_H -# include -struct timeval rb_time_timeval(VALUE); -#endif -#ifdef __cplusplus -} -#endif -%} - -%typemap(in) struct timeval *INPUT (struct timeval temp) -{ - if (NIL_P($input)) - $1 = NULL; - else { - temp = rb_time_timeval($input); - $1 = &temp; - } -} - -%typemap(in,numinputs=0) struct timeval *OUTPUT(struct timeval temp) -{ - $1 = &temp; -} - -%typemap(argout) struct timeval *OUTPUT -{ - $result = rb_time_new($1->tv_sec, $1->tv_usec); -} - -%typemap(out) struct timeval * -{ - $result = rb_time_new($1->tv_sec, $1->tv_usec); -} - -%typemap(out) struct timespec * -{ - $result = rb_time_new($1->tv_sec, $1->tv_nsec / 1000); -} - -// time_t -%typemap(in) time_t -{ - if (NIL_P($input)) - $1 = (time_t)-1; - else - $1 = NUM2LONG(rb_funcall($input, rb_intern("tv_sec"), 0)); -} - -%typemap(out) time_t -{ - $result = rb_time_new($1, 0); -} - -// argc and argv -%typemap(in,numinputs=0) int PROG_ARGC { - $1 = RARRAY(rb_argv)->len + 1; -} - -%typemap(in,numinputs=0) char **PROG_ARGV { - int i, n; - VALUE ary = rb_eval_string("[$0] + ARGV"); - n = RARRAY(ary)->len; - $1 = (char **)malloc(n + 1); - for (i = 0; i < n; i++) { - VALUE v = rb_obj_as_string(RARRAY(ary)->ptr[i]); - $1[i] = (char *)malloc(RSTRING(v)->len + 1); - strcpy($1[i], RSTRING(v)->ptr); - } -} - -%typemap(freearg) char **PROG_ARGV { - int i, n = RARRAY(rb_argv)->len + 1; - for (i = 0; i < n; i++) free($1[i]); - free($1); -} - -// FILE * -%{ -#ifdef __cplusplus -extern "C" { -#endif -#include "rubyio.h" -#ifdef __cplusplus -} -#endif -%} - -%typemap(in) FILE *READ { - OpenFile *of; - GetOpenFile($input, of); - rb_io_check_readable(of); - $1 = GetReadFile(of); - rb_read_check($1); -} - -%typemap(in) FILE *READ_NOCHECK { - OpenFile *of; - GetOpenFile($input, of); - rb_io_check_readable(of); - $1 = GetReadFile(of); -} - -%typemap(in) FILE *WRITE { - OpenFile *of; - GetOpenFile($input, of); - rb_io_check_writable(of); - $1 = GetWriteFile(of); -} - -/* Overloading information */ - -%typemap(typecheck) double *INOUT = double; -%typemap(typecheck) signed char *INOUT = signed char; -%typemap(typecheck) unsigned char *INOUT = unsigned char; -%typemap(typecheck) unsigned long *INOUT = unsigned long; -%typemap(typecheck) unsigned long long *INOUT = unsigned long long; -%typemap(typecheck) unsigned short *INOUT = unsigned short; -%typemap(typecheck) unsigned int *INOUT = unsigned int; -%typemap(typecheck) long *INOUT = long; -%typemap(typecheck) long long *INOUT = long long; -%typemap(typecheck) short *INOUT = short; -%typemap(typecheck) int *INOUT = int; -%typemap(typecheck) float *INOUT = float; - -%typemap(typecheck) double &INOUT = double; -%typemap(typecheck) signed char &INOUT = signed char; -%typemap(typecheck) unsigned char &INOUT = unsigned char; -%typemap(typecheck) unsigned long &INOUT = unsigned long; -%typemap(typecheck) unsigned long long &INOUT = unsigned long long; -%typemap(typecheck) unsigned short &INOUT = unsigned short; -%typemap(typecheck) unsigned int &INOUT = unsigned int; -%typemap(typecheck) long &INOUT = long; -%typemap(typecheck) long long &INOUT = long long; -%typemap(typecheck) short &INOUT = short; -%typemap(typecheck) int &INOUT = int; -%typemap(typecheck) float &INOUT = float; - +%include diff --git a/Lib/std/std_common.i b/Lib/std/std_common.i index a412f5e5e..a83861226 100644 --- a/Lib/std/std_common.i +++ b/Lib/std/std_common.i @@ -142,7 +142,7 @@ namespace swig { template <> struct traits_asval { typedef Type value_type; static int asval(PyObject *obj, value_type *val) { - return SWIG_AsVal(Type)(obj, val); + return SWIG_AsVal(Type)(obj, val) == SWIG_OK; } }; template <> struct traits_from { @@ -160,14 +160,12 @@ namespace swig { */ %define %typemap_traits(Code,Type...) - %typemap_ascheckfrom(SWIG_arg(Code), - SWIG_arg(swig::as), - SWIG_arg(swig::check), - SWIG_arg(swig::from), - SWIG_arg(SWIG_Traits_frag(Type)), - SWIG_arg(SWIG_Traits_frag(Type)), - SWIG_arg(SWIG_Traits_frag(Type)), - Type); + %typemap_asvalfrom(SWIG_arg(Code), + SWIG_arg(swig::asval), + SWIG_arg(swig::from), + SWIG_arg(SWIG_Traits_frag(Type)), + SWIG_arg(SWIG_Traits_frag(Type)), + Type); %enddef /* diff --git a/Lib/swig.swg b/Lib/swig.swg index c1d04aa09..759499726 100644 --- a/Lib/swig.swg +++ b/Lib/swig.swg @@ -282,7 +282,13 @@ namespace std { /* Macro for overload resolution */ -#define %typecheck(_x) %typemap(typecheck, precedence=_x) +%define %typecheck(_x, _opt...) +#if #_opt != "" +%typemap(typecheck, precedence=_x, _opt) +#else +%typemap(typecheck, precedence=_x) +#endif +%enddef /* Macros for precedence levels */ diff --git a/Lib/swigrun.swg b/Lib/swigrun.swg index f24b7697e..2092c2b64 100644 --- a/Lib/swigrun.swg +++ b/Lib/swigrun.swg @@ -36,6 +36,39 @@ # define SWIGRUNTIMEINLINE SWIGRUNTIME SWIGINLINE #endif +/* Generic buffer size */ +#ifndef SWIG_BUFFER_SIZE +# define SWIG_BUFFER_SIZE 1024 +#endif + +/* Flags for pointer conversions */ +#define SWIG_POINTER_DISOWN 0x1 + +/* Flags for new pointer objects */ +#define SWIG_POINTER_OWN 0x1 + +/* Alloc. memory flags */ +#define SWIG_OLDOBJ 1 +#define SWIG_NEWOBJ 2 + +/* Error flags */ +#define SWIG_OK 0 +#define SWIG_ERROR -1 + +#define SWIG_MemoryError 1 +#define SWIG_IOError 2 +#define SWIG_RuntimeError 3 +#define SWIG_IndexError 4 +#define SWIG_TypeError 5 +#define SWIG_DivisionByZero 6 +#define SWIG_OverflowError 7 +#define SWIG_SyntaxError 8 +#define SWIG_ValueError 9 +#define SWIG_SystemError 10 +#define SWIG_AttributeError 11 +#define SWIG_UnknownError 99 + + #include #ifdef __cplusplus @@ -74,7 +107,6 @@ typedef struct swig_module_info { void *clientdata; /* Language specific module data */ } swig_module_info; - /* Compare two type names skipping the space characters, therefore "char*" == "char *" and "Class" == "Class", etc. @@ -208,6 +240,7 @@ SWIG_TypePrettyName(const swig_type_info *type) { type, separated by vertical-bar characters. We choose to print the last name, as it is often (?) the most specific. */ + if (!type) return NULL; if (type->str != NULL) { const char *last_name = type->str; const char *s; @@ -318,7 +351,6 @@ SWIG_TypeQueryModule(swig_module_info *start, return 0; } - /* Pack binary data into a string */ diff --git a/Lib/tcl/cdata.i b/Lib/tcl/cdata.i new file mode 100644 index 000000000..367965990 --- /dev/null +++ b/Lib/tcl/cdata.i @@ -0,0 +1 @@ +%include diff --git a/Lib/tcl/cstring.i b/Lib/tcl/cstring.i index b0d3bd385..2427366a4 100644 --- a/Lib/tcl/cstring.i +++ b/Lib/tcl/cstring.i @@ -10,70 +10,45 @@ * some way. */ -/* %cstring_input_binary(TYPEMAP, SIZE) +/* + * %cstring_input_binary(TYPEMAP, SIZE) * * Macro makes a function accept binary string data along with * a size. */ -%define %cstring_input_binary(TYPEMAP, SIZE) -%apply (char *STRING, int LENGTH) { (TYPEMAP, SIZE) }; -%enddef - /* * %cstring_bounded_output(TYPEMAP, MAX) * * This macro is used to return a NULL-terminated output string of * some maximum length. For example: * - * %cstring_bounded_output(char *outx, 512); - * void foo(char *outx) { + * %cstring_bounded_output(Char *outx, 512); + * void foo(Char *outx) { * sprintf(outx,"blah blah\n"); * } * */ -%define %cstring_bounded_output(TYPEMAP,MAX) -%typemap(ignore) TYPEMAP(char temp[MAX+1]) { - $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper") TYPEMAP { - Tcl_Obj *o; - $1[MAX] = 0; - o = Tcl_NewStringObj($1,-1); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), o); -} -%enddef - /* * %cstring_chunk_output(TYPEMAP, SIZE) * * This macro is used to return a chunk of binary string data. * Embedded NULLs are okay. For example: * - * %cstring_chunk_output(char *outx, 512); - * void foo(char *outx) { + * %cstring_chunk_output(Char *outx, 512); + * void foo(Char *outx) { * memmove(outx, somedata, 512); * } * */ -%define %cstring_chunk_output(TYPEMAP,SIZE) -%typemap(ignore) TYPEMAP(char temp[SIZE]) { - $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper") TYPEMAP { - Tcl_Obj *o = Tcl_NewStringObj($1,SIZE); - Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp),o); -} -%enddef - /* * %cstring_bounded_mutable(TYPEMAP, SIZE) * * This macro is used to wrap a string that's going to mutate. * - * %cstring_bounded_mutable(char *in, 512); + * %cstring_bounded_mutable(Char *in, 512); * void foo(in *x) { * while (*x) { * *x = toupper(*x); @@ -83,28 +58,13 @@ * */ - -%define %cstring_bounded_mutable(TYPEMAP,MAX) -%typemap(in) TYPEMAP(char temp[MAX+1]) { - char *t = Tcl_GetStringFromObj($input,NULL); - strncpy(temp,t,MAX); - $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper") TYPEMAP { - Tcl_Obj *o; - $1[MAX] = 0; - o = Tcl_NewStringObj($1,-1); - Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp),o); -} -%enddef - /* * %cstring_mutable(TYPEMAP [, expansion]) * * This macro is used to wrap a string that will mutate in place. * It may change size up to a user-defined expansion. * - * %cstring_mutable(char *in); + * %cstring_mutable(Char *in); * void foo(in *x) { * while (*x) { * *x = toupper(*x); @@ -114,177 +74,64 @@ * */ -%define %cstring_mutable(TYPEMAP,...) -%typemap(in) TYPEMAP { - int n; - char *t = Tcl_GetStringFromObj($input,&n); - $1 = ($1_ltype) t; - -#if #__VA_ARGS__ == "" -#ifdef __cplusplus - $1 = ($1_ltype) new char[n+1]; -#else - $1 = ($1_ltype) malloc(n+1); -#endif -#else -#ifdef __cplusplus - $1 = ($1_ltype) new char[n+1+__VA_ARGS__]; -#else - $1 = ($1_ltype) malloc(n+1+__VA_ARGS__); -#endif -#endif - memmove($1,t,n); - $1[n] = 0; -} - -%typemap(argout,fragment="t_output_helper") TYPEMAP { - Tcl_Obj *o; - o = Tcl_NewStringObj($1,-1); - Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), o); -#ifdef __cplusplus - delete[] $1; -#else - free($1); -#endif -} -%enddef - /* * %cstring_output_maxsize(TYPEMAP, SIZE) * * This macro returns data in a string of some user-defined size. * - * %cstring_output_maxsize(char *outx, int max) { - * void foo(char *outx, int max) { + * %cstring_output_maxsize(Char *outx, int max) { + * void foo(Char *outx, int max) { * sprintf(outx,"blah blah\n"); * } */ -%define %cstring_output_maxsize(TYPEMAP, SIZE) -%typemap(in) (TYPEMAP, SIZE) { - long temp; - if (Tcl_GetLongFromObj(interp,$input,&temp) != TCL_OK) { - SWIG_fail; - } - $2 = ($2_ltype) temp; -#ifdef __cplusplus - $1 = ($1_ltype) new char[$2+1]; -#else - $1 = ($1_ltype) malloc($2+1); -#endif -} -%typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) { - Tcl_Obj *o; - o = Tcl_NewStringObj($1,-1); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o); -#ifdef __cplusplus - delete [] $1; -#else - free($1); -#endif -} -%enddef - /* * %cstring_output_withsize(TYPEMAP, SIZE) * - * This macro is used to return character data along with a size + * This macro is used to return Character data along with a size * parameter. * - * %cstring_output_maxsize(char *outx, int *max) { - * void foo(char *outx, int *max) { + * %cstring_output_maxsize(Char *outx, int *max) { + * void foo(Char *outx, int *max) { * sprintf(outx,"blah blah\n"); * *max = strlen(outx); * } */ - -%define %cstring_output_withsize(TYPEMAP, SIZE) -%typemap(in) (TYPEMAP, SIZE) { - long n; - if (Tcl_GetLongFromObj(interp,$input,&n) != TCL_OK) { - SWIG_fail; - } -#ifdef __cplusplus - $1 = ($1_ltype) new char[n+1]; - $2 = ($2_ltype) new $*1_ltype; -#else - $1 = ($1_ltype) malloc(n+1); - $2 = ($2_ltype) malloc(sizeof($*1_ltype)); -#endif - *$2 = n; -} -%typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) { - Tcl_Obj *o; - o = Tcl_NewStringObj($1,*$2); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), o); -#ifdef __cplusplus - delete [] $1; - delete $2; -#else - free($1); - free($2); -#endif -} -%enddef - + /* * %cstring_output_allocate(TYPEMAP, RELEASE) * - * This macro is used to return character data that was + * This macro is used to return Character data that was * allocated with new or malloc. * - * %cstring_output_allocated(char **outx, free($1)); - * void foo(char **outx) { - * *outx = (char *) malloc(512); + * %cstring_output_allocated(Char **outx, free($1)); + * void foo(Char **outx) { + * *outx = (Char *) malloc(512); * sprintf(outx,"blah blah\n"); * } */ -%define %cstring_output_allocate(TYPEMAP, RELEASE) -%typemap(ignore) TYPEMAP($*1_ltype temp = 0) { - $1 = &temp; -} - -%typemap(argout,fragment="t_output_helper") TYPEMAP { - if (*$1) { - Tcl_Obj *o = Tcl_NewStringObj(*$1,-1); - RELEASE; - Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), o); - } -} -%enddef - /* * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) * - * This macro is used to return character data that was + * This macro is used to return Character data that was * allocated with new or malloc. * - * %cstring_output_allocated(char **outx, int *sz, free($1)); - * void foo(char **outx, int *sz) { - * *outx = (char *) malloc(512); + * %cstring_output_allocated(Char **outx, int *sz, free($1)); + * void foo(Char **outx, int *sz) { + * *outx = (Char *) malloc(512); * sprintf(outx,"blah blah\n"); * *sz = strlen(outx); * } */ -%define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) -%typemap(ignore) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) { - $1 = &temp; - $2 = &tempn; -} - -%typemap(argout,fragment="t_output_helper")(TYPEMAP,SIZE) { - if (*$1) { - Tcl_Obj *o = Tcl_NewStringObj(*$1,*$2); - RELEASE; - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp), o); - } -} -%enddef - - - - +%include +%include +%typemap_cstrings(%cstring, + char, + SWIG_AsCharPtr, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtr, + SWIG_FromCharPtrAndSize); diff --git a/Lib/tcl/cwstring.i b/Lib/tcl/cwstring.i new file mode 100644 index 000000000..c46791b6b --- /dev/null +++ b/Lib/tcl/cwstring.i @@ -0,0 +1,137 @@ +/* + * cstring.i + * $Header$ + * + + * This file provides typemaps and macros for dealing with various forms + * of C character string handling. The primary use of this module + * is in returning character data that has been allocated or changed in + * some way. + */ + +/* + * %cwstring_input_binary(TYPEMAP, SIZE) + * + * Macro makes a function accept binary string data along with + * a size. + */ + +/* + * %cwstring_bounded_output(TYPEMAP, MAX) + * + * This macro is used to return a NULL-terminated output string of + * some maximum length. For example: + * + * %cwstring_bounded_output(wchar_t *outx, 512); + * void foo(wchar_t *outx) { + * sprintf(outx,"blah blah\n"); + * } + * + */ + +/* + * %cwstring_chunk_output(TYPEMAP, SIZE) + * + * This macro is used to return a chunk of binary string data. + * Embedded NULLs are okay. For example: + * + * %cwstring_chunk_output(wchar_t *outx, 512); + * void foo(wchar_t *outx) { + * memmove(outx, somedata, 512); + * } + * + */ + +/* + * %cwstring_bounded_mutable(TYPEMAP, SIZE) + * + * This macro is used to wrap a string that's going to mutate. + * + * %cwstring_bounded_mutable(wchar_t *in, 512); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +/* + * %cwstring_mutable(TYPEMAP [, expansion]) + * + * This macro is used to wrap a string that will mutate in place. + * It may change size up to a user-defined expansion. + * + * %cwstring_mutable(wchar_t *in); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +/* + * %cwstring_output_maxsize(TYPEMAP, SIZE) + * + * This macro returns data in a string of some user-defined size. + * + * %cwstring_output_maxsize(wchar_t *outx, int max) { + * void foo(wchar_t *outx, int max) { + * sprintf(outx,"blah blah\n"); + * } + */ + +/* + * %cwstring_output_withsize(TYPEMAP, SIZE) + * + * This macro is used to return wchar_tacter data along with a size + * parameter. + * + * %cwstring_output_maxsize(wchar_t *outx, int *max) { + * void foo(wchar_t *outx, int *max) { + * sprintf(outx,"blah blah\n"); + * *max = strlen(outx); + * } + */ + +/* + * %cwstring_output_allocate(TYPEMAP, RELEASE) + * + * This macro is used to return wchar_tacter data that was + * allocated with new or malloc. + * + * %cwstring_output_allocated(wchar_t **outx, free($1)); + * void foo(wchar_t **outx) { + * *outx = (wchar_t *) malloc(512); + * sprintf(outx,"blah blah\n"); + * } + */ + +/* + * %cwstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) + * + * This macro is used to return wchar_tacter data that was + * allocated with new or malloc. + * + * %cwstring_output_allocated(wchar_t **outx, int *sz, free($1)); + * void foo(wchar_t **outx, int *sz) { + * *outx = (wchar_t *) malloc(512); + * sprintf(outx,"blah blah\n"); + * *sz = strlen(outx); + * } + */ + + +%include +%include + +%typemap_cstrings(%cwstring, + wchar_t, + SWIG_AsWCharPtr, + SWIG_AsWCharPtrAndSize, + SWIG_FromWCharPtr, + SWIG_FromWCharArray); + diff --git a/Lib/tcl/std_string.i b/Lib/tcl/std_string.i index ad61399f3..9a850fa12 100644 --- a/Lib/tcl/std_string.i +++ b/Lib/tcl/std_string.i @@ -1,56 +1,37 @@ // -// SWIG typemaps for std::string -// Luigi Ballabio and Manu ??? -// Apr 26, 2002 +// std::string // -// Tcl implementation - -// ------------------------------------------------------------------------ -// std::string is typemapped by value -// This can prevent exporting methods which return a string -// in order for the user to modify it. -// However, I think I'll wait until someone asks for it... -// ------------------------------------------------------------------------ - -%include exception.i +#ifndef SWIG_STD_BASIC_STRING +#define SWIG_STD_STRING %{ #include %} - -namespace std { - - class string; - - /* Overloading check */ - %typemap(typecheck) string = char *; - %typemap(typecheck) const string & = char *; - - %typemap(in) string { - $1 = std::string(Tcl_GetStringFromObj($input,NULL)); - } - - %typemap(in) const string & (std::string temp) { - temp = std::string(Tcl_GetStringFromObj($input,NULL)); - $1 = &temp; - } - - %typemap(out) string { - Tcl_SetStringObj($result,(char*)$1.c_str(),$1.length()); - } - - %typemap(out) const string & { - Tcl_SetStringObj($result,(char*)$1->c_str(),$1->length()); - } - - %typemap(throws) string { - Tcl_SetObjResult(interp, Tcl_NewStringObj((char*) $1.c_str(), -1)); - SWIG_fail; - } - - %typemap(throws) const string & { - Tcl_SetObjResult(interp, Tcl_NewStringObj((char*) $1.c_str(), -1)); - SWIG_fail; - } + +namespace std +{ + class string; } + +%include +%include + +%fragment("Tcl_std_string_asptr","header",fragment="SWIG_AsCharPtrAndSize") { +%tcl_asptr_decl(std::string) +} +%fragment("Tcl_std_string_asval","header",fragment="Tcl_std_string_asptr") { +%tcl_asptr_decl(std::string) +} + +%std_string_asptr_frag(std::string, char, SWIG_AsCharPtrAndSize, "Tcl_std_string_asptr") +%std_string_asval_frag(std::string, "Tcl_std_string_asval") +%std_string_from(std::string, SWIG_FromCharPtrAndSize) + +%typemap_asptrfromn(SWIG_CCode(STRING), std::string); + +#else + +%include + +#endif diff --git a/Lib/tcl/std_wstring.i b/Lib/tcl/std_wstring.i new file mode 100644 index 000000000..39fa92c8b --- /dev/null +++ b/Lib/tcl/std_wstring.i @@ -0,0 +1,37 @@ +// +// std::wstring +// + +#ifndef SWIG_STD_BASIC_STRING +#define SWIG_STD_WSTRING + +%{ +#include +%} + +namespace std +{ + class wstring; +} + +%include +%include + +%fragment("Tcl_std_wstring_asptr","header",fragment="SWIG_AsCharPtrAndSize") { +%tcl_asptr_decl(std::wstring) +} +%fragment("Tcl_std_wstring_asval","header",fragment="Tcl_std_wstring_asptr") { +%tcl_asptr_decl(std::wstring) +} + +%std_string_asptr_frag(std::wstring, wchar_t, SWIG_AsCharPtrAndSize, "Tcl_std_wstring_asptr") +%std_string_asval_frag(std::wstring, "Tcl_std_wstring_asval") +%std_string_from(std::wstring, SWIG_FromCharPtrAndSize) + +%typemap_asptrfromn(SWIG_CCode(UNISTRING), std::wstring); + +#else + +%include + +#endif diff --git a/Lib/tcl/tcl8.swg b/Lib/tcl/tcl8.swg index 89320eafa..4b076f210 100644 --- a/Lib/tcl/tcl8.swg +++ b/Lib/tcl/tcl8.swg @@ -1,730 +1,53 @@ /* ----------------------------------------------------------------------------- - * tcl8.swg + * tcl.swg * - * Tcl8 configuration module. + * Tcl configuration module. * ----------------------------------------------------------------------------- */ -%runtime "swigrun.swg" // Common C API type-checking code -%runtime "swigtcl8.swg" - /* ----------------------------------------------------------------------------- - * --- standard typemaps --- + * The runtime part + * ----------------------------------------------------------------------------- */ +%include + +/* ----------------------------------------------------------------------------- + * Special user directives + * ----------------------------------------------------------------------------- */ +%include + +/* ----------------------------------------------------------------------------- + * Inner macros + * ----------------------------------------------------------------------------- */ +%include + +/* ----------------------------------------------------------------------------- + * Error manipulation * ----------------------------------------------------------------------------- */ -/* Input arguments */ - -/* For primitive types, the Tcl module uses a special function - - SWIG_GetArgs(Tcl_Interp *, int objc, Tcl_Obj *CONST objv[], const char *fmt, ...) - - The fmt field contains special conversion characters i,h,l,b,f,d,c,p, and o - that are used to marshal different types. The parse codes below correspond - to these special codes */ - -%typemap(in,parse="i") int ""; -%typemap(in,parse="h") short, unsigned short ""; -%typemap(in,parse="l") long ""; -%typemap(in,parse="b") signed char, unsigned char ""; -%typemap(in,parse="f") float ""; -%typemap(in,parse="d") double ""; -%typemap(in,parse="c") char ""; -%typemap(in,parse="s") char *, char [ANY] ""; - -/* Pointers */ -%typemap(in) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] - "if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | $disown) != TCL_OK)) SWIG_fail;"; - -%typemap(in) void * - "if ((SWIG_ConvertPtr($input, (void **) &$1, 0,SWIG_POINTER_EXCEPTION | $disown) != TCL_OK)) SWIG_fail;"; - -/* For bools, we first convert to an integer and then to a bool. There - is no guarantee that a bool is the same size as an int so we have to do this */ - -%typemap(in) bool (int tempb) "if (Tcl_GetIntFromObj(interp,$input,&tempb) == TCL_ERROR) SWIG_fail; - $1 = tempb ? true : false;"; - -%typemap(in) unsigned int "$1 = ($1_ltype) strtoul(Tcl_GetStringFromObj($input,NULL), 0, 0);"; -%typemap(in) unsigned long "$1 = ($1_ltype) strtoul(Tcl_GetStringFromObj($input,NULL), 0, 0);"; - -/* These will pass an integer as an unsigned long. However, the implementation is crippled due - to limited precision in Tcl */ - -%typemap(in) long long "$1 = ($1_ltype) strtoll(Tcl_GetStringFromObj($input,NULL),0,0);"; -%typemap(in) unsigned long long "$1 = ($1_ltype) strtoull(Tcl_GetStringFromObj($input,NULL), 0, 0);"; - -/* Enum parsing. Note: internally SWIG converts enums to/from integers so it's okay to use - the "i" parse code here */ - -%typemap(in,parse="i") enum SWIGTYPE ""; - -/* Unknown type. We convert from a pointer */ -%typemap(in) SWIGTYPE ($&1_ltype argp) - "if ((SWIG_ConvertPtr($input, (void **) &argp, $&1_descriptor,SWIG_POINTER_EXCEPTION ) != TCL_OK)) SWIG_fail; - $1 = *argp; "; - -/* Member pointer */ -%typemap(in) SWIGTYPE (CLASS::*) "if ((SWIG_ConvertPacked($input, (void *) &$1, sizeof($1_type), $1_descriptor, SWIG_POINTER_EXCEPTION)) != TCL_OK) SWIG_fail;"; - -/* Special constant variations. These typemaps can be used to parse objects that are both constants - or values. A Hash table lookup will occur. */ - -%typemap(in,parse="I") int CONSTANT, unsigned int CONSTANT ""; -%typemap(in,parse="H") short CONSTANT, unsigned short CONSTANT ""; -%typemap(in,parse="L") long CONSTANT, unsigned long CONSTANT ""; -%typemap(in,parse="B") signed char CONSTANT, unsigned char CONSTANT ""; -%typemap(in,parse="F") float CONSTANT ""; -%typemap(in,parse="D") double CONSTANT ""; -%typemap(in,parse="C") char CONSTANT ""; -%typemap(in,parse="S") char * CONSTANT ""; -%typemap(in,parse="P") SWIGTYPE *CONSTANT, SWIGTYPE &CONSTANT, SWIGTYPE CONSTANT [] ""; -%typemap(in,parse="I") enum SWIGTYPE CONSTANT ""; - -/* Constant references. Passed by value */ -/* Const primitive references. Passed by value */ - -%typemap(in) const int & ($*1_ltype temp), - const short & ($*1_ltype temp), - const long & ($*1_ltype temp), - const unsigned short & ($*1_ltype temp), - const signed char & ($*1_ltype temp), - const unsigned char & ($*1_ltype temp), - const enum SWIGTYPE & ($*1_ltype temp) -{ - long ltemp; - if (Tcl_GetLongFromObj(interp, $input, <emp) != TCL_OK) { - SWIG_fail; - } - temp = ($*1_ltype) ltemp; - $1 = &temp; -} - -%typemap(in) const unsigned int & ($*1_ltype temp) - "temp = ($*1_ltype) strtoul(Tcl_GetStringFromObj($input,NULL),0,0); - $1 = &temp;"; - -%typemap(in) const unsigned long & ($*1_ltype temp) - "temp = ($*1_ltype) strtoul(Tcl_GetStringFromObj($input,NULL),0,0); - $1 = &temp;"; - -%typemap(in) const bool & (bool temp) -{ - long ltemp; - if (Tcl_GetLongFromObj(interp, $input, <emp) != TCL_OK) { - SWIG_fail; - } - temp = ltemp ? true : false; - $1 = &temp; -} - - -%typemap(in) const float & (float temp), - const double & (double temp) -{ - double dtemp; - if (Tcl_GetDoubleFromObj(interp, $input, &dtemp) != TCL_OK) { - SWIG_fail; - } - temp = ($*1_ltype) dtemp; - $1 = &temp; -} - -%typemap(in) const long long & ($*1_ltype temp) - "temp = ($*1_ltype) strtoll(Tcl_GetStringFromObj($input,NULL),0,0); - $1 = &temp;"; - -%typemap(in) const unsigned long long & ($*1_ltype temp) - "temp = ($*1_ltype) strtoull(Tcl_GetStringFromObj($input,NULL),0,0); - $1 = &temp;"; - -%typemap(in) const char &(char temp) { - char *stemp = Tcl_GetStringFromObj($input,NULL); - temp = *stemp; - $1 = &temp; -} - -/* Output values */ - -%typemap(out) bool, int, short, unsigned short, signed char, unsigned char, enum SWIGTYPE - "Tcl_SetObjResult(interp,Tcl_NewIntObj((long) $1));"; - -%typemap(out) long - "Tcl_SetObjResult(interp,Tcl_NewLongObj((long) $1));"; - -%typemap(out) unsigned int, unsigned long { - char temp[256]; - sprintf(temp,"%lu", (unsigned long) $1); - Tcl_SetObjResult(interp,Tcl_NewStringObj(temp,-1)); -} - - -%typemap(out) long long { - char temp[256]; - sprintf(temp,"%lld", (long long) $1); - Tcl_SetObjResult(interp,Tcl_NewStringObj(temp,-1)); -} - -%typemap(out) unsigned long long { - char temp[256]; - sprintf(temp,"%llu", (unsigned long long) $1); - Tcl_SetObjResult(interp,Tcl_NewStringObj(temp,-1)); -} - -%typemap(out) char - "Tcl_SetObjResult(interp,Tcl_NewStringObj(&$1,1));"; - -%typemap(out) float, double - "Tcl_SetObjResult(interp,Tcl_NewDoubleObj((double) $1));"; - -%typemap(out) char * - "Tcl_SetObjResult(interp,Tcl_NewStringObj($1,-1));"; - -%typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] - "Tcl_SetObjResult(interp,SWIG_NewPointerObj((void *) $1, $1_descriptor, $owner));"; - -%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { - swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor,(void **) &$1); - Tcl_SetObjResult(interp,SWIG_NewPointerObj((void *) $1, ty, $owner)); -} - -%typemap(out) SWIGTYPE *INSTANCE, SWIGTYPE &INSTANCE, SWIGTYPE INSTANCE[] - "Tcl_SetObjResult(interp,SWIG_NewInstanceObj((void *) $1, $1_descriptor, $owner));"; - -%typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] - "Tcl_SetObjResult(interp,SWIG_NewInstanceObj((void *) $1, $1_descriptor, $owner));"; - -%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { - swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor,(void **) &$1); - Tcl_SetObjResult(interp,SWIG_NewInstanceObj((void *) $1, ty, $owner)); -} - -%typemap(out) SWIGTYPE (CLASS::*) - "Tcl_SetObjResult(interp, SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor, $owner));"; - -%typemap(out) void ""; - -/* Primitive types--return by value */ -%typemap(out) SWIGTYPE NOINSTANCE -#ifdef __cplusplus -{ - $&1_ltype resultptr; - resultptr = new $1_ltype(($1_ltype &) $1); - Tcl_SetObjResult(interp,SWIG_NewPointerObj((void*) resultptr, $&1_descriptor, $owner)); -} -#else -{ - $&1_ltype resultptr; - resultptr = ($&1_ltype) malloc(sizeof($1_type)); - memmove(resultptr, &$1, sizeof($1_type)); - Tcl_SetObjResult(interp,SWIG_NewPointerObj((void*) resultptr, $&1_descriptor, $owner)); -} -#endif - -/* Primitive types--return by value */ -%typemap(out) SWIGTYPE INSTANCE -#ifdef __cplusplus -{ - $&1_ltype resultptr; - resultptr = new $1_ltype(($1_ltype &) $1); - Tcl_SetObjResult(interp,SWIG_NewInstanceObj((void*) resultptr, $&1_descriptor,1)); -} -#else -{ - $&1_ltype resultptr; - resultptr = ($&1_ltype) malloc(sizeof($1_type)); - memmove(resultptr, &$1, sizeof($1_type)); - Tcl_SetObjResult(interp,SWIG_NewInstanceObj((void*) resultptr, $&1_descriptor,1)); -} -#endif - -%typemap(out) SWIGTYPE = SWIGTYPE INSTANCE; - -/* Special typemap for character array returns */ -%typemap(out) char [ANY] "Tcl_SetObjResult(interp,Tcl_NewStringObj($1,-1));" - - -/* Primitive references */ - -%typemap(out) const int &, - const short &, const unsigned short &, - const signed char &, const unsigned char &, - const bool &, - const enum SWIGTYPE & - "Tcl_SetObjResult(interp,Tcl_NewIntObj((long) *($1)));"; - -%typemap(out) const long & - "Tcl_SetObjResult(interp,Tcl_NewLongObj((long) *($1)));"; - -%typemap(out) const unsigned int &, const unsigned long & -{ - char temp[256]; - sprintf(temp,"%lu", (unsigned long)*($1)); - Tcl_SetObjResult(interp,Tcl_NewStringObj(temp,-1)); -} - - -%typemap(out) const float &, const double & - "Tcl_SetObjResult(interp,Tcl_NewDoubleObj((double) *($1)));"; - -%typemap(out) const long long & { - char temp[256]; - sprintf(temp,"%lld", (long long)*($1)); - Tcl_SetObjResult(interp,Tcl_NewStringObj(temp,-1)); -} - -%typemap(out) const unsigned long long & -{ - char temp[256]; - sprintf(temp,"%llu", (unsigned long long)*($1)); - Tcl_SetObjResult(interp,Tcl_NewStringObj(temp,-1)); -} - -%typemap(out) const char & - "Tcl_SetObjResult(interp,Tcl_NewStringObj($1,1));"; - - -/* --- Variable output --- */ - -%typemap(varout) int, short, unsigned short, signed char, unsigned char, bool, enum SWIGTYPE - "$result = Tcl_NewIntObj((long) $1);"; - -%typemap(varout) long - "$result = Tcl_NewLongObj((long) $1);"; - -%typemap(varout) unsigned int, unsigned long { - char temp[256]; - sprintf(temp,"%lu", (unsigned long)$1); - $result = Tcl_NewStringObj(temp,-1); -} - -%typemap(varout) long long { - char temp[256]; - sprintf(temp,"%lld", (long long)$1); - $result = Tcl_NewStringObj(temp,-1); -} - -%typemap(varout) unsigned long long { - char temp[256]; - sprintf(temp,"%llu", (unsigned long long)$1); - $result = Tcl_NewStringObj(temp,-1); -} - -%typemap(varout) double,float "$result = Tcl_NewDoubleObj((double) $1);"; -%typemap(varout) char * "$result = Tcl_NewStringObj((char*) $1,-1);"; -%typemap(varout) char [ANY] "$result = Tcl_NewStringObj((char *) $1,-1);"; -%typemap(varout) char "$result = Tcl_NewStringObj(&$1,1);"; -%typemap(varout) SWIGTYPE *, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 0);"; -%typemap(varout) SWIGTYPE & "$result = SWIG_NewPointerObj((void *) &$1, $1_descriptor, 0);"; - -%typemap(varout) SWIGTYPE *INSTANCE, SWIGTYPE INSTANCE[] - "$result = SWIG_NewInstanceObj((void *) $1, $1_descriptor, 0);"; - -%typemap(varout) SWIGTYPE &INSTANCE - "$result = SWIG_NewInstanceObj((void *) &$1, $1_descriptor, 0);"; - -%typemap(varout) SWIGTYPE INSTANCE "$result = SWIG_NewInstanceObj((void *) &$1, $&1_descriptor, 0);"; -%typemap(varout) SWIGTYPE "$result = SWIG_NewInstanceObj((void *) &$1, $&1_descriptor, 0);"; -%typemap(varout) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor, 0);"; - -/* -- Variable input --- */ - -%typemap(varin) int, short, unsigned short, long, signed char, unsigned char -{ - long temp; - if (Tcl_GetLongFromObj(interp, $input, &temp) != TCL_OK) { - return (char*) "Type error. expected an integer"; - } - $1 = ($1_type) temp; -} - -%typemap(varin) unsigned int, unsigned long "$1 = ($1_ltype) strtoul(Tcl_GetStringFromObj($input,NULL),0,0);"; - -%typemap(varin) enum SWIGTYPE -{ - int temp; - if (Tcl_GetIntFromObj(interp, $input, &temp) != TCL_OK) { - return (char*) "Type error. expected an integer"; - } - if (sizeof(int) != sizeof($1)) { - return (char*) "enum variable '$name' can not be set."; - } - *(int *)(void *)&($1) = temp; -} - -%typemap(varin) bool -{ - long temp; - if (Tcl_GetLongFromObj(interp, $input, &temp) != TCL_OK) { - return (char*) "Type error. expected an integer"; - } - $1 = temp ? true : false; -} - -%typemap(varin) long long "$1 = ($1_ltype) strtoll(Tcl_GetStringFromObj($input,NULL),0,0);"; -%typemap(varin) unsigned long long "$1 = ($1_ltype) strtoull(Tcl_GetStringFromObj($input,NULL),0,0);"; - -%typemap(varin) double, float { - double temp; - if (Tcl_GetDoubleFromObj(interp, $input, &temp) != TCL_OK) { - return (char*) "Type error. expected a double."; - } - $1 = ($1_type) temp; -} - -%typemap(varin) char * -#ifdef __cplusplus -{ - char *temp = Tcl_GetStringFromObj($input,NULL); - if ($1) delete [] $1; - $1 = ($1_type) new char[strlen(temp)+1]; - strcpy((char *) $1,temp); -} -#else -{ - char *temp = Tcl_GetStringFromObj($input,NULL); - if ($1) free((char*)$1); - $1 = ($1_type) malloc(strlen(temp)+1); - strcpy((char *) $1,temp); -} -#endif - -%typemap(varin,warning="451:Setting const char * variable may leak memory") const char * -#ifdef __cplusplus -{ - char *temp = Tcl_GetStringFromObj($input,NULL); - $1 = ($1_type) new char[strlen(temp)+1]; - strcpy((char *) $1,temp); -} -#else -{ - char *temp = Tcl_GetStringFromObj($input,NULL); - $1 = ($1_type) malloc(strlen(temp)+1); - strcpy((char *) $1,temp); -} -#endif - -%typemap(varin) char [ANY] { - char *temp = Tcl_GetStringFromObj($input,NULL); - strncpy((char*)$1,temp,$1_dim0); -} - -%typemap(varin,warning="462: Unable to set variable of type char []") char [] { - return (char*)"Variable $symname is read-only."; -} - -%typemap(varin) char -{ - char *temp = Tcl_GetStringFromObj($input,NULL); - $1 = *temp; -} - -%typemap(varin) SWIGTYPE * { - if (SWIG_ConvertPtr($input,(void **)&$1,$1_descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN) != TCL_OK) { - return (char*)"Type error. Expected $1_ltype"; - } -} - -%typemap(varin,warning="462: Unable to set dimensionless array variable") SWIGTYPE [] { - return (char*)"Variable $symname is read-only."; -} - -%typemap(varin) void * { - void *temp; - if (SWIG_ConvertPtr($input,&temp,0, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN) != TCL_OK) { - return (char*)"Type error. Expected $1_ltype"; - } - $1 = ($1_type) temp; -} - -%typemap(varin) SWIGTYPE & { - void *temp; - if (SWIG_ConvertPtr($input,&temp,$1_descriptor, SWIG_POINTER_EXCEPTION) != TCL_OK) { - return (char*)"Type error. Expected $1_ltype"; - } - $1 = *($1_ltype) temp; -} - -%typemap(varin) SWIGTYPE { - void *temp; - if (SWIG_ConvertPtr($input,&temp,$&1_descriptor, SWIG_POINTER_EXCEPTION) != TCL_OK) { - return (char*)"Type error. Expected $&1_ltype"; - } - $1 = *(($&1_type) temp); -} - -%typemap(varin) SWIGTYPE [ANY] { - void *temp; - if (SWIG_ConvertPtr($input,&temp,$1_descriptor, SWIG_POINTER_EXCEPTION) != TCL_OK) { - return (char *)"Type error. Expected $1_ltype"; - } - memmove((void *) $1,temp,$1_size*sizeof($1_basetype)); -} - -%typemap(varin) SWIGTYPE (CLASS::*) { - char temp[sizeof($1_type)]; - if (SWIG_ConvertPacked($input, temp, sizeof($1_type), $1_descriptor, SWIG_POINTER_EXCEPTION) != TCL_OK) { - return (char *) "Type error. Expected $1_ltype"; - } - memmove((void *) &$1, temp, sizeof($1_type)); -} - -/* --- Constants --- */ - -%typemap(consttab) int, short, unsigned short, unsigned char, signed char, bool, enum SWIGTYPE - { SWIG_TCL_INT, (char *)"$nsname", (long) $value, 0, 0, 0} - -%typemap(consttab) unsigned int, unsigned long - { SWIG_TCL_STRING, (char *) "$nsname", 0, 0, (void *)"$value", 0} - -%typemap(consttab) float, double - { SWIG_TCL_FLOAT, (char*)"$nsname", 0, (double) $value, 0, 0} - -%typemap(consttab) char, char * - { SWIG_TCL_STRING, (char*)"$nsname", 0, 0, (void *)"$value", 0} - -%typemap(consttab) long long, unsigned long long - { SWIG_TCL_STRING, (char *) "$nsname", 0, 0, (void *)"$value", 0} - -%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] - { SWIG_TCL_POINTER, (char*)"$nsname", 0, 0, (void *)$value, &$1_descriptor} - -%typemap(consttab) SWIGTYPE (CLASS::*) - { SWIG_TCL_BINARY, (char *)"$nsname", sizeof($type), 0, (void *)&$value, &$1_descriptor} - +%include +/* ----------------------------------------------------------------------------- + * Look for user fragments file. If not found, include empty system one. + * ----------------------------------------------------------------------------- */ +%include "tclfragments.swg" +/* ----------------------------------------------------------------------------- + * Typemap specializations + * ----------------------------------------------------------------------------- */ +%include /* ------------------------------------------------------------ - * String & length + * Overloaded operator support * ------------------------------------------------------------ */ - -%typemap(in) (char *STRING, int LENGTH) { - int temp; - $1 = ($1_ltype) Tcl_GetStringFromObj($input,&temp); - $2 = ($2_ltype) temp; -} +%include /* ------------------------------------------------------------ - * Typechecking rules + * Warnings for Python keywords * ------------------------------------------------------------ */ - -%typecheck(SWIG_TYPECHECK_INTEGER) - int, short, long, - unsigned int, unsigned short, unsigned long, - signed char, unsigned char, - const int &, const short &, const long &, - const unsigned int &, const unsigned short &, const unsigned long &, - enum SWIGTYPE, const enum SWIGTYPE &, - bool, const bool & -{ - long tmp; - if (Tcl_GetLongFromObj(NULL,$input,&tmp) == TCL_ERROR) $1 = 0; - else $1 = 1; -} - -%typecheck(SWIG_TYPECHECK_INTEGER) - long long, const long long & -{ - int len; - char *end = 0; - char *str = Tcl_GetStringFromObj($input, &len); - strtoll(str, &end, 0); - $1 = (end != 0) && (*end != '\0') && (str != end) && (errno == 0); -} - -%typecheck(SWIG_TYPECHECK_INTEGER) - unsigned long long, const unsigned long long & -{ - int len; - char *end = 0; - char *str = Tcl_GetStringFromObj($input, &len); - strtoull(str, &end, 0); - $1 = (end != 0) && (*end != '\0') && (str != end) && (errno == 0); -} - -%typecheck(SWIG_TYPECHECK_DOUBLE) - float, double, - const float &, const double & -{ - double tmp; - if (Tcl_GetDoubleFromObj(NULL,$input,&tmp) == TCL_ERROR) $1 = 0; - else $1 = 1; -} - -%typecheck(SWIG_TYPECHECK_CHAR) char { - char *tmp; - int len; - tmp = Tcl_GetStringFromObj($input,&len); - $1 = (len == 1) ? 1 : 0; -} - -%typecheck(SWIG_TYPECHECK_STRING) char * { - $1 = 1; -} - -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] { - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, $1_descriptor, 0) == TCL_ERROR) { - $1 = 0; - } else { - $1 = 1; - } -} - -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE { - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, $&1_descriptor, 0) == TCL_ERROR) { - $1 = 0; - } else { - $1 = 1; - } -} - -%typecheck(SWIG_TYPECHECK_VOIDPTR) void * { - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, 0, 0) == TCL_ERROR) { - $1 = 0; - } else { - $1 = 1; - } -} +%include /* ------------------------------------------------------------ - * Exception handling + * The Python initialization function * ------------------------------------------------------------ */ - -%typemap(throws) int, - long, - short, - unsigned int, - unsigned long, - unsigned short { - Tcl_SetObjResult(interp, Tcl_NewIntObj((long) $1)); - SWIG_fail; -} - -%typemap(throws) SWIGTYPE CLASS { - $&1_ltype temp = new $1_ltype($1); - Tcl_SetObjResult(interp, SWIG_NewInstanceObj((void *) temp, $&1_descriptor, 1)); - SWIG_fail; -} - -%typemap(throws) SWIGTYPE { - (void)$1; - Tcl_SetObjResult(interp, Tcl_NewStringObj((char*) "$1_type", -1)); - SWIG_fail; -} - -%typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE *, SWIGTYPE [ANY] { - (void)$1; - Tcl_SetObjResult(interp, Tcl_NewStringObj((char*) "$1_type", -1)); - SWIG_fail; -} - -%typemap(throws) enum SWIGTYPE { - (void)$1; - Tcl_SetObjResult(interp, Tcl_NewStringObj((char*) "$1_type", -1)); - SWIG_fail; -} - -%typemap(throws) char * { - Tcl_SetObjResult(interp, Tcl_NewStringObj((char*) $1, -1)); - SWIG_fail; -} - -/* ------------------------------------------------------------ - * ANSI C typemaps - * ------------------------------------------------------------ */ - -%types(size_t); -%apply unsigned long { size_t }; -%apply const unsigned long& { const size_t& }; - -%types(ptrdiff_t); -%apply long { ptrdiff_t }; -%apply const long& { const ptrdiff_t& }; +%include -// Some special reserved words in classes -%include "tcl8kw.swg" - -/* C++ overloaded operators. - - These declarations define how SWIG is going to rename C++ - overloaded operators in Tcl. Since Tcl allows identifiers - to be essentially any valid string, we'll just use the - normal operator names */ - -#ifdef __cplusplus -%rename("+") *::operator+; -//%rename("u+") *::operator+(); // Unary + -//%rename("u+") *::operator+() const; // Unary + -%rename("-") *::operator-; -//%rename("u-") *::operator-(); // Unary - -//%rename("u-") *::operator-() const; // Unary - -%rename("*") *::operator*; -%rename("/") *::operator/; -%rename("<<") *::operator<<; -%rename(">>") *::operator>>; -%rename("&") *::operator&; -%rename("|") *::operator|; -%rename("^") *::operator^; -%rename("%") *::operator%; -%rename("=") *::operator=; -#endif - - -/* This initialization code exports the module initialization function */ - -%header %{ - -#ifdef __cplusplus -extern "C" { -#endif -#ifdef MAC_TCL -#pragma export on -#endif -SWIGEXPORT int SWIG_init(Tcl_Interp *); -#ifdef MAC_TCL -#pragma export off -#endif -#ifdef __cplusplus -} -#endif - -%} - -/* Start the initialization function */ - -%insert(init) "swiginit.swg" - -%init %{ -SWIGEXPORT int SWIG_init(Tcl_Interp *interp) { - int i; - if (interp == 0) return TCL_ERROR; -#ifdef USE_TCL_STUBS - if (Tcl_InitStubs(interp, (char*)"8.1", 0) == NULL) { - return TCL_ERROR; - } -#endif - - Tcl_PkgProvide(interp, (char*)SWIG_name, (char*)SWIG_version); - -#ifdef SWIG_namespace - Tcl_Eval(interp, "namespace eval " SWIG_namespace " { }"); -#endif - - SWIG_InitializeModule((void *) interp); - SWIG_PropagateClientData(); - - for (i = 0; swig_commands[i].name; i++) { - Tcl_CreateObjCommand(interp, (char *) swig_commands[i].name, (swig_wrapper_func) swig_commands[i].wrapper, swig_commands[i].clientdata, NULL); - } - for (i = 0; swig_variables[i].name; i++) { - Tcl_SetVar(interp, (char *) swig_variables[i].name, (char *) "", TCL_GLOBAL_ONLY); - Tcl_TraceVar(interp, (char *) swig_variables[i].name, TCL_TRACE_READS | TCL_GLOBAL_ONLY, (Tcl_VarTraceProc *) swig_variables[i].get, (ClientData) swig_variables[i].addr); - Tcl_TraceVar(interp, (char *) swig_variables[i].name, TCL_TRACE_WRITES | TCL_GLOBAL_ONLY, (Tcl_VarTraceProc *) swig_variables[i].set, (ClientData) swig_variables[i].addr); - } - SWIG_InstallConstants(interp, swig_constants); -%} - -/* Note: the initialization function is closed after all code is generated */ diff --git a/Lib/tcl/tclapi.swg b/Lib/tcl/tclapi.swg new file mode 100644 index 000000000..6e94e7aa3 --- /dev/null +++ b/Lib/tcl/tclapi.swg @@ -0,0 +1,107 @@ +/* ----------------------------------------------------------------------------- + * SWIG API. Portion that goes into the runtime + * ----------------------------------------------------------------------------- */ +#ifdef __cplusplus +extern "C" { +#endif + +/* ----------------------------------------------------------------------------- + * Constant declarations + * ----------------------------------------------------------------------------- */ + +/* Constant Types */ +#define SWIG_TCL_POINTER 4 +#define SWIG_TCL_BINARY 5 + +/* Constant information structure */ +typedef struct swig_const_info { + int type; + char *name; + long lvalue; + double dvalue; + void *pvalue; + swig_type_info **ptype; +} swig_const_info; + +typedef int (*swig_wrapper)(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []); +typedef int (*swig_wrapper_func)(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []); +typedef char *(*swig_variable_func)(ClientData, Tcl_Interp *, char *, char *, int); +typedef void (*swig_delete_func)(ClientData); + +typedef struct swig_method { + const char *name; + swig_wrapper method; +} swig_method; + +typedef struct swig_attribute { + const char *name; + swig_wrapper getmethod; + swig_wrapper setmethod; +} swig_attribute; + +typedef struct swig_class { + const char *name; + swig_type_info **type; + swig_wrapper constructor; + void (*destructor)(void *); + swig_method *methods; + swig_attribute *attributes; + struct swig_class **bases; + char **base_names; + swig_module_info *module; +} swig_class; + +typedef struct swig_instance { + Tcl_Obj *thisptr; + void *thisvalue; + swig_class *classptr; + int destroy; + Tcl_Command cmdtok; +} swig_instance; + +/* Structure for command table */ +typedef struct { + const char *name; + int (*wrapper)(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []); + ClientData clientdata; +} swig_command_info; + +/* Structure for variable linking table */ +typedef struct { + const char *name; + void *addr; + char * (*get)(ClientData, Tcl_Interp *, char *, char *, int); + char * (*set)(ClientData, Tcl_Interp *, char *, char *, int); +} swig_var_info; + + +/* -----------------------------------------------------------------------------* + * Install a constant object + * -----------------------------------------------------------------------------*/ + +static Tcl_HashTable swigconstTable; +static int swigconstTableinit = 0; + +SWIGINTERN void +SWIG_Tcl_SetConstantObj(Tcl_Interp *interp, const char* name, Tcl_Obj *obj) { + int newobj; + Tcl_ObjSetVar2(interp,Tcl_NewStringObj(name,-1), NULL, obj, TCL_GLOBAL_ONLY); + Tcl_SetHashValue(Tcl_CreateHashEntry(&swigconstTable, name, &newobj), (ClientData) obj); +} + +SWIGINTERN Tcl_Obj * +SWIG_Tcl_GetConstantObj(const char *key) { + Tcl_HashEntry *entryPtr; + if (!swigconstTableinit) return 0; + entryPtr = Tcl_FindHashEntry(&swigconstTable, key); + if (entryPtr) { + return (Tcl_Obj *) Tcl_GetHashValue(entryPtr); + } + return 0; +} + +#ifdef __cplusplus +} +#endif + + diff --git a/Lib/tcl/tclerrors.swg b/Lib/tcl/tclerrors.swg new file mode 100644 index 000000000..30846e961 --- /dev/null +++ b/Lib/tcl/tclerrors.swg @@ -0,0 +1,75 @@ +/* ----------------------------------------------------------------------------- + * error manipulation + * ----------------------------------------------------------------------------- */ + +%insert("header") %{ + + +SWIGINTERN const char* +SWIG_Tcl_ErrorType(int code) { + switch(code) { + case SWIG_MemoryError: + return "MemoryError"; + break; + case SWIG_IOError: + return "IOError"; + break; + case SWIG_RuntimeError: + return "RuntimeError"; + break; + case SWIG_IndexError: + return "IndexError"; + break; + case SWIG_TypeError: + return "TypeError"; + break; + case SWIG_DivisionByZero: + return "ZeroDivisionError"; + break; + case SWIG_OverflowError: + return "OverflowError"; + break; + case SWIG_SyntaxError: + return "SyntaxError"; + break; + case SWIG_ValueError: + return "ValueError"; + break; + case SWIG_SystemError: + return "SystemError"; + break; + case SWIG_AttributeError: + return "AttributeError"; + break; + default: + return "RuntimeError"; + break; + } +} + + +SWIGINTERN void +SWIG_Tcl_SetErrorObj(Tcl_Interp *interp, const char *ctype, Tcl_Obj *obj) +{ + Tcl_ResetResult(interp); + Tcl_SetObjResult(interp, obj); + Tcl_SetErrorCode(interp, "SWIG", ctype, NULL); +} + +SWIGINTERN void +SWIG_Tcl_SetErrorMsg(Tcl_Interp *interp, const char *ctype, const char *mesg) +{ + Tcl_ResetResult(interp); + Tcl_SetErrorCode(interp, "SWIG", ctype, NULL); + Tcl_AddErrorInfo(interp, mesg); +} + +SWIGINTERNINLINE void +SWIG_Tcl_AddErrorMsg(Tcl_Interp *interp, const char* mesg) +{ + Tcl_AddErrorInfo(interp, mesg); +} + + + +%} diff --git a/Lib/tcl/tclfragments.swg b/Lib/tcl/tclfragments.swg new file mode 100644 index 000000000..a5ca4c7df --- /dev/null +++ b/Lib/tcl/tclfragments.swg @@ -0,0 +1,23 @@ +/* + + Create a file with this name, 'fragments.i', in your working + directory and add all the %fragments you want to take precedence + over the ones defined by default by swig. + + For example, if you add: + + %fragment(SWIG_AsVal_frag(int),"header") { + SWIGINTERNINLINE int + SWIG_AsVal_dec(int)(TclObject *obj, int *val) + { + ; + } + } + + this will replace the code used to retreive an integer value for all + the typemaps that need it, including: + + int, std::vector, std::list >, etc. + + +*/ diff --git a/Lib/tcl/tclinit.swg b/Lib/tcl/tclinit.swg new file mode 100644 index 000000000..a9807f459 --- /dev/null +++ b/Lib/tcl/tclinit.swg @@ -0,0 +1,106 @@ +/* ------------------------------------------------------------ + * The start of the Tcl initialization function + * ------------------------------------------------------------ */ + +%insert(init) "swiginit.swg" + +/* This initialization code exports the module initialization function */ + +%header %{ + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef MAC_TCL +#pragma export on +#endif +SWIGEXPORT int SWIG_init(Tcl_Interp *); +#ifdef MAC_TCL +#pragma export off +#endif +#ifdef __cplusplus +} +#endif + +%} + +%init %{ +#ifdef __cplusplus +extern "C" { +#endif + +/* ----------------------------------------------------------------------------- + * constants/methods manipulation + * ----------------------------------------------------------------------------- */ + +/* Install Constants */ + +SWIGINTERN void +SWIG_Tcl_InstallConstants(Tcl_Interp *interp, swig_const_info constants[]) { + int i; + Tcl_Obj *obj; + + if (!swigconstTableinit) { + Tcl_InitHashTable(&swigconstTable, TCL_STRING_KEYS); + swigconstTableinit = 1; + } + for (i = 0; constants[i].type; i++) { + switch(constants[i].type) { + case SWIG_TCL_POINTER: + obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0); + break; + case SWIG_TCL_BINARY: + obj = SWIG_NewPackedObj(constants[i].pvalue, constants[i].lvalue, *(constants[i].ptype),0); + break; + default: + obj = 0; + break; + } + if (obj) { + SWIG_Tcl_SetConstantObj(interp, constants[i].name, obj); + } + } +} + +#ifdef __cplusplus +} +#endif + +/* -----------------------------------------------------------------------------* + * Partial Init method + * -----------------------------------------------------------------------------*/ + +SWIGEXPORT int SWIG_init(Tcl_Interp *interp) { + int i; + if (interp == 0) return TCL_ERROR; +#ifdef USE_TCL_STUBS + if (Tcl_InitStubs(interp, (char*)"8.1", 0) == NULL) { + return TCL_ERROR; + } +#endif + Tcl_PkgProvide(interp, (char*)SWIG_name, (char*)SWIG_version); + +#ifdef SWIG_namespace + Tcl_Eval(interp, "namespace eval " SWIG_namespace " { }"); +#endif + + SWIG_InitializeModule((void *) interp); + SWIG_PropagateClientData(); + + for (i = 0; swig_commands[i].name; i++) { + Tcl_CreateObjCommand(interp, (char *) swig_commands[i].name, (swig_wrapper_func) swig_commands[i].wrapper, + swig_commands[i].clientdata, NULL); + } + for (i = 0; swig_variables[i].name; i++) { + Tcl_SetVar(interp, (char *) swig_variables[i].name, (char *) "", TCL_GLOBAL_ONLY); + Tcl_TraceVar(interp, (char *) swig_variables[i].name, TCL_TRACE_READS | TCL_GLOBAL_ONLY, + (Tcl_VarTraceProc *) swig_variables[i].get, (ClientData) swig_variables[i].addr); + Tcl_TraceVar(interp, (char *) swig_variables[i].name, TCL_TRACE_WRITES | TCL_GLOBAL_ONLY, + (Tcl_VarTraceProc *) swig_variables[i].set, (ClientData) swig_variables[i].addr); + } + + SWIG_Tcl_InstallConstants(interp, swig_constants); + +%} + +/* Note: the initialization function is closed after all code is generated */ diff --git a/Lib/tcl/tcl8kw.swg b/Lib/tcl/tclkw.swg similarity index 100% rename from Lib/tcl/tcl8kw.swg rename to Lib/tcl/tclkw.swg diff --git a/Lib/tcl/tclmacros.swg b/Lib/tcl/tclmacros.swg new file mode 100644 index 000000000..80da82bfb --- /dev/null +++ b/Lib/tcl/tclmacros.swg @@ -0,0 +1,26 @@ +%include + + +/* aux macros to deal with the interp argument */ + +#undef SWIG_As_dec +#undef SWIG_AsPtr_dec +#undef SWIG_AsVal_dec + +#define SWIG_Tcl_AsVal(Type...) SWIG_Tcl_ ## AsVal ## _ ## #@Type +#define SWIG_AsVal_dec(Type...) SWIG_ ## AsVal_dec ## _ ## #@Type + +#define SWIG_Tcl_AsPtr(Type...) SWIG_Tcl_ ## AsPtr ## _ ## #@Type +#define SWIG_AsPtr_dec(Type...) SWIG_ ## AsPtr_dec ## _ ## #@Type + + +%define %tcl_asval_decl(Type...) +%#define SWIG_AsVal_dec(Type)(obj,val) SWIG_Tcl_AsVal(Type)(Tcl_Interp *interp, obj, val) +%#define SWIG_AsVal(Type)(obj,val) SWIG_Tcl_AsVal(Type)(interp, obj, val) +%enddef + + +%define %tcl_asptr_decl(Type...) +%#define SWIG_AsPtr_dec(Type)(obj,ptr) SWIG_Tcl_AsPtr(Type)(Tcl_Interp *interp, obj, ptr) +%#define SWIG_AsPtr(Type)(obj,ptr) SWIG_Tcl_AsPtr(Type)(interp, obj, ptr) +%enddef diff --git a/Lib/tcl/tclopers.swg b/Lib/tcl/tclopers.swg new file mode 100644 index 000000000..76ebd7f3c --- /dev/null +++ b/Lib/tcl/tclopers.swg @@ -0,0 +1,24 @@ +/* C++ overloaded operators. + + These declarations define how SWIG is going to rename C++ + overloaded operators in Tcl. Since Tcl allows identifiers + to be essentially any valid string, we'll just use the + normal operator names */ + +#ifdef __cplusplus +%rename("+") *::operator+; +//%rename("u+") *::operator+(); // Unary + +//%rename("u+") *::operator+() const; // Unary + +%rename("-") *::operator-; +//%rename("u-") *::operator-(); // Unary - +//%rename("u-") *::operator-() const; // Unary - +%rename("*") *::operator*; +%rename("/") *::operator/; +%rename("<<") *::operator<<; +%rename(">>") *::operator>>; +%rename("&") *::operator&; +%rename("|") *::operator|; +%rename("^") *::operator^; +%rename("%") *::operator%; +%rename("=") *::operator=; +#endif diff --git a/Lib/tcl/tclprimtypes.swg b/Lib/tcl/tclprimtypes.swg new file mode 100644 index 000000000..94fac7c1b --- /dev/null +++ b/Lib/tcl/tclprimtypes.swg @@ -0,0 +1,308 @@ +%include + +/* Macro for 'signed long' derived types */ + +%define %type_slong(Type, Frag, Min, Max) +%derived_type_from(long, Type) +%fragment("Tcl_asval_"{Type},"header",fragment=Frag) %{ +%tcl_asval_decl(Type) +%} +%signed_derived_type_asval(long, Type, "Tcl_asval_"{Type} , Min, Max) +%enddef + +/* Macro for 'unsigned long' derived types */ + +%define %type_ulong(Type, Frag, Max) +%derived_type_from(unsigned long, Type) +%fragment("Tcl_asval_"{Type},"header",fragment=Frag) %{ +%tcl_asval_decl(Type) +%} +%unsigned_derived_type_asval(unsigned long, Type, "Tcl_asval_"{Type}, Max) +%enddef + +/* ------------------------------------------------------------ + * Primitive Types + * ------------------------------------------------------------ */ + +/* boolean */ + +%fragment(SWIG_From_frag(bool),"header") { + SWIG_define(SWIG_From_dec(bool), Tcl_NewBooleanObj) +} + +%fragment(SWIG_AsVal_frag(bool),"header") { +%tcl_asval_decl(bool) + +SWIGINTERN int +SWIG_AsVal_dec(bool)(Tcl_Obj *obj, bool *val) +{ + int v; + if (Tcl_GetBooleanFromObj(0, obj, &v) == TCL_OK) { + if (val) *val = v ? true : false; + return SWIG_OK; + } + return SWIG_TypeError; +} +} + +/* signed/unsigned char */ + +%type_slong(signed char, "", SCHAR_MIN, SCHAR_MAX) +%type_ulong(unsigned char, "", UCHAR_MAX) + +/* short/unsigned short */ + +%type_slong(short, "", SHRT_MIN, SHRT_MAX) +%type_ulong(unsigned short, "", USHRT_MAX) + +/* int/unsigned int */ + +%type_slong(int, "", INT_MIN, INT_MAX) +%type_ulong(unsigned int, "", UINT_MAX) + +/* signed/unsigned wchar_t */ + +#ifdef __cplusplus +%type_slong(signed wchar_t, "", WCHAR_MIN, WCHAR_MAX) +%type_ulong(unsigned wchar_t, "", UWCHAR_MAX) +#endif + + +/* long */ + +%fragment(SWIG_From_frag(long),"header", + fragment="") { +SWIGINTERNINLINE Tcl_Obj* +SWIG_From_dec(long)(long value) +{ + if (((long) INT_MIN <= value) && (value <= (long) INT_MAX)) { + return Tcl_NewIntObj(SWIG_numeric_cast(value,int)); + } else { + return Tcl_NewLongObj(value); + } +} +} + +%fragment(SWIG_AsVal_frag(long),"header") { +%tcl_asval_decl(long) + +SWIGINTERN int +SWIG_AsVal_dec(long)(Tcl_Obj *obj, long* val) +{ + long v; + if (Tcl_GetLongFromObj(0,obj, &v) == TCL_OK) { + if (val) *val = (long) v; + return SWIG_OK; + } + return SWIG_TypeError; +} +} + +/* unsigned long */ + +%fragment(SWIG_From_frag(unsigned long),"header", + fragment=SWIG_From_frag(long)) { +SWIGINTERNINLINE Tcl_Obj* +SWIG_From_dec(unsigned long)(unsigned long value) +{ + if (value < (unsigned long) LONG_MAX) { + return SWIG_From(long)(SWIG_numeric_cast(value, long)); + } else { + char temp[256]; + snprintf(temp, sizeof(temp),"%lu", value); + return Tcl_NewStringObj(temp,-1); + } +} +} + +%fragment(SWIG_AsVal_frag(unsigned long),"header") { +%tcl_asval_decl(unsigned long) + +SWIGINTERN int +SWIG_AsVal_dec(unsigned long)(Tcl_Obj *obj, unsigned long *val) { + int len = 0; + const char *nptr = Tcl_GetStringFromObj(obj, &len); + if (nptr && len > 0) { + char *endptr; + unsigned long v = strtoul(nptr, &endptr,0); + if (errno == ERANGE) { + errno = 0; + return SWIG_OverflowError; + } else { + if (*endptr == '\0') { + if (val) *val = v; + return SWIG_OK; + } + } + } + return SWIG_TypeError; +} +} + +/* long long */ + +%fragment(SWIG_From_frag(long long),"header", + fragment=SWIG_From_frag(long), + fragment="") { +SWIGINTERNINLINE Tcl_Obj* +SWIG_From_dec(long long)(long long value) +{ + if (((long long) LONG_MIN <= value) && (value <= (long long) LONG_MAX)) { + return SWIG_From(long)(SWIG_numeric_cast(value,long)); + } else { + char temp[256]; + snprintf(temp, sizeof(temp),"%lld", value); + return Tcl_NewStringObj(temp,-1); + } +} +} + +%fragment(SWIG_AsVal_frag(long long),"header") { +%tcl_asval_decl(long long) + +SWIGINTERN int +SWIG_AsVal_dec(long long)(Tcl_Obj *obj, long long *val) +{ + int len = 0; + const char *nptr = Tcl_GetStringFromObj(obj, &len); + if (nptr && len > 0) { + char *endptr; + long long v = strtoll(nptr, &endptr,0); + if (errno == ERANGE) { + errno = 0; + return SWIG_OverflowError; + } else { + if (*endptr == '\0') { + if (val) *val = v; + return SWIG_OK; + } + } + } + return SWIG_TypeError; +} +} + +/* unsigned long long */ + +%fragment(SWIG_From_frag(unsigned long long),"header", + fragment=SWIG_From_frag(long long), + fragment="") { +SWIGINTERNINLINE Tcl_Obj* +SWIG_From_dec(unsigned long long)(unsigned long long value) +{ + if (value < (unsigned long long) LONG_MAX) { + return SWIG_From(long long)(SWIG_numeric_cast(value, long long)); + } else { + char temp[256]; + snprintf(temp, sizeof(temp),"%llu", value); + return Tcl_NewStringObj(temp,-1); + } +} +} + +%fragment(SWIG_AsVal_frag(unsigned long long),"header", + fragment=SWIG_AsVal_frag(unsigned long)) { +%tcl_asval_decl(unsigned long long) + +SWIGINTERN int +SWIG_AsVal_dec(unsigned long long)(Tcl_Obj *obj, unsigned long long *val) +{ + int len = 0; + const char *nptr = Tcl_GetStringFromObj(obj, &len); + if (nptr && len > 0) { + char *endptr; + unsigned long long v = strtoull(nptr, &endptr,0); + if (errno == ERANGE) { + errno = 0; + return SWIG_OverflowError; + } else { + if (*endptr == '\0') { + if (val) *val = v; + return SWIG_OK; + } + } + } + return SWIG_TypeError; +} +} + +/* float */ + +%derived_type_from(double, float) +%fragment("Tcl_asval_float","header",fragment="") { +%tcl_asval_decl(float) +} +%signed_derived_type_asval(double, float, "Tcl_asval_float", -FLT_MAX, FLT_MAX) + +/* double */ + +%fragment(SWIG_From_frag(double),"header") { + SWIG_define(SWIG_From_dec(double), Tcl_NewDoubleObj) +} + +%fragment(SWIG_AsVal_frag(double),"header") { +%tcl_asval_decl(double) + +SWIGINTERN int +SWIG_AsVal_dec(double)(Tcl_Obj *obj, double *val) +{ + double v; + if (Tcl_GetDoubleFromObj(0, obj, &v) == TCL_OK) { + if (val) *val = v; + return SWIG_OK; + } + return SWIG_TypeError; +} +} + +/* char */ + +%fragment(SWIG_From_frag(char),"header") { +SWIGINTERNINLINE Tcl_Obj* +SWIG_From_dec(char)(char c) +{ + return Tcl_NewStringObj(&c,1); +} +} + +%fragment(SWIG_AsVal_frag(char),"header", + fragment="SWIG_AsCharArray", + fragment=SWIG_AsVal_frag(signed char)) { +%tcl_asval_decl(char) + +SWIGINTERNINLINE int +SWIG_AsVal_dec(char)(Tcl_Obj *obj, char *val) +{ + return SWIG_AsCharArray(obj, val, 1) == SWIG_OK ? SWIG_OK : SWIG_TypeError; +} +} + + +/* wchar_t */ + +%fragment(SWIG_From_frag(wchar_t),"header") { +SWIGINTERNINLINE Tcl_Obj* +SWIG_From_dec(wchar_t)(wchar_t c) +{ + return Tcl_NewUnicodeObj(&c, 1); +} +} + +%fragment(SWIG_AsVal_frag(wchar_t),"header", + fragment="SWIG_AsWCharArray", + fragment="", + fragment=SWIG_AsVal_frag(long)) { +%tcl_asval_decl(wchar_t) + +SWIGINTERNINLINE int +SWIG_AsVal_dec(wchar_t)(Tcl_Obj *obj, wchar_t *val) +{ + return (SWIG_AsWCharArray(obj, val, 1) == SWIG_OK) ? SWIG_OK : SWIG_TypeError; +} +} + +/* ------------------------------------------------------------ + * Apply the primitive typemap for all the types with checkcode + * ------------------------------------------------------------ */ + +%apply_checkctypes(%typemap_primitive) diff --git a/Lib/tcl/swigtcl8.swg b/Lib/tcl/tclrun.swg similarity index 69% rename from Lib/tcl/swigtcl8.swg rename to Lib/tcl/tclrun.swg index eb6a58c0a..282f0be00 100644 --- a/Lib/tcl/swigtcl8.swg +++ b/Lib/tcl/tclrun.swg @@ -1,139 +1,96 @@ -/* - * $Header$ - * - * swigtcl8.swg - */ +/*********************************************************************** + * tclrun.swg + * + * This file contains the runtime support for Tcl modules + * and includes code for managing global variables and pointer + * type checking. + * + ************************************************************************/ -#include -#include -#include -#include -#include +/* Common SWIG API */ + +/* for raw pointers */ +#define SWIG_ConvertPtr(oc, ptr, ty, flags) SWIG_Tcl_ConvertPtr(interp, oc, ptr, ty, flags) +#define SWIG_NewPointerObj(ptr, type, flags) SWIG_Tcl_NewPointerObj(ptr, type, flags) + +/* for raw packed data */ +#define SWIG_ConvertPacked(obj, ptr, sz, ty, flags) SWIG_Tcl_ConvertPacked(interp, obj, ptr, sz, ty, flags) +#define SWIG_NewPackedObj(ptr, sz, type, flags) SWIG_Tcl_NewPackedObj(ptr, sz, type, flags) + +/* for class or struct pointers */ +#define SWIG_ConvertInstance(obj, pptr, type, flags) SWIG_Tcl_ConvertPtr(interp, obj, pptr, type, flags) +#define SWIG_NewInstanceObj(thisvalue, type, flags) SWIG_Tcl_NewInstanceObj(interp, thisvalue, type, flags) + +/* for C or C++ function pointers */ +#define SWIG_ConvertFunctionPtr(obj, pptr, type, flags) SWIG_Tcl_ConvertPtr(interp, obj, pptr, type, flags) +#define SWIG_NewFunctionPtrObj(ptr, type) SWIG_Tcl_NewPointerObj(ptr, type, 0) + +/* for C++ member pointers, ie, member methods */ +#define SWIG_ConvertMember(obj, ptr, sz, ty, flags) SWIG_Tcl_ConvertPacked(interp,obj, ptr, sz, ty, flags) +#define SWIG_NewMemberObj(ptr, sz, type) SWIG_Tcl_NewPackedObj(ptr, sz, type, 0) + +/* Runtime API */ +#define SWIG_GetModule(clientdata) SWIG_Tcl_GetModule((Tcl_Interp *) (clientdata)) +#define SWIG_SetModule(clientdata, pointer) SWIG_Tcl_SetModule((Tcl_Interp *) (clientdata), pointer) + +/* Tcl-specific SWIG API */ +#define SWIG_Acquire(ptr) SWIG_Tcl_Acquire(ptr) +#define SWIG_MethodCommand SWIG_Tcl_MethodCommand +#define SWIG_Disown(ptr) SWIG_Tcl_Disown(ptr) +#define SWIG_ConvertPtrFromString(c, ptr, ty, flags) SWIG_Tcl_ConvertPtrFromString(interp, c, ptr, ty, flags) +#define SWIG_MakePtr(c, ptr, ty, flags) SWIG_Tcl_MakePtr(c, ptr, ty, flags) +#define SWIG_PointerTypeFromString(c) SWIG_Tcl_PointerTypeFromString(c) +#define SWIG_GetArgs SWIG_Tcl_GetArgs +#define SWIG_GetConstantObj(key) SWIG_Tcl_GetConstantObj(key) +#define SWIG_ObjectConstructor SWIG_Tcl_ObjectConstructor +#define SWIG_Thisown(ptr) SWIG_Tcl_Thisown(ptr) +#define SWIG_ObjectDelete SWIG_Tcl_ObjectDelete + +/* Error manipulation */ +#define SWIG_fail goto fail +#define SWIG_var_fail return "$name" +#define SWIG_exception(code, msg) do { SWIG_Tcl_SetErrorMsg(interp, SWIG_Tcl_ErrorType(code), msg); SWIG_fail; } while (0) +#define SWIG_contract_assert(expr, msg) if (!(expr)) { SWIG_Tcl_SetErrorMsg(interp, 0, msg); SWIG_fail; } else #ifdef __cplusplus extern "C" { #endif -/* Constant table */ - -#define SWIG_TCL_INT 1 -#define SWIG_TCL_FLOAT 2 -#define SWIG_TCL_STRING 3 -#define SWIG_TCL_POINTER 4 -#define SWIG_TCL_BINARY 5 - -/* Flags for pointer conversion */ -#define SWIG_POINTER_EXCEPTION 0x1 -#define SWIG_POINTER_DISOWN 0x2 - -/* Swig fail macro */ - -#define SWIG_fail goto fail - -/* Constant information structure */ -typedef struct swig_const_info { - int type; - char *name; - long lvalue; - double dvalue; - void *pvalue; - swig_type_info **ptype; -} swig_const_info; - -typedef int (*swig_wrapper)(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []); -typedef int (*swig_wrapper_func)(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []); -typedef char *(*swig_variable_func)(ClientData, Tcl_Interp *, char *, char *, int); -typedef void (*swig_delete_func)(ClientData); - -typedef struct swig_method { - const char *name; - swig_wrapper method; -} swig_method; - -typedef struct swig_attribute { - const char *name; - swig_wrapper getmethod; - swig_wrapper setmethod; -} swig_attribute; - -typedef struct swig_class { - const char *name; - swig_type_info **type; - swig_wrapper constructor; - void (*destructor)(void *); - swig_method *methods; - swig_attribute *attributes; - struct swig_class **bases; - char **base_names; - swig_module_info *module; -} swig_class; - -typedef struct swig_instance { - Tcl_Obj *thisptr; - void *thisvalue; - swig_class *classptr; - int destroy; - Tcl_Command cmdtok; -} swig_instance; - -#define SWIG_NewPointerObj(ptr, type, flags) \ - SWIG_Tcl_NewPointerObj(ptr, type, flags) -#define SWIG_ConvertPtr(oc, ptr, ty, flags) \ - SWIG_Tcl_ConvertPtr(interp, oc, ptr, ty, flags) -#define SWIG_ConvertPtrFromString(c, ptr, ty, flags) \ - SWIG_Tcl_ConvertPtrFromString(interp, c, ptr, ty, flags) -#define SWIG_ConvertPacked(obj, ptr, sz, ty, flags) \ - SWIG_Tcl_ConvertPacked(interp, obj, ptr, sz, ty, flags) -#define SWIG_MakePtr(c, ptr, ty, flags) \ - SWIG_Tcl_MakePtr(c, ptr, ty, flags) -#define SWIG_NewPackedObj(ptr, sz, type, flags) \ - SWIG_Tcl_NewPackedObj(ptr, sz, type, flags) -#define SWIG_GetArgs SWIG_Tcl_GetArgs -#define SWIG_PointerTypeFromString(c) \ - SWIG_Tcl_PointerTypeFromString(c) -#define SWIG_Acquire(ptr) \ - SWIG_Tcl_Acquire(ptr) -#define SWIG_Disown(ptr) \ - SWIG_Tcl_Disown(ptr) -#define SWIG_Thisown(ptr) \ - SWIG_Tcl_Thisown(ptr) -#define SWIG_InstallConstants(interp, constants) \ - SWIG_Tcl_InstallConstants(interp, constants) -#define SWIG_GetConstant(key) \ - SWIG_Tcl_GetConstant(key) -#define SWIG_NewInstanceObj(thisvalue, type, flags) \ - SWIG_Tcl_NewInstanceObj(interp, thisvalue, type, flags) -#define SWIG_ObjectConstructor SWIG_Tcl_ObjectConstructor -#define SWIG_MethodCommand SWIG_Tcl_MethodCommand -#define SWIG_ObjectDelete SWIG_Tcl_ObjectDelete - -/* Runtime API */ -#define SWIG_GetModule(clientdata) SWIG_Tcl_GetModule((Tcl_Interp *) (clientdata)) -#define SWIG_SetModule(clientdata, pointer) SWIG_Tcl_SetModule((Tcl_Interp *) (clientdata), pointer) -#define SWIG_MODULE_CLIENTDATA_TYPE Tcl_Interp * - +/* ----------------------------------------------------------------------------- + * pointers/data manipulation + * ----------------------------------------------------------------------------- */ /* Object support */ -static Tcl_HashTable swigobjectTable; -static int swigobjectTableinit = 0; -/* Acquire ownership of a pointer */ -static void -SWIG_Tcl_Acquire(void *ptr) { - Tcl_HashEntry *entryPtr; - int newobj; +SWIGRUNTIME Tcl_HashTable* +SWIG_Tcl_ObjectTable() { + static Tcl_HashTable swigobjectTable; + static int swigobjectTableinit = 0; if (!swigobjectTableinit) { Tcl_InitHashTable(&swigobjectTable, TCL_ONE_WORD_KEYS); swigobjectTableinit = 1; } - entryPtr = Tcl_CreateHashEntry(&swigobjectTable, (char *) ptr, &newobj); + return &swigobjectTable; +} + +/* Acquire ownership of a pointer */ +SWIGRUNTIME void +SWIG_Tcl_Acquire(void *ptr) { + int newobj; + Tcl_CreateHashEntry(SWIG_Tcl_ObjectTable(), (char *) ptr, &newobj); +} + +SWIGRUNTIME int +SWIG_Tcl_Thisown(void *ptr) { + if (Tcl_FindHashEntry(SWIG_Tcl_ObjectTable(), (char *) ptr)) { + return 1; + } + return 0; } /* Disown a pointer. Returns 1 if we owned it to begin with */ -static int +SWIGRUNTIME int SWIG_Tcl_Disown(void *ptr) { - Tcl_HashEntry *entryPtr; - if (!swigobjectTableinit) return 0; - entryPtr = Tcl_FindHashEntry(&swigobjectTable, (char *) ptr); + Tcl_HashEntry *entryPtr = Tcl_FindHashEntry(SWIG_Tcl_ObjectTable(), (char *) ptr); if (entryPtr) { Tcl_DeleteHashEntry(entryPtr); return 1; @@ -141,23 +98,14 @@ SWIG_Tcl_Disown(void *ptr) { return 0; } -static int -SWIG_Tcl_Thisown(void *ptr) { - if (!swigobjectTableinit) return 0; - if (Tcl_FindHashEntry(&swigobjectTable, (char *) ptr)) { - return 1; - } - return 0; -} - /* Convert a pointer value */ -static int +SWIGRUNTIME int SWIG_Tcl_ConvertPtrFromString(Tcl_Interp *interp, const char *c, void **ptr, swig_type_info *ty, int flags) { swig_cast_info *tc; /* Pointer values must start with leading underscore */ while (*c != '_') { *ptr = (void *) 0; - if (strcmp(c,"NULL") == 0) return TCL_OK; + if (strcmp(c,"NULL") == 0) return SWIG_OK; /* Hmmm. It could be an object name. */ if (Tcl_VarEval(interp,c," cget -this", (char *) NULL) == TCL_OK) { Tcl_Obj *result = Tcl_GetObjResult(interp); @@ -165,38 +113,32 @@ SWIG_Tcl_ConvertPtrFromString(Tcl_Interp *interp, const char *c, void **ptr, swi continue; } Tcl_ResetResult(interp); - if (flags & SWIG_POINTER_EXCEPTION) - Tcl_SetResult(interp, (char *) "Type error. Expected a pointer", TCL_STATIC); - return TCL_ERROR; + return SWIG_ERROR; } c++; c = SWIG_UnpackData(c,ptr,sizeof(void *)); if (ty) { tc = SWIG_TypeCheck(c,ty); - if ((!tc) && (flags & SWIG_POINTER_EXCEPTION)) { - Tcl_SetResult(interp, (char *) "Type error. Expected ", TCL_STATIC); - Tcl_AppendElement(interp, (char *) ty->name); - return TCL_ERROR; - } else if (!tc) { + if (!tc) { Tcl_ResetResult(interp); - return TCL_ERROR; + return SWIG_ERROR; } if (flags & SWIG_POINTER_DISOWN) { SWIG_Disown((void *) *ptr); } *ptr = SWIG_TypeCast(tc,(void *) *ptr); } - return TCL_OK; + return SWIG_OK; } /* Convert a pointer value */ -static SWIGINLINE int +SWIGRUNTIMEINLINE int SWIG_Tcl_ConvertPtr(Tcl_Interp *interp, Tcl_Obj *oc, void **ptr, swig_type_info *ty, int flags) { return SWIG_Tcl_ConvertPtrFromString(interp, Tcl_GetStringFromObj(oc,NULL), ptr, ty, flags); } /* Convert a pointer value */ -static char * +SWIGRUNTIME char * SWIG_Tcl_PointerTypeFromString(char *c) { char d; /* Pointer values must start with leading underscore. NULL has no type */ @@ -213,7 +155,7 @@ SWIG_Tcl_PointerTypeFromString(char *c) { } /* Convert a packed value value */ -static int +SWIGRUNTIME int SWIG_Tcl_ConvertPacked(Tcl_Interp *interp, Tcl_Obj *obj, void *ptr, int sz, swig_type_info *ty, int flags) { swig_cast_info *tc; const char *c; @@ -228,7 +170,7 @@ SWIG_Tcl_ConvertPacked(Tcl_Interp *interp, Tcl_Obj *obj, void *ptr, int sz, swig tc = SWIG_TypeCheck(c,ty); if (!tc) goto type_error; } - return TCL_OK; + return SWIG_OK; type_error: @@ -236,18 +178,18 @@ type_error: if (ty) { Tcl_SetResult(interp, (char *) "Type error. Expected ", TCL_STATIC); Tcl_AppendElement(interp, (char *) ty->name); - return TCL_ERROR; + return SWIG_ERROR; } else { Tcl_SetResult(interp, (char *) "Expected packed data.", TCL_STATIC); - return TCL_ERROR; + return SWIG_ERROR; } } - return TCL_ERROR; + return SWIG_ERROR; } /* Take a pointer and convert it to a string */ -static void +SWIGRUNTIME void SWIG_Tcl_MakePtr(char *c, void *ptr, swig_type_info *ty, int flags) { if (ptr) { *(c++) = '_'; @@ -260,16 +202,16 @@ SWIG_Tcl_MakePtr(char *c, void *ptr, swig_type_info *ty, int flags) { } /* Create a new pointer object */ -static SWIGINLINE Tcl_Obj * +SWIGRUNTIMEINLINE Tcl_Obj * SWIG_Tcl_NewPointerObj(void *ptr, swig_type_info *type, int flags) { Tcl_Obj *robj; - char result[512]; + char result[SWIG_BUFFER_SIZE]; SWIG_MakePtr(result,ptr,type,flags); robj = Tcl_NewStringObj(result,-1); return robj; } -static Tcl_Obj * +SWIGRUNTIME Tcl_Obj * SWIG_Tcl_NewPackedObj(void *ptr, int sz, swig_type_info *type, int flags) { char result[1024]; char *r = result; @@ -281,160 +223,42 @@ SWIG_Tcl_NewPackedObj(void *ptr, int sz, swig_type_info *type, int flags) { return Tcl_NewStringObj(result,-1); } -static Tcl_HashTable swigconstTable; -static int swigconstTableinit = 0; +/* -----------------------------------------------------------------------------* + * Get type list + * -----------------------------------------------------------------------------*/ -/* Install Constants */ -static void -SWIG_Tcl_InstallConstants(Tcl_Interp *interp, swig_const_info constants[]) { - int i; - Tcl_Obj *obj; - Tcl_HashEntry *entryPtr; - int newobj; +SWIGRUNTIME swig_module_info * +SWIG_Tcl_GetModule(Tcl_Interp *interp) { + char *data; + swig_module_info *ret = 0; + + /* first check if pointer already created */ + data = (char *) Tcl_GetVar(interp, "swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, + TCL_GLOBAL_ONLY); + if (data) { + SWIG_UnpackData(data, &ret, sizeof(swig_type_info **)); + } - if (!swigconstTableinit) { - Tcl_InitHashTable(&swigconstTable, TCL_STRING_KEYS); - swigconstTableinit = 1; - } - for (i = 0; constants[i].type; i++) { - switch(constants[i].type) { - case SWIG_TCL_INT: - obj = Tcl_NewIntObj(constants[i].lvalue); - break; - case SWIG_TCL_FLOAT: - obj = Tcl_NewDoubleObj(constants[i].dvalue); - break; - case SWIG_TCL_STRING: - obj = Tcl_NewStringObj((char *) constants[i].pvalue,-1); - break; - case SWIG_TCL_POINTER: - obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0); - break; - case SWIG_TCL_BINARY: - obj = SWIG_NewPackedObj(constants[i].pvalue, constants[i].lvalue, *(constants[i].ptype),0); - break; - default: - obj = 0; - break; - } - if (obj) { - Tcl_ObjSetVar2(interp,Tcl_NewStringObj(constants[i].name,-1), NULL, obj, TCL_GLOBAL_ONLY); - entryPtr = Tcl_CreateHashEntry(&swigconstTable, constants[i].name, &newobj); - Tcl_SetHashValue(entryPtr, (ClientData) obj); - } - } + return ret; } -static Tcl_Obj * -SWIG_Tcl_GetConstant(const char *key) { - Tcl_HashEntry *entryPtr; - if (!swigconstTableinit) return 0; - entryPtr = Tcl_FindHashEntry(&swigconstTable, key); - if (entryPtr) { - return (Tcl_Obj *) Tcl_GetHashValue(entryPtr); - } - return 0; +SWIGRUNTIME void +SWIG_Tcl_SetModule(Tcl_Interp *interp, swig_module_info *module) { + char buf[SWIG_BUFFER_SIZE]; + char *data; + + /* create a new pointer */ + data = SWIG_PackData(buf, &module, sizeof(swig_type_info **)); + *data = 0; + Tcl_SetVar(interp, "swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, buf, 0); } -/* Get arguments */ -static int -SWIG_Tcl_GetArgs(Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], const char *fmt, ...) { - int argno = 0, opt = 0; - long tempi; - double tempd; - const char *c; - va_list ap; - void *vptr; - Tcl_Obj *obj = 0; - swig_type_info *ty; +/* -----------------------------------------------------------------------------* + * Object auxiliars + * -----------------------------------------------------------------------------*/ - va_start(ap,fmt); - for (c = fmt; (*c && (*c != ':') && (*c != ';')); c++,argno++) { - if (*c == '|') { - opt = 1; - c++; - } - if (argno >= (objc-1)) { - if (!opt) { - Tcl_SetResult(interp, (char *) "Wrong # args. ", TCL_STATIC); - goto argerror; - } else { - va_end(ap); - return TCL_OK; - } - } - vptr = va_arg(ap,void *); - if (vptr) { - if (isupper(*c)) { - obj = SWIG_GetConstant(Tcl_GetStringFromObj(objv[argno+1],0)); - if (!obj) obj = objv[argno+1]; - } else { - obj = objv[argno+1]; - } - switch(*c) { - case 'i': case 'I': - case 'l': case 'L': - case 'h': case 'H': - case 'b': case 'B': - if (Tcl_GetLongFromObj(interp,obj,&tempi) != TCL_OK) goto argerror; - if ((*c == 'i') || (*c == 'I')) *((int *)vptr) = (int)tempi; - else if ((*c == 'l') || (*c == 'L')) *((long *)vptr) = (long)tempi; - else if ((*c == 'h') || (*c == 'H')) *((short*)vptr) = (short)tempi; - else if ((*c == 'b') || (*c == 'B')) *((unsigned char *)vptr) = (unsigned char)tempi; - break; - case 'f': case 'F': - case 'd': case 'D': - if (Tcl_GetDoubleFromObj(interp,obj,&tempd) != TCL_OK) goto argerror; - if ((*c == 'f') || (*c == 'F')) *((float *) vptr) = (float)tempd; - else if ((*c == 'd') || (*c == 'D')) *((double*) vptr) = tempd; - break; - case 's': case 'S': - if (*(c+1) == '#') { - int *vlptr = (int *) va_arg(ap, void *); - *((char **) vptr) = Tcl_GetStringFromObj(obj, vlptr); - c++; - } else { - *((char **)vptr) = Tcl_GetStringFromObj(obj,NULL); - } - break; - case 'c': case 'C': - *((char *)vptr) = *(Tcl_GetStringFromObj(obj,NULL)); - break; - case 'p': case 'P': - ty = (swig_type_info *) va_arg(ap, void *); - if (SWIG_Tcl_ConvertPtr(interp, obj, (void **) vptr, ty, SWIG_POINTER_EXCEPTION) == TCL_ERROR) goto argerror; - break; - case 'o': case 'O': - *((Tcl_Obj **)vptr) = objv[argno+1]; - break; - default: - break; - } - } - } - - if ((*c != ';') && ((objc-1) > argno)) { - Tcl_SetResult(interp, (char *) "Wrong # args.", TCL_STATIC); - goto argerror; - } - va_end(ap); - return TCL_OK; - - argerror: - { - char temp[32]; - sprintf(temp,"%d", argno+1); - c = strchr(fmt,':'); - if (!c) c = strchr(fmt,';'); - if (!c) c = (char *)""; - Tcl_AppendResult(interp,c," argument ", temp, NULL); - va_end(ap); - return TCL_ERROR; - } -} - -static void +SWIGRUNTIME void SWIG_Tcl_ObjectDelete(ClientData clientData) { swig_instance *si = (swig_instance *) clientData; if ((si) && (si->destroy) && (SWIG_Disown(si->thisvalue))) { @@ -447,7 +271,7 @@ SWIG_Tcl_ObjectDelete(ClientData clientData) { } /* Function to invoke object methods given an instance */ -static int +SWIGRUNTIME int SWIG_Tcl_MethodCommand(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST _objv[]) { char *method, *attrname; swig_instance *inst = (swig_instance *) clientData; @@ -598,7 +422,6 @@ SWIG_Tcl_MethodCommand(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_ Tcl_SetResult(interp,(char *) "Invalid attribute name.", TCL_STATIC); return TCL_ERROR; } - Tcl_SetResult(interp, (char *) "Invalid method. Must be one of: configure cget -acquire -disown -delete", TCL_STATIC); cls = inst->classptr; bi = 0; @@ -628,8 +451,33 @@ SWIG_Tcl_MethodCommand(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_ return TCL_ERROR; } +/* This function takes the current result and turns it into an object command */ +SWIGRUNTIME Tcl_Obj * +SWIG_Tcl_NewInstanceObj(Tcl_Interp *interp, void *thisvalue, swig_type_info *type, int flags) { + Tcl_Obj *robj = SWIG_NewPointerObj(thisvalue, type,0); + /* Check to see if this pointer belongs to a class or not */ + if ((type->clientdata) && (interp)) { + Tcl_CmdInfo ci; + char *name; + name = Tcl_GetStringFromObj(robj,NULL); + if (!Tcl_GetCommandInfo(interp,name, &ci) || (flags)) { + swig_instance *newinst = (swig_instance *) malloc(sizeof(swig_instance)); + newinst->thisptr = Tcl_DuplicateObj(robj); + Tcl_IncrRefCount(newinst->thisptr); + newinst->thisvalue = thisvalue; + newinst->classptr = (swig_class *) type->clientdata; + newinst->destroy = flags; + newinst->cmdtok = Tcl_CreateObjCommand(interp, Tcl_GetStringFromObj(robj,NULL), (swig_wrapper_func) SWIG_MethodCommand, (ClientData) newinst, (swig_delete_func) SWIG_ObjectDelete); + if (flags) { + SWIG_Acquire(thisvalue); + } + } + } + return robj; +} + /* Function to create objects */ -static int +SWIGRUNTIME int SWIG_Tcl_ObjectConstructor(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Tcl_Obj *newObj = 0; void *thisvalue = 0; @@ -689,7 +537,7 @@ SWIG_Tcl_ObjectConstructor(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_SetResult(interp, (char *) "No constructor available.", TCL_STATIC); return TCL_ERROR; } - if (SWIG_Tcl_ConvertPtr(interp,newObj, (void **) &thisvalue, *(classptr->type), SWIG_POINTER_EXCEPTION) == TCL_ERROR) { + if (SWIG_Tcl_ConvertPtr(interp,newObj, (void **) &thisvalue, *(classptr->type), 0) != SWIG_OK) { Tcl_DecrRefCount(newObj); return TCL_ERROR; } @@ -706,81 +554,106 @@ SWIG_Tcl_ObjectConstructor(ClientData clientData, Tcl_Interp *interp, int objc, return TCL_OK; } +/* -----------------------------------------------------------------------------* + * Get arguments + * -----------------------------------------------------------------------------*/ +SWIGRUNTIME int +SWIG_Tcl_GetArgs(Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], const char *fmt, ...) { + int argno = 0, opt = 0; + long tempi; + double tempd; + const char *c; + va_list ap; + void *vptr; + Tcl_Obj *obj = 0; + swig_type_info *ty; -/* This function takes the current result and turns it into an object command */ -static Tcl_Obj * -SWIG_Tcl_NewInstanceObj(Tcl_Interp *interp, void *thisvalue, swig_type_info *type, int flags) { - Tcl_Obj *robj = SWIG_NewPointerObj(thisvalue, type,0); - /* Check to see if this pointer belongs to a class or not */ - if ((type->clientdata) && (interp)) { - Tcl_CmdInfo ci; - char *name; - name = Tcl_GetStringFromObj(robj,NULL); - if (!Tcl_GetCommandInfo(interp,name, &ci) || (flags)) { - swig_instance *newinst = (swig_instance *) malloc(sizeof(swig_instance)); - newinst->thisptr = Tcl_DuplicateObj(robj); - Tcl_IncrRefCount(newinst->thisptr); - newinst->thisvalue = thisvalue; - newinst->classptr = (swig_class *) type->clientdata; - newinst->destroy = flags; - newinst->cmdtok = Tcl_CreateObjCommand(interp, Tcl_GetStringFromObj(robj,NULL), (swig_wrapper_func) SWIG_MethodCommand, (ClientData) newinst, (swig_delete_func) SWIG_ObjectDelete); - if (flags) { - SWIG_Acquire(thisvalue); + va_start(ap,fmt); + for (c = fmt; (*c && (*c != ':') && (*c != ';')); c++,argno++) { + if (*c == '|') { + opt = 1; + c++; + } + if (argno >= (objc-1)) { + if (!opt) { + Tcl_SetResult(interp, (char *) "Wrong number of arguments ", TCL_STATIC); + goto argerror; + } else { + va_end(ap); + return TCL_OK; + } + } + + vptr = va_arg(ap,void *); + if (vptr) { + if (isupper(*c)) { + obj = SWIG_Tcl_GetConstantObj(Tcl_GetStringFromObj(objv[argno+1],0)); + if (!obj) obj = objv[argno+1]; + } else { + obj = objv[argno+1]; + } + switch(*c) { + case 'i': case 'I': + case 'l': case 'L': + case 'h': case 'H': + case 'b': case 'B': + if (Tcl_GetLongFromObj(interp,obj,&tempi) != TCL_OK) goto argerror; + if ((*c == 'i') || (*c == 'I')) *((int *)vptr) = (int)tempi; + else if ((*c == 'l') || (*c == 'L')) *((long *)vptr) = (long)tempi; + else if ((*c == 'h') || (*c == 'H')) *((short*)vptr) = (short)tempi; + else if ((*c == 'b') || (*c == 'B')) *((unsigned char *)vptr) = (unsigned char)tempi; + break; + case 'f': case 'F': + case 'd': case 'D': + if (Tcl_GetDoubleFromObj(interp,obj,&tempd) != TCL_OK) goto argerror; + if ((*c == 'f') || (*c == 'F')) *((float *) vptr) = (float)tempd; + else if ((*c == 'd') || (*c == 'D')) *((double*) vptr) = tempd; + break; + case 's': case 'S': + if (*(c+1) == '#') { + int *vlptr = (int *) va_arg(ap, void *); + *((char **) vptr) = Tcl_GetStringFromObj(obj, vlptr); + c++; + } else { + *((char **)vptr) = Tcl_GetStringFromObj(obj,NULL); + } + break; + case 'c': case 'C': + *((char *)vptr) = *(Tcl_GetStringFromObj(obj,NULL)); + break; + case 'p': case 'P': + ty = (swig_type_info *) va_arg(ap, void *); + if (SWIG_Tcl_ConvertPtr(interp, obj, (void **) vptr, ty, 0) != SWIG_OK) goto argerror; + break; + case 'o': case 'O': + *((Tcl_Obj **)vptr) = objv[argno+1]; + break; + default: + break; } } } - return robj; -} -/* Structure for command table */ -typedef struct { - const char *name; - int (*wrapper)(ClientData, Tcl_Interp *, int, Tcl_Obj *CONST []); - ClientData clientdata; -} swig_command_info; - -/* Structure for variable linking table */ -typedef struct { - const char *name; - void *addr; - char * (*get)(ClientData, Tcl_Interp *, char *, char *, int); - char * (*set)(ClientData, Tcl_Interp *, char *, char *, int); -} swig_var_info; - - -/* Contract support */ - -#define SWIG_contract_assert(expr, msg) if (!(expr)) { Tcl_SetResult(interp, (char *) msg, TCL_STATIC ); goto fail; } else - -static swig_module_info * -SWIG_Tcl_GetModule(Tcl_Interp *interp) { - char *data; - swig_module_info *ret = 0; - - /* first check if pointer already created */ - data = (char *) Tcl_GetVar(interp, "swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, TCL_GLOBAL_ONLY); - if (data) { - SWIG_UnpackData(data, &ret, sizeof(swig_type_info **)); + if ((*c != ';') && ((objc-1) > argno)) { + Tcl_SetResult(interp, (char *) "Wrong # args.", TCL_STATIC); + goto argerror; } + va_end(ap); + return TCL_OK; - return ret; -} - -static void -SWIG_Tcl_SetModule(Tcl_Interp *interp, swig_module_info *module) { - char buf[512]; - char *data; - - /* create a new pointer */ - data = SWIG_PackData(buf, &module, sizeof(swig_type_info **)); - *data = 0; - Tcl_SetVar(interp, "swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, buf, 0); + argerror: + { + char temp[32]; + sprintf(temp,"%d", argno+1); + c = strchr(fmt,':'); + if (!c) c = strchr(fmt,';'); + if (!c) c = (char *)""; + Tcl_AppendResult(interp,c," argument ", temp, NULL); + va_end(ap); + return TCL_ERROR; + } } #ifdef __cplusplus } #endif - - - - diff --git a/Lib/tcl/tclruntime.swg b/Lib/tcl/tclruntime.swg new file mode 100644 index 000000000..f29848ba0 --- /dev/null +++ b/Lib/tcl/tclruntime.swg @@ -0,0 +1,13 @@ +/* tcl.h has to appear first */ +%insert(runtime) %{ +#include +#include +#include +#include +#include +#include +%} + +%insert(runtime) "swigrun.swg"; /* Common C API type-checking code */ +%insert(runtime) "tclapi.swg"; /* SWIG/Tcl API */ +%insert(runtime) "tclrun.swg"; /* Tcl run-time code */ diff --git a/Lib/tcl/tclstrings.swg b/Lib/tcl/tclstrings.swg new file mode 100644 index 000000000..e934dddad --- /dev/null +++ b/Lib/tcl/tclstrings.swg @@ -0,0 +1,36 @@ +/* ------------------------------------------------------------ + * utility methods for char strings + * ------------------------------------------------------------ */ + +%fragment("SWIG_AsCharPtrAndSize","header") { +SWIGINTERN int +SWIG_AsCharPtrAndSize(Tcl_Obj *obj, char** cptr, size_t* psize, int *alloc) +{ + int len = 0; + char *cstr = Tcl_GetStringFromObj(obj, &len); + if (cstr) { + if (cptr) *cptr = cstr; + if (psize) *psize = len + 1; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; + } + return SWIG_TypeError; +} +} + + +%fragment("SWIG_FromCharPtrAndSize","header", + fragment="") { +SWIGINTERNINLINE Tcl_Obj * +SWIG_FromCharPtrAndSize(const char* carray, size_t size) +{ + return (size < INT_MAX) ? Tcl_NewStringObj(carray, SWIG_numeric_cast(size,int)) : NULL; +} +} + +/* ------------------------------------------------------------ + * The plain char * handling + * ------------------------------------------------------------ */ + +%include +%typemap_string(char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize, strlen) diff --git a/Lib/tcl/tclswigtype.swg b/Lib/tcl/tclswigtype.swg new file mode 100644 index 000000000..525e7a093 --- /dev/null +++ b/Lib/tcl/tclswigtype.swg @@ -0,0 +1,13 @@ +%include + +%typemap(out) SWIGTYPE = SWIGTYPE INSTANCE; +%typemap(out) SWIGTYPE * = SWIGTYPE *INSTANCE; +%typemap(out) SWIGTYPE & = SWIGTYPE &INSTANCE; +%typemap(out) SWIGTYPE [] = SWIGTYPE INSTANCE[]; +%typemap(varout) SWIGTYPE = SWIGTYPE INSTANCE; + +%typemap(throws,noblock=1) SWIGTYPE CLASS { + SWIG_set_result(SWIG_NewInstanceObj(SWIG_as_voidptr(SWIG_new_copy($1, $1_ltype)), $&1_descriptor, 1)); + SWIG_fail; +} + diff --git a/Lib/tcl/tcltypemaps.swg b/Lib/tcl/tcltypemaps.swg new file mode 100644 index 000000000..953076863 --- /dev/null +++ b/Lib/tcl/tcltypemaps.swg @@ -0,0 +1,42 @@ +/* ----------------------------------------------------------------------------- + * Typemap specializations + * ----------------------------------------------------------------------------- */ + +/* no director supported in Tcl */ +#ifdef SWIG_DIRECTOR_TYPEMAPS +#undef SWIG_DIRECTOR_TYPEMAPS +#endif + +/* ----------------------------------------------------------------------------- + * Basic definitions + * ----------------------------------------------------------------------------- */ + +%define_swig_object(Tcl_Obj *) + +#define SWIG_SetResultObj(obj) $result = obj +#define SWIG_AppendResultObj(obj) $result = (Tcl_ListObjAppendElement(NULL,$result,obj) == TCL_OK) ? $result : NULL; +#define SWIG_SetConstantObj(name, obj) SWIG_Tcl_SetConstantObj(interp, name, obj) +#define SWIG_NoneObject() NULL + +/* error manipulation */ +#define SWIG_ErrorType(code) SWIG_Tcl_ErrorType(code) +#define SWIG_SetErrorObj(code,obj) SWIG_Tcl_SetErrorObj(interp,SWIG_ErrorType(code),obj) +#define SWIG_SetErrorMsg(code,msg) SWIG_Tcl_SetErrorMsg(interp,SWIG_ErrorType(code),msg) +#define SWIG_ExceptionObj(d,type,obj) SWIG_Tcl_SetErrorObj(interp,type,obj) + +/* ----------------------------------------------------------------------------- + * All the typemaps + * ----------------------------------------------------------------------------- */ + +%include "tclfragments.swg" + +%include +%include +%include +%include +%include +%include +%include +%include +%include +%include diff --git a/Lib/tcl/tcluserdir.swg b/Lib/tcl/tcluserdir.swg new file mode 100644 index 000000000..d5b41fb9a --- /dev/null +++ b/Lib/tcl/tcluserdir.swg @@ -0,0 +1,5 @@ +/* ----------------------------------------------------------------------------- + * Special user directives + * ----------------------------------------------------------------------------- */ + + diff --git a/Lib/tcl/tclwstrings.swg b/Lib/tcl/tclwstrings.swg new file mode 100644 index 000000000..8ac2101ca --- /dev/null +++ b/Lib/tcl/tclwstrings.swg @@ -0,0 +1,36 @@ +/* ------------------------------------------------------------ + * utility methods for wchar strings + * ------------------------------------------------------------ */ + +%fragment("SWIG_AsCharPtrAndSize","header") { +SWIGINTERN int +SWIG_AsWCharPtrAndSize(Tcl_Obj *obj, wchar_t** cptr, size_t* psize, int *alloc) +{ + int len = 0; + wchar_t *cstr = Tcl_GetUnicodeFromObj(obj, &len); + if (cstr) { + if (cptr) *cptr = cstr; + if (psize) *psize = len + 1; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; + } + return SWIG_TypeError; +} +} + +%fragment("SWIG_FromWCharPtrAndSize","header", + fragment="") { +SWIGINTERNINLINE Tcl_Obj * +SWIG_FromWCharPtrAndSize(const wchar_t* carray, size_t size) +{ + return (size < INT_MAX) ? Tcl_NewUnicodeObj(carray, SWIG_numeric_cast(size,int)) : NULL; +} +} + + +/* ------------------------------------------------------------ + * The plain char * handling + * ------------------------------------------------------------ */ + +%include +%typemap_string(wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize, wcslen) diff --git a/Lib/tcl/typemaps.i b/Lib/tcl/typemaps.i index ad8c4d86c..1f9b9c432 100644 --- a/Lib/tcl/typemaps.i +++ b/Lib/tcl/typemaps.i @@ -1,507 +1 @@ -/* ----------------------------------------------------------------------------- - * typemaps.i - * - * Swig typemap library for Tcl8. This file contains various sorts - * of typemaps for modifying Swig's code generation. - * - * Author: David Beazley (beazley@cs.uchicago.edu) - * - * ----------------------------------------------------------------------------- */ - -/* -The SWIG typemap library provides a language independent mechanism for -supporting output arguments, input values, and other C function -calling mechanisms. The primary use of the library is to provide a -better interface to certain C function--especially those involving -pointers. -*/ - -// INPUT typemaps. -// These remap a C pointer to be an "INPUT" value which is passed by value -// instead of reference. - -/* -The following methods can be applied to turn a pointer into a simple -"input" value. That is, instead of passing a pointer to an object, -you would use a real value instead. - - int *INPUT - short *INPUT - long *INPUT - long long *INPUT - unsigned int *INPUT - unsigned short *INPUT - unsigned long *INPUT - unsigned long long *INPUT - unsigned char *INPUT - bool *INPUT - float *INPUT - double *INPUT - -To use these, suppose you had a C function like this : - - double fadd(double *a, double *b) { - return *a+*b; - } - -You could wrap it with SWIG as follows : - - %include typemaps.i - double fadd(double *INPUT, double *INPUT); - -or you can use the %apply directive : - - %include typemaps.i - %apply double *INPUT { double *a, double *b }; - double fadd(double *a, double *b); - -*/ - -%typemap(in) double *INPUT(double temp), double &INPUT(double temp) -{ - if (Tcl_GetDoubleFromObj(interp,$input,&temp) == TCL_ERROR) { - SWIG_fail; - } - $1 = &temp; -} - -%typemap(in) float *INPUT(double dvalue, float temp), float &INPUT(double dvalue, float temp) -{ - if (Tcl_GetDoubleFromObj(interp,$input,&dvalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (float) dvalue; - $1 = &temp; -} - -%typemap(in) int *INPUT(int temp), int &INPUT(int temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&temp) == TCL_ERROR) { - SWIG_fail; - } - $1 = &temp; -} - -%typemap(in) short *INPUT(int ivalue, short temp), short &INPUT(int ivalue, short temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (short) ivalue; - $1 = &temp; -} - -%typemap(in) long *INPUT(int ivalue, long temp), long &INPUT(int ivalue, long temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (long) ivalue; - $1 = &temp; -} - -%typemap(in) unsigned int *INPUT(int ivalue, unsigned int temp), - unsigned int &INPUT(int ivalue, unsigned int temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (unsigned int) ivalue; - $1 = &temp; -} - -%typemap(in) unsigned short *INPUT(int ivalue, unsigned short temp), - unsigned short &INPUT(int ivalue, unsigned short temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (unsigned short) ivalue; - $1 = &temp; -} - -%typemap(in) unsigned long *INPUT(int ivalue, unsigned long temp), - unsigned long &INPUT(int ivalue, unsigned long temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (unsigned long) ivalue; - $1 = &temp; -} - -%typemap(in) unsigned char *INPUT(int ivalue, unsigned char temp), - unsigned char &INPUT(int ivalue, unsigned char temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (unsigned char) ivalue; - $1 = &temp; -} - -%typemap(in) signed char *INPUT(int ivalue, signed char temp), - signed char &INPUT(int ivalue, signed char temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = (signed char) ivalue; - $1 = &temp; -} - -%typemap(in) bool *INPUT(int ivalue, bool temp), - bool &INPUT(int ivalue, bool temp) -{ - if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) { - SWIG_fail; - } - temp = ivalue ? true : false; - $1 = &temp; -} - -%typemap(in) long long *INPUT($*1_ltype temp), - long long &INPUT($*1_ltype temp) -{ - temp = ($*1_ltype) strtoll(Tcl_GetStringFromObj($input,NULL),0,0); - $1 = &temp; -} - -%typemap(in) unsigned long long *INPUT($*1_ltype temp), - unsigned long long &INPUT($*1_ltype temp) -{ - temp = ($*1_ltype) strtoull(Tcl_GetStringFromObj($input,NULL),0,0); - $1 = &temp; -} - -// OUTPUT typemaps. These typemaps are used for parameters that -// are output only. The output value is appended to the result as -// a list element. - -/* -The following methods can be applied to turn a pointer into an "output" -value. When calling a function, no input value would be given for -a parameter, but an output value would be returned. In the case of -multiple output values, they are returned in the form of a Tcl list. - - int *OUTPUT - short *OUTPUT - long *OUTPUT - long long *OUTPUT - unsigned int *OUTPUT - unsigned short *OUTPUT - unsigned long *OUTPUT - unsigned long long *OUTPUT - unsigned char *OUTPUT - bool *OUTPUT - float *OUTPUT - double *OUTPUT - -For example, suppose you were trying to wrap the modf() function in the -C math library which splits x into integral and fractional parts (and -returns the integer part in one of its parameters).K: - - double modf(double x, double *ip); - -You could wrap it with SWIG as follows : - - %include typemaps.i - double modf(double x, double *OUTPUT); - -or you can use the %apply directive : - - %include typemaps.i - %apply double *OUTPUT { double *ip }; - double modf(double x, double *ip); - -The Tcl output of the function would be a list containing both -output values. - -*/ - -%typemap(in,numinputs=0) int *OUTPUT(int temp), - short *OUTPUT(short temp), - long *OUTPUT(long temp), - unsigned int *OUTPUT(unsigned int temp), - unsigned short *OUTPUT(unsigned short temp), - unsigned long *OUTPUT(unsigned long temp), - unsigned char *OUTPUT(unsigned char temp), - signed char *OUTPUT(signed char temp), - bool *OUTPUT(bool temp), - float *OUTPUT(float temp), - double *OUTPUT(double temp), - long long *OUTPUT($*1_ltype temp), - unsigned long long *OUTPUT($*1_ltype temp), - int &OUTPUT(int temp), - short &OUTPUT(short temp), - long &OUTPUT(long temp), - unsigned int &OUTPUT(unsigned int temp), - unsigned short &OUTPUT(unsigned short temp), - unsigned long &OUTPUT(unsigned long temp), - signed char &OUTPUT(signed char temp), - bool &OUTPUT(bool temp), - unsigned char &OUTPUT(unsigned char temp), - float &OUTPUT(float temp), - double &OUTPUT(double temp), - long long &OUTPUT($*1_ltype temp), - unsigned long long &OUTPUT($*1_ltype temp) -"$1 = &temp;"; - -%typemap(argout) int *OUTPUT, int &OUTPUT, - short *OUTPUT, short &OUTPUT, - long *OUTPUT, long &OUTPUT, - unsigned int *OUTPUT, unsigned int &OUTPUT, - unsigned short *OUTPUT, unsigned short &OUTPUT, - unsigned long *OUTPUT, unsigned long &OUTPUT, - unsigned char *OUTPUT, unsigned char &OUTPUT, - signed char *OUTPUT, signed char &OUTPUT, - bool *OUTPUT, bool &OUTPUT -{ - Tcl_Obj *o; - o = Tcl_NewIntObj((int) *($1)); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o); -} - -%typemap(argout) float *OUTPUT, float &OUTPUT, - double *OUTPUT, double &OUTPUT -{ - Tcl_Obj *o; - o = Tcl_NewDoubleObj((double) *($1)); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o); -} - -%typemap(argout) long long *OUTPUT, long long &OUTPUT -{ - char temp[256]; - Tcl_Obj *o; - sprintf(temp,"%lld",(long long)*($1)); - o = Tcl_NewStringObj(temp,-1); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o); -} - -%typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT -{ - char temp[256]; - Tcl_Obj *o; - sprintf(temp,"%llu",(unsigned long long)*($1)); - o = Tcl_NewStringObj(temp,-1); - Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o); -} - -// INOUT -// Mappings for an argument that is both an input and output -// parameter - -/* -The following methods can be applied to make a function parameter both -an input and output value. This combines the behavior of both the -"INPUT" and "OUTPUT" methods described earlier. Output values are -returned in the form of a Tcl list. - - int *INOUT - short *INOUT - long *INOUT - long long *INOUT - unsigned int *INOUT - unsigned short *INOUT - unsigned long *INOUT - unsigned long long *INOUT - unsigned char *INOUT - bool *INOUT - float *INOUT - double *INOUT - -For example, suppose you were trying to wrap the following function : - - void neg(double *x) { - *x = -(*x); - } - -You could wrap it with SWIG as follows : - - %include typemaps.i - void neg(double *INOUT); - -or you can use the %apply directive : - - %include typemaps.i - %apply double *INOUT { double *x }; - void neg(double *x); - -Unlike C, this mapping does not directly modify the input value (since -this makes no sense in Tcl). Rather, the modified input value shows -up as the return value of the function. Thus, to apply this function -to a Tcl variable you might do this : - - set x [neg $x] - -*/ - - -%typemap(in) int *INOUT = int *INPUT; -%typemap(in) short *INOUT = short *INPUT; -%typemap(in) long *INOUT = long *INPUT; -%typemap(in) unsigned int *INOUT = unsigned int *INPUT; -%typemap(in) unsigned short *INOUT = unsigned short *INPUT; -%typemap(in) unsigned long *INOUT = unsigned long *INPUT; -%typemap(in) unsigned char *INOUT = unsigned char *INPUT; -%typemap(in) signed char *INOUT = signed char *INPUT; -%typemap(in) bool *INOUT = bool *INPUT; -%typemap(in) float *INOUT = float *INPUT; -%typemap(in) double *INOUT = double *INPUT; -%typemap(in) long long *INOUT = long long *INPUT; -%typemap(in) unsigned long long *INOUT = unsigned long long *INPUT; - -%typemap(in) int &INOUT = int &INPUT; -%typemap(in) short &INOUT = short &INPUT; -%typemap(in) long &INOUT = long &INPUT; -%typemap(in) unsigned int &INOUT = unsigned int &INPUT; -%typemap(in) unsigned short &INOUT = unsigned short &INPUT; -%typemap(in) unsigned long &INOUT = unsigned long &INPUT; -%typemap(in) unsigned char &INOUT = unsigned char &INPUT; -%typemap(in) signed char &INOUT = signed char &INPUT; -%typemap(in) bool &INOUT = bool &INPUT; -%typemap(in) float &INOUT = float &INPUT; -%typemap(in) double &INOUT = double &INPUT; -%typemap(in) long long &INOUT = long long &INPUT; -%typemap(in) unsigned long long &INOUT = unsigned long long &INPUT; - -%typemap(argout) int *INOUT = int *OUTPUT; -%typemap(argout) short *INOUT = short *OUTPUT; -%typemap(argout) long *INOUT = long *OUTPUT; -%typemap(argout) unsigned int *INOUT = unsigned int *OUTPUT; -%typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT; -%typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT; -%typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT; -%typemap(argout) signed char *INOUT = signed char *OUTPUT; -%typemap(argout) bool *INOUT = bool *OUTPUT; -%typemap(argout) float *INOUT = float *OUTPUT; -%typemap(argout) double *INOUT = double *OUTPUT; -%typemap(argout) long long *INOUT = long long *OUTPUT; -%typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT; - -%typemap(argout) int &INOUT = int &OUTPUT; -%typemap(argout) short &INOUT = short &OUTPUT; -%typemap(argout) long &INOUT = long &OUTPUT; -%typemap(argout) unsigned int &INOUT = unsigned int &OUTPUT; -%typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT; -%typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT; -%typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT; -%typemap(argout) signed char &INOUT = signed char &OUTPUT; -%typemap(argout) bool &INOUT = bool &OUTPUT; -%typemap(argout) float &INOUT = float &OUTPUT; -%typemap(argout) double &INOUT = double &OUTPUT; -%typemap(argout) long long &INOUT = long long &OUTPUT; -%typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT; - -// -------------------------------------------------------------------- -// Special types -// -------------------------------------------------------------------- - -// If interp * appears as a function argument, we ignore it and get -// it from the wrapper function. - -/* -The typemaps.i library also provides the following mappings : - -Tcl_Interp *interp - - Passes the current Tcl_Interp value directly to a C function. - This can be used to work with existing wrapper functions or - if you just need the interp value for some reason. When used, - the 'interp' parameter becomes hidden in the Tcl interface--that - is, you don't specify it explicitly. SWIG fills in its value - automatically. - -int Tcl_Result - - Makes the integer return code of a function the return value - of a SWIG generated wrapper function. For example : - - int foo() { - ... do stuff ... - return TCL_OK; - } - - could be wrapped as follows : - - %include typemaps.i - %apply int Tcl_Result { int foo }; - int foo(); - -*/ - -%typemap(in,numinputs=0) Tcl_Interp *interp { - $1 = interp; -} - -// If return code is a Tcl_Result, simply pass it on - -%typemap(out) int Tcl_Result { - return $1; -} - -/* Overloading information */ - -%typemap(typecheck) double *INPUT = double; -%typemap(typecheck) bool *INPUT = bool; -%typemap(typecheck) signed char *INPUT = signed char; -%typemap(typecheck) unsigned char *INPUT = unsigned char; -%typemap(typecheck) unsigned long *INPUT = unsigned long; -%typemap(typecheck) unsigned short *INPUT = unsigned short; -%typemap(typecheck) unsigned int *INPUT = unsigned int; -%typemap(typecheck) long *INPUT = long; -%typemap(typecheck) short *INPUT = short; -%typemap(typecheck) int *INPUT = int; -%typemap(typecheck) float *INPUT = float; -%typemap(typecheck) long long *INPUT = long long; -%typemap(typecheck) unsigned long long *INPUT = unsigned long long; - -%typemap(typecheck) double &INPUT = double; -%typemap(typecheck) bool &INPUT = bool; -%typemap(typecheck) signed char &INPUT = signed char; -%typemap(typecheck) unsigned char &INPUT = unsigned char; -%typemap(typecheck) unsigned long &INPUT = unsigned long; -%typemap(typecheck) unsigned short &INPUT = unsigned short; -%typemap(typecheck) unsigned int &INPUT = unsigned int; -%typemap(typecheck) long &INPUT = long; -%typemap(typecheck) short &INPUT = short; -%typemap(typecheck) int &INPUT = int; -%typemap(typecheck) float &INPUT = float; -%typemap(typecheck) long long &INPUT = long long; -%typemap(typecheck) unsigned long long &INPUT = unsigned long long; - -%typemap(typecheck) double *INOUT = double; -%typemap(typecheck) bool *INOUT = bool; -%typemap(typecheck) signed char *INOUT = signed char; -%typemap(typecheck) unsigned char *INOUT = unsigned char; -%typemap(typecheck) unsigned long *INOUT = unsigned long; -%typemap(typecheck) unsigned short *INOUT = unsigned short; -%typemap(typecheck) unsigned int *INOUT = unsigned int; -%typemap(typecheck) long *INOUT = long; -%typemap(typecheck) short *INOUT = short; -%typemap(typecheck) int *INOUT = int; -%typemap(typecheck) float *INOUT = float; -%typemap(typecheck) long long *INOUT = long long; -%typemap(typecheck) unsigned long long *INOUT = unsigned long long; - -%typemap(typecheck) double &INOUT = double; -%typemap(typecheck) bool &INOUT = bool; -%typemap(typecheck) signed char &INOUT = signed char; -%typemap(typecheck) unsigned char &INOUT = unsigned char; -%typemap(typecheck) unsigned long &INOUT = unsigned long; -%typemap(typecheck) unsigned short &INOUT = unsigned short; -%typemap(typecheck) unsigned int &INOUT = unsigned int; -%typemap(typecheck) long &INOUT = long; -%typemap(typecheck) short &INOUT = short; -%typemap(typecheck) int &INOUT = int; -%typemap(typecheck) float &INOUT = float; -%typemap(typecheck) long long &INOUT = long long; -%typemap(typecheck) unsigned long long &INOUT = unsigned long long; - - - - - - - +%include diff --git a/Lib/typemaps/README b/Lib/typemaps/README new file mode 100644 index 000000000..d01da504b --- /dev/null +++ b/Lib/typemaps/README @@ -0,0 +1,16 @@ +/* ----------------------------------------------------------------------------- + * Internal typemap specializations + * ----------------------------------------------------------------------------- */ + +enumint.swg enum especializations as int types +inoutlist.swg return the OUTPUT types in a list +misctypes.swg Miscellaneos types (size_t, ptrdiff_t, etc) +primtypes.swg Macros to manage primitive types (shot,int,double,etc) +ptrtypes.swg Typemaps for types with a 'ptr' behavior +strings.swg String base typemaps +cstring.swg Various forms of C character string handling +swigobject.swg Language object, such as PyObject, Tcl_Obj, etc +swigtype.swg SWIGTYPE +valtypes.swg Typemaps for types with a 'by value' behavior +void.swg void * typemaps +implicit.swg Allow the use of implicit C++ constructors diff --git a/Lib/typemaps/cdata.swg b/Lib/typemaps/cdata.swg new file mode 100644 index 000000000..349f34507 --- /dev/null +++ b/Lib/typemaps/cdata.swg @@ -0,0 +1,79 @@ +/* ----------------------------------------------------------------------------- + * cdata.i + * + * Author(s): David Beazley (beazley@cs.uchicago.edu) + * + * This library file contains macros for manipulating raw C data as strings. + * + * $Header$ + * ----------------------------------------------------------------------------- */ + +%{ +typedef struct SWIGCDATA { + char *data; + size_t len; +} SWIGCDATA; +%} + +/* ----------------------------------------------------------------------------- + * Typemaps for returning binary data + * ----------------------------------------------------------------------------- */ + +%typemap(out,noblock=1,fragment="SWIG_FromCharPtrAndSize") SWIGCDATA { + $result = SWIG_FromCharPtrAndSize($1.data,$1.len); +} +%typemap(in) (const void *indata, int inlen) = (char *STRING, int SIZE); + + +/* ----------------------------------------------------------------------------- + * %cdata(TYPE [, NAME]) + * + * Convert raw C data to a binary string. + * ----------------------------------------------------------------------------- */ + +%define %cdata(TYPE,NAME...) + +%insert("header") { +#ifdef __cplusplus +extern "C" { +#endif +#if #NAME == "" +static SWIGCDATA cdata_##TYPE(TYPE *ptr, size_t nelements) +#else +static SWIGCDATA cdata_##NAME(TYPE *ptr, size_t nelements) +#endif +{ + SWIGCDATA d; + d.data = (char *) ptr; +#if #TYPE != "void" + d.len = nelements*sizeof(TYPE); +#else + d.len = nelements; +#endif + return d; +} +#ifdef __cplusplus +} +#endif +} + +#ifdef __cplusplus +extern "C" +#endif +#if #NAME == "" +SWIGCDATA cdata_##TYPE(TYPE *ptr, size_t nelements = 1); +#else +SWIGCDATA cdata_##NAME(TYPE *ptr, size_t nelements = 1); +#endif +%enddef + +%rename(cdata) ::cdata_void(void *ptr, size_t nelements = 1); + +%cdata(void); + +/* Memory move function */ +void memmove(void *data, const void *indata, size_t inlen); + + + + diff --git a/Lib/python/cstrbase.swg b/Lib/typemaps/cstring.swg similarity index 52% rename from Lib/python/cstrbase.swg rename to Lib/typemaps/cstring.swg index 2938d24c0..0801da86d 100644 --- a/Lib/python/cstrbase.swg +++ b/Lib/typemaps/cstring.swg @@ -10,13 +10,11 @@ * some way. */ -%include - %define %typemap_cstrings(Name, Char, SWIG_AsCharPtr, SWIG_AsCharPtrAndSize, SWIG_FromCharPtr, - SWIG_FromCharArray) + SWIG_FromCharPtrAndSize) /* %cstring_input_binary(TYPEMAP, SIZE) * @@ -30,14 +28,17 @@ */ %define Name ## _input_binary(TYPEMAP, SIZE) -%typemap(in, fragment=#SWIG_AsCharPtrAndSize) (TYPEMAP, SIZE) - (Char *buf, size_t size) -{ - SWIG_AsCharPtrAndSize($input, &buf, &size); - if (SWIG_arg_fail($argnum)) SWIG_fail; +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) (TYPEMAP, SIZE) + (Char *buf, size_t size, int alloc) { + if (SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "(TYPEMAP, SIZE)", $argnum); + } $1 = ($1_ltype) buf; $2 = ($2_ltype) size - 1; } +%typemap(freearg) (TYPEMAP, SIZE) { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} %enddef @@ -55,11 +56,14 @@ */ %define Name ## _bounded_output(TYPEMAP,MAX) -%typemap(in,numinputs=0) TYPEMAP(Char temp[MAX+1]) - "$1 = ($1_ltype) temp;"; - -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr ) TYPEMAP - "$1[MAX] = 0; $result = t_output_helper($result, SWIG_FromCharPtr($1));"; +%typemap(in,noblock=1,numinputs=0) TYPEMAP (Char temp[MAX+1]) { + $1 = ($1_ltype) temp; +} +%typemap(freearg) TYPEMAP ""; +%typemap(argout,noblock=1,fragment= #SWIG_FromCharPtr ) TYPEMAP { + $1[MAX] = 0; + SWIG_append_result(SWIG_FromCharPtr($1)); +} %enddef @@ -77,11 +81,13 @@ */ %define Name ## _chunk_output(TYPEMAP,SIZE) -%typemap(in,numinputs=0) TYPEMAP(Char temp[SIZE]) - "$1 = ($1_ltype) temp;"; - -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray) TYPEMAP - "$result = t_output_helper($result, SWIG_FromCharArray($1,SIZE));"; +%typemap(in,noblock=1,numinputs=0) TYPEMAP(Char temp[SIZE]) { + $1 = ($1_ltype) temp; +} +%typemap(freearg) TYPEMAP ""; +%typemap(argout,noblock=1,fragment= #SWIG_FromCharPtrAndSize) TYPEMAP { + SWIG_append_result(SWIG_FromCharPtrAndSize($1,SIZE)); +} %enddef @@ -102,17 +108,21 @@ %define Name ## _bounded_mutable(TYPEMAP,MAX) -%typemap(in,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP(Char temp[MAX+1]) { - Char *t = 0; size_t n; - SWIG_AsCharPtrAndSize($input, &t, &n); - if (SWIG_arg_fail($argnum)) SWIG_fail; - if ( n > (size_t)MAX ) n = (size_t)MAX; - memcpy(temp, t, sizeof(Char)*n); - temp[n] = 0; +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP (Char temp[MAX+1], Char *t, size_t n, int alloc) { + if (SWIG_AsCharPtrAndSize($input, &t, &n, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "TYPEMAP", $argnum); + } + if ( n > (size_t) MAX ) n = (size_t) MAX; + memcpy(temp, t, sizeof(Char)*n); + if (alloc == SWIG_NEWOBJ) SWIG_delete_array(t); + temp[n - 1] = 0; $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP - "$1[MAX] = 0; $result = t_output_helper($result, SWIG_FromCharPtr($1));"; +} +%typemap(freearg) TYPEMAP ""; +%typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP { + $1[MAX] = 0; + SWIG_append_result(SWIG_FromCharPtr($1)); +} %enddef @@ -133,22 +143,21 @@ */ %define Name ## _mutable(TYPEMAP,EXP...) -%typemap(in,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP { -#if #EXP == "" - const size_t expansion = 1; -#else - const size_t expansion = 1 + EXP; +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP (Char* t, size_t n, int alloc, size_t expansion = 0) { +#if #EXP != "" + expansion += EXP; #endif - Char* t = 0; size_t n = 0; - SWIG_AsCharPtrAndSize($input, &t, &n); - if (SWIG_arg_fail($argnum)) SWIG_fail; + if (SWIG_AsCharPtrAndSize($input, &t, &n, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "TYPEMAP", $argnum); + } $1 = SWIG_new_array(n+expansion, $*1_ltype); - memcpy($1,t,sizeof(Char)*n); - $1[n] = 0; -} - -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP { - $result = t_output_helper($result,SWIG_FromCharPtr($1)); + memcpy($1,t,sizeof(Char)*n); + if (alloc == SWIG_NEWOBJ) SWIG_delete_array(t); + $1[n-1] = 0; +} +%typemap(freearg) TYPEMAP ""; +%typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP { + SWIG_append_result(SWIG_FromCharPtr($1)); SWIG_delete_array($1); } %enddef @@ -165,19 +174,19 @@ */ %define Name ## _output_maxsize(TYPEMAP, SIZE) -%typemap(in,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) { - $2 = ($2_ltype) SWIG_As(unsigned long)($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = SWIG_new_array($2+1, $*1_ltype); +%typemap(in,noblock=1,fragment=SWIG_AsVal_frag(unsigned long)) (TYPEMAP, SIZE) (unsigned long size) { + if (SWIG_AsVal(unsigned long)($input, &size) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "(TYPEMAP, SIZE)", $argnum); + } + $2 = SWIG_numeric_cast(size, $2_ltype); + $1 = SWIG_new_array(size+1, $*1_ltype); } -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) (TYPEMAP,SIZE) { - $result = t_output_helper($result,SWIG_FromCharPtr($1)); +%typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) (TYPEMAP,SIZE) { + SWIG_append_result(SWIG_FromCharPtr($1)); SWIG_delete_array($1); } %enddef - - /* * %cstring_output_withsize(TYPEMAP, SIZE) * @@ -192,15 +201,16 @@ */ %define Name ## _output_withsize(TYPEMAP, SIZE) -%typemap(in,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) { - size_t n = SWIG_As(unsigned long)($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; +%typemap(in,noblock=1,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) (unsigned long n) { + if (SWIG_AsVal(unsigned long)($input, &n) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "(TYPEMAP, SIZE)", $argnum); + } $1 = SWIG_new_array(n+1, $*1_ltype); - $2 = SWIG_new($*2_ltype); - *$2 = n; + $2 = SWIG_new($*2_ltype); + *$2 = SWIG_numeric_cast(n, $*2_ltype); } -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray) (TYPEMAP,SIZE) { - $result = t_output_helper($result, SWIG_FromCharArray($1,*$2)); +%typemap(argout,noblock=1,fragment=#SWIG_FromCharPtrAndSize) (TYPEMAP,SIZE) { + SWIG_append_result(SWIG_FromCharPtrAndSize($1,*$2)); SWIG_delete_array($1); SWIG_delete($2); } @@ -221,14 +231,14 @@ */ %define Name ## _output_allocate(TYPEMAP, RELEASE) -%typemap(in,numinputs=0) TYPEMAP($*1_ltype temp = 0) - "$1 = &temp;"; - -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP { - if (*$1) { - $result = t_output_helper($result,SWIG_FromCharPtr(*$1)); +%typemap(in,noblock=1,numinputs=0) TYPEMAP($*1_ltype temp = 0) { + $1 = &temp; +} +%typemap(argout,noblock=1,fragment=#SWIG_FromCharPtr) TYPEMAP { + if (*$1) { + SWIG_append_result(SWIG_FromCharPtr(*$1)); RELEASE; - } + } } %enddef @@ -247,16 +257,16 @@ * } */ -%define Name ## _output_allocate_size(TYPEMAP, SIZE, RELEASE) -%typemap(in,numinputs=0) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) - "$1 = &temp; $2 = &tempn;"; - -%typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray)(TYPEMAP,SIZE) { - if (*$1) { - $result = t_output_helper($result,SWIG_FromCharArray(*$1,*$2)); - RELEASE; - } -} +%define Name ## _output_allocate_size(TYPEMAP, SIZE, RELEASE) +%typemap(in,noblock=1,numinputs=0) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) { + $1 = &temp; $2 = &tempn; +} +%typemap(argout,noblock=1,fragment=#SWIG_FromCharPtrAndSize)(TYPEMAP,SIZE) { + if (*$1) { + SWIG_append_result(SWIG_FromCharPtrAndSize(*$1,*$2)); + RELEASE; + } +} %enddef %enddef diff --git a/Lib/typemaps/enumint.swg b/Lib/typemaps/enumint.swg new file mode 100644 index 000000000..a9128d5cc --- /dev/null +++ b/Lib/typemaps/enumint.swg @@ -0,0 +1,64 @@ +/* ------------------------------------------------------------ + * Enums mapped as integer values + * ------------------------------------------------------------ */ + +%apply int { enum SWIGTYPE }; +%apply const int& { const enum SWIGTYPE& }; + +%typemap(in,fragment=SWIG_AsVal_frag(int),noblock=1) const enum SWIGTYPE& (int val, int ecode, $basetype temp) { + ecode = SWIG_AsVal(int)($input, &val); + if (ecode != SWIG_OK) { + SWIG_arg_fail(ecode, "$basetype", $argnum); + } else { + temp = SWIG_static_cast(val,$basetype); + $1 = &temp; + } +} + +%typemap(varin,fragment=SWIG_AsVal_frag(int),noblock=1) enum SWIGTYPE { + if (sizeof(int) != sizeof($1)) { + SWIG_var_fail(SWIG_AttributeError,"$type", "$name"); + } else { + int ecode = SWIG_AsVal(int)($input, SWIG_reinterpret_cast(&$1,int*)); + if (ecode != SWIG_OK) { + SWIG_var_fail(ecode, "$type", "$name"); + } + } +} + +/* + typemaps needed due to unnamed enums +*/ +%define SWIG_ENUM_OUT_TYPEMAPS(from_meth,frag) + %typemap(out,noblock=1,fragment=frag) enum SWIGTYPE { + $result = from_meth(($1)); + } + %typemap(out,noblock=1,fragment=frag) const enum SWIGTYPE& { + $result = from_meth((*$1)); + } + %typemap(varout,noblock=1,fragment=frag) enum SWIGTYPE, const enum SWIGTYPE& { + $result = from_meth($1); + } + %typemap(constcode,noblock=1,fragment=frag) enum SWIGTYPE { + SWIG_set_constant("$symname", from_meth($value)); + } + %typemap(directorin,noblock=1,fragment=frag) enum SWIGTYPE *DIRECTORIN { + $input = from_meth(*$1_name); + } + %typemap(directorin,noblock=1,fragment=frag) enum SWIGTYPE, const enum SWIGTYPE& { + $input = from_meth($1_name); + } + %typemap(throws,noblock=1,fragment=frag) enum SWIGTYPE { + SWIG_exception_obj(0,"$type", from_meth($1)); + } +%enddef + +SWIG_ENUM_OUT_TYPEMAPS(SWIG_From(int),SWIG_From_frag(int)); + + + + + + + + diff --git a/Lib/typemaps/implicit.swg b/Lib/typemaps/implicit.swg new file mode 100644 index 000000000..857dcda0f --- /dev/null +++ b/Lib/typemaps/implicit.swg @@ -0,0 +1,204 @@ +/* + The %implict macro allows a SwigType (Class) to be accepted + as an input parameter and use its implicit constructors when needed. + + For example: + + + %implicit(A, int, double, B); + + %inline + { + struct B { }; + struct A + { + int ii; + A(int i) { ii = 1; } + A(double d) { ii = 2; } + A(const B& b) { ii = 3; } + }; + + int get(A a) { return a.ii; } + } + + Here, you can call 'get' as + + get(1) ==> get(A(1)) + get(2.0) ==> get(A(2.0)) + get(B()) ==> get(A(B())) + + and swig will construct an 'A' temporal variable using the + corresponding implicit constructor. + + + The plain implicit macro takes care of simple type list. If it doesn't + work because you are passing template types with commas, then use + the %implicit_{1,2,3} versions and/or the SWIG_arg macro. + +*/ + +%check_swig_object() + +%define %implicit_type(Type...) +%traits_swigtype(Type); +%enddef + +%define %implicit_frag(Type...) ,fragment=SWIG_Traits_frag(Type) %enddef + +%define %implicit_code(Type...) + if (swig::asval(obj, 0)) { + Type _v; + swig::asval(obj, &_v); + if (val) *val = new value_type(_v); + return SWIG_NEWOBJ; + } +%enddef + +/* implicit */ + +%define %implicit(Type, ...) + +%formacro_1(%implicit_type,__VA_ARGS__); + +%fragment(SWIG_Traits_frag(Type),"header", + fragment="StdTraits" + %formacro_1(%implicit_frag,__VA_ARGS__)) %{ +namespace swig { + template <> struct traits { + typedef pointer_category category; + static const char* type_name() { return "Type"; } + }; + + template <> struct traits_asptr< Type > { + typedef Type value_type; + static int asptr(SWIG_Object obj, value_type **val) { + Type *vptr; + static swig_type_info* desc = SWIG_TypeQuery("Type *"); + if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) == SWIG_OK)) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + %formacro_1(%implicit_code,__VA_ARGS__) + } + return 0; + } + }; +} +%} + +%typemap_traits_ptr(SWIG_CCode(POINTER),Type); +%enddef + +/* implicit_1 */ + + +%define %implicit_1(Type, Imp1) +%traits_swigtype(Imp1); + +%fragment(SWIG_Traits_frag(Type),"header", + fragment="StdTraits", + fragment=SWIG_Traits_frag(Imp1)) %{ +namespace swig { + template <> struct traits< Type > { + typedef pointer_category category; + static const char* type_name() { return "Type"; } + }; + + template <> struct traits_asptr< Type > { + typedef Type value_type; + static int asptr(SWIG_Object obj, value_type **val) { + Type *vptr; + static swig_type_info* desc = SWIG_TypeQuery("Type *"); + if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) == SWIG_OK)) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + %implicit_code(Imp1); + } + return 0; + } + }; +} +%} + +%typemap_traits_ptr(SWIG_CCode(POINTER),Type); + +%enddef + +/* implicit_2 */ + +%define %implicit_2(Type, Imp1, Imp2) +%traits_swigtype(Imp1); +%traits_swigtype(Imp2); + +%fragment(SWIG_Traits_frag(Type),"header", + fragment="StdTraits", + fragment=SWIG_Traits_frag(Imp1), + fragment=SWIG_Traits_frag(Imp2)) %{ +namespace swig { + template <> struct traits< Type > { + typedef pointer_category category; + static const char* type_name() { return "Type"; } + }; + + template <> struct traits_asptr< Type > { + typedef Type value_type; + static int asptr(SWIG_Object obj, value_type **val) { + Type *vptr; + static swig_type_info* desc = SWIG_TypeQuery("Type *"); + if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) == SWIG_OK)) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + %implicit_code(Imp1); + %implicit_code(Imp2); + } + return 0; + } + }; +} +%} + +%typemap_traits_ptr(SWIG_CCode(POINTER),Type); +%enddef + + +/* implicit_3 */ + +%define %implicit_3(Type, Imp1, Imp2, Imp3) +%traits_swigtype(Imp1); +%traits_swigtype(Imp2); +%traits_swigtype(Imp3); + +%fragment(SWIG_Traits_frag(Type),"header", + fragment="StdTraits", + fragment=SWIG_Traits_frag(Imp1), + fragment=SWIG_Traits_frag(Imp2), + fragment=SWIG_Traits_frag(Imp3)) %{ +namespace swig { + template <> struct traits< Type > { + typedef pointer_category category; + static const char* type_name() { return "Type"; } + }; + + template <> struct traits_asptr< Type > { + typedef Type value_type; + static int asptr(SWIG_Object obj, value_type **val) { + Type *vptr; + static swig_type_info* desc = SWIG_TypeQuery("Type *"); + if ((SWIG_ConvertPtr(obj, (void **)&vptr, desc, 0) == SWIG_OK)) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + %implicit_code(Imp1); + %implicit_code(Imp2); + %implicit_code(Imp3); + } + return 0; + } + }; +} +%} + +%typemap_traits_ptr(SWIG_CCode(POINTER),Type); +%enddef diff --git a/Lib/typemaps/inoutlist.swg b/Lib/typemaps/inoutlist.swg new file mode 100644 index 000000000..df35f3898 --- /dev/null +++ b/Lib/typemaps/inoutlist.swg @@ -0,0 +1,278 @@ +/* ------------------------------------------------------------ + * + * Define the IN/OUTPUT typemaps assuming the output parameters are + * returned in a list, i.e., they are not directly modified. + * + * The user should provide the SWIG_append_result(result, obj) method, + * via a macro, which append a particular object to the result. + * + * + * In Tcl, for example, the file is used as: + * + * #define SWIG_append_result(obj) Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),obj); + * %include + * + * while in Python it is used as: + * + * #define SWIG_append_result(obj) $result = SWIG_Python_AppendResult($result, obj) + * %include + * + * where the method SWIG_Python_AppendResult is defined inside the + * SWIG_append_result fragment. + * + * If you forget to define SWIG_append_result, this file will generate + * an error. + * + * ------------------------------------------------------------ */ + + +#ifndef SWIG_append_result +#error Undefined Method SWIG_append_result: used in the OUTPUT typemaps +#endif + + +// +// Uncomment the following definition if you don't want the in/out +// typemaps by default, ie, you prefer to use typemaps.i. +// +//#define SWIG_INOUT_NODEF + +// +// Use the following definition to enable the INPUT parameters to +// accept both 'by value' and 'pointer' objects. +// +#define SWIG_INPUT_ACCEPT_PTRS + +// ------------------------------------------------------------------------ +// Pointer handling +// +// These mappings provide support for input/output arguments and common +// uses for C/C++ pointers. +// ------------------------------------------------------------------------ + +// INPUT typemaps. +// These remap a C pointer to be an "INPUT" value which is passed by value +// instead of reference. + +/* +The following methods can be applied to turn a pointer into a simple +"input" value. That is, instead of passing a pointer to an object, +you would use a real value instead. + +To use these, suppose you had a C function like this : + + double fadd(double *a, double *b) { + return *a+*b; + } + +You could wrap it with SWIG as follows : + + double fadd(double *INPUT, double *INPUT); + +or you can use the %apply directive : + + %apply double *INPUT { double *a, double *b }; + double fadd(double *a, double *b); + +*/ +#ifdef SWIG_INPUT_ACCEPT_PTRS +#define SWIG_CheckInputPtr(input,arg,desc,disown) (SWIG_ConvertPtr(input,SWIG_as_voidptrptr(arg),desc,disown) == SWIG_OK) +#else +#define SWIG_CheckInputPtr(input,arg,desc,disown) (0) +#endif + +%define _SWIG_VALUE_INPUT_TYPEMAP(code, asval_meth, asval_frag, Type) + %typemap(in,noblock=1,fragment=asval_frag) Type *INPUT ($*ltype temp, int res = 0) { + if (!SWIG_CheckInputPtr($input,&$1,$descriptor,$disown)) { + Type val; + int ecode = asval_meth($input, &val); + if (ecode != SWIG_OK) { + SWIG_arg_fail(ecode, "$*ltype",$argnum); + } + temp = SWIG_static_cast(val, $*ltype); + $1 = &temp; + res = SWIG_NEWOBJ; + } + } + %typemap(in,noblock=1,fragment=asval_frag) Type &INPUT($*ltype temp, int res = 0) { + if (!SWIG_CheckInputPtr($input,&$1,$descriptor,$disown)) { + Type val; + int ecode = asval_meth($input, &val); + if (ecode != SWIG_OK) { + SWIG_arg_fail(ecode, "$*ltype",$argnum); + } + temp = SWIG_static_cast(val, $*ltype); + $1 = &temp; + res = SWIG_NEWOBJ; + } + } + %typemap(typecheck,noblock=1,precedence=code,fragment=asval_frag) Type *INPUT, Type &INPUT { + void *ptr; + $1 = ((asval_meth($input, 0) == SWIG_OK) || (SWIG_CheckInputPtr($input,&ptr,$1_descriptor,0))); + } +%enddef + +%define _SWIG_PTR_INPUT_TYPEMAP(code,asptr_meth,asptr_frag,Type) + %typemap(in,noblock=1,fragment=asptr_frag) Type *INPUT(int res = 0) { + res = asptr_meth($input, &$1); + if (!res) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + } + %typemap(in,noblock=1,fragment=asptr_frag) Type &INPUT(int res = 0) { + res = asptr_meth($input, &$1); + if (!res) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + if (!$1) { + SWIG_arg_nullref("$type",$argnum); + } + } + %typemap(freearg,noblock=1) Type *INPUT, Type &INPUT { + if (res$argnum == SWIG_NEWOBJ) SWIG_delete($1); + } + %typemap(typecheck,noblock=1,precedence=code,fragment=asptr_frag) Type *INPUT, Type &INPUT { + $1 = asptr_meth($input, (Type**)0) != 0; + } +%enddef + +// OUTPUT typemaps. These typemaps are used for parameters that +// are output only. The output value is appended to the result as +// a list element. + +/* +The following methods can be applied to turn a pointer into an "output" +value. When calling a function, no input value would be given for +a parameter, but an output value would be returned. In the case of +multiple output values, they are returned in the form of a list. + + +For example, suppose you were trying to wrap the modf() function in the +C math library which splits x into integral and fractional parts (and +returns the integer part in one of its parameters).K: + + double modf(double x, double *ip); + +You could wrap it with SWIG as follows : + + double modf(double x, double *OUTPUT); + +or you can use the %apply directive : + + %apply double *OUTPUT { double *ip }; + double modf(double x, double *ip); + +The output of the function would be a list containing both output +values. + +*/ + +%define _SWIG_VALUE_OUTPUT_TYPEMAP(from_meth, from_frag, Type) + %typemap(in,numinputs=0,noblock=1) + Type *OUTPUT ($*1_ltype temp, int res = SWIG_NEWOBJ), + Type &OUTPUT ($*1_ltype temp, int res = SWIG_NEWOBJ) { + $1 = &temp; + } + %typemap(argout,noblock=1,fragment=from_frag) Type *OUTPUT, Type &OUTPUT { + SWIG_append_result(((res$argnum == SWIG_NEWOBJ) ? + from_meth((*$1)) : SWIG_NewPointerObj((void*)($1), $1_descriptor, 0))); + } +%enddef + + +// INOUT +// Mappings for an argument that is both an input and output +// parameter + +/* +The following methods can be applied to make a function parameter both +an input and output value. This combines the behavior of both the +"INPUT" and "OUTPUT" methods described earlier. Output values are +returned in the form of a list. + +For example, suppose you were trying to wrap the following function : + + void neg(double *x) { + *x = -(*x); + } + +You could wrap it with SWIG as follows : + + void neg(double *INOUT); + +or you can use the %apply directive : + + %apply double *INOUT { double *x }; + void neg(double *x); + +Unlike C, this mapping does not directly modify the input value. +Rather, the modified input value shows up as the return value of the +function. Thus, to apply this function to a variable you might do +this : + + x = neg(x) + +Note : previous versions of SWIG used the symbol 'BOTH' to mark +input/output arguments. This is still supported, but will be slowly +phased out in future releases. + +*/ + +%define _SWIG_VALUE_INOUT_TYPEMAP(Type) + %typemap(in) Type *INOUT = Type *INPUT; + %typemap(in) Type &INOUT = Type &INPUT; + %typemap(typecheck) Type *INOUT = Type *INPUT; + %typemap(typecheck) Type &INOUT = Type &INPUT; + %typemap(argout) Type *INOUT = Type *OUTPUT; + %typemap(argout) Type &INOUT = Type &OUTPUT; +%enddef + + +%define _SWIG_PTR_INOUT_TYPEMAP(Type) + _SWIG_VALUE_INOUT_TYPEMAP(SWIG_arg(Type)) + %typemap(freearg) Type *INOUT = Type *INPUT; + %typemap(freearg) Type &INOUT = Type &INPUT; +%enddef + +#ifndef SWIG_INOUT_NODEF +#define SWIG_VALUE_INPUT_TYPEMAP(code,_a,_af,...) \ + _SWIG_VALUE_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a), \ + SWIG_arg(_af),SWIG_arg(__VA_ARGS__)) + +#define SWIG_PTR_INPUT_TYPEMAP(code,_a,_af,...) \ + _SWIG_PTR_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_af), \ + SWIG_arg(__VA_ARGS__)) + +#define SWIG_VALUE_OUTPUT_TYPEMAP(_f,_ff,...) \ + _SWIG_VALUE_OUTPUT_TYPEMAP(SWIG_arg(_f),SWIG_arg(_ff),SWIG_arg(__VA_ARGS__)) + +#define SWIG_VALUE_INOUT_TYPEMAP(...) _SWIG_VALUE_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__)) +#define SWIG_PTR_INOUT_TYPEMAP(...) _SWIG_PTR_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__)) + +#else /* You need to include typemaps.i */ + + +#define SWIG_VALUE_OUTPUT_TYPEMAP(...) +#define SWIG_VALUE_INPUT_TYPEMAP(...) +#define SWIG_VALUE_INOUT_TYPEMAP(...) +#define SWIG_PTR_INPUT_TYPEMAP(...) +#define SWIG_PTR_INOUT_TYPEMAP(...) + +#endif /* SWIG_INOUT_DEFAULT */ + + +%define %typemap_inout(Code, AsValMeth, FromMeth, AsValFrag, FromFrag, Type...) + _SWIG_VALUE_INPUT_TYPEMAP(SWIG_arg(Code), SWIG_arg(AsValMeth), + SWIG_arg(AsValFrag), SWIG_arg(Type)); + _SWIG_VALUE_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), SWIG_arg(Type)); + _SWIG_VALUE_INOUT_TYPEMAP(SWIG_arg(Type)); +%enddef + +%define %typemap_inoutn(Code,Type...) + %typemap_inout(SWIG_arg(Code), + SWIG_arg(SWIG_AsVal(Type)), + SWIG_arg(SWIG_From(Type)), + SWIG_arg(SWIG_AsVal_frag(Type)), + SWIG_arg(SWIG_From_frag(Type)), + SWIG_arg(Type)); +%enddef diff --git a/Lib/python/pymisctypes.swg b/Lib/typemaps/misctypes.swg similarity index 89% rename from Lib/python/pymisctypes.swg rename to Lib/typemaps/misctypes.swg index 001f7b285..6f366783b 100644 --- a/Lib/python/pymisctypes.swg +++ b/Lib/typemaps/misctypes.swg @@ -3,23 +3,19 @@ * --- ANSI/Posix C/C++ types --- * ------------------------------------------------------------ */ -%types(size_t); %apply unsigned long { size_t }; %apply const unsigned long& { const size_t& }; %apply unsigned long& { size_t& }; -%types(ptrdiff_t); %apply long { ptrdiff_t }; %apply const long& { const ptrdiff_t& }; %apply long& { ptrdiff_t& }; #ifdef __cplusplus -%types(std::size_t); %apply unsigned long { std::size_t }; %apply const unsigned long& { const std::size_t& }; %apply unsigned long& { std::size_t& }; -%types(std::ptrdiff_t); %apply long { std::ptrdiff_t }; %apply const long& { const std::ptrdiff_t& }; %apply long& { std::ptrdiff_t& }; diff --git a/Lib/typemaps/primtypes.swg b/Lib/typemaps/primtypes.swg new file mode 100644 index 000000000..183d76a5e --- /dev/null +++ b/Lib/typemaps/primtypes.swg @@ -0,0 +1,132 @@ +/* ------------------------------------------------------------ + * typemap for primitive type with no pointer representation + * ------------------------------------------------------------ */ + +%define %typemap_primitive(Code, Type...) +%typemap_asvalfromn(SWIG_arg(Code), Type); +%enddef + +/* ------------------------------------------------------------ + * Primitive Type Macros + * ------------------------------------------------------------ */ + +/* useful macros to derive typemap declarations from primitive types */ + +%define _apply_macro(macro, arg2, arg1...) +#if #arg1 != "" + macro(SWIG_arg(arg1),arg2); +#else + macro(arg2); +#endif +%enddef + +/* Apply macro to the order types */ +%define %apply_ctypes(Macro,...) +_apply_macro(Macro, bool , __VA_ARGS__); +_apply_macro(Macro, signed char , __VA_ARGS__); +_apply_macro(Macro, unsigned char , __VA_ARGS__); +_apply_macro(Macro, short , __VA_ARGS__); +_apply_macro(Macro, unsigned short , __VA_ARGS__); +_apply_macro(Macro, int , __VA_ARGS__); +_apply_macro(Macro, unsigned int , __VA_ARGS__); +_apply_macro(Macro, long , __VA_ARGS__); +_apply_macro(Macro, unsigned long , __VA_ARGS__); +_apply_macro(Macro, long long , __VA_ARGS__); +_apply_macro(Macro, unsigned long long , __VA_ARGS__); +_apply_macro(Macro, float , __VA_ARGS__); +_apply_macro(Macro, double , __VA_ARGS__); +_apply_macro(Macro, char , __VA_ARGS__); +_apply_macro(Macro, wchar_t , __VA_ARGS__); +%enddef + +/* apply the Macro(Type) to all the C++ types */ +%define %apply_cpptypes(Macro,...) +%apply_ctypes(Macro, __VA_ARGS__) +_apply_macro(Macro, std::string, __VA_ARGS__); +_apply_macro(Macro, std::complex, __VA_ARGS__); +_apply_macro(Macro, std::complex, __VA_ARGS__); +%enddef + +/* apply the Macro2(Type1, Type2) to all the C++ types */ +%define %apply_cpptypes_2(Macro2) +%apply_cpptypes(%apply_cpptypes, Macro2) +%enddef + +%define %apply_checkctypes(Macro) +Macro(SWIG_CCode(BOOL), bool); +Macro(SWIG_CCode(INT8), signed char); +Macro(SWIG_CCode(UINT8), unsigned char); +Macro(SWIG_CCode(INT16), short); +Macro(SWIG_CCode(UINT16), unsigned short); +Macro(SWIG_CCode(INT32), int); +Macro(SWIG_CCode(UINT32), unsigned int); +Macro(SWIG_CCode(INT64), long); +Macro(SWIG_CCode(UINT64), unsigned long); +Macro(SWIG_CCode(INT128), long long); +Macro(SWIG_CCode(UINT128), unsigned long long); +Macro(SWIG_CCode(FLOAT), float); +Macro(SWIG_CCode(DOUBLE), double); +Macro(SWIG_CCode(CHAR), char); +Macro(SWIG_CCode(UNICHAR), wchar_t); +%enddef + + + +/* ------------------------------------------------------------ + * common fragments and macro helpers + * ------------------------------------------------------------ */ + + +%fragment("","header") %{ +#include +%} + +%fragment("","header") %{ +#include +%} + +%fragment("","header") %{ +#include +%} + +/* Macros for derived types */ + +%define %derived_type_from(Base, Type) +%fragment(SWIG_From_frag(Type),"header", + fragment=SWIG_From_frag(Base)) { + SWIG_define(SWIG_From_dec(Type), SWIG_From_dec(Base)) +} +%enddef + +%define %derived_type_asval(Base, Type, Frag, OverflowCond) +%check_swig_object() + +%fragment(SWIG_AsVal_frag(Type),"header", + fragment=Frag, + fragment=SWIG_AsVal_frag(Base)) { +SWIGINTERN int +SWIG_AsVal_dec(Type)(SWIG_Object obj, Type *val) +{ + Base v; + int res = SWIG_AsVal(Base)(obj, &v); + if (res == SWIG_OK) { + if (OverflowCond) { + return SWIG_OverflowError; + } else { + if (val) *val = SWIG_numeric_cast(v, Type); + return SWIG_OK; + } + } + return res; +} +} +%enddef + +%define %signed_derived_type_asval(Base, Type, Frag, Min, Max) +%derived_type_asval(Base, Type, Frag, (v < Min || v > Max)) +%enddef + +%define %unsigned_derived_type_asval(Base, Type, Frag, Max) +%derived_type_asval(Base, Type, Frag, (v > Max)) +%enddef + diff --git a/Lib/typemaps/ptrtypes.swg b/Lib/typemaps/ptrtypes.swg new file mode 100644 index 000000000..d8eac2c60 --- /dev/null +++ b/Lib/typemaps/ptrtypes.swg @@ -0,0 +1,160 @@ +/* + Value typemaps (Type, const Type&) for "Ptr" types, such as swig + wrapped classes, that define the AsPtr/From methods +*/ + +/* in */ + +%define SWIG_PTR_IN_TYPEMAP(asptr_meth,frag,Type...) + %typemap(in,fragment=frag) Type { + Type *ptr = (Type *)0; + int res = asptr_meth($input, &ptr); + if (!res || !ptr) { SWIG_arg_fail(SWIG_TypeError, "$type", $argnum); } + $1 = *ptr; + if (res == SWIG_NEWOBJ) SWIG_delete(ptr); + } + %typemap(in,fragment=frag) const Type & (int res = 0) { + Type *ptr = (Type *)0; + res = asptr_meth($input, &ptr); + if (!res) { SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); } + if (!ptr) { SWIG_arg_nullref("$type",$argnum); } + $1 = ptr; + } + %typemap(freearg,noblock=1) const Type & { + if (res$argnum == SWIG_NEWOBJ) SWIG_delete($1); + } +%enddef + +/* varin */ + +%define SWIG_PTR_VARIN_TYPEMAP(asptr_meth,frag,Type...) + %typemap(varin,fragment=frag) Type { + Type *ptr = (Type *)0; + int res = asptr_meth($input, &ptr); + if (!res || !ptr) { SWIG_var_fail(SWIG_TypeError, "$type", "$name"); } + $1 = *ptr; + if (res == SWIG_NEWOBJ) SWIG_delete(ptr); + } +%enddef + +#ifdef SWIG_DIRECTOR_TYPEMAPS +/* directorout */ + +%define SWIG_PTR_DIRECTOROUT_TYPEMAP(asptr_meth,frag,Type...) + %typemap(directorargout,fragment=frag) Type *DIRECTOROUT ($*1_ltype temp) { + Type *ptr = 0; + int res = $input ? asptr_meth($input, &ptr) : 0; + if (!res || !ptr) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } else { + temp = *ptr; + $result = &temp; + if (res == SWIG_NEWOBJ) SWIG_delete(ptr); + } + } + + %typemap(directorout,fragment=frag) Type { + Type *ptr = 0; + int res = asptr_meth($input, &ptr); + if (!res || !ptr) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } else { + $result = *ptr; + if (res == SWIG_NEWOBJ) SWIG_delete(ptr); + } + } + + %typemap(directorout,fragment=frag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& { + Type *ptr = 0; + int res = asptr_meth($input, &ptr); + if (!res) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } else { + if (!ptr) { + SWIG_dout_nullref("$type"); + } else { + if (res == SWIG_NEWOBJ) { + /* Possible thread/reentrant problem here! */ + static $*ltype temp = *ptr; + $result = &temp; + SWIG_delete(ptr); + } else { + $result = ptr; + } + } + } + } + + %typemap(directorout,fragment=frag) Type &DIRECTOROUT = Type + +%enddef + +#else + +#define SWIG_PTR_DIRECTOROUT_TYPEMAP(asptr_meth,frag,Type...) + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + +/* typecheck */ + +%define SWIG_PTR_TYPECHECK_TYPEMAP(check,asptr_meth,frag,Type...) +%typemap(typecheck,precedence=check,fragment=frag) + Type, const Type& + "$1 = asptr_meth($input, (Type**)(0));"; +%enddef + +/* + typemap definition for types with AsPtr/From methods + */ + +%define %typemap_asptrfrom(CheckCode, AsPtrMeth, FromMeth, AsPtrFrag, FromFrag, Type...) + %fragment(SWIG_AsVal_frag(Type),"header", + fragment=SWIG_AsPtr_frag(Type)) %{ + SWIGINTERNINLINE int + SWIG_AsVal(Type)(SWIG_Object obj, Type *val) + { + Type *v = (Type *)0; + int res = SWIG_AsPtr(Type)(obj, &v); + if (!res || !v) return 0; + if (val) { + *val = *v; + if (res == SWIG_NEWOBJ) SWIG_delete(v); + } + return 1; + } + %} + %fragment(SWIG_As_frag(Type),"header", + fragment=SWIG_AsVal_frag(Type)) %{ + SWIGINTERNINLINE Type + SWIG_As(Type)(SWIG_Object obj) + { + Type v; + SWIG_AsVal(Type)(obj, &v); + return v; + } + %} + SWIG_PTR_IN_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type); + SWIG_PTR_VARIN_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type); + SWIG_PTR_DIRECTOROUT_TYPEMAP(SWIG_arg(AsPtrMeth), SWIG_arg(AsPtrFrag), Type); + SWIG_PTR_TYPECHECK_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(AsPtrMeth), + SWIG_arg(AsPtrFrag), Type); + %typemap_from(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + + SWIG_PTR_INPUT_TYPEMAP(SWIG_arg(CheckCode),SWIG_arg(AsPtrMeth), + SWIG_arg(AsPtrFrag),Type); + SWIG_VALUE_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_PTR_INOUT_TYPEMAP(Type); +%enddef + +/* + typemap for simple swig types with only AsPtr/From methods +*/ + +%define %typemap_asptrfromn(CheckCode, Type...) +%typemap_asptrfrom(SWIG_arg(CheckCode), + SWIG_arg(SWIG_AsPtr(Type)), + SWIG_arg(SWIG_From(Type)), + SWIG_arg(SWIG_AsPtr_frag(Type)), + SWIG_arg(SWIG_From_frag(Type)), + Type); +%enddef diff --git a/Lib/typemaps/std_string.swg b/Lib/typemaps/std_string.swg new file mode 100644 index 000000000..0342b6184 --- /dev/null +++ b/Lib/typemaps/std_string.swg @@ -0,0 +1,69 @@ +// +// String +// + + + + + +/* defining the String asptr/from methods */ + +%define %std_string_asptr_frag(String, Char, SWIG_AsCharPtrAndSize, Frag) +%check_swig_object() +%fragment(SWIG_AsPtr_frag(String),"header",fragment=Frag) { +SWIGINTERN int +SWIG_AsPtr_dec(String)(SWIG_Object obj, String **val) +{ + static swig_type_info* string_info = SWIG_TypeQuery(#String " *"); + String *vptr; + if (SWIG_ConvertPtr(obj, (void**)&vptr, string_info, 0) != -1) { + if (val) *val = vptr; + return SWIG_OLDOBJ; + } else { + Char* buf = 0 ; size_t size = 0; int alloc = SWIG_OLDOBJ; + if (SWIG_AsCharPtrAndSize(obj, &buf, &size, &alloc) == SWIG_OK) { + if (buf) { + if (val) *val = new String(buf, size - 1); + if (alloc == SWIG_NEWOBJ) SWIG_delete_array(buf); + return SWIG_NEWOBJ; + } + } + return 0; + } +} +} +%enddef + +%define %std_string_from_frag(String, SWIG_FromCharPtrAndSize, Frag) +%check_swig_object() +%fragment(SWIG_From_frag(String),"header",fragment=Frag) { +SWIGINTERNINLINE SWIG_Object +SWIG_From_dec(String)(const String& s) +{ + return SWIG_FromCharPtrAndSize(s.data(), s.size()); +} +} +%enddef + +%define %std_string_asval_frag(String, Frag) +%check_swig_object() +%fragment(SWIG_AsVal_frag(String),"header", fragment=Frag) { +SWIGINTERN int +SWIG_AsVal_dec(String)(SWIG_Object obj, String *val) +{ + String* s; + int res = SWIG_AsPtr(String)(obj, &s); + if ((res != 0) && s) { + if (val) *val = *s; + if (res == SWIG_NEWOBJ) delete s; + return SWIG_OK; + } + return SWIG_TypeError; +} +} +%enddef + + +#define %std_string_asptr(String, Char, Method) %std_string_asptr_frag(String, Char, Method, #Method) +#define %std_string_asval(String) %std_string_asval_frag(String, SWIG_AsPtr_frag(String)) +#define %std_string_from(String, Method) %std_string_from_frag(String, Method, #Method) diff --git a/Lib/typemaps/strings.swg b/Lib/typemaps/strings.swg new file mode 100644 index 000000000..898682e2f --- /dev/null +++ b/Lib/typemaps/strings.swg @@ -0,0 +1,505 @@ +// +// Use the macro SWIG_PRESERVE_CARRAY_SIZE if you prefer to preserve +// the size of char arrays, ie +// ------------------------------------------ +// C Side => Language Side +// ------------------------------------------ +// char name[5] = "hola" => 'hola\0' +// +// the default behaviour is +// +// char name[5] = "hola" => 'hola' +// +// +//#define SWIG_PRESERVE_CARRAY_SIZE + +/* ------------------------------------------------------------ + * String typemaps for type Char (char or wchar_t) + * ------------------------------------------------------------ */ + +%define %_typemap_string(Char, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtrAndSize, + SWIG_CharPtrLen, + SWIG_AsCharPtr, + SWIG_FromCharPtr, + SWIG_AsCharArray) + +/* in */ + +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtr) + Char * (Char *buf = 0, int alloc = 0), + const Char * (Char *buf = 0, int alloc = SWIG_OLDOBJ) { + if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $1 = buf; +} +%typemap(freearg,noblock=1) Char *, const Char * { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} + +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtr) Char const*& (Char *buf, int alloc) +{ + if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $1 = &temp; +} +%typemap(freearg, noblock=1) Char const*& { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} + +/* out */ + +%typemap(out,noblock=1,fragment=#SWIG_FromCharPtr) Char *, const Char* { + SWIG_set_result(SWIG_FromCharPtr($1)); +} + + +%typemap(out,noblock=1,fragment=#SWIG_FromCharPtr) Char const*& { + SWIG_set_result(SWIG_FromCharPtr(*$1)); +} + + +/* varin */ + +%typemap(varin,noblock=1,fragment=#SWIG_AsCharPtrAndSize) Char * { + Char *cptr = 0; size_t csize = 0; int alloc = SWIG_NEWOBJ; + if (SWIG_AsCharPtrAndSize($input, &cptr, &csize, &alloc) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError,"$type","$name"); + } + if ($1) SWIG_delete_array($1); + if (alloc == SWIG_NEWOBJ) { + $1 = cptr; + } else { + $1 = csize ? SWIG_new_copy_array(cptr, csize, Char) : 0; + } +} + +%typemap(varin,noblock=1,fragment=#SWIG_AsCharPtrAndSize,warning="451:Setting const Char * variable may leak memory") const Char * { + Char *cptr = 0; size_t csize = 0; int alloc = SWIG_NEWOBJ; + if (SWIG_AsCharPtrAndSize($input, &cptr, &csize, &alloc) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } + if (alloc == SWIG_NEWOBJ) { + $1 = cptr; + } else { + $1 = csize ? SWIG_new_copy_array(cptr, csize, Char) : 0; + } +} + +/* varout */ + +%typemap(varout,noblock=1,fragment=#SWIG_FromCharPtr) Char*, const Char* { + $result = SWIG_FromCharPtr($1); +} + +/* meberin */ + +%typemap(memberin,noblock=1) Char * { + if ($1) SWIG_delete_array($1); + if ($input) { + size_t size = SWIG_CharPtrLen($input) + 1; + $1 = SWIG_new_copy_array($input, size, Char); + } else { + $1 = 0; + } +} + +%typemap(memberin,noblock=1,warning="451:Setting const char * member may leak memory.") const Char * { + if ($input) { + size_t size = SWIG_CharPtrLen($input) + 1; + $1 = SWIG_new_copy_array($input, size, Char); + } else { + $1 = 0; + } +} + +/* globalin */ + +%typemap(globalin,noblock=1) Char * { + if ($1) SWIG_delete_array($1); + if ($input) { + size_t size = SWIG_CharPtrLen($input) + 1; + $1 = SWIG_new_copy_array($input, size, Char); + } else { + $1 = 0; + } +} + +%typemap(globalin,noblock=1,warning="451:Setting const char * variable may leak memory.") const Char * { + if ($input) { + size_t size = SWIG_CharPtrLen($input) + 1; + $1 = SWIG_new_copy_array($input, size, Char); + } else { + $1 = 0; + } +} + +/* constant */ + +%typemap(constcode,noblock=1,fragment=#SWIG_FromCharPtr) + Char *, Char const*, Char * const, Char const* const { + SWIG_set_constant("$symname", SWIG_FromCharPtr($value)); +} + + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +/* directorin */ + +%typemap(directorin,fragment=#SWIG_FromCharPtr,noblock=1) + Char *, Char const*, Char *const, Char const *const, + Char const *&, Char *const &, Char const *const & { + $input = SWIG_FromCharPtr($1_name); +} + + +/* directorout */ + +%typemap(directorout,fragment=#SWIG_AsCharPtr,noblock=1) Char * (Char* buf, int alloc) { + if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError, "$type"); + } + $result = buf; +} + +%typemap(directorout,fragment=#SWIG_AsCharPtr) Char * const& (Char* buf, int alloc) { + if (SWIG_AsCharPtr($input, &buf, &alloc) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError, "$type"); + } + $result = ($1_ltype) &buf; +} + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + +/* typecheck */ + +%typemap(typecheck,noblock=1,precedence=SWIG_TYPECHECK_STRING, + fragment=#SWIG_AsCharPtr) Char *, Char const*& { + $1 = (SWIG_AsCharPtr($input, 0, 0) == SWIG_OK); +} + + +/* throws */ + +%typemap(throws,noblock=1,fragment=#SWIG_FromCharPtr) Char * { + SWIG_exception_obj(0, "$type", SWIG_FromCharPtr($1)); +} + + +/* ------------------------------------------------------------ + * Unknown size const Character array Char[ANY] handling + * ------------------------------------------------------------ */ + +%apply Char* { Char [] }; +%apply const Char* { const Char [] }; + +%typemap(varin,noblock=1,warning="462:Unable to set variable of type Char []") Char [] +{ + SWIG_var_fail(SWIG_AttributeError, "$type", "read-only $name"); +} + + +/* ------------------------------------------------------------ + * Fix size Character array Char[ANY] handling + * ------------------------------------------------------------ */ + +/* memberin and globalin typemaps */ + +%typemap(memberin,noblock=1) Char [ANY] +{ + if ($input) memcpy($1,$input,$1_dim0*sizeof(Char)); + else memset($1,0,$1_dim0*sizeof(Char)); +} + +%typemap(globalin,noblock=1) Char [ANY] +{ + if ($input) memcpy($1,$input,$1_dim0*sizeof(Char)); + else memset($1,0,$1_dim0*sizeof(Char)); +} + +/* in */ + +%typemap(in,noblock=1,fragment=#SWIG_AsCharArray) + Char [ANY] (Char temp[$1_dim0]), + const Char [ANY](Char temp[$1_dim0]) +{ + if (SWIG_AsCharArray($input, temp, $1_dim0) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $1 = temp; +} + +%typemap(in,noblock=1,fragment=#SWIG_AsCharArray) const Char (&)[ANY] (Char temp[$1_dim0]) +{ + if (SWIG_AsCharArray($input, temp, $1_dim0) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $1 = &temp; +} + +%typemap(out,fragment=#SWIG_FromCharPtrAndSize) + Char [ANY], const Char[ANY] +{ + size_t size = $1_dim0; +%#ifndef SWIG_PRESERVE_CARRAY_SIZE + while (size && ($1[size - 1] == '\0')) --size; +%#endif + SWIG_set_result(SWIG_FromCharPtrAndSize($1, size)); +} + +/* varin */ + +%typemap(varin,noblock=1,fragment=#SWIG_AsCharArray) Char [ANY] +{ + if (SWIG_AsCharArray($input, $1, $1_dim0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } +} + +/* varout */ + +%typemap(varout,noblock=1,fragment=#SWIG_FromCharPtrAndSize) + Char [ANY], const Char [ANY] { + size_t size = $1_dim0; +%#ifndef SWIG_PRESERVE_CARRAY_SIZE + while (size && ($1[size - 1] == '\0')) --size; +%#endif + $result = SWIG_FromCharPtrAndSize($1, size); +} + +/* constant */ + +%typemap(constcode,fragment=#SWIG_FromCharPtrAndSize) + Char [ANY], const Char [ANY] +{ + size_t size = $value_dim0; +%#ifndef SWIG_PRESERVE_CARRAY_SIZE + while (size && ($value[size - 1] == '\0')) --size; +%#endif + SWIG_set_constant("$symname", SWIG_FromCharPtrAndSize($value,size)); +} + + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +/* directorin */ +%typemap(directorin,fragment=#SWIG_FromCharPtrAndSize) + Char [ANY], const Char [ANY] +{ + size_t size = $1_dim0; +%#ifndef SWIG_PRESERVE_CARRAY_SIZE + while (size && ($1_name[size - 1] == '\0')) --size; +%#endif + $input = SWIG_FromCharPtrAndSize($1_name, size); +} + +/* directorout */ + +%typemap(directorout,noblock=1,fragment=#SWIG_AsCharArray) + Char [ANY] (Char temp[$result_dim0]), + const Char [ANY] (Char temp[$result_dim0]) +{ + if (SWIG_AsCharArray($input, temp, $result_dim0) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError, "$type"); + } + $result = temp; +} + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + +/* typecheck */ + +%typemap(typecheck,noblock=1,precedence=SWIG_TYPECHECK_STRING, + fragment=#SWIG_AsCharArray) + Char [ANY], const Char[ANY] { + $1 = (SWIG_AsCharArray($input, (Char *)0, $1_dim0) == SWIG_OK); +} + + +/* throws */ + +%typemap(throws,fragment=#SWIG_FromCharPtrAndSize) + Char [ANY], const Char[ANY] +{ + size_t size = $1_dim0; +%#ifndef SWIG_PRESERVE_CARRAY_SIZE + while (size && ($1[size - 1] == '\0')) --size; +%#endif + SWIG_exception_obj(0, "$type", SWIG_FromCharPtrAndSize($1, size)); +} + +/* ------------------------------------------------------------------- + * --- Really fix size Char arrays, including '\0'chars at the end --- + * ------------------------------------------------------------------- */ + +%typemap(varout,noblock=1,fragment=#SWIG_FromCharPtrAndSize) + Char FIXSIZE[ANY], const Char FIXSIZE[ANY] +{ + $result = SWIG_FromCharPtrAndSize($1, $1_dim0); +} + +%typemap(out,noblock=1,fragment=#SWIG_FromCharPtrAndSize) + Char FIXSIZE[ANY], const Char FIXSIZE[ANY] +{ + SWIG_set_result(SWIG_FromCharPtrAndSize($1, $1_dim0)); +} + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +%typemap(directorin,noblock=1,fragment=#SWIG_FromCharPtrAndSize) + Char FIXSIZE[ANY], const Char FIXSIZE[ANY] +{ + $input = SWIG_FromCharPtrAndSize($1_name, $1_dim0); +} + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + +%typemap(throws,noblock=1,fragment=#SWIG_FromCharPtrAndSize) + Char FIXSIZE[ANY], const Char FIXSIZE[ANY] +{ + SWIG_exception_obj(0, "$type", SWIG_FromCharPtrAndSize($1, $1_dim0)); +} + +/* ------------------------------------------------------------ + * --- String & length --- + * ------------------------------------------------------------ */ + +/* Here len doesn't include the '0' terminator */ +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) + (Char *STRING, size_t LENGTH) (Char *buf = 0, size_t size = 0, int alloc = 0), + (const Char *STRING, size_t LENGTH) (Char *buf = 0, size_t size = 0, int alloc = SWIG_OLDOBJ) +{ + if (SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $1 = SWIG_static_cast(buf, $1_ltype); + $2 = SWIG_numeric_cast(size - 1, $2_ltype); +} +%typemap(freearg,noblock=1) (Char *STRING, size_t LENGTH) { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} +/* old 'int' form */ +%typemap(in) (Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH); +%typemap(freearg) (Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH); + + +/* Here size includes the '0' terminator */ +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) + (Char *STRING, size_t SIZE) (Char *buf = 0, size_t size = 0, int alloc = 0), + (const Char *STRING, size_t SIZE) (Char *buf = 0, size_t size = 0, int alloc = SWIG_OLDOBJ) +{ + if (SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $1 = SWIG_static_cast(buf, $1_ltype); + $2 = SWIG_numeric_cast(size, $2_ltype); +} +%typemap(freearg, noblock=1) (Char *STRING, size_t SIZE) { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} +/* old 'int' form */ +%typemap(in) (Char *STRING, int SIZE) = (Char *STRING, size_t SIZE); +%typemap(freearg) (Char *STRING, int SIZE) = (Char *STRING, size_t SIZE); + + +/* reverse order versions */ + +/* Here len doesn't include the '0' terminator */ +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) + (size_t LENGTH, Char *STRING) (Char *buf = 0, size_t size = 0, int alloc = 0), + (size_t LENGHT, const Char *STRING) (Char *buf = 0, size_t size = 0, int alloc = SWIG_OLDOBJ) +{ + if (SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } + $2 = SWIG_static_cast(buf, $2_ltype) ; + $1 = SWIG_numeric_cast(size - 1, $1_ltype) ; +} +%typemap(freearg, noblock=1) (size_t LENGTH, Char *STRING) { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} +/* old 'int' form */ +%typemap(in) (int LENGTH, Char *STRING) = (size_t LENGTH, Char *STRING); +%typemap(freearg) (int LENGTH, Char *STRING) = (size_t LENGTH, Char *STRING); + +/* Here size includes the '0' terminator */ +%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize) + (size_t SIZE, Char *STRING) (Char *buf = 0, size_t size = 0, int alloc = 0), + (size_t SIZE, const Char *STRING) (Char *buf = 0, size_t size = 0, int alloc = SWIG_OLDOBJ) +{ + if (SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "$type",$argnum); + } + $2 = SWIG_static_cast(buf, $2_ltype) ; + $1 = SWIG_numeric_cast(size, $1_ltype) ; +} +%typemap(freearg, noblock=1) (size_t SIZE, Char *STRING) { + if (alloc$argnum == SWIG_NEWOBJ) SWIG_delete_array(buf$argnum); +} +/* old 'int' form */ +%typemap(in) (int SIZE, Char *STRING) = (size_t SIZE, Char *STRING); +%typemap(freearg) (int SIZE, Char *STRING) = (size_t SIZE, Char *STRING); + + +%enddef + +%define %typemap_string(Char, CharName, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtrAndSize, + SWIG_CharPtrLen) +/* String fragment methods */ + +%fragment("SWIG_From"#CharName"Ptr","header",fragment=#SWIG_FromCharPtrAndSize) { +SWIGINTERNINLINE SWIG_Object +SWIG_From##CharName##Ptr(const Char *cptr) +{ + return SWIG_FromCharPtrAndSize(cptr, (cptr ? SWIG_CharPtrLen(cptr) : 0)); +} +} + +%fragment("SWIG_From"#CharName"Array","header",fragment=#SWIG_FromCharPtrAndSize) { +SWIGINTERNINLINE SWIG_Object +SWIG_From##CharName##Array(const Char *cptr, size_t size) +{ + return SWIG_FromCharPtrAndSize(cptr, size); +} +} + +%fragment("SWIG_As" #CharName "Ptr","header",fragment=#SWIG_AsCharPtrAndSize) { + SWIG_define(SWIG_As##CharName##Ptr(obj, val, alloc), SWIG_AsCharPtrAndSize(obj, val, NULL, alloc)) +} + +%check_swig_object() + +%fragment("SWIG_As" #CharName "Array","header",fragment=#SWIG_AsCharPtrAndSize) { +SWIGINTERN int +SWIG_As##CharName##Array(SWIG_Object obj, Char *val, size_t size) +{ + Char* cptr = 0; size_t csize = 0; int alloc = SWIG_OLDOBJ; + if (SWIG_AsCharPtrAndSize(obj, &cptr, &csize, &alloc) == SWIG_OK) { + if ((csize == size + 1) && cptr && !(cptr[csize-1])) --csize; + if (csize <= size) { + if (val) { + if (csize) memcpy(val, cptr, csize); + if (csize < size) memset(val + csize, 0, (size - csize)*sizeof(Char)); + if (alloc == SWIG_NEWOBJ) SWIG_delete_array(cptr); + } + return SWIG_OK; + } + } + return SWIG_TypeError; +} +} + +%_typemap_string(Char, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtrAndSize, + SWIG_CharPtrLen, + SWIG_As##CharName##Ptr, + SWIG_From##CharName##Ptr, + SWIG_As##CharName##Array) + +%enddef diff --git a/Lib/typemaps/swigmacros.swg b/Lib/typemaps/swigmacros.swg new file mode 100644 index 000000000..1183c6a12 --- /dev/null +++ b/Lib/typemaps/swigmacros.swg @@ -0,0 +1,241 @@ +/* ----------------------------------------------------------------------------- + * SWIG API. Portion only visible from SWIG + * ----------------------------------------------------------------------------- */ + +/* basic preprocessor macros */ + +#define SWIG_arg(Arg...) Arg +#define SWIG_str(Type...) #Type +#define SWIG_block(Block...) do { Block; } while(0) + +/* casting operators */ + +#ifdef SWIG_NO_CPLUSPLUS_CAST +/* Disable 'modern' cplusplus casting operators */ +# ifdef SWIG_CPLUSPLUS_CAST +# undef SWIG_CPLUSPLUS_CAST +# endif +#endif + +#if defined(__cplusplus) && defined(SWIG_CPLUSPLUS_CAST) +# define SWIG_const_cast(a,Type...) const_cast(a) +# define SWIG_static_cast(a,Type...) static_cast(a) +# define SWIG_reinterpret_cast(a,Type...) reinterpret_cast(a) +# define SWIG_numeric_cast(a,Type...) static_cast(a) +# define SWIG_as_voidptr(a) const_cast(static_cast(a)) +# define SWIG_as_voidptrptr(a) reinterpret_cast(a) +#else /* C case */ +# define SWIG_const_cast(a,Type...) (Type)(a) +# define SWIG_static_cast(a,Type...) (Type)(a) +# define SWIG_reinterpret_cast(a,Type...) (Type)(a) +# define SWIG_numeric_cast(a,Type...) (Type)(a) +# define SWIG_as_voidptr(a) (void *)(a) +# define SWIG_as_voidptrptr(a) (void **)(a) +#endif /* __cplusplus */ + +/* macros for allocating/freeing elements */ + +#if defined(__cplusplus) +# define SWIG_new(Type...) (new Type) +# define SWIG_new_copy(val,Type...) (new Type(val)) +# define SWIG_new_array(size,Type...) (new Type[size]) +# define SWIG_new_copy_array(ptr,size,Type...) SWIG_reinterpret_cast(memcpy(SWIG_new_array(size,Type), ptr, sizeof(Type)*(size)), Type*) +# define SWIG_delete(cptr) delete cptr +# define SWIG_delete_array(cptr) delete[] cptr +#else /* C case */ +# define SWIG_new(Type...) (Type *)malloc(sizeof(Type)) +# define SWIG_new_copy(val,Type...) (Type *)memcpy(SWIG_new(Type),&val,sizeof(Type)) +# define SWIG_new_array(size,Type...) (Type *)malloc((size)*sizeof(Type)) +# define SWIG_new_copy_array(ptr,size,Type...) (Type *)memcpy(SWIG_new_array(size,Type), ptr, sizeof(Type)*(size)) +# define SWIG_delete(cptr) free((char*)cptr) +# define SWIG_delete_array(cptr) free((char*)cptr) +#endif /* __cplusplus */ + +/* macros for fragments/typemaps */ + +#define SWIG_Mangle(Type...) #@Type +#define SWIG_Descriptor(Type...) SWIGTYPE_ ## #@Type +#define SWIG_NameType(Name, Type...) SWIG_ ## Name ## _ ## #@Type +#define SWIG_StringType(Name, Type...) "SWIG_" #Name "_" {Type} + +#define SWIG_AsVal(Type...) SWIG_NameType(AsVal, Type) +#define SWIG_AsPtr(Type...) SWIG_NameType(AsPtr, Type) +#define SWIG_As(Type...) SWIG_NameType(As, Type) +#define SWIG_From(Type...) SWIG_NameType(From, Type) +#define SWIG_Check(Type...) SWIG_NameType(Check, Type) +#define SWIG_OrderType(Type...) SWIG_NameType(OrderType, Type) +#define SWIG_EqualType(Type...) SWIG_NameType(EqualType, Type) + +#define SWIG_AsVal_dec(Type...) SWIG_NameType(AsVal, Type) +#define SWIG_AsPtr_dec(Type...) SWIG_NameType(AsPtr, Type) +#define SWIG_As_dec(Type...) SWIG_NameType(As, Type) +#define SWIG_From_dec(Type...) SWIG_NameType(From, Type) +#define SWIG_Check_dec(Type...) SWIG_NameType(Check, Type) + +#define SWIG_Traits_frag(Type...) SWIG_StringType(Traits, Type) +#define SWIG_AsPtr_frag(Type...) SWIG_StringType(AsPtr, Type) +#define SWIG_AsVal_frag(Type...) SWIG_StringType(AsVal, Type) +#define SWIG_As_frag(Type...) SWIG_StringType(As, Type) +#define SWIG_From_frag(Type...) SWIG_StringType(From, Type) +#define SWIG_Check_frag(Type...) SWIG_StringType(Check, Type) + +#define SWIG_CCode(Type...) SWIG_NameType(TYPECHECK, Type) +#define SWIG_CCode_frag(Type...) SWIG_StringType(TYPECHECK, Type) + + +/* macros for result manipulation */ + +#define SWIG_set_result(obj) SWIG_SetResultObj(obj) +#define SWIG_append_result(obj) SWIG_AppendResultObj(obj) +#define SWIG_set_constant(n, v) SWIG_SetConstantObj(n,v) + +/* macros for error manipulation */ +#define SWIG_TypeErrorFmt(_type,_name) "expected '"_type"'" +#define SWIG_VarFailFmt(_type,_name) "in variable '"_name"' of type '"_type"'" +#define SWIG_ArgFailFmt(_type,_argn) "in argument " #_argn" of type '" _type"'" +#define SWIG_OutFailFmt(_type) "in output of type '"_type"'" +#define SWIG_ArgNullRefFmt(_type, _argn) "null reference " SWIG_ArgFailFmt(_type, _argn) +#define SWIG_VarNullRefFmt(_type, _name) "null reference " SWIG_VarFailFmt(_type, _name) +#define SWIG_OutNullRefFmt(_type) "null reference " SWIG_OutFailFmt(_type) + +#define SWIG_ArgFail(code,type,argn) SWIG_SetErrorMsg(code,SWIG_ArgFailFmt(type, argn)) +#define SWIG_VarFail(code,type,name) SWIG_SetErrorMsg(code,SWIG_VarFailFmt(type, name)) +#define SWIG_ArgNullRef(type,argn) SWIG_SetErrorMsg(SWIG_ValueError, SWIG_ArgNullRefFmt(type, argn)) +#define SWIG_VarNullRef(type,name) SWIG_SetErrorMsg(SWIG_ValueError, SWIG_VarNullRefFmt(type, name)) +#define SWIG_OutNullRef(type) SWIG_SetErrorMsg(SWIG_ValueError, SWIG_OutNullRefFmt(type)) + +#define SWIG_set_errmsg(code,msg...) SWIG_SetErrorMsg(code,msg) +#define SWIG_set_errobj(code,obj) SWIG_SetErrorObj(code,obj) + +#define SWIG_error_block(Block...) SWIG_block(Block) + +#define SWIG_arg_fail(code, type, arg) SWIG_error_block(SWIG_ArgFail(code, type, arg); SWIG_fail) +#define SWIG_arg_nullref(type, arg) SWIG_error_block(SWIG_ArgNullRef(type, arg); SWIG_fail) + +#define SWIG_member_fail(code, type, name) SWIG_error_block(SWIG_VarFail(code, type, name); SWIG_fail) +#define SWIG_member_nullref(type, name) SWIG_error_block(SWIG_VarNullRef(type, name); SWIG_fail) + +#define SWIG_global_fail(code, type, name) SWIG_error_block(SWIG_VarFail(code, type, name); SWIG_fail) +#define SWIG_global_nullref(type, name) SWIG_error_block(SWIG_VarNullRef(type, name); SWIG_fail) + +#define SWIG_var_fail(code, type, name) SWIG_error_block(SWIG_VarFail(code, type, name); SWIG_var_fail) +#define SWIG_var_nullref(type, name) SWIG_error_block(SWIG_VarNullRef(type, name); SWIG_var_fail) + +#define SWIG_dout_fail(code, type) SWIG_DirOutFail(code, SWIG_OutFailFmt(type)) +#define SWIG_dout_nullref(type) SWIG_DirOutFail(SWIG_ValueError, SWIG_OutNullRefFmt(type)) + + +#define SWIG_exception_obj(desc, type, obj) SWIG_error_block(SWIG_ExceptionObj(desc, type, obj); SWIG_fail) + + +/* + Macros to define and check the Language dependent Swig object + + Use it, for example in Tcl, as + + %define_swig_object(Tcl_Obj) +*/ + +#define SWIG_Object __NULL__ +%define %define_swig_object(Obj) +#undef SWIG_Object +#define SWIG_Object Obj +%enddef + +%define %check_swig_object() +#if SWIG_str(SWIG_Object) == "__NULL__" +#error "SWIG_Object must be defined using %define_swig_object" +#endif +%enddef + +/* ----------------------------------------------------------------------------- + * Auxiliar macros used to write typemaps + * ----------------------------------------------------------------------------- */ + +/* define a new macro */ +%define SWIG_define(Def, Val) +%#define Def Val +%enddef + +/* include C++ or C value */ +%define SWIG_cplusplus(cppval, cval) +#ifdef __cplusplus +cppval +#else +cval +#endif +%enddef + +/* for loop for macro with one argument */ +%define %_formacro_1(macro, arg1,...) +macro(arg1) +#if #__VA_ARGS__ != "__fordone__" +%_formacro_1(macro, __VA_ARGS__) +#endif +%enddef + +/* for loop for macro with one argument */ +%define %formacro_1(macro,...) +%_formacro_1(macro,__VA_ARGS__,__fordone__) +%enddef + +/* for loop for macro with two arguments */ +%define %_formacro_2(macro, arg1, arg2, ...) +macro(arg1, arg2) +#if #__VA_ARGS__ != "__fordone__" +%_formacro_2(macro, __VA_ARGS__) +#endif +%enddef + +/* for loop for macro with two arguments */ +%define %formacro_2(macro,...) +%_formacro_2(macro, __VA_ARGS__, __fordone__) +%enddef + + +/* + mark a flag, ie, define a macro name but ignore it in + the interface. + + the flags latter can be used with %evalif +*/ + +%define %swig_mark_flag(x) +%ignore x; +#define x 1 +%enddef + +/* + %swig_equal_type and %swig_order_type flagged a type of having equal (==,!=) + and/or order methods (<=,>=,<,>). +*/ +#define %swig_equal_type(...) %swig_mark_flag(SWIG_EqualType(__VA_ARGS__)) +#define %swig_order_type(...) \ + %swig_mark_flag(SWIG_EqualType(__VA_ARGS__)) \ + %swig_mark_flag(SWIG_OrderType(__VA_ARGS__)) + +/* + %evalif and %evalif_2 are use to evaluate or process + an expression if the given predicate is 'true' (1). +*/ +%define %_evalif(_x,_expr) +#if _x == 1 +_expr +#endif +%enddef + +%define %_evalif_2(_x,_y,_expr) +#if _x == 1 && _y == 1 +_expr +#endif +%enddef + +%define %evalif(_x,...) + %_evalif(SWIG_arg(_x),SWIG_arg(__VA_ARGS__)) +%enddef + +%define %evalif_2(_x,_y,...) + %_evalif_2(SWIG_arg(_x),SWIG_arg(_y),SWIG_arg(__VA_ARGS__)) +%enddef + + diff --git a/Lib/typemaps/swigobject.swg b/Lib/typemaps/swigobject.swg new file mode 100644 index 000000000..e2d0a21a0 --- /dev/null +++ b/Lib/typemaps/swigobject.swg @@ -0,0 +1,29 @@ +/* ------------------------------------------------------------ + * Language Object * - Just pass straight through unmodified + * ------------------------------------------------------------ */ + +%check_swig_object() + +%typemap(in) SWIG_Object "$1 = $input;"; + +%typemap(out,noblock=1) SWIG_Object { + SWIG_set_result($1); +} + +%typecheck(SWIG_TYPECHECK_POINTER) SWIG_Object "$1 = ($input != 0);"; + +%typemap(throws,noblock=1) SWIG_Object { + SWIG_exception_obj(0, "$type", $1); +} + +%typemap(constcode,noblock=1) SWIG_Object { + SWIG_set_constant("$symname", $value); +} + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +%typemap(directorin) SWIG_Object "$input = $1_name"; +%typemap(directorout) SWIG_Object "$result = $input;"; + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + diff --git a/Lib/typemaps/swigtype.swg b/Lib/typemaps/swigtype.swg new file mode 100644 index 000000000..d121b3ef6 --- /dev/null +++ b/Lib/typemaps/swigtype.swg @@ -0,0 +1,426 @@ +/* ----------------------------------------------------------------------------- + * --- Input arguments --- + * ----------------------------------------------------------------------------- */ + +/* Pointers and arrays */ +%typemap(in, noblock=1) SWIGTYPE *, SWIGTYPE [] { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&$1), $descriptor, $disown) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "$type", $argnum); + } +} + +%typemap(freearg) SWIGTYPE *, SWIGTYPE [] ""; + +%typemap(in, noblock=1) SWIGTYPE* const& ($*ltype temp) { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&temp), $*descriptor, $disown) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "$*ltype", $argnum); + } + $1 = &temp; +} + +/* Reference */ +%typemap(in, noblock=1) SWIGTYPE & { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&$1), $descriptor, 0) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "$type", $argnum); + } + if (!$1) { SWIG_arg_nullref("$type", $argnum); } +} + +/* By value */ +%typemap(in,noblock=1) SWIGTYPE ($<ype argp) { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&argp), $&descriptor, 0) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "$type", $argnum); + } + if (!argp) { SWIG_arg_nullref("$type", $argnum); } + $1 = *argp; +} + +/* ----------------------------------------------------------------------------- + * --- Output arguments --- + * ----------------------------------------------------------------------------- */ + +/* Pointers, references */ +%typemap(out,noblock=1) SWIGTYPE *, SWIGTYPE &, SWIGTYPE[] { + SWIG_set_result(SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor, $owner)); +} + +%typemap(out, noblock=1) SWIGTYPE* const& { + SWIG_set_result(SWIG_NewPointerObj(SWIG_as_voidptr(*$1), $*descriptor, $owner)); +} + +/* Return by value */ +%typemap(out, noblock=1) SWIGTYPE { + SWIG_set_result(SWIG_NewPointerObj(SWIG_new_copy($1, $ltype), $&descriptor, SWIG_POINTER_OWN)); +} + +/* ----------------------------------------------------------------------------- + * --- Variable input --- + * ----------------------------------------------------------------------------- */ + +/* memberin/globalin/varin, for fix arrays. */ + +%typemap(memberin) SWIGTYPE [ANY] { + $basetype *inp = SWIG_static_cast($input, $basetype *); + if (inp) { + $basetype *dest = SWIG_static_cast($1, $basetype *); + size_t ii = 0; + for (; ii < $dim0; ++ii) dest[ii] = inp[ii]; + } else { + SWIG_member_nullref("$type","$name"); + } +} + +%typemap(globalin) SWIGTYPE [ANY] { + $basetype *inp = SWIG_static_cast($input, $basetype *); + if (inp) { + $basetype *dest = SWIG_static_cast($1, $basetype *); + size_t ii = 0; + for (; ii < $dim0; ++ii) dest[ii] = inp[ii]; + } else { + SWIG_global_nullref("$type","$name"); + } +} + +%typemap(varin,noblock=1) SWIGTYPE [ANY] { + $basetype *inp = 0; + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&inp), $descriptor, 0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } else if (inp) { + size_t ii = 0; + $basetype *dest = SWIG_static_cast($1, $basetype *); + for (; ii < $dim0; ++ii) dest[ii] = inp[ii]; + } else { + SWIG_var_nullref("$type", "$name"); + } +} + +/* memberin/globalin/varin, for fix double arrays. */ + +%typemap(memberin) SWIGTYPE [ANY][ANY] { + $basetype (*inp)[$dim1] = SWIG_static_cast($input, $basetype (*)[$dim1]); + if (inp) { + $basetype (*dest)[$dim1] = SWIG_static_cast($1, $basetype (*)[$dim1]); + size_t ii = 0; + for (; ii < $dim0; ++ii) { + $basetype *ip = inp[ii]; + if (ip) { + $basetype *dp = dest[ii]; + size_t jj = 0; + for (; jj < $dim1; ++jj) dp[jj] = ip[jj]; + } else { + SWIG_member_nullref("$type","$name"); + } + } + } else { + SWIG_member_nullref("$type","$name"); + } +} + +%typemap(globalin) SWIGTYPE [ANY][ANY] { + $basetype (*inp)[$dim1] = SWIG_static_cast($input, $basetype (*)[$dim1]); + if (inp) { + $basetype (*dest)[$dim1] = SWIG_static_cast($1, $basetype (*)[$dim1]); + size_t ii = 0; + for (; ii < $dim0; ++ii) { + $basetype *ip = inp[ii]; + if (ip) { + $basetype *dp = dest[ii]; + size_t jj = 0; + for (; jj < $dim1; ++jj) dp[jj] = ip[jj]; + } else { + SWIG_global_nullref("$type","$name"); + } + } + } else { + SWIG_global_nullref("$type","$name"); + } +} + +%typemap(varin,noblock=1) SWIGTYPE [ANY][ANY] { + $basetype (*inp)[$dim1] = 0; + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&inp), $descriptor, 0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } else if (inp) { + $basetype (*dest)[$dim1] = SWIG_static_cast($1, $basetype (*)[$dim1]); + size_t ii = 0; + for (; ii < $dim0; ++ii) { + $basetype *ip = inp[ii]; + if (ip) { + $basetype *dp = dest[ii]; + size_t jj = 0; + for (; jj < $dim1; ++jj) dp[jj] = ip[jj]; + } else { + SWIG_var_nullref("$type", "$name"); + } + } + } else { + SWIG_var_nullref("$type", "$name"); + } +} + +/* Pointers, references, and variable size arrays */ + +%typemap(varin,noblock=1) SWIGTYPE * { + void *temp = 0; + if (SWIG_ConvertPtr($input, &temp, $descriptor, 0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } + $1 = ($ltype) temp; +} + +%typemap(varin,noblock=1,warning="462:Unable to set dimensionless array variable") SWIGTYPE [] +{ + SWIG_var_fail(SWIG_AttributeError, "$type", "read-only $name"); +} + +%typemap(varin,noblock=1) SWIGTYPE & { + void *temp = 0; + if (SWIG_ConvertPtr($input, &temp, $descriptor, 0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } + if (!temp) { + SWIG_var_nullref("$type", "$name"); + } + $1 = *(SWIG_reinterpret_cast(temp, $ltype)); +} + +%typemap(varin,noblock=1) SWIGTYPE { + void *temp = 0; + if (SWIG_ConvertPtr($input, &temp, $&descriptor, 0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } + if (!temp) { + SWIG_var_nullref("$type", "$name"); + } + $1 = *(SWIG_reinterpret_cast(temp, $&type)); +} + +/* ----------------------------------------------------------------------------- + * --- Variable output --- + * ----------------------------------------------------------------------------- */ + +/* Pointers and arrays */ +%typemap(varout, noblock=1) SWIGTYPE * { + $result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor, 0); +} + +%typemap(varout, noblock=1) SWIGTYPE [] { + $result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor, 0); +} + +/* References */ +%typemap(varout, noblock=1) SWIGTYPE & { + $result = SWIG_NewPointerObj(SWIG_as_voidptr(&$1), $descriptor, 0); +} + +/* Value */ +%typemap(varout, noblock=1) SWIGTYPE { + $result = SWIG_NewPointerObj(SWIG_as_voidptr(&$1), $&descriptor, 0); +} + +/* ------------------------------------------------------------ + * --- Typechecking rules --- + * ------------------------------------------------------------ */ + +%typecheck(SWIG_TYPECHECK_POINTER,noblock=1) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] { + void *ptr = 0; + $1 = (SWIG_ConvertPtr($input, &ptr, $descriptor, 0) == SWIG_OK); +} + +%typecheck(SWIG_TYPECHECK_POINTER,noblock=1) SWIGTYPE & { + void *ptr = 0; + $1 = (SWIG_ConvertPtr($input, &ptr, $descriptor, 0) == SWIG_OK) && (ptr != 0); +} + +%typecheck(SWIG_TYPECHECK_POINTER,noblock=1) SWIGTYPE { + void *ptr = 0; + $1 = (SWIG_ConvertPtr($input, &ptr, $&descriptor, 0) == SWIG_OK) && (ptr != 0); +} + +/* ------------------------------------------------------------ + * --- CLASS::* typemaps --- + * ------------------------------------------------------------ */ + +%typemap(in,noblock=1) SWIGTYPE (CLASS::*) { + if (SWIG_ConvertMember($input, SWIG_as_voidptr(&$1), sizeof($type),$descriptor, 0) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } +} + +%typemap(out,noblock=1) SWIGTYPE (CLASS::*) { + SWIG_set_result(SWIG_NewMemberObj(SWIG_as_voidptr(&$1), sizeof($type), $descriptor)); +} + +%typemap(varin,noblock=1) SWIGTYPE (CLASS::*) { + if (SWIG_ConvertMember($input,SWIG_as_voidptr(&$1), sizeof($type), $descriptor,0) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } +} + +%typemap(varout,noblock=1) SWIGTYPE (CLASS::*) { + $result = SWIG_NewMemberObj(SWIG_as_voidptr(&$1), sizeof($type), $descriptor); +} + + +/* ------------------------------------------------------------ + * --- function ptr typemaps --- + * ------------------------------------------------------------ */ + +/* + ISO C++ doesn't allow direct casting of a function ptr to a object + ptr. So, maybe the ptr sizes are not the same, and we need to take + some providences. + */ +%typemap(in, noblock=1) SWIGTYPE ((*)(ANY)) { + if (SWIG_ConvertFunctionPtr($input, SWIG_as_voidptrptr(&$1), $descriptor, 0) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type",$argnum); + } +} + +%typemap(out, noblock=1) SWIGTYPE ((*)(ANY)) { + SWIG_set_result(SWIG_NewFunctionPtrObj((void *)($1), $descriptor)); +} + + +/* ----------------------------------------------------------------------------- + * --- Director typemaps --- * + * ----------------------------------------------------------------------------- */ + + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +/* directorin */ + +%typemap(directorin,noblock=1) SWIGTYPE* { + $input = SWIG_NewPointerObj(SWIG_as_voidptr($1_name), $descriptor, 0); +} + +%typemap(directorin,noblock=1) SWIGTYPE { + $input = SWIG_NewPointerObj(SWIG_as_voidptr(&$1_name), $&descriptor, 0); +} + +%typemap(directorin,noblock=1) SWIGTYPE& { + $input = SWIG_NewPointerObj(SWIG_as_voidptr(&$1_name), $descriptor, 0); +} + +/* directorout */ + +%typemap(directorout,noblock=1) SWIGTYPE (void * argp) { + if (SWIG_ConvertPtr($input,&argp,$&descriptor, 0) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } + $result = *(SWIG_reinterpret_cast(argp, $<ype); +} +%typemap(directorout,noblock=1) SWIGTYPE *(void *argp), SWIGTYPE [](void *argp) { + if (SWIG_ConvertPtr($input, &argp, $descriptor, 0) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } + $result = SWIG_reinterpret_cast(argp, $ltype); +} +%typemap(directorout,noblock=1) SWIGTYPE &(void *argp) { + if (SWIG_ConvertPtr($input, &argp, $descriptor, $disown ) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } + if (!argp) { SWIG_dout_nullref("$type"); } + $result = SWIG_reinterpret_cast(argp, $ltype); +} +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + + +/* ------------------------------------------------------------ + * --- Constants --- + * ------------------------------------------------------------ */ + +%typemap(constcode,noblock=1) SWIGTYPE *, SWIGTYPE &, SWIGTYPE []{ + SWIG_set_constant("$symname", SWIG_NewPointerObj(SWIG_as_voidptr($value),$descriptor,0)); +} + +%typemap(constcode,noblock=1) SWIGTYPE (CLASS::*) { + SWIG_set_constant("$symname", SWIG_NewPackedObj(SWIG_as_voidptr(&$value), sizeof($type), $descriptor, 0)); +} + +%typemap(constcode,noblock=1) SWIGTYPE { + SWIG_set_constant("$symname", SWIG_NewPointerObj(SWIG_as_voidptr(&$value),$&descriptor,0)); +} + +%typemap(constcode,noblock=1) SWIGTYPE ((*)(ANY)){ + if (sizeof($type) == sizeof(void *)) { + SWIG_set_constant("$symname", SWIG_NewPointerObj((void *)$value, $descriptor, 0)); + } else { + SWIG_set_constant("$symname", SWIG_NewPackedObj((void *)$value, sizeof($type), $descriptor, 0)); + } +} + + +/* ------------------------------------------------------------ + * --- Exception handling --- + * ------------------------------------------------------------ */ + +%typemap(throws,noblock=1) SWIGTYPE { + SWIG_exception_obj($&descriptor, "$type", SWIG_NewPointerObj(SWIG_new_copy($1, $ltype),$&descriptor,SWIG_POINTER_OWN)); +} + +%typemap(throws,noblock=1) SWIGTYPE * { + SWIG_exception_obj($descriptor, "$type", SWIG_NewPointerObj(SWIG_as_voidptr($1),$descriptor,0)); +} + +%typemap(throws,noblock=1) SWIGTYPE [ANY] { + SWIG_exception_obj($descriptor, "$type", SWIG_NewPointerObj(SWIG_as_voidptr($1),$descriptor,0)); +} + +%typemap(throws,noblock=1) SWIGTYPE & { + SWIG_exception_obj($descriptor, "$type", SWIG_NewPointerObj(SWIG_as_voidptr(&$1),$descriptor,0)); +} + +/* ------------------------------------------------------------ + * --- Special typemaps --- + * ------------------------------------------------------------ */ + +/* VARARGS_SENTINEL typemap. Used by the %varargs directive. */ + +%typemap(in,numinputs=0) SWIGTYPE *VARARGS_SENTINEL, SWIGTYPE VARARGS_SENTINEL ""; + + +/* DISOWN typemap */ + +%typemap(in, noblock=1) SWIGTYPE *DISOWN { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&$1), $descriptor, SWIG_POINTER_DISOWN) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError,"$type", $argnum); + } +} + +%typemap(varin,noblock=1) SWIGTYPE *DISOWN { + void *temp = 0; + if (SWIG_ConvertPtr($input, &temp, $descriptor, SWIG_POINTER_DISOWN) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } + $1 = ($ltype) temp; +} + +/* DYNAMIC typemap */ + +%typemap(out,noblock=1) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { + SWIG_set_result(SWIG_NewPointerObj(SWIG_as_voidptr($1), SWIG_TypeDynamicCast($descriptor, SWIG_as_voidptrptr(&$1)), $owner)); +} + +/* INSTANCE typemap */ + +%typemap(out,noblock=1) SWIGTYPE INSTANCE { + SWIG_set_result(SWIG_NewInstanceObj(SWIG_new_copy($1, $1_ltype), $&1_descriptor,SWIG_POINTER_OWN)); +} + +%typemap(out,noblock=1) SWIGTYPE *INSTANCE, SWIGTYPE &INSTANCE, SWIGTYPE INSTANCE[] { + SWIG_set_result(SWIG_NewInstanceObj(SWIG_as_voidptr($1), $1_descriptor, $owner)); +} + +%typemap(varout,noblock=1) SWIGTYPE *INSTANCE, SWIGTYPE INSTANCE[] { + $result = SWIG_NewInstanceObj(SWIG_as_voidptr($1), $1_descriptor, 0); +} + +%typemap(varout,noblock=1) SWIGTYPE &INSTANCE { + $result = SWIG_NewInstanceObj(SWIG_as_voidptr(&$1), $1_descriptor, 0); +} + +%typemap(varout,noblock=1) SWIGTYPE INSTANCE { + $result = SWIG_NewInstanceObj(SWIG_as_voidptr(&$1), $&1_descriptor, 0); +} diff --git a/Lib/typemaps/typemaps.swg b/Lib/typemaps/typemaps.swg new file mode 100644 index 000000000..9b200b810 --- /dev/null +++ b/Lib/typemaps/typemaps.swg @@ -0,0 +1,167 @@ +// +// SWIG Typemap library +// Dave Beazley +// May 5, 1997 +// +// Tcl implementation +// +// This library provides standard typemaps for modifying SWIG's behavior. +// With enough entries in this file, I hope that very few people actually +// ever need to write a typemap. +// +// Disclaimer : Unless you really understand how typemaps work, this file +// probably isn't going to make much sense. +// + +// ------------------------------------------------------------------------ +// Pointer handling +// +// These mappings provide support for input/output arguments and common +// uses for C/C++ pointers. +// ------------------------------------------------------------------------ + +// INPUT typemaps. +// These remap a C pointer to be an "INPUT" value which is passed by value +// instead of reference. + +/* +The following methods can be applied to turn a pointer into a simple +"input" value. That is, instead of passing a pointer to an object, +you would use a real value instead. + + int *INPUT + short *INPUT + long *INPUT + long long *INPUT + unsigned int *INPUT + unsigned short *INPUT + unsigned long *INPUT + unsigned long long *INPUT + unsigned char *INPUT + bool *INPUT + float *INPUT + double *INPUT + +To use these, suppose you had a C function like this : + + double fadd(double *a, double *b) { + return *a+*b; + } + +You could wrap it with SWIG as follows : + + %include + double fadd(double *INPUT, double *INPUT); + +or you can use the %apply directive : + + %include + %apply double *INPUT { double *a, double *b }; + double fadd(double *a, double *b); + +*/ + +// OUTPUT typemaps. These typemaps are used for parameters that +// are output only. The output value is appended to the result as +// a list element. + +/* +The following methods can be applied to turn a pointer into an "output" +value. When calling a function, no input value would be given for +a parameter, but an output value would be returned. In the case of +multiple output values, they are returned in the form of a Tcl tuple. + + int *OUTPUT + short *OUTPUT + long *OUTPUT + long long *OUTPUT + unsigned int *OUTPUT + unsigned short *OUTPUT + unsigned long *OUTPUT + unsigned long long *OUTPUT + unsigned char *OUTPUT + bool *OUTPUT + float *OUTPUT + double *OUTPUT + +For example, suppose you were trying to wrap the modf() function in the +C math library which splits x into integral and fractional parts (and +returns the integer part in one of its parameters).K: + + double modf(double x, double *ip); + +You could wrap it with SWIG as follows : + + %include + double modf(double x, double *OUTPUT); + +or you can use the %apply directive : + + %include + %apply double *OUTPUT { double *ip }; + double modf(double x, double *ip); + +The Tcl output of the function would be a tuple containing both +output values. + +*/ + +// INOUT +// Mappings for an argument that is both an input and output +// parameter + +/* +The following methods can be applied to make a function parameter both +an input and output value. This combines the behavior of both the +"INPUT" and "OUTPUT" methods described earlier. Output values are +returned in the form of a Tcl tuple. + + int *INOUT + short *INOUT + long *INOUT + long long *INOUT + unsigned int *INOUT + unsigned short *INOUT + unsigned long *INOUT + unsigned long long *INOUT + unsigned char *INOUT + bool *INOUT + float *INOUT + double *INOUT + +For example, suppose you were trying to wrap the following function : + + void neg(double *x) { + *x = -(*x); + } + +You could wrap it with SWIG as follows : + + %include + void neg(double *INOUT); + +or you can use the %apply directive : + + %include + %apply double *INOUT { double *x }; + void neg(double *x); + +Unlike C, this mapping does not directly modify the input value (since +this makes no sense in Tcl). Rather, the modified input value shows +up as the return value of the function. Thus, to apply this function +to a Tcl variable you might do this : + + x = neg(x) + +Note : previous versions of SWIG used the symbol 'BOTH' to mark +input/output arguments. This is still supported, but will be slowly +phased out in future releases. + +*/ + + +#ifdef SWIG_INOUT_NODEF + +%apply_checkctypes(%typemap_inoutn) + +#endif diff --git a/Lib/typemaps/valtypes.swg b/Lib/typemaps/valtypes.swg new file mode 100644 index 000000000..2ab73c65a --- /dev/null +++ b/Lib/typemaps/valtypes.swg @@ -0,0 +1,189 @@ +/*--------------------------------------------------------------------- + * Value typemaps (Type, const Type&) for value types, such as + * fundamental types (int, double), that define the AsVal/From + * methods. + *---------------------------------------------------------------------*/ + +/* in */ + +%define SWIG_VALUE_IN_TYPEMAP(asval_meth,frag,Type...) + %typemap(in,noblock=1,fragment=frag) Type (Type val, int ecode = 0) { + ecode = asval_meth($input, &val); + if (ecode != SWIG_OK) { + SWIG_arg_fail(ecode, "$ltype", $argnum); + } else { + $1 = SWIG_static_cast(val,$ltype); + } + } + %typemap(in,noblock=1,fragment=frag) const Type & ($*ltype temp, Type val, int ecode = 0) { + ecode = asval_meth($input, &val); + if (ecode != SWIG_OK) { + SWIG_arg_fail(ecode, "$*ltype", $argnum); + } else { + temp = SWIG_static_cast(val, $*ltype); + $1 = &temp; + } + } +%enddef + +/* out */ + +%define SWIG_VALUE_OUT_TYPEMAP(from_meth,frag,Type...) + %typemap(out,noblock=1,fragment=frag) Type, const Type { + SWIG_set_result(from_meth(SWIG_static_cast($1,Type))); + } + %typemap(out,noblock=1,fragment=frag) const Type& { + SWIG_set_result(from_meth(SWIG_static_cast(*$1,Type))); + } +%enddef + +/* varin */ + +%define SWIG_VALUE_VARIN_TYPEMAP(asval_meth,frag,Type...) + %typemap(varin,noblock=1,fragment=frag) Type { + Type val; + int res = asval_meth($input, &val); + if (res != SWIG_OK) { + SWIG_var_fail(res, "$type", "$name"); + } else { + $1 = SWIG_static_cast(val,$ltype); + } + } +%enddef + +/* varout */ + +%define SWIG_VALUE_VAROUT_TYPEMAP(from_meth,frag,Type...) + %typemap(varout,noblock=1,fragment=frag) Type, const Type& { + $result = from_meth(SWIG_static_cast($1,$basetype)); + } +%enddef + +/* constant installation code */ + +%define SWIG_VALUE_CONSTCODE_TYPEMAP(from_meth,frag,Type...) + %typemap(constcode,noblock=1,fragment=frag) Type { + SWIG_set_constant("$symname", from_meth(SWIG_static_cast($value,Type))); + } +%enddef + + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +/* directorin */ + +%define SWIG_VALUE_DIRECTORIN_TYPEMAP(from_meth,frag,Type...) + %typemap(directorin,noblock=1,fragment=frag) Type *DIRECTORIN { + $input = from_meth(SWIG_static_cast(*$1_name,Type)); + } + %typemap(directorin,noblock=1,fragment=frag) Type, const Type& { + $input = from_meth(SWIG_static_cast($1_name,Type)); + } +%enddef + +/* directorout */ + +%define SWIG_VALUE_DIRECTOROUT_TYPEMAP(asval_meth,frag,Type...) + %typemap(directorargout,noblock=1,fragment=frag) Type *DIRECTOROUT { + Type val; + int res = asval_meth($input, &val); + if (res != SWIG_OK) { + SWIG_dout_fail(res, "$type"); + } else { + *$result = SWIG_static_cast(val, $type); + } + } + %typemap(directorout,noblock=1,fragment=frag) Type { + Type val; + int res = asval_meth($input, &val); + if (res != SWIG_OK) { + SWIG_dout_fail(res, "$type"); + } else { + $result = SWIG_static_cast(val,$type); + } + } + %typemap(directorout,noblock=1,fragment=frag,warning=SWIG_WARN_TYPEMAP_THREAD_UNSAFE) const Type& { + Type val; + int res = asval_meth($input, &val); + if (res != SWIG_OK) { + SWIG_dout_fail(res, "$type"); + } else { + static $basetype temp = SWIG_static_cast(val, $basetype); + $result = &temp; + } + } + %typemap(directorout,fragment=frag) Type &DIRECTOROUT = Type +%enddef + +#else + +#define SWIG_VALUE_DIRECTORIN_TYPEMAP(from_meth,frag,Type...) +#define SWIG_VALUE_DIRECTOROUT_TYPEMAP(asval_meth,frag,Type...) + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + + +/* throws */ + +%define SWIG_VALUE_THROWS_TYPEMAP(from_meth,frag,Type...) + %typemap(throws,noblock=1,fragment=frag) Type { + SWIG_exception_obj(0, "Type", from_meth(SWIG_static_cast($1,$basetype))); + } +%enddef + +/* typecheck */ + +%define SWIG_VALUE_TYPECHECK_TYPEMAP(check,asval_meth,frag,Type...) + %typemap(typecheck,noblock=1,precedence=check,fragment=frag) Type, const Type& { + $1 = (asval_meth($input, 0) == SWIG_OK); + } +%enddef + +/*--------------------------------------------------------------------- + * typemap definition for types with AsVal methods + *---------------------------------------------------------------------*/ +%define %typemap_asval(CheckCode, AsValMeth, AsValFrag, Type...) + SWIG_VALUE_IN_TYPEMAP(SWIG_arg(AsValMeth), SWIG_arg(AsValFrag), Type); + SWIG_VALUE_VARIN_TYPEMAP(SWIG_arg(AsValMeth), SWIG_arg(AsValFrag), Type); + SWIG_VALUE_DIRECTOROUT_TYPEMAP(SWIG_arg(AsValMeth), SWIG_arg(AsValFrag), Type); + SWIG_VALUE_TYPECHECK_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(AsValMeth), SWIG_arg(AsValFrag), Type); + SWIG_VALUE_INPUT_TYPEMAP(SWIG_arg(CheckCode), SWIG_arg(AsValMeth), SWIG_arg(AsValFrag), Type); +%enddef + + +/*--------------------------------------------------------------------- + * typemap definition for types with from method + *---------------------------------------------------------------------*/ +%define %typemap_from(FromMeth, FromFrag, Type...) + SWIG_VALUE_OUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_VALUE_VAROUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_VALUE_CONSTCODE_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_VALUE_DIRECTORIN_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_VALUE_THROWS_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_VALUE_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); +%enddef + + +/*--------------------------------------------------------------------- + * typemap definition for types with alval/from method + *---------------------------------------------------------------------*/ + +%define %typemap_asvalfrom(CheckCode, AsValMeth, FromMeth, + AsValFrag, FromFrag, Type...) + %typemap_asval(SWIG_arg(CheckCode), SWIG_arg(AsValMeth), SWIG_arg(AsValFrag), Type); + %typemap_from(SWIG_arg(FromMeth), SWIG_arg(FromFrag), Type); + SWIG_VALUE_INOUT_TYPEMAP(Type); +%enddef + + +/*--------------------------------------------------------------------- + * typemap definition for types with for 'normalized' asval/from methods + *---------------------------------------------------------------------*/ +%define %typemap_asvalfromn(CheckCode, Type...) + %typemap_asvalfrom(SWIG_arg(CheckCode), + SWIG_AsVal(Type), + SWIG_From(Type), + SWIG_arg(SWIG_AsVal_frag(Type)), + SWIG_arg(SWIG_From_frag(Type)), + Type); +%enddef diff --git a/Lib/typemaps/void.swg b/Lib/typemaps/void.swg new file mode 100644 index 000000000..b541a86ed --- /dev/null +++ b/Lib/typemaps/void.swg @@ -0,0 +1,60 @@ +/* ------------------------------------------------------------ + * Void * - Accepts any kind of pointer + * ------------------------------------------------------------ */ + +/* in */ + +%typemap(in,noblock=1) void * { + if (SWIG_ConvertPtr($input,SWIG_as_voidptrptr(&$1), 0, $disown) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "$type", $argnum); + } +} + +%typemap(freearg,noblock=1) void * ""; + +%typemap(in,noblock=1) void * const& ($*ltype temp) { + if (SWIG_ConvertPtr($input, SWIG_as_voidptrptr(&temp), 0, $disown) != SWIG_OK) { + SWIG_arg_fail(SWIG_TypeError, "Stype", $argnum); + } + $1 = &temp; +} + + +/* out */ + +%typemap(out,noblock=1) void { + SWIG_set_result(SWIG_NoneObject()); +} + +/* varin */ + +%typemap(varin,noblock=1) void * { + void *temp = 0; + if (SWIG_ConvertPtr($input, &temp, 0, SWIG_POINTER_DISOWN) != SWIG_OK) { + SWIG_var_fail(SWIG_TypeError, "$type", "$name"); + } + $1 = ($1_ltype) temp; +} + +/* typecheck */ + +%typecheck(SWIG_TYPECHECK_VOIDPTR, noblock=1) void * +{ + void *ptr = 0; + $1 = (SWIG_ConvertPtr($input, &ptr, 0, 0) == SWIG_OK); +} + +#ifdef SWIG_DIRECTOR_TYPEMAPS + +/* directorout */ + +%typemap(directorout,noblock=1) void * (void *argp) { + if (SWIG_ConvertPtr($input, &argp, 0, 0) != SWIG_OK) { + SWIG_dout_fail(SWIG_TypeError,"$type"); + } + $result = SWIG_reinterpret_cast(argp, $ltype); +} + + +#endif /* SWIG_DIRECTOR_TYPEMAPS */ + diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 48e80ddf8..7bd64e38c 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -156,11 +156,9 @@ public: apply = 1; Swig_mark_arg(i); } else if (strcmp(argv[i],"-cppcast") == 0) { - /* Turn on new value wrapper mpde */ cppcast = 1; Swig_mark_arg(i); } else if (strcmp(argv[i],"-nocppcast") == 0) { - /* Turn on new value wrapper mpde */ cppcast = 0; Swig_mark_arg(i); } else if (strcmp(argv[i],"-nortti") == 0) { @@ -187,7 +185,6 @@ public: } if (cppcast) { - /* Turn on new value wrapper mpde */ Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0); } @@ -1352,7 +1349,7 @@ public: /* Insert cleanup code */ for (p = l; p;) { - if ((tm = Getattr(p,"tmap:freearg"))) { + if (!checkAttribute(p,"tmap:in:numinputs","0") && (tm = Getattr(p,"tmap:freearg"))) { Replaceall(tm,"$source",Getattr(p,"lname")); Printv(cleanup,tm,"\n",NIL); p = Getattr(p,"tmap:freearg:next"); diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx index 435c3c593..e721e0bd4 100644 --- a/Source/Modules/ruby.cxx +++ b/Source/Modules/ruby.cxx @@ -118,6 +118,8 @@ usage = "\ Ruby Options (available with -ruby)\n\ -globalmodule - Wrap everything into the global module\n\ -minherit - Attempt to support multiple inheritance\n\ + -nocppcast - Disable C++ casting operators, useful for generating bugs\n\ + -cppcast - Enable C++ casting operators\n\ -prefix - Set a prefix to be prepended to all names\n\ -feature - Set feature name to (used by `require')\n"; @@ -205,6 +207,7 @@ public: * --------------------------------------------------------------------- */ virtual void main(int argc, char *argv[]) { + int cppcast = 1; /* Set location of SWIG library */ SWIG_library_directory("ruby"); @@ -229,6 +232,12 @@ public: multipleInheritance = true; director_multiple_inheritance = 1; Swig_mark_arg(i); + } else if (strcmp(argv[i],"-cppcast") == 0) { + cppcast = 1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-nocppcast") == 0) { + cppcast = 0; + Swig_mark_arg(i); } else if (strcmp(argv[i],"-prefix") == 0) { if (argv[i+1]) { char *name = argv[i+1]; @@ -245,6 +254,11 @@ public: } } + if (cppcast) { + /* Turn on new value wrapper mpde */ + Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0); + } + /* Add a symbol to the parser for conditional compilation */ Preprocessor_define("SWIGRUBY 1", 0); @@ -889,7 +903,7 @@ public: void insertCleanupCode(ParmList *l, String *cleanup) { String *tm; for (Parm *p = l; p; ) { - if ((tm = Getattr(p,"tmap:freearg"))) { + if (!checkAttribute(p,"tmap:in:numinputs","0") && (tm = Getattr(p,"tmap:freearg"))) { Replaceall(tm,"$source",Getattr(p,"lname")); Printv(cleanup,tm,"\n",NIL); p = Getattr(p,"tmap:freearg:next"); @@ -1132,12 +1146,12 @@ public: /* Now write the wrapper function itself */ if (current == CONSTRUCTOR_ALLOCATE) { Printf(f->def, "#ifdef HAVE_RB_DEFINE_ALLOC_FUNC\n"); - Printv(f->def, "static VALUE\n", wname, "(VALUE self) {", NIL); + Printv(f->def, "SWIGINTERN VALUE\n", wname, "(VALUE self) {", NIL); Printf(f->def, "#else\n"); - Printv(f->def, "static VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); + Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); Printf(f->def, "#endif\n"); } else if (current == CONSTRUCTOR_INITIALIZE) { - Printv(f->def, "static VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); + Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); if (!varargs) { Printf(f->code,"if ((argc < %d) || (argc > %d))\n", numreq-start, numarg-start); } else { @@ -1145,7 +1159,7 @@ public: } Printf(f->code,"rb_raise(rb_eArgError, \"wrong # of arguments(%%d for %d)\",argc);\n",numreq-start); } else { - Printv(f->def, "static VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); + Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL); if (!varargs) { Printf(f->code,"if ((argc < %d) || (argc > %d))\n", numreq-start, numarg-start); } else { @@ -1379,7 +1393,7 @@ public: String *wname = Swig_name_wrapper(symname); Printv(f->def, - "static VALUE ", wname, + "SWIGINTERN VALUE ", wname, "(int nargs, VALUE *args, VALUE self) {", NIL); @@ -1438,7 +1452,7 @@ public: /* create getter */ getfname = NewString(Swig_name_get(iname)); - Printv(getf->def, "static VALUE\n", getfname, "(", NIL); + Printv(getf->def, "SWIGINTERN VALUE\n", getfname, "(", NIL); Printf(getf->def, "VALUE self"); Printf(getf->def, ") {"); Wrapper_add_local(getf,"_val","VALUE _val"); @@ -1461,7 +1475,7 @@ public: } else { /* create setter */ setfname = NewString(Swig_name_set(iname)); - Printv(setf->def, "static VALUE\n", setfname, "(VALUE self, ", NIL); + Printv(setf->def, "SWIGINTERN VALUE\n", setfname, "(VALUE self, ", NIL); Printf(setf->def, "VALUE _val) {"); tm = Swig_typemap_lookup_new("varin",n,name,0); @@ -1594,6 +1608,7 @@ public: value = Char(wname); } String *tm = Swig_typemap_lookup_new("constant", n, value, 0); + if (!tm) tm = Swig_typemap_lookup_new("constcode", n, value, 0); if (tm) { Replaceall(tm, "$source", value); Replaceall(tm, "$target", iname); @@ -1962,7 +1977,7 @@ public: String *freebody = NewString(""); Printv(freefunc, "free_", klass->mname, NIL); - Printv(freebody, "static void\n", + Printv(freebody, "SWIGINTERN void\n", freefunc, "(", klass->type, " *", Swig_cparm_name(0,0), ") {\n", tab4, NIL); diff --git a/Source/Modules/tcl8.cxx b/Source/Modules/tcl8.cxx index 6045d7a53..a04ce6ac4 100644 --- a/Source/Modules/tcl8.cxx +++ b/Source/Modules/tcl8.cxx @@ -85,6 +85,7 @@ public: * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { + int cppcast = 1; SWIG_library_directory("tcl"); @@ -113,11 +114,22 @@ public: } else if (strcmp(argv[i],"-nosafe") == 0) { nosafe = 1; Swig_mark_arg(i); + } else if (strcmp(argv[i],"-cppcast") == 0) { + cppcast = 1; + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-nocppcast") == 0) { + cppcast = 0; + Swig_mark_arg(i); } else if (strcmp(argv[i],"-help") == 0) { fputs(usage,stdout); } } } + + if (cppcast) { + Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0); + } + Preprocessor_define("SWIGTCL 1",0); Preprocessor_define("SWIGTCL8 1", 0); SWIG_typemap_lang("tcl8"); @@ -283,8 +295,11 @@ public: outarg = NewString(""); argstr = NewString("\""); args = NewString(""); - + f = NewWrapper(); + + Wrapper_add_local(f,"resultobj", "Tcl_Obj *resultobj = NULL"); + String *wname = Swig_name_wrapper(iname); if (overname) { Append(wname, overname); @@ -292,7 +307,7 @@ public: Setattr(n,"wrap:name",wname); Printv(f->def, - "static int\n ", wname, "(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {", + "SWIGINTERN int\n ", wname, "(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {", NIL); /* Print out variables for storing arguments. */ @@ -404,7 +419,7 @@ public: /* Insert cleanup code */ for (i = 0, p = parms; p; i++) { - if ((tm = Getattr(p,"tmap:freearg"))) { + if (!checkAttribute(p,"tmap:in:numinputs","0") && (tm = Getattr(p,"tmap:freearg"))) { Replaceall(tm,"$source",Getattr(p,"lname")); Printv(cleanup,tm,"\n",NIL); p = Getattr(p,"tmap:freearg:next"); @@ -417,8 +432,8 @@ public: for (i=0,p = parms; p;i++) { if ((tm = Getattr(p,"tmap:argout"))) { Replaceall(tm,"$source",Getattr(p,"lname")); - Replaceall(tm,"$target","(Tcl_GetObjResult(interp))"); - Replaceall(tm,"$result","(Tcl_GetObjResult(interp))"); + Replaceall(tm,"$target","resultobj"); + Replaceall(tm,"$result","resultobj"); Replaceall(tm,"$arg",Getattr(p,"emit:input")); Replaceall(tm,"$input",Getattr(p,"emit:input")); Printv(outarg,tm,"\n",NIL); @@ -436,8 +451,8 @@ public: /* Return value if necessary */ if ((tm = Swig_typemap_lookup_new("out",n,"result",0))) { Replaceall(tm,"$source", "result"); - Replaceall(tm,"$target", "Tcl_GetObjResult(interp)"); - Replaceall(tm,"$result", "Tcl_GetObjResult(interp)"); + Replaceall(tm,"$target", "resultobj"); + Replaceall(tm,"$result", "resultobj"); if(GetFlag(n,"feature:new")) { Replaceall(tm,"$owner","1"); } else { @@ -467,6 +482,7 @@ public: Replaceall(tm,"$source","result"); Printf(f->code,"%s\n", tm); } + Printv(f->code, "if (resultobj) Tcl_SetObjResult(interp, resultobj);\n", NIL); Printv(f->code, "return TCL_OK;\n", NIL); Printv(f->code, "fail:\n", cleanup, "return TCL_ERROR;\n", NIL); Printv(f->code,"}\n", NIL); @@ -494,7 +510,7 @@ public: String *dname = Swig_name_wrapper(iname); Printv(df->def, - "static int\n", dname, + "SWIGINTERN int\n", dname, "(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {", NIL); Printf(df->code,"Tcl_Obj *CONST *argv = objv+1;\n"); @@ -541,7 +557,7 @@ public: /* Create a function for getting a variable */ getf = NewWrapper(); getname = Swig_name_wrapper(Swig_name_get(iname)); - Printv(getf->def,"static char *",getname,"(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {",NIL); + Printv(getf->def,"SWIGINTERN char *",getname,"(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {",NIL); Wrapper_add_local(getf,"value", "Tcl_Obj *value = 0"); if ((tm = Swig_typemap_lookup_new("varout",n,name,0))) { @@ -568,7 +584,7 @@ public: if (!GetFlag(n,"feature:immutable")) { setf = NewWrapper(); setname = Swig_name_wrapper(Swig_name_set(iname)); - Printv(setf->def,"static char *",setname, "(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {",NIL); + Printv(setf->def,"SWIGINTERN char *",setname, "(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {",NIL); Wrapper_add_local(setf,"value", "Tcl_Obj *value = 0"); Wrapper_add_local(setf,"name1o", "Tcl_Obj *name1o = 0"); @@ -597,7 +613,7 @@ public: static int readonlywrap = 0; if (!readonlywrap) { Wrapper *ro = NewWrapper(); - Printf(ro->def, "static const char *swig_readonly(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {"); + Printf(ro->def, "SWIGINTERN const char *swig_readonly(ClientData clientData, Tcl_Interp *interp, char *name1, char *name2, int flags) {"); Printv(ro->code, "return (char*) \"Variable is read-only\";\n", "}\n", NIL); Wrapper_print(ro,f_wrappers); readonlywrap = 1; @@ -621,7 +637,8 @@ public: String *iname = Getattr(n,"sym:name"); String *nsname = !nspace ? Copy(iname) : NewStringf("%s::%s",ns_name,iname); SwigType *type = Getattr(n,"type"); - String *value = Getattr(n,"value"); + String *rawval = Getattr(n,"rawval"); + String *value = rawval ? rawval : Getattr(n,"value"); String *tm; if (!addSymbol(iname,n)) return SWIG_ERROR; @@ -734,7 +751,7 @@ public: // Register the class structure with the type checker /* Printf(f_init,"SWIG_TypeClientData(SWIGTYPE%s, (void *) &_wrap_class_%s);\n", SwigType_manglestr(t), mangled_classname); */ if (have_destructor) { - Printv(f_wrappers, "static void swig_delete_", class_name, "(void *obj) {\n", NIL); + Printv(f_wrappers, "SWIGINTERN void swig_delete_", class_name, "(void *obj) {\n", NIL); if (destructor_action) { Printv(f_wrappers, SwigType_str(rt,"arg1"), " = (", SwigType_str(rt,0), ") obj;\n", NIL); Printv(f_wrappers, destructor_action, NIL);