resonance 1.0.1
Loading...
Searching...
No Matches
pythonextension.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4
5#include "os/hxstr.h"
7#include "script/cxx/config.h"
9#include "script/cxx/list.h"
11#include "script/cxx/object.h"
14#include "script/cxx/string.h"
15#include "script/cxx/tuple.h"
16
17namespace Py {
18
34template <typename T>
36public:
42 static PyTypeObject *type_object() {
43 return behaviors().type_object();
44 }
45
52 virtual ~PythonExtension() {
53 }
54
67 virtual Object getattr_methods(const char *pszName) {
68 const HxStr name(pszName);
69 method_map_t &methodMap = methods();
70 if (name == "__methods__") {
71 List names;
72 for (auto it = methodMap.begin(); it != methodMap.end(); ++it) {
73 names.append(String(it->first));
74 }
75 return names;
76 }
77 if (methodMap.find(name) == methodMap.end()) {
78 throw AttributeError(name);
79 }
80 Tuple self(2);
81 self[0] = Object(this);
82 self[1] = String(name);
83 MethodDefExt<T> *pDefinition = methodMap[name];
84 return Object(FromAPI(PyCFunction_New(&pDefinition->mExtMethodDef, self.mPtr)).mPtr);
85 }
86
87protected:
92 ob_type = type_object();
93 ob_refcnt = 1;
94 }
95
104 static PythonType *pType = nullptr;
105 if (pType == nullptr) {
106 pType = new PythonType(sizeof(T), 0);
107 pType->dealloc(extension_object_deallocator);
108 }
109 return *pType;
110 }
111
112private:
113 typedef std::map<HxStr, MethodDefExt<T> *> method_map_t;
114
115 // NTSC-U/C: 0x005aab58, PAL: 0x005ed080
116 static method_map_t &methods() {
117 static method_map_t *pMethods = nullptr;
118 if (pMethods == nullptr) {
119 pMethods = new method_map_t;
120 }
121 return *pMethods;
122 }
123
124 // NTSC-U/C: 0x005ae638, PAL: 0x005f0ba0
125 static void extension_object_deallocator(PyObject *pyob) {
126 delete static_cast<T *>(pyob);
127 }
128};
129
130} // namespace Py
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
Exception for a missing attribute, mirroring Python's AttributeError.
Definition attributeerror.h:16
Temporary owner of a new reference returned by the CPython API.
Definition fromapi.h:25
PyObject * mPtr
Adopted reference, null when the API call failed.
Definition fromapi.h:55
Handle on a Python list.
Definition list.h:21
void append(const Object &ob)
Append one element.
Definition list.h:85
Method record of an extension type, keyed by name in PythonExtension::methods().
Definition methoddefext.h:20
PyMethodDef mExtMethodDef
The record handed to the interpreter.
Definition methoddefext.h:23
Reference-counted handle on any Python object, and the root of the PyCXX object hierarchy.
Definition object.h:42
PyObject * mPtr
Wrapped reference, null only between release() and the next assignment.
Definition object.h:191
Base of every C++ object the binding exposes to Python as an extension object.
Definition pythonextensionbase.h:33
static PyTypeObject * type_object()
Read the type object every instance of T shares.
Definition pythonextension.h:42
static PythonType & behaviors()
Build T's type object on first use.
Definition pythonextension.h:103
virtual Object getattr_methods(const char *pszName)
Resolve a method by name, or list the method names for __methods__.
Definition pythonextension.h:67
virtual ~PythonExtension()
Destroy the object.
Definition pythonextension.h:52
PythonExtension()
Start with one reference, typed as T.
Definition pythonextension.h:91
Builder for the CPython type object of an extension type.
Definition pythontype.h:20
PyTypeObject * type_object() const
Read the type object.
Definition pythontype.h:47
void dealloc(destructor pfnDealloc)
Set the deallocator.
Definition pythontype.h:58
Handle on a Python string.
Definition string.h:34
Handle on a Python tuple.
Definition tuple.h:31
Configuration header of the PyCXX binding layer.
C++ wrappers for the embedded Python interpreter's objects, types, and extension modules.
Definition attributeerror.h:7