resonance 1.0.1
Loading...
Searching...
No Matches
methodtable.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include "os/hxstr.h"
6#include "script/cxx/config.h"
8
9namespace Py {
10
24inline PyMethodDef method(const char *pszMethodName,
25 PyCFunction pfnMethod,
26 int nFlags = 1,
27 const char *pszDoc = nullptr) {
28 PyMethodDef record;
29 record.ml_name = const_cast<char *>(pszMethodName);
30 record.ml_meth = pfnMethod;
31 record.ml_flags = nFlags;
32 record.ml_doc = const_cast<char *>(pszDoc);
33 return record;
34}
35
55public:
63 mRecords.push_back(method(nullptr, nullptr, 0, nullptr));
64 mTable = nullptr;
65 }
66
73 virtual ~MethodTable() {
74 delete[] mTable;
75 }
76
90 void
91 add(const char *pszMethodName, PyCFunction pfnMethod, const char *pszDoc = "", int nFlags = 1) {
92 if (mTable != nullptr) {
93 throw RuntimeError(HxStr("Too late to add a module method!"));
94 }
95 mRecords.insert(mRecords.end() - 1, method(pszMethodName, pfnMethod, nFlags, pszDoc));
96 }
97
108 PyMethodDef *table() {
109 if (mTable == nullptr) {
110 int nCount = static_cast<int>(mRecords.size());
111 mTable = new PyMethodDef[nCount];
112 for (int i = 0; i < nCount; ++i) {
113 mTable[i] = mRecords[i];
114 }
115 }
116 return mTable;
117 }
118
119private:
120 std::vector<PyMethodDef> mRecords; // +0x00
121 PyMethodDef *mTable; // +0x0c
122};
123
124} // namespace Py
Heap-allocated NUL-terminated string.
Definition hxstr.h:41
PyMethodDef * table()
Flatten the records into an array the interpreter can retain.
Definition methodtable.h:108
MethodTable()
Start with the terminator record alone.
Definition methodtable.h:62
void add(const char *pszMethodName, PyCFunction pfnMethod, const char *pszDoc="", int nFlags=1)
Append one method ahead of the terminator.
Definition methodtable.h:91
virtual ~MethodTable()
Release the flattened table.
Definition methodtable.h:73
Exception for a binding rule broken at run time, mirroring Python's RuntimeError.
Definition runtimeerror.h:20
Configuration header of the PyCXX binding layer.
C++ wrappers for the embedded Python interpreter's objects, types, and extension modules.
Definition attributeerror.h:7
PyMethodDef method(const char *pszMethodName, PyCFunction pfnMethod, int nFlags=1, const char *pszDoc=nullptr)
Build a method record for MethodTable::add().
Definition methodtable.h:24