OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
CPythonObject.h
Go to the documentation of this file.
1/*****************************************************************/
10#pragma once
11#include <Python.h>
12
14public:
15 CPythonObject(PyObject* ref) : _ref{ ref } {}
16 Py_ssize_t getRefCount() const { return _ref != nullptr ? Py_REFCNT(_ref) : 0; }
17
18 CPythonObject& operator=(const CPythonObject& other) = delete;
19 CPythonObject(const CPythonObject& other) = delete;
20
22 {
23 this->_ref = other._ref;
24 other._ref = nullptr;
25 return *this;
26 };
27
28 CPythonObject(CPythonObject&& other) noexcept
29 {
30 this->_ref = other._ref;
31 other._ref = nullptr;
32 }
33
34 void reset(CPythonObject&& other)
35 {
36 if (_ref != nullptr)
37 {
38 Py_XDECREF(_ref);
39 _ref = nullptr;
40 }
41 _ref = other._ref;
42 other._ref = nullptr;
43 }
44
45 // Allow setting of the (optional) argument with PyArg_ParseTupleAndKeywords
46 //PyObject** operator&() {
47 // Py_XDECREF(_ref);
48 // _ref = nullptr;
49 // return &_ref;
50 //}
51
52 // Access the argument
53 operator PyObject* () const { return _ref; }
54 // Test if constructed successfully from the new reference.
55 bool ReferenceIsSet() { return _ref != nullptr; }
56
57 void DropOwnership() { _ref = nullptr; }
58
60 {
61 Py_XDECREF(_ref);
62 _ref = nullptr;
63 };
64
66 {
67 Py_XDECREF(_ref);
68 }
69protected:
70 PyObject* _ref;
71};
Definition CPythonObject.h:13
bool ReferenceIsSet()
Definition CPythonObject.h:55
PyObject * _ref
Definition CPythonObject.h:70
CPythonObject(CPythonObject &&other) noexcept
Definition CPythonObject.h:28
void DropOwnership()
Definition CPythonObject.h:57
CPythonObject & operator=(const CPythonObject &other)=delete
Py_ssize_t getRefCount() const
Definition CPythonObject.h:16
void reset(CPythonObject &&other)
Definition CPythonObject.h:34
CPythonObject(const CPythonObject &other)=delete
CPythonObject & operator=(CPythonObject &&other) noexcept
Definition CPythonObject.h:21
void FreePythonObject()
Definition CPythonObject.h:59
CPythonObject(PyObject *ref)
Definition CPythonObject.h:15
~CPythonObject()
Definition CPythonObject.h:65