pyclasses.swg 3.4 KB

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