forked from Framework-R-D/phlex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigwrap.cpp
More file actions
220 lines (200 loc) · 9.23 KB
/
configwrap.cpp
File metadata and controls
220 lines (200 loc) · 9.23 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <cstdint>
#include <string>
#include "phlex/configuration.hpp"
#include "wrap.hpp"
using namespace phlex::experimental;
// Create a dict-like access to the configuration from Python.
// clang-format off
struct phlex::experimental::py_config_map {
PyObject_HEAD
phlex::configuration const* ph_config;
PyObject* ph_config_cache;
};
// clang-format on
PyObject* phlex::experimental::wrap_configuration(configuration const& config)
{
py_config_map* pyconfig =
(py_config_map*)PhlexConfig_Type.tp_new(&PhlexConfig_Type, nullptr, nullptr);
pyconfig->ph_config = &config;
return (PyObject*)pyconfig;
}
//= CPyCppyy low level view construction/destruction =========================
static py_config_map* pcm_new(PyTypeObject* subtype, PyObject*, PyObject*)
{
py_config_map* pcm = (py_config_map*)subtype->tp_alloc(subtype, 0);
if (!pcm)
return nullptr;
pcm->ph_config_cache = PyDict_New();
return pcm;
}
static void pcm_dealloc(py_config_map* pcm)
{
Py_DECREF(pcm->ph_config_cache);
Py_TYPE(pcm)->tp_free((PyObject*)pcm);
}
static PyObject* pcm_subscript(py_config_map* pycmap, PyObject* pykey)
{
// Retrieve a named configuration setting.
//
// Configuration should have a single in-memory representation, which is why
// the current approach retrieves it from the equivalent C++ object, ie. after
// the JSON input has been parsed, even as there are Python JSON parsers.
//
// pykey: the lookup key to retrieve the configuration value
if (!PyUnicode_Check(pykey)) {
PyErr_SetString(PyExc_TypeError, "__getitem__ expects a string key");
return nullptr;
}
// cached lookup
PyObject* pyvalue = PyDict_GetItem(pycmap->ph_config_cache, pykey);
if (pyvalue) {
Py_INCREF(pyvalue);
return pyvalue;
}
PyErr_Clear();
std::string ckey = PyUnicode_AsUTF8(pykey);
// Note: Python3.14 adds PyLong_FromInt64/PyLong_FromUInt64 to replace the
// long long variants
static_assert(sizeof(long long) >= sizeof(int64_t));
static_assert(sizeof(unsigned long long) >= sizeof(uint64_t));
try {
auto k = pycmap->ph_config->prototype_internal_kind(ckey);
if (k.second /* is array */) {
if (k.first == boost::json::kind::bool_) {
auto const& cvalue = pycmap->ph_config->get<std::vector<bool>>(ckey);
pyvalue = PyTuple_New(cvalue.size());
for (Py_ssize_t i = 0; i < (Py_ssize_t)cvalue.size(); ++i) {
PyObject* item = PyLong_FromLong((long)cvalue[i]);
PyTuple_SetItem(pyvalue, i, item);
}
} else if (k.first == boost::json::kind::int64) {
auto const& cvalue = pycmap->ph_config->get<std::vector<std::int64_t>>(ckey);
pyvalue = PyTuple_New(cvalue.size());
for (Py_ssize_t i = 0; i < (Py_ssize_t)cvalue.size(); ++i) {
// Note Python3.14 is expected to add PyLong_FromInt64
PyObject* item = PyLong_FromLongLong(cvalue[i]);
PyTuple_SetItem(pyvalue, i, item);
}
} else if (k.first == boost::json::kind::uint64) {
auto const& cvalue = pycmap->ph_config->get<std::vector<std::uint64_t>>(ckey);
pyvalue = PyTuple_New(cvalue.size());
for (Py_ssize_t i = 0; i < (Py_ssize_t)cvalue.size(); ++i) {
// Note Python3.14 is expected to add PyLong_FromUInt64
PyObject* item = PyLong_FromUnsignedLongLong(cvalue[i]);
PyTuple_SetItem(pyvalue, i, item);
}
} else if (k.first == boost::json::kind::double_) {
auto const& cvalue = pycmap->ph_config->get<std::vector<double>>(ckey);
pyvalue = PyTuple_New(cvalue.size());
for (Py_ssize_t i = 0; i < (Py_ssize_t)cvalue.size(); ++i) {
PyObject* item = PyFloat_FromDouble(cvalue[i]);
PyTuple_SetItem(pyvalue, i, item);
}
} else if (k.first == boost::json::kind::string) {
auto const& cvalue = pycmap->ph_config->get<std::vector<std::string>>(ckey);
pyvalue = PyTuple_New(cvalue.size());
for (Py_ssize_t i = 0; i < (Py_ssize_t)cvalue.size(); ++i) {
PyObject* item = PyUnicode_FromStringAndSize(cvalue[i].c_str(), cvalue[i].size());
PyTuple_SetItem(pyvalue, i, item);
}
} else if (k.first == boost::json::kind::null) {
// special case: empty array
pyvalue = PyTuple_New(0);
}
} else {
if (k.first == boost::json::kind::bool_) {
auto cvalue = pycmap->ph_config->get<bool>(ckey);
pyvalue = PyBool_FromLong((long)cvalue);
} else if (k.first == boost::json::kind::int64) {
auto cvalue = pycmap->ph_config->get<std::int64_t>(ckey);
// Note Python3.14 is expected to add PyLong_FromInt64
pyvalue = PyLong_FromLongLong(cvalue);
} else if (k.first == boost::json::kind::uint64) {
auto cvalue = pycmap->ph_config->get<std::uint64_t>(ckey);
// Note Python3.14 is expected to add PyLong_FromUInt64
pyvalue = PyLong_FromUnsignedLongLong(cvalue);
} else if (k.first == boost::json::kind::double_) {
auto cvalue = pycmap->ph_config->get<double>(ckey);
pyvalue = PyFloat_FromDouble(cvalue);
} else if (k.first == boost::json::kind::string) {
auto const& cvalue = pycmap->ph_config->get<std::string>(ckey);
pyvalue = PyUnicode_FromStringAndSize(cvalue.c_str(), cvalue.size());
}
}
} catch (std::runtime_error const&) {
PyErr_Format(PyExc_TypeError, "property \"%s\" does not exist", ckey.c_str());
}
// cache if found
if (pyvalue) {
PyDict_SetItem(pycmap->ph_config_cache, pykey, pyvalue);
}
return pyvalue;
}
static PyMappingMethods pcm_as_mapping = {nullptr, (binaryfunc)pcm_subscript, nullptr};
// clang-format off
PyTypeObject phlex::experimental::PhlexConfig_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
(char*) "pyphlex.configuration", // tp_name
sizeof(py_config_map), // tp_basicsize
0, // tp_itemsize
(destructor)pcm_dealloc, // tp_dealloc
0, // tp_vectorcall_offset / tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_as_async / tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
&pcm_as_mapping, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
(char*)"phlex configuration object-as-dictionary", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
0, // tp_methods
0, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
offsetof(py_config_map, ph_config_cache), // tp_dictoffset
0, // tp_init
0, // tp_alloc
(newfunc)pcm_new, // tp_new
0, // tp_free
0, // tp_is_gc
0, // tp_bases
0, // tp_mro
0, // tp_cache
0, // tp_subclasses
0 // tp_weaklist
#if PY_VERSION_HEX >= 0x02030000
, 0 // tp_del
#endif
#if PY_VERSION_HEX >= 0x02060000
, 0 // tp_version_tag
#endif
#if PY_VERSION_HEX >= 0x03040000
, 0 // tp_finalize
#endif
#if PY_VERSION_HEX >= 0x03080000
, 0 // tp_vectorcall
#endif
#if PY_VERSION_HEX >= 0x030c0000
, 0 // tp_watched
#endif
#if PY_VERSION_HEX >= 0x030d0000
, 0 // tp_versions_used
#endif
};
// clang-format on