| 1 |
#ifndef PYPTR_H |
| 2 |
#define PYPTR_H |
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <Python.h> |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
template <class O = PyObject> |
| 14 |
class PyPtr { |
| 15 |
public: |
| 16 |
PyPtr(O* o) : _obj(o) {} |
| 17 |
|
| 18 |
PyPtr(const PyPtr& ptr) : _obj(ptr._obj) { Py_IncRef((PyObject*)_obj); } |
| 19 |
|
| 20 |
|
| 21 |
explicit operator O*() { return _obj; } |
| 22 |
|
| 23 |
PyPtr& operator=(const PyPtr& ptr) { |
| 24 |
if (this != &ptr) { |
| 25 |
Py_IncRef((PyObject*)ptr._obj); |
| 26 |
Py_DecRef((PyObject*)_obj); |
| 27 |
_obj = ptr._obj; |
| 28 |
} |
| 29 |
return *this; |
| 30 |
} |
| 31 |
|
| 32 |
~PyPtr() { Py_DecRef((PyObject*)_obj); } |
| 33 |
|
| 34 |
private: |
| 35 |
O* _obj; |
| 36 |
}; |
| 37 |
|
| 38 |
#endif |