resonance 1.0.1
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1#pragma once
2
3#include "os/hxstr.h"
4#include "script/cxx/config.h"
8
10namespace Py {
11
12// Py::String derives from Py::SeqBase<Py::Char>, which derives from this class, so the two
13// headers cannot include each other. str() returns a String by value and is defined out of line
14// in src/script/cxx/object.cpp, where the complete type is available. Py::Type and type() stand
15// in the same relation.
16class String;
17class Type;
18
42class Object {
43public:
52 explicit Object(PyObject *pyob = Py_None) : mPtr(pyob) {
53 Py_XINCREF(mPtr);
54 validate();
55 }
56
67 Object(const Object &other) : mPtr(other.mPtr) {
68 Py_XINCREF(mPtr);
69 validate();
70 }
71
78 virtual ~Object() {
79 release();
80 }
81
88 Object &operator=(const Object &other) {
89 set(other.mPtr);
90 return *this;
91 }
92
104 virtual bool accepts(PyObject *pyob) const {
105 return pyob != nullptr;
106 }
107
119 Object getAttr(const HxStr &name) const;
120
128 String str() const;
129
138
146 Type type() const;
147
156 bool isType(const Type &type) const;
157
166 bool isTuple() const {
167 return PyTuple_Check(mPtr);
168 }
169
177 bool isList() const {
178 return PyList_Check(mPtr);
179 }
180
191 PyObject *mPtr;
192
193protected:
200 void release() {
201 Py_XDECREF(mPtr);
202 mPtr = nullptr;
203 }
204
215 void set(PyObject *pyob) {
216 Py_XDECREF(mPtr);
217 mPtr = pyob;
218 Py_XINCREF(mPtr);
219 validate();
220 }
221
237 void validate() {
238 if (accepts(mPtr)) {
239 return;
240 }
241 release();
242 if (PyErr_Occurred() != nullptr) {
243 throw Exception();
244 }
245 throw TypeError(HxStr("CXX: type error."));
246 }
247};
248
257inline PyObject *new_reference_to(const Object &object) {
258 PyObject *pyob = object.mPtr;
259 Py_XINCREF(pyob);
260 return pyob;
261}
262
263} // namespace Py
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
Root of the PyCXX exception hierarchy.
Definition exception.h:19
Reference-counted handle on any Python object, and the root of the PyCXX object hierarchy.
Definition object.h:42
String str() const
Produce the text of str() applied to the object.
Object getAttr(const HxStr &name) const
Read one attribute.
Type type() const
Produce the object's type.
HxStr as_string() const
Produce the text of str() applied to the object, as a game string.
virtual ~Object()
Release the reference.
Definition object.h:78
bool isList() const
Report whether the object is a list.
Definition object.h:177
void set(PyObject *pyob)
Replace the reference with a borrowed one and add a count of its own.
Definition object.h:215
Object(PyObject *pyob=Py_None)
Take a borrowed reference and add a count of its own.
Definition object.h:52
bool isType(const Type &type) const
Report whether the object's type is a given type.
bool isTuple() const
Report whether the object is a tuple.
Definition object.h:166
Object(const Object &other)
Copy another handle and add a count of its own.
Definition object.h:67
void release()
Release the reference and empty the handle.
Definition object.h:200
void validate()
Enforce this type's accepts() test on the stored reference.
Definition object.h:237
PyObject * mPtr
Wrapped reference, null only between release() and the next assignment.
Definition object.h:191
Object & operator=(const Object &other)
Replace the reference with another handle's.
Definition object.h:88
virtual bool accepts(PyObject *pyob) const
Whether a reference is acceptable for this handle's type.
Definition object.h:104
Handle on a Python string.
Definition string.h:34
Exception for a value of the wrong Python type, mirroring Python's TypeError.
Definition typeerror.h:21
Handle on a Python type object.
Definition type.h:22
Configuration header of the PyCXX binding layer.
C++ wrappers for the embedded Python interpreter's objects, types, and extension modules.
Definition attributeerror.h:7
PyObject * new_reference_to(const Object &object)
Produce an owned reference from a handle, for returning to the interpreter.
Definition object.h:257