1 #ifdef __cplusplus 2 3 /* 4 SwigPtr_PyObject is used as a replacement of PyObject *, where 5 the INCREF/DECREF are applied as needed. 6 7 You can use SwigPtr_PyObject in a container, such as 8 9 std::vector<SwigPtr_PyObject>; 10 11 or as a member variable: 12 13 struct A { 14 SwigPtr_PyObject obj; 15 A(PyObject *o) : _obj(o) { 16 } 17 }; 18 19 or as a input/output value 20 21 SwigPtr_PyObject func(SwigPtr_PyObject obj) { 22 SwigPtr_PyObject out = PyString_FromFormat("hello %s", PyObject_AsString(obj)); 23 Py_DECREF(out); 24 return out; 25 } 26 27 just remember to pair the object creation with the proper DECREF, 28 the same as with plain PyObject *ptr, since SwigPtr_PyObject always add 29 one reference at construction. 30 31 SwigPtr_PyObject is 'visible' at the wrapped side, so you can do: 32 33 34 %template(pyvector) std::vector<swig::SwigPtr_PyObject>; 35 36 and all the proper typemaps will be used. 37 38 */ 39 40 namespace swig { 41 %ignore SwigPtr_PyObject; 42 struct SwigPtr_PyObject {}; 43 %apply PyObject * {SwigPtr_PyObject}; 44 %apply PyObject * const& {SwigPtr_PyObject const&}; 45 46 %typemap(typecheck,precedence=SWIG_TYPECHECK_SWIGOBJECT,noblock=1) SwigPtr_PyObject const& "$1 = ($input != 0);"; 47 48 49 /* For output */ 50 %typemap(out,noblock=1) SwigPtr_PyObject { 51 $result = (PyObject *)$1; 52 Py_INCREF($result); 53 } 54 55 %typemap(out,noblock=1) SwigPtr_PyObject const & { 56 $result = (PyObject *)*$1; 57 Py_INCREF($result); 58 } 59 60 } 61 62 %{ 63 namespace swig { 64 class SwigPtr_PyObject { 65 protected: 66 PyObject *_obj; 67 68 public: 69 SwigPtr_PyObject() :_obj(0) 70 { 71 } 72 73 SwigPtr_PyObject(const SwigPtr_PyObject& item) : _obj(item._obj) 74 { 75 Py_XINCREF(_obj); 76 } 77 78 SwigPtr_PyObject(PyObject *obj, bool initial_ref = true) :_obj(obj) 79 { 80 if (initial_ref) { 81 Py_XINCREF(_obj); 82 } 83 } 84 85 SwigPtr_PyObject & operator=(const SwigPtr_PyObject& item) 86 { 87 Py_XINCREF(item._obj); 88 Py_XDECREF(_obj); 89 _obj = item._obj; 90 return *this; 91 } 92 93 ~SwigPtr_PyObject() 94 { 95 Py_XDECREF(_obj); 96 } 97 98 operator PyObject *() const 99 { 100 return _obj; 101 } 102 103 PyObject *operator->() const 104 { 105 return _obj; 106 } 107 }; 108 } 109 %} 110 111 /* 112 SwigVar_PyObject is used to manage 'in the scope' PyObject * variables, 113 as in 114 115 int func () { 116 SwigVar_PyObject obj = PyString_FromString("hello"); 117 } 118 119 ie, 'obj' is created and destructed in the same scope from 120 a python object that carries at least one reference value. 121 122 SwigVar_PyObject just take care of applying the proper Py_DECREF. 123 124 Hence, this class is purely internal and not visible at the wrapped side. 125 */ 126 namespace swig { 127 %ignore SwigVar_PyObject; 128 struct SwigVar_PyObject {}; 129 %apply PyObject * {SwigVar_PyObject}; 130 %apply PyObject * const& {SwigVar_PyObject const&}; 131 } 132 133 %{ 134 namespace swig { 135 struct SwigVar_PyObject : SwigPtr_PyObject { 136 SwigVar_PyObject(PyObject* obj = 0) : SwigPtr_PyObject(obj, false) { } 137 138 SwigVar_PyObject & operator = (PyObject* obj) 139 { 140 Py_XDECREF(_obj); 141 _obj = obj; 142 return *this; 143 } 144 }; 145 } 146 %} 147 148 149 #endif 150