OLD | NEW |
(Empty) | |
| 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 /* For output */ |
| 47 %typemap(out,noblock=1) SwigPtr_PyObject { |
| 48 $result = (PyObject *)$1; |
| 49 Py_INCREF($result); |
| 50 } |
| 51 |
| 52 %typemap(out,noblock=1) SwigPtr_PyObject const & { |
| 53 $result = (PyObject *)*$1; |
| 54 Py_INCREF($result); |
| 55 } |
| 56 |
| 57 } |
| 58 |
| 59 %{ |
| 60 namespace swig { |
| 61 class SwigPtr_PyObject { |
| 62 protected: |
| 63 PyObject *_obj; |
| 64 |
| 65 public: |
| 66 SwigPtr_PyObject() :_obj(0) |
| 67 { |
| 68 } |
| 69 |
| 70 SwigPtr_PyObject(const SwigPtr_PyObject& item) : _obj(item._obj) |
| 71 { |
| 72 Py_XINCREF(_obj); |
| 73 } |
| 74 |
| 75 SwigPtr_PyObject(PyObject *obj, bool initial_ref = true) :_obj(obj) |
| 76 { |
| 77 if (initial_ref) { |
| 78 Py_XINCREF(_obj); |
| 79 } |
| 80 } |
| 81 |
| 82 SwigPtr_PyObject & operator=(const SwigPtr_PyObject& item) |
| 83 { |
| 84 Py_XINCREF(item._obj); |
| 85 Py_XDECREF(_obj); |
| 86 _obj = item._obj; |
| 87 return *this; |
| 88 } |
| 89 |
| 90 ~SwigPtr_PyObject() |
| 91 { |
| 92 Py_XDECREF(_obj); |
| 93 } |
| 94 |
| 95 operator PyObject *() const |
| 96 { |
| 97 return _obj; |
| 98 } |
| 99 |
| 100 PyObject *operator->() const |
| 101 { |
| 102 return _obj; |
| 103 } |
| 104 }; |
| 105 } |
| 106 %} |
| 107 |
| 108 /* |
| 109 SwigVar_PyObject is used to manage 'in the scope' PyObject * variables, |
| 110 as in |
| 111 |
| 112 int func () { |
| 113 SwigVar_PyObject obj = PyString_FromString("hello"); |
| 114 } |
| 115 |
| 116 ie, 'obj' is created and destructed in the same scope from |
| 117 a python object that carries at least one reference value. |
| 118 |
| 119 SwigVar_PyObject just take care of applying the proper Py_DECREF. |
| 120 |
| 121 Hence, this class is purely internal and not visible at the wrapped side. |
| 122 */ |
| 123 namespace swig { |
| 124 %ignore SwigVar_PyObject; |
| 125 struct SwigVar_PyObject {}; |
| 126 %apply PyObject * {SwigVar_PyObject}; |
| 127 %apply PyObject * const& {SwigVar_PyObject const&}; |
| 128 } |
| 129 |
| 130 %{ |
| 131 namespace swig { |
| 132 struct SwigVar_PyObject : SwigPtr_PyObject { |
| 133 SwigVar_PyObject(PyObject* obj = 0) : SwigPtr_PyObject(obj, false) { } |
| 134 |
| 135 SwigVar_PyObject & operator = (PyObject* obj) |
| 136 { |
| 137 Py_XDECREF(_obj); |
| 138 _obj = obj; |
| 139 return *this; |
| 140 } |
| 141 }; |
| 142 } |
| 143 %} |
| 144 |
| 145 |
| 146 #endif |
OLD | NEW |