1 %include <std/std_except.i> 2 %include <pystdcommon.swg> 3 4 5 /* 6 Generate the traits for a 'primitive' type, such as 'double', 7 for which the SWIG_AsVal and SWIG_From methods are already defined. 8 */ 9 10 %define %traits_ptypen(Type...) 11 %fragment(SWIG_Traits_frag(Type),"header", 12 fragment=SWIG_AsVal_frag(Type), 13 fragment=SWIG_From_frag(Type), 14 fragment="StdTraits") { 15 namespace swig { 16 template <> struct traits<Type > { 17 typedef value_category category; 18 static const char* type_name() { return #Type; } 19 }; 20 template <> struct traits_asval<Type > { 21 typedef Type value_type; 22 static int asval(PyObject *obj, value_type *val) { 23 return SWIG_AsVal(Type)(obj, val); 24 } 25 }; 26 template <> struct traits_from<Type > { 27 typedef Type value_type; 28 static PyObject *from(const value_type& val) { 29 return SWIG_From(Type)(val); 30 } 31 }; 32 } 33 } 34 %enddef 35 36 /* Traits for enums. This is bit of a sneaky trick needed because a generic template specialization of enums 37 is not possible (unless using template meta-programming which SWIG doesn't support because of the explicit 38 instantiations required using %template). The STL containers define the 'front' method and the typemap 39 below is used whenever the front method is wrapped returning an enum. This typemap simply picks up the 40 standard enum typemap, but additionally drags in a fragment containing the traits_asval and traits_from 41 required in the generated code for enums. */ 42 43 %define %traits_enum(Type...) 44 %fragment("SWIG_Traits_enum_"{Type},"header", 45 fragment=SWIG_AsVal_frag(int), 46 fragment=SWIG_From_frag(int), 47 fragment="StdTraits") { 48 namespace swig { 49 template <> struct traits_asval<Type > { 50 typedef Type value_type; 51 static int asval(PyObject *obj, value_type *val) { 52 return SWIG_AsVal(int)(obj, (int *)val); 53 } 54 }; 55 template <> struct traits_from<Type > { 56 typedef Type value_type; 57 static PyObject *from(const value_type& val) { 58 return SWIG_From(int)((int)val); 59 } 60 }; 61 } 62 } 63 %typemap(out, fragment="SWIG_Traits_enum_"{Type}) const enum SWIGTYPE& front %{$typemap(out, const enum SWIGTYPE&)%} 64 %enddef 65 66 67 %include <std/std_common.i> 68 69 // 70 // Generates the traits for all the known primitive 71 // C++ types (int, double, ...) 72 // 73 %apply_cpptypes(%traits_ptypen); 74 75