diff --git a/Lib/python/attribute.i b/Lib/python/attribute.i index 67909e384..7955d5768 100644 --- a/Lib/python/attribute.i +++ b/Lib/python/attribute.i @@ -77,6 +77,23 @@ */ +#ifdef __cplusplus +%define %_attribute(Class, Wrap, type, attr, getcode, setcode) +%extend Class { + type attr; +} +%{ + template inline + type Wrap ##_## attr ## _get(const C* _t) + { return getcode; } + + template inline + void Wrap ##_## attr ## _set(C* _t, type _val) + { setcode; } +%} +%enddef + +#else %define %_attribute(Class, Wrap, type, attr, getcode, setcode) %extend Class { @@ -87,7 +104,7 @@ #define Wrap ##_## attr ## _set(_t, _val) setcode %} %enddef - +#endif // // Internal versions, need Wrap name // @@ -124,10 +141,10 @@ // User versions // -%define %attribute(Class, type, attr, get, ...) - %attribute_T(Class, #@Class, SWIG_arg(type), attr, get, __VA_ARGS__) +%define %attribute(Class, type, attr, get, ...) + %attribute_T(Class, #@Class, SWIG_arg(type), attr, get, __VA_ARGS__) %enddef -%define %attribute_ref(Class, type, refname, ...) -%attribute_ref_T(Class, #@Class, SWIG_arg(type), refname, __VA_ARGS__) +%define %attribute_ref(Class, type, refname, ...) + %attribute_ref_T(Class, #@Class, SWIG_arg(type), refname, __VA_ARGS__) %enddef diff --git a/Lib/python/complex_common.i b/Lib/python/complex_common.i index 23fb6b6cc..ca2e86c3f 100644 --- a/Lib/python/complex_common.i +++ b/Lib/python/complex_common.i @@ -12,7 +12,7 @@ %fragment(SWIG_From_frag(Type),"header") %{ SWIGSTATICINLINE(PyObject*) - SWIG_From_meth(Type)(SWIG_cplusplus(const Type&, Type) c) + SWIG_From(Type)(SWIG_cplusplus(const Type&, Type) c) { return PyComplex_FromDoubles(Real(c), Imag(c)); } @@ -25,7 +25,7 @@ SWIGSTATICINLINE(PyObject*) fragment=SWIG_AsVal_frag(double)) %{ SWIGSTATICINLINE(int) - SWIG_AsVal_meth(Type) (PyObject *o, Type* val) + SWIG_AsVal(Type) (PyObject *o, Type* val) { if (PyComplex_Check(o)) { if (val) *val = Constructor(PyComplex_RealAsDouble(o), @@ -33,7 +33,7 @@ SWIGSTATICINLINE(int) return 1; } else { double d; - if (SWIG_AsVal_meth(double)(o, &d)) { + if (SWIG_AsVal(double)(o, &d)) { if (val) *val = Constructor(d, 0); return 1; } else { @@ -56,7 +56,7 @@ SWIGSTATICINLINE(int) fragment=SWIG_AsVal_frag(float)) %{ SWIGSTATICINLINE(int) - SWIG_AsVal_meth(Type)(PyObject *o, Type *val) + SWIG_AsVal(Type)(PyObject *o, Type *val) { const char* errmsg = val ? #Type : 0; if (PyComplex_Check(o)) { @@ -72,7 +72,7 @@ SWIGSTATICINLINE(int) } } else { double re; - if (SWIG_AsVal_meth(double)(o, &re)) { + if (SWIG_AsVal(double)(o, &re)) { if (SWIG_CheckDoubleInRange(re, FLT_MIN, FLT_MAX, errmsg)) { if (val) *val = Constructor(swig_numeric_cast(re,float), 0); return 1; diff --git a/Lib/python/pymacros.swg b/Lib/python/pymacros.swg index c541cd9b3..45b02cb4c 100644 --- a/Lib/python/pymacros.swg +++ b/Lib/python/pymacros.swg @@ -2,12 +2,13 @@ /* Auxiliar swig macros that appear in the header */ #define SWIG_OLDOBJ 1 -#define SWIG_NEWOBJ 2 +#define SWIG_NEWOBJ SWIG_OLDOBJ + 1 #ifdef __cplusplus #define SWIGSTATICINLINE(a) static inline a #define SWIGSTATIC(a) static a #define swig_new_array(size,Type) (new Type[(size)]) +#define swig_delete(cptr) delete cptr #define swig_delete_array(cptr) delete[] cptr #define swig_const_cast(a,Type) const_cast(a) #define swig_static_cast(a,Type) static_cast(a) @@ -20,6 +21,7 @@ #define SWIGSTATICINLINE(a) static a #define SWIGSTATIC(a) static a #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) #define swig_const_cast(a,Type) (Type)(a) #define swig_static_cast(a,Type) (Type)(a) @@ -36,18 +38,19 @@ #define SWIG_str(...) #__VA_ARGS__ #define SWIG_Mangle(...) #@__VA_ARGS__ +#define SWIG_Descriptor(...) SWIGTYPE_ ## #@__VA_ARGS__ -#define SWIG_MethodType(Name, ...) SWIG_ ## Name ## _ ## #@__VA_ARGS__ +#define SWIG_NameType(Name, ...) SWIG_ ## Name ## _ ## #@__VA_ARGS__ #define SWIG_StringType(Name, ...) "SWIG_" #Name "_" {__VA_ARGS__} -#define SWIG_AsVal_meth(...) SWIG_MethodType(AsVal, __VA_ARGS__) -#define SWIG_AsPtr_meth(...) SWIG_MethodType(AsPtr, __VA_ARGS__) -#define SWIG_As_meth(...) SWIG_MethodType(As, __VA_ARGS__) -#define SWIG_From_meth(...) SWIG_MethodType(From, __VA_ARGS__) -#define SWIG_Check_meth(...) SWIG_MethodType(Check, __VA_ARGS__) -#define SWIG_CCode(...) SWIG_MethodType(TYPECHECK, __VA_ARGS__) -#define SWIG_OrderType(...) SWIG_MethodType(OrderType, __VA_ARGS__) -#define SWIG_EqualType(...) SWIG_MethodType(EqualType, __VA_ARGS__) +#define SWIG_AsVal(...) SWIG_NameType(AsVal, __VA_ARGS__) +#define SWIG_AsPtr(...) SWIG_NameType(AsPtr, __VA_ARGS__) +#define SWIG_As(...) SWIG_NameType(As, __VA_ARGS__) +#define SWIG_From(...) SWIG_NameType(From, __VA_ARGS__) +#define SWIG_Check(...) SWIG_NameType(Check, __VA_ARGS__) +#define SWIG_CCode(...) SWIG_NameType(TYPECHECK, __VA_ARGS__) +#define SWIG_OrderType(...) SWIG_NameType(OrderType, __VA_ARGS__) +#define SWIG_EqualType(...) SWIG_NameType(EqualType, __VA_ARGS__) #define SWIG_Traits_frag(...) SWIG_StringType(Traits, __VA_ARGS__) #define SWIG_AsPtr_frag(...) SWIG_StringType(AsPtr, __VA_ARGS__) @@ -97,32 +100,50 @@ macro(arg1, arg2) %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 -%define %_evalif(x,macro) -#if x == 1 -macro -#endif -%enddef - -%define %_evalif_2(x,y,macro) -#if x == 1 && #y == 1 -macro -#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 +/* + %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/python/pyprimtypes.swg b/Lib/python/pyprimtypes.swg index b0eb61671..76b03720d 100644 --- a/Lib/python/pyprimtypes.swg +++ b/Lib/python/pyprimtypes.swg @@ -10,16 +10,16 @@ %insert(header) { -SWIG_define(SWIG_From_meth(signed char), PyInt_FromLong); -SWIG_define(SWIG_From_meth(unsigned char), PyInt_FromLong); -SWIG_define(SWIG_From_meth(short), PyInt_FromLong); -SWIG_define(SWIG_From_meth(unsigned short), PyInt_FromLong); -SWIG_define(SWIG_From_meth(int), PyInt_FromLong); -SWIG_define(SWIG_From_meth(long), PyInt_FromLong); -SWIG_define(SWIG_From_meth(float), PyFloat_FromDouble); -SWIG_define(SWIG_From_meth(double), PyFloat_FromDouble); -SWIG_define(SWIG_From_meth(float), PyFloat_FromDouble); -SWIG_define(SWIG_From_meth(double), PyFloat_FromDouble); +SWIG_define(SWIG_From(signed char), PyInt_FromLong); +SWIG_define(SWIG_From(unsigned char), PyInt_FromLong); +SWIG_define(SWIG_From(short), PyInt_FromLong); +SWIG_define(SWIG_From(unsigned short), PyInt_FromLong); +SWIG_define(SWIG_From(int), PyInt_FromLong); +SWIG_define(SWIG_From(long), PyInt_FromLong); +SWIG_define(SWIG_From(float), PyFloat_FromDouble); +SWIG_define(SWIG_From(double), PyFloat_FromDouble); +SWIG_define(SWIG_From(float), PyFloat_FromDouble); +SWIG_define(SWIG_From(double), PyFloat_FromDouble); } %fragment("","header") %{ @@ -28,10 +28,10 @@ SWIG_define(SWIG_From_meth(double), PyFloat_FromDouble); %fragment(SWIG_AsVal_frag(unsigned long),"header") { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(unsigned long)(PyObject * obj, unsigned long *val) + SWIG_AsVal(unsigned long)(PyObject * obj, unsigned long *val) { if (PyInt_Check(obj)) { - long v = PyInt_AsLong(obj); + long v = PyInt_AS_LONG(obj); if (v >= 0) { if (val) *val = v; return 1; @@ -101,14 +101,14 @@ SWIGSTATICINLINE(int) %fragment(SWIG_AsVal_frag(double),"header") { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(double)(PyObject *obj, double *val) + SWIG_AsVal(double)(PyObject *obj, double *val) { if (PyFloat_Check(obj)) { - if (val) *val = PyFloat_AsDouble(obj); + if (val) *val = PyFloat_AS_DOUBLE(obj); return 1; } if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); + if (val) *val = PyInt_AS_LONG(obj); return 1; } if (PyLong_Check(obj)) { @@ -130,10 +130,10 @@ SWIGSTATICINLINE(int) %fragment(SWIG_AsVal_frag(long),"header") { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(long)(PyObject * obj, long* val) + SWIG_AsVal(long)(PyObject * obj, long* val) { if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); + if (val) *val = PyInt_AS_LONG(obj); return 1; } if (PyLong_Check(obj)) { @@ -157,7 +157,7 @@ SWIGSTATICINLINE(int) %fragment(SWIG_From_frag(long long),"header", fragment="") { SWIGSTATICINLINE(PyObject* ) - SWIG_From_meth(long long)(long long value) + SWIG_From(long long)(long long value) { return ((value < LONG_MIN) || (value > LONG_MAX)) ? PyLong_FromLongLong(value) @@ -169,7 +169,7 @@ SWIGSTATICINLINE(PyObject* ) %fragment(SWIG_From_frag(unsigned long long),"header", fragment="") { SWIGSTATICINLINE(PyObject* ) - SWIG_From_meth(unsigned long long)(unsigned long long value) + SWIG_From(unsigned long long)(unsigned long long value) { return (value > LONG_MAX) ? PyLong_FromUnsignedLongLong(value) : @@ -179,7 +179,7 @@ SWIGSTATICINLINE(PyObject* ) %fragment(SWIG_AsVal_frag(long long),"header") { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(long long)(PyObject *obj, long long *val) + SWIG_AsVal(long long)(PyObject *obj, long long *val) { if (PyLong_Check(obj)) { long long v = PyLong_AsLongLong(obj); @@ -192,7 +192,7 @@ SWIGSTATICINLINE(int) } } if (PyInt_Check(obj)) { - if (val) *val = PyInt_AsLong(obj); + if (val) *val = PyInt_AS_LONG(obj); return 1; } if (val) { @@ -205,7 +205,7 @@ SWIGSTATICINLINE(int) %fragment(SWIG_AsVal_frag(unsigned long long),"header", fragment=SWIG_AsVal_frag(unsigned long)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(unsigned long long)(PyObject *obj, unsigned long long *val) + SWIG_AsVal(unsigned long long)(PyObject *obj, unsigned long long *val) { if (PyLong_Check(obj)) { unsigned long long v = PyLong_AsUnsignedLongLong(obj); @@ -218,7 +218,7 @@ SWIGSTATICINLINE(int) } } unsigned long v; - if (SWIG_AsVal_meth(unsigned long)(obj,&v)) { + if (SWIG_AsVal(unsigned long)(obj,&v)) { if (val) *val = v; return 1; } else { @@ -233,7 +233,7 @@ SWIGSTATICINLINE(int) %fragment(SWIG_From_frag(unsigned long),"header") { SWIGSTATICINLINE(PyObject* ) - SWIG_From_meth(unsigned long)(unsigned long value) + SWIG_From(unsigned long)(unsigned long value) { return (value > LONG_MAX) ? PyLong_FromUnsignedLong(value) @@ -245,11 +245,11 @@ SWIGSTATICINLINE(PyObject* ) fragment="SWIG_CheckLongInRange", fragment=SWIG_AsVal_frag(long)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(signed char)(PyObject *obj, signed char *val) + SWIG_AsVal(signed char)(PyObject *obj, signed char *val) { const char* errmsg = val ? "signed char" : 0; long v; - if (SWIG_AsVal_meth(long)(obj, &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; @@ -270,11 +270,11 @@ SWIGSTATICINLINE(int) fragment="SWIG_CheckLongInRange", fragment=SWIG_AsVal_frag(long)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(short)(PyObject *obj, short *val) + SWIG_AsVal(short)(PyObject *obj, short *val) { const char* errmsg = val ? "short" : 0; long v; - if (SWIG_AsVal_meth(long)(obj, &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; @@ -298,11 +298,11 @@ SWIGSTATICINLINE(int) fragment=SWIG_AsVal_frag(long)) { %#if INT_MAX != LONG_MAX SWIGSTATICINLINE(int) -SWIG_AsVal_meth(int)(PyObject *obj, int *val) + SWIG_AsVal(int)(PyObject *obj, int *val) { const char* errmsg = val ? "int" : 0; long v; - if (SWIG_AsVal_meth(long)(obj, &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; @@ -319,9 +319,9 @@ SWIG_AsVal_meth(int)(PyObject *obj, int *val) } %#else SWIGSTATICINLINE(int) -SWIG_AsVal_meth(int)(PyObject *obj, int *val) + SWIG_AsVal(int)(PyObject *obj, int *val) { - return SWIG_AsVal_meth(long)(obj,(long*)val); + return SWIG_AsVal(long)(obj,(long*)val); } %#endif } @@ -331,11 +331,11 @@ SWIG_AsVal_meth(int)(PyObject *obj, int *val) fragment=SWIG_AsVal_frag(unsigned long)) { %#if UINT_MAX != ULONG_MAX SWIGSTATICINLINE(int) -SWIG_AsVal_meth(unsigned int)(PyObject *obj, unsigned int *val) +SWIG_AsVal(unsigned int)(PyObject *obj, unsigned int *val) { const char* errmsg = val ? "unsigned int" : 0; unsigned long v; - if (SWIG_AsVal_meth(unsigned long)(obj, &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; @@ -350,9 +350,9 @@ SWIG_AsVal_meth(unsigned int)(PyObject *obj, unsigned int *val) } %#else SWIGSTATICINLINE(unsigned int) - SWIG_AsVal_meth(unsigned int)(PyObject *obj, unsigned int *val) + SWIG_AsVal(unsigned int)(PyObject *obj, unsigned int *val) { - return SWIG_AsVal_meth(unsigned long)(obj,(unsigned long *)val); + return SWIG_AsVal(unsigned long)(obj,(unsigned long *)val); } %#endif } @@ -361,9 +361,9 @@ SWIGSTATICINLINE(unsigned int) fragment=SWIG_From_frag(long), fragment=SWIG_From_frag(unsigned long)) { %#if UINT_MAX < LONG_MAX -SWIG_define(SWIG_From_meth(unsigned int), SWIG_From_meth(long)); +SWIG_define(SWIG_From(unsigned int), SWIG_From(long)); %#else -SWIG_define(SWIG_From_meth(unsigned int), SWIG_From_meth(unsigned long)); +SWIG_define(SWIG_From(unsigned int), SWIG_From(unsigned long)); %#endif } @@ -371,11 +371,11 @@ SWIG_define(SWIG_From_meth(unsigned int), SWIG_From_meth(unsigned long)); fragment=SWIG_AsVal_frag(unsigned long), fragment="SWIG_CheckUnsignedLongInRange") { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(unsigned char)(PyObject *obj, unsigned char *val) + SWIG_AsVal(unsigned char)(PyObject *obj, unsigned char *val) { const char* errmsg = val ? "unsigned char" : 0; unsigned long v; - if (SWIG_AsVal_meth(unsigned long)(obj, &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; @@ -396,11 +396,11 @@ SWIGSTATICINLINE(int) fragment="SWIG_CheckUnsignedLongInRange", fragment=SWIG_AsVal_frag(unsigned long)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(unsigned short)(PyObject *obj, unsigned short *val) + SWIG_AsVal(unsigned short)(PyObject *obj, unsigned short *val) { const char* errmsg = val ? "unsigned short" : 0; unsigned long v; - if (SWIG_AsVal_meth(unsigned long)(obj, &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; @@ -421,7 +421,7 @@ SWIGSTATICINLINE(int) %fragment("SWIG_CheckDoubleInRange","header") { %#include -SWIGSTATIC(int) +SWIGSTATICINLINE(int) SWIG_CheckDoubleInRange(double value, double min_value, double max_value, const char* errmsg) { @@ -448,11 +448,11 @@ SWIGSTATIC(int) fragment="SWIG_CheckDoubleInRange", fragment=SWIG_AsVal_frag(double)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(float)(PyObject *obj, float *val) + SWIG_AsVal(float)(PyObject *obj, float *val) { const char* errmsg = val ? "float" : 0; double v; - if (SWIG_AsVal_meth(double)(obj, &v)) { + if (SWIG_AsVal(double)(obj, &v)) { if (SWIG_CheckDoubleInRange(v, FLT_MIN, FLT_MAX, errmsg)) { if (val) *val = v; return 1; @@ -471,7 +471,7 @@ SWIGSTATICINLINE(int) %fragment(SWIG_From_frag(char),"header") { SWIGSTATICINLINE(PyObject*) - SWIG_From_meth(char)(char c) + SWIG_From(char)(char c) { return PyString_FromStringAndSize(&c,1); } @@ -482,11 +482,11 @@ SWIGSTATICINLINE(PyObject*) fragment="SWIG_CheckLongInRange", fragment=SWIG_AsVal_frag(long)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(char)(PyObject *obj, char *val) + SWIG_AsVal(char)(PyObject *obj, char *val) { const char* errmsg = val ? "char" : 0; long v; - if (SWIG_AsVal_meth(long)(obj, &v)) { + if (SWIG_AsVal(long)(obj, &v)) { if (SWIG_CheckLongInRange(v, CHAR_MIN,CHAR_MAX, errmsg)) { if (val) *val = v; return 1; @@ -502,7 +502,7 @@ SWIGSTATICINLINE(int) %fragment(SWIG_From_frag(bool),"header") { SWIGSTATICINLINE(PyObject*) - SWIG_From_meth(bool)(bool value) + SWIG_From(bool)(bool value) { PyObject *obj = value ? Py_True : Py_False; Py_INCREF(obj); @@ -513,7 +513,7 @@ SWIGSTATICINLINE(PyObject*) %fragment(SWIG_AsVal_frag(bool),"header", fragment=SWIG_AsVal_frag(int)) { SWIGSTATICINLINE(int) - SWIG_AsVal_meth(bool)(PyObject *obj, bool *val) + SWIG_AsVal(bool)(PyObject *obj, bool *val) { /* if (val) *val = PyObject_IsTrue(obj); return 1; */ if (obj == Py_True) { @@ -525,7 +525,7 @@ SWIGSTATICINLINE(int) return 1; } int res; - if (SWIG_AsVal_meth(int)(obj, &res)) { + if (SWIG_AsVal(int)(obj, &res)) { if (val) *val = (bool)res; return 1; } @@ -582,7 +582,7 @@ _apply_macro(Macro, char , __VA_ARGS__); /* apply the Macro(Type) to all the C++ types */ %define %apply_cpptypes(Macro,...) %apply_ctypes(Macro, __VA_ARGS__) -_apply_macro(Macro, std::basic_string, __VA_ARGS__); +_apply_macro(Macro, std::string, __VA_ARGS__); _apply_macro(Macro, std::complex , __VA_ARGS__); _apply_macro(Macro, std::complex , __VA_ARGS__); %enddef @@ -616,29 +616,3 @@ Macro(SWIG_CCode(CHAR), char); %apply_checkctypes(%typemap_primitive) -/* ------------------------------------------------------------ - * equal and order types definition. - * these are needed to decide when we the comparison - * operators ==, !=, <=, etc, can be used. - * ------------------------------------------------------------ */ - -/* the operators ==, != can used with these types */ -%swig_equal_type(bool); -%swig_equal_type(std::complex); -%swig_equal_type(std::complex); - -/* the operators <,>,<=,=> can used with these types */ -%swig_order_type(std::basic_string); -%swig_order_type(signed char); -%swig_order_type(unsigned char); -%swig_order_type(short); -%swig_order_type(unsigned short); -%swig_order_type(int); -%swig_order_type(unsigned int); -%swig_order_type(long); -%swig_order_type(unsigned long); -%swig_order_type(long long); -%swig_order_type(unsigned long long); -%swig_order_type(float); -%swig_order_type(double); -%swig_order_type(char); diff --git a/Lib/python/pyptrtypes.swg b/Lib/python/pyptrtypes.swg index 5ca9207e4..0faa58a93 100644 --- a/Lib/python/pyptrtypes.swg +++ b/Lib/python/pyptrtypes.swg @@ -13,7 +13,7 @@ $1 = *ptr; if (res == SWIG_NEWOBJ) delete ptr; } - %typemap(in,fragment=pyfrag) const __VA_ARGS__ & (int res = 0) + %typemap(in,fragment=pyfrag) const __VA_ARGS__ & (int res = 0) "if (!(res = asptr_meth($input, &$1)) || !($1)) SWIG_fail;"; %typemap(freearg) const __VA_ARGS__ & @@ -83,10 +83,10 @@ %fragment(SWIG_AsVal_frag(__VA_ARGS__),"header", fragment=SWIG_AsPtr_frag(__VA_ARGS__)) %{ SWIGSTATICINLINE(int) - SWIG_AsVal_meth(__VA_ARGS__)(PyObject* obj, __VA_ARGS__ *val) + SWIG_AsVal(__VA_ARGS__)(PyObject* obj, __VA_ARGS__ *val) { __VA_ARGS__ *v = (__VA_ARGS__ *)0; - int res = SWIG_AsPtr_meth(__VA_ARGS__)(obj, &v); + int res = SWIG_AsPtr(__VA_ARGS__)(obj, &v); if (!res || !v) return 0; if (val) { *val = *v; @@ -98,10 +98,10 @@ %fragment(SWIG_As_frag(__VA_ARGS__),"header", fragment=SWIG_AsVal_frag(__VA_ARGS__)) %{ SWIGSTATICINLINE(__VA_ARGS__) - SWIG_As_meth(__VA_ARGS__)(PyObject* obj) + SWIG_As(__VA_ARGS__)(PyObject* obj) { __VA_ARGS__ v; - SWIG_AsVal_meth(__VA_ARGS__)(obj, &v); + SWIG_AsVal(__VA_ARGS__)(obj, &v); return v; } %} @@ -123,8 +123,8 @@ %define %typemap_asptrfromn(CheckCode, ...) %typemap_asptrfrom(SWIG_arg(CheckCode), - SWIG_arg(SWIG_AsPtr_meth(__VA_ARGS__)), - SWIG_arg(SWIG_From_meth(__VA_ARGS__)), + SWIG_arg(SWIG_AsPtr(__VA_ARGS__)), + SWIG_arg(SWIG_From(__VA_ARGS__)), SWIG_arg(SWIG_AsPtr_frag(__VA_ARGS__)), SWIG_arg(SWIG_From_frag(__VA_ARGS__)), __VA_ARGS__); diff --git a/Lib/python/pystrings.swg b/Lib/python/pystrings.swg index acf7fb3f5..6e7765f71 100644 --- a/Lib/python/pystrings.swg +++ b/Lib/python/pystrings.swg @@ -4,35 +4,34 @@ * ------------------------------------------------------------ */ %types(char *); - +%{ +#define SWIG_PYSTR SWIG_NEWOBJ + 1 +%} %fragment("SWIG_AsCharPtrAndSize","header") %{ -/* returns '1' if the input is a raw char*, '2' if is a PyString */ +/* returns SWIG_OLDOBJ if the input is a raw char*, SWIG_PYSTR if is a PyString */ SWIGSTATIC(int) -SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* size) +SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize) { static swig_type_info* pchar_info = 0; - int vsize = 0; char* vptr = 0; if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); if (SWIG_ConvertPtr(obj, (void**)&vptr, pchar_info, 0) != -1) { if (cptr) *cptr = vptr; - if (size) *size = vptr ? (strlen(vptr) + 1) : 0; + if (psize) *psize = vptr ? (strlen(vptr) + 1) : 0; return SWIG_OLDOBJ; } else { if (PyString_Check(obj)) { -#if PY_VERSION_HEX >= 0x02000000 - PyString_AsStringAndSize(obj, &vptr, &vsize); -#else - vptr = PyString_AsString(obj); - vsize = PyString_Size(obj); -#endif - if (cptr) *cptr = vptr; - if (size) *size = vsize; - return SWIG_NEWOBJ; + if (cptr) { + *cptr = PyString_AS_STRING(obj); + if (psize) { + *psize = PyString_GET_SIZE(obj) + 1; + } + } + return SWIG_PYSTR; } } - if (cptr || size) { + if (cptr) { PyErr_SetString(PyExc_TypeError, "a string is expected"); } return 0; @@ -44,8 +43,8 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* size) SWIGSTATICINLINE(int) SWIG_AsCharPtr(PyObject *obj, char **val) { - char* cptr; - if (SWIG_AsCharPtrAndSize(obj, &cptr, 0)) { + char* cptr = 0; + if (SWIG_AsCharPtrAndSize(obj, &cptr, (size_t*)(0))) { if (val) *val = cptr; return 1; } @@ -60,18 +59,21 @@ SWIGSTATICINLINE(int) SWIGSTATICINLINE(PyObject *) SWIG_FromCharPtr(const char* cptr) { - size_t size = cptr ? strlen(cptr) : 0; if (cptr) { + size_t size = strlen(cptr); if (size > INT_MAX) { return SWIG_NewPointerObj(swig_const_cast(cptr,char*), SWIG_TypeQuery("char *"), 0); } else { - return PyString_FromStringAndSize(cptr, swig_numeric_cast(size,int)); + if (size != 0) { + return PyString_FromStringAndSize(cptr, size); + } else { + return PyString_FromString(cptr); + } } - } else { - Py_INCREF(Py_None); - return Py_None; } + Py_INCREF(Py_None); + return Py_None; } %} @@ -84,22 +86,18 @@ SWIGSTATIC(int) int res = SWIG_AsCharPtrAndSize(obj, &cptr, &csize); if (res) { if (val) { - /* we add the '0' terminator if needed for PyString */ - size_t size = ((res == 2) && csize && (cptr[csize - 1])) ? - csize + 1 : csize; - if (size) { - *val = swig_new_array(size, char); - if (csize) memcpy(*val, cptr, csize); - if (csize < size) (*val)[csize] = 0; + 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 1; + return SWIG_NEWOBJ; } if (val) { PyErr_SetString(PyExc_TypeError, "a char* is expected"); @@ -115,15 +113,13 @@ SWIG_AsCharArray(PyObject *obj, char *val, size_t size) { char* cptr; size_t csize; if (SWIG_AsCharPtrAndSize(obj, &cptr, &csize)) { - /* in C (but not in C++) you can do: - + /* in C you can do: + char x[5] = "hello"; ie, assing the array using an extra '0' char. */ -#ifndef __cplusplus if ((csize == size + 1) && !(cptr[csize-1])) --csize; -#endif if (csize <= size) { if (val) { if (csize) memcpy(val, cptr, csize); @@ -370,7 +366,7 @@ SWIG_FromCharArray(const char* carray, size_t size) int res = SWIG_AsCharPtrAndSize($input, &buf, &size); if (!res) SWIG_fail; $1 = ($1_ltype) buf; - $2 = ($2_ltype) ((res == 1) && size) ? size - 1 : size; + $2 = ($2_ltype) size - 1; } /* Here size includes the '0' terminator */ diff --git a/Lib/python/pyvaltypes.swg b/Lib/python/pyvaltypes.swg index dfdaf20aa..3331eda0f 100644 --- a/Lib/python/pyvaltypes.swg +++ b/Lib/python/pyvaltypes.swg @@ -117,24 +117,31 @@ %fragment(SWIG_As_frag(__VA_ARGS__),"header", fragment=SWIG_AsVal_frag(__VA_ARGS__)) %{ SWIGSTATICINLINE(__VA_ARGS__) -SWIG_As_meth(__VA_ARGS__)(PyObject* obj) +SWIG_As(__VA_ARGS__)(PyObject* obj) { __VA_ARGS__ v; - SWIG_AsVal_meth(__VA_ARGS__)(obj, &v); + if (!SWIG_AsVal(__VA_ARGS__)(obj, &v)) { + /* + this is needed to make valgrind/purify happier. the other + solution is throw an exception, but since this code should work + with plain C .... + */ + memset((void*)&v, 0, sizeof(__VA_ARGS__)); + } return v; } %} %fragment(SWIG_Check_frag(__VA_ARGS__),"header", fragment=SWIG_AsVal_frag(__VA_ARGS__)) %{ SWIGSTATICINLINE(int) -SWIG_Check_meth(__VA_ARGS__)(PyObject* obj) +SWIG_Check(__VA_ARGS__)(PyObject* obj) { - return SWIG_AsVal_meth(__VA_ARGS__)(obj, (__VA_ARGS__*)0); + return SWIG_AsVal(__VA_ARGS__)(obj, (__VA_ARGS__*)0); } %} %typemap_ascheck(SWIG_arg(CheckCode), - SWIG_As_meth(__VA_ARGS__), - SWIG_Check_meth(__VA_ARGS__), + SWIG_As(__VA_ARGS__), + SWIG_Check(__VA_ARGS__), SWIG_arg(SWIG_As_frag(__VA_ARGS__)), SWIG_arg(SWIG_Check_frag(__VA_ARGS__)), __VA_ARGS__); @@ -172,7 +179,7 @@ SWIG_Check_meth(__VA_ARGS__)(PyObject* obj) */ %define %typemap_asvalfromn(CheckCode, ...) %typemap_asvaln(SWIG_arg(CheckCode), __VA_ARGS__); - %typemap_from(SWIG_arg(SWIG_From_meth(__VA_ARGS__)), + %typemap_from(SWIG_arg(SWIG_From(__VA_ARGS__)), SWIG_arg(SWIG_From_frag(__VA_ARGS__)), __VA_ARGS__); @@ -186,9 +193,9 @@ SWIG_Check_meth(__VA_ARGS__)(PyObject* obj) */ %define %typemap_ascheckfromn(CheckCode, ...) %typemap_ascheckfrom(SWIG_arg(CheckCode), - SWIG_As_meth(__VA_ARGS__), - SWIG_From_meth(__VA_ARGS__), - SWIG_Check_meth(__VA_ARGS__), + SWIG_As(__VA_ARGS__), + SWIG_From(__VA_ARGS__), + SWIG_Check(__VA_ARGS__), SWIG_arg(SWIG_As_frag(__VA_ARGS__)), SWIG_arg(SWIG_From_frag(__VA_ARGS__)), SWIG_arg(SWIG_Check_frag(__VA_ARGS__)), diff --git a/Lib/python/std_common.i b/Lib/python/std_common.i index dcd6f9c47..7fb934190 100644 --- a/Lib/python/std_common.i +++ b/Lib/python/std_common.i @@ -327,14 +327,14 @@ namespace swigpy { template <> struct traits_asval<__VA_ARGS__ > { typedef __VA_ARGS__ value_type; static int asval(PyObject *obj, value_type *val) { - return SWIG_AsVal_meth(__VA_ARGS__)(obj, val); + return SWIG_AsVal(__VA_ARGS__)(obj, val); } }; template <> struct traits_from<__VA_ARGS__ > { typedef __VA_ARGS__ value_type; static PyObject *from(const value_type& val) { - return SWIG_From_meth(__VA_ARGS__)(val); + return SWIG_From(__VA_ARGS__)(val); } }; } @@ -413,6 +413,15 @@ namespace swigpy { } %enddef +/* + Comparison methods +*/ + +%define %std_comp_methods(...) +%std_equal_methods(__VA_ARGS__ ) +%std_order_methods(__VA_ARGS__ ) +%enddef + #ifdef SWIG_STD_EXTEND_COMPARISON %define %std_extcomp(Class,T) %evalif(SWIG_EqualType(T), %std_equal_methods(std::Class)) @@ -438,6 +447,34 @@ namespace swigpy { #define %std_definst_2(Class,...) #endif +/* ------------------------------------------------------------ + * equal and order types definition. + * these are needed to decide when we the comparison + * operators ==, !=, <=, etc, can be used. + * ------------------------------------------------------------ */ + +/* the operators ==, != can used with these types */ +%swig_equal_type(bool); +%swig_equal_type(std::complex); +%swig_equal_type(std::complex); + +/* the operators <,>,<=,=> can used with these types */ +%swig_order_type(std::string); +%swig_order_type(std::basic_string); +%swig_order_type(signed char); +%swig_order_type(unsigned char); +%swig_order_type(short); +%swig_order_type(unsigned short); +%swig_order_type(int); +%swig_order_type(unsigned int); +%swig_order_type(long); +%swig_order_type(unsigned long); +%swig_order_type(long long); +%swig_order_type(unsigned long long); +%swig_order_type(float); +%swig_order_type(double); +%swig_order_type(char); + // // Backward compatibility diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index 9539664b0..b60a5c29e 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -75,8 +75,25 @@ namespace std { %typemap_traits(SWIG_CCode(PAIR), std::pair); + pair(); + pair(const T& __a, const U& __b); + pair(const pair& __p); + T first; U second; + + %extend + { + const T& f() { + return self->first; + } + + %pythoncode { + def __repr__(self): + return "(%s, %s)" %(str(self.first),str(self.second)) + } + } + }; // *** @@ -104,6 +121,10 @@ namespace std { %typemap_traits(SWIG_CCode(PAIR), std::pair); + pair(); + pair(const T& __a, U* __b); + pair(const pair& __p); + T first; U* second; }; @@ -129,6 +150,10 @@ namespace std { %typemap_traits(SWIG_CCode(PAIR), std::pair); + pair(); + pair(T* __a, const U& __b); + pair(const pair& __p); + T* first; U second; }; @@ -151,6 +176,10 @@ namespace std { %typemap_traits(SWIG_CCode(PAIR), std::pair); + pair(); + pair(T* __a, U* __b); + pair(const pair& __p); + T* first; U* second; }; diff --git a/Lib/python/std_string.i b/Lib/python/std_string.i index 6604424ee..fe7ce831b 100644 --- a/Lib/python/std_string.i +++ b/Lib/python/std_string.i @@ -98,6 +98,83 @@ namespace std { basic_string& replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c); + + size_type + copy(_CharT* __s, size_type __n, size_type __pos = 0) const; + + // String operations: + const _CharT* c_str() const; + + size_type + find(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + find(const basic_string& __str, size_type __pos = 0) const; + + size_type + find(_CharT __c, size_type __pos = 0) const; + + size_type + rfind(const basic_string& __str, size_type __pos = npos) const; + + size_type + rfind(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + rfind(_CharT __c, size_type __pos = npos) const; + + size_type + find_first_of(const basic_string& __str, size_type __pos = 0) const; + + size_type + find_first_of(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + find_first_of(_CharT __c, size_type __pos = 0) const; + + size_type + find_last_of(const basic_string& __str, size_type __pos = npos) const; + + size_type + find_last_of(const _CharT* __s, size_type __pos, size_type __n) const; + + size_type + find_last_of(_CharT __c, size_type __pos = npos) const; + + size_type + find_first_not_of(const basic_string& __str, size_type __pos = 0) const; + + size_type + find_first_not_of(const _CharT* __s, size_type __pos, + size_type __n) const; + + size_type + find_first_not_of(_CharT __c, size_type __pos = 0) const; + + size_type + find_last_not_of(const basic_string& __str, size_type __pos = npos) const; + + size_type + find_last_not_of(const _CharT* __s, size_type __pos, + size_type __n) const; + + size_type + find_last_not_of(_CharT __c, size_type __pos = npos) const; + + basic_string + substr(size_type __pos = 0, size_type __n = npos) const; + + int + compare(const basic_string& __str) const; + + int + compare(size_type __pos, size_type __n, const basic_string& __str) const; + + int + compare(size_type __pos1, size_type __n1, const basic_string& __str, + size_type __pos2, size_type __n2) const; + + %ignore pop_back(); %ignore front() const; %ignore back() const; @@ -148,18 +225,33 @@ namespace std { #endif }; - /* - swig workaround. if used as expected, __iadd__ deletes 'self'. - */ - %newobject basic_string::__iadd__; + %newobject basic_string::__iadd__; + %newobject basic_string::__add__; + %newobject basic_string::__radd__; %extend basic_string { + /* + swig workaround. if used as expected, __iadd__ deletes 'self'. + */ std::string* __iadd__(const std::string& v) { *self += v; return new std::string(*self); } - std::string __str__() { - return *self; + + std::string* __add__(const std::string& v) { + std::string* res = new std::string(*self); + *res += v; + return res; } + + std::string* __radd__(const std::string& v) { + std::string* res = new std::string(v); + *res += *self; + return res; + } + + const std::string& __str__() { + return *self; + } } %std_equal_methods(basic_string); @@ -173,42 +265,74 @@ namespace std { %fragment(SWIG_AsPtr_frag(std::basic_string),"header", fragment="SWIG_AsCharPtrAndSize") { -SWIGSTATICINLINE(int) - SWIG_AsPtr_meth(std::basic_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); - return SWIG_NEWOBJ; - } + SWIGSTATICINLINE(int) + SWIG_AsPtr(std::basic_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(); - } - if (val) { - PyErr_SetString(PyExc_TypeError,"a string is expected"); + 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; } - return 0; } - } + + SWIGSTATICINLINE(int) + SWIG_AsPtr(std::string)(PyObject* obj, std::string **val) + { + return SWIG_AsPtr(std::basic_string)(obj, val); + } } %fragment(SWIG_From_frag(std::basic_string),"header", fragment="SWIG_FromCharArray") { SWIGSTATICINLINE(PyObject*) - SWIG_From_meth(std::basic_string)(const std::string& s) + SWIG_From(std::basic_string)(const std::string& s) { return SWIG_FromCharArray(s.data(), s.size()); } +SWIGSTATICINLINE(PyObject*) + SWIG_From(std::string)(const std::string& s) + { + return SWIG_From(std::basic_string)(s); + } } + +%fragment(SWIG_AsVal_frag(std::string),"header", + fragment=SWIG_AsPtr_frag(std::basic_string)) { +SWIGSTATICINLINE(int) + SWIG_AsVal(std::string)(PyObject* obj, std::string *val) + { + std::string* s; + int res = SWIG_AsPtr(std::basic_string)(obj, &s); + if (res && 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; + } +} + + %typemap_asptrfromn(SWIG_CCode(STRING), std::basic_string); - +%typemap_asptrfromn(SWIG_CCode(STRING), std::string); diff --git a/Lib/python/typemaps.i b/Lib/python/typemaps.i index 4f27b6cd2..d86cb7867 100644 --- a/Lib/python/typemaps.i +++ b/Lib/python/typemaps.i @@ -176,9 +176,9 @@ phased out in future releases. %define %typemap_inoutn(Code,...) %typemap_inout(SWIG_arg(Code), - SWIG_arg(SWIG_As_meth(__VA_ARGS__)), - SWIG_arg(SWIG_Check_meth(__VA_ARGS__)), - SWIG_arg(SWIG_From_meth(__VA_ARGS__)), + SWIG_arg(SWIG_As(__VA_ARGS__)), + SWIG_arg(SWIG_Check(__VA_ARGS__)), + SWIG_arg(SWIG_From(__VA_ARGS__)), SWIG_arg(SWIG_As_frag(__VA_ARGS__)), SWIG_arg(SWIG_Check_frag(__VA_ARGS__)), SWIG_arg(SWIG_From_frag(__VA_ARGS__)),