-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.h
139 lines (106 loc) · 3.02 KB
/
Object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
#include "./PythonCore.h"
#include <concepts>
#include <iostream>
#include <stdio.h>
namespace xpo {
namespace python {
// this struct is a wrapper over a PyObject*
struct Object {
Object(PyObject* pyObject) noexcept
: m_pyObject(pyObject)
{
}
Object(Object const& pyObject) noexcept
: Object(pyObject.m_pyObject)
{}
Object(Object && pyObject) noexcept
: Object(pyObject.m_pyObject)
{}
Object const& incref() const {
Py_INCREF(m_pyObject);
return *this;
}
Object const& decref() const {
Py_DECREF(m_pyObject);
return *this;
}
PyObject* ptr() const {
return m_pyObject;
}
bool is_null() const {
return m_pyObject == nullptr;
}
bool is_none() const {
return m_pyObject == Py_None;
}
template <std::convertible_to<Object> T>
T as() const {
return static_cast<T>(*this);
}
template <class T>
T to_native() const {
return static_cast<T>(AutoObject<T>(*this));
}
bool has_attribute(Object name) const {
return PyObject_HasAttr(m_pyObject, name.m_pyObject);
}
bool has_attribute(char const* name) const {
return PyObject_HasAttrString(m_pyObject, name);
}
Object get_attribute(Object name) const {
return Object(PyObject_GetAttr(m_pyObject, name.m_pyObject));
}
Object get_attribute(char const* name) const {
return Object(PyObject_GetAttrString(m_pyObject, name));
}
bool set_attribute(Object name, Object value) const {
return !PyObject_SetAttr(m_pyObject, name, value);
}
bool set_attribute(char const* name, Object value) const {
return !PyObject_SetAttrString(m_pyObject, name, value);
}
bool delete_attribute(Object name) const {
return !PyObject_DelAttr(m_pyObject, name);
}
bool delete_attribute(char const* name) const {
return !PyObject_DelAttrString(m_pyObject, name);
}
Object repr() {
return Object(PyObject_Repr(m_pyObject));
}
Object str() {
return Object(PyObject_Str(m_pyObject));
}
int operator<(Object const& other) {
return PyObject_RichCompareBool(m_pyObject, other.m_pyObject, Py_LT);
}
int operator>(Object const& other) {
return PyObject_RichCompareBool(m_pyObject, other.m_pyObject, Py_GT);
}
int operator<=(Object const& other) {
return PyObject_RichCompareBool(m_pyObject, other.m_pyObject, Py_LE);
}
int operator>=(Object const& other) {
return PyObject_RichCompareBool(m_pyObject, other.m_pyObject, Py_GE);
}
int operator==(Object const& other) {
return PyObject_RichCompareBool(m_pyObject, other.m_pyObject, Py_EQ);
}
int operator!=(Object const& other) {
return PyObject_RichCompareBool(m_pyObject, other.m_pyObject, Py_NE);
}
friend std::ostream& operator<<(std::ostream& os, Object const& pyObject) {
return os << PyUnicode_AsUTF8(PyObject_Repr(pyObject));
}
operator PyObject* () const {
return m_pyObject;
}
static Object none() {
return Object(Py_None);
}
protected:
PyObject* const m_pyObject;
};
}
}