-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_enum.cc
More file actions
215 lines (200 loc) · 10 KB
/
Copy pathpy_enum.cc
File metadata and controls
215 lines (200 loc) · 10 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
/* BugEngine <bugengine.devel@gmail.com>
see LICENSE for detail */
#include <bugengine/plugin.scripting.pythonlib/stdafx.h>
#include <bugengine/meta/engine/methodinfo.script.hh>
#include <bugengine/plugin.scripting.pythonlib/pythonlib.hh>
#include <py_enum.hh>
namespace BugEngine { namespace Python {
PyTypeObject::Py2NumberMethods PyBugEnum::s_py2EnumNumber = {{0, 0, 0},
0,
0,
0,
0,
0,
0,
0,
&PyBugEnum::nonZero,
0,
0,
0,
0,
0,
0,
0,
&PyBugEnum::toint,
&PyBugEnum::tolong,
&PyBugEnum::tofloat,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0};
PyTypeObject::Py3NumberMethods PyBugEnum::s_py3EnumNumber = {{0, 0, 0},
0,
0,
0,
0,
0,
0,
&PyBugEnum::nonZero,
0,
0,
0,
0,
0,
0,
&PyBugEnum::tolong,
0,
&PyBugEnum::tofloat,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0};
PyTypeObject PyBugEnum::s_pyType = {{{0, 0}, 0},
"py_bugengine.Enum",
sizeof(PyBugEnum),
0,
PyBugEnum::dealloc,
0,
0,
0,
0,
&PyBugEnum::str,
0,
0,
0,
0,
0,
&PyBugEnum::str,
0,
0,
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT,
"Wrapper class for the C++ BugEngine Enum types",
0,
0,
0,
0,
0,
0,
PyBugObject::s_methods,
0,
0,
&PyBugObject::s_pyType,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0};
PyObject* PyBugEnum::stealValue(PyObject* owner, Meta::Value& value)
{
be_assert(value.type().metaclass->type() == Meta::ClassType_Enum,
"PyBugNumber only accepts Enum types");
PyObject* result = s_pyType.tp_alloc(&s_pyType, 0);
static_cast< PyBugEnum* >(result)->owner = owner;
if(owner)
{
Py_INCREF(owner);
}
new(&(static_cast< PyBugEnum* >(result))->value) Meta::Value();
(static_cast< PyBugEnum* >(result))->value.swap(value);
return result;
}
static istring s_toString = istring("toString");
static istring s_toInt = istring("toInt");
PyObject* PyBugEnum::str(PyObject* self)
{
PyBugEnum* self_ = static_cast< PyBugEnum* >(self);
const Meta::Value& v = self_->value;
raw< const Meta::Method > toString = self_->value[s_toString].as< raw< const Meta::Method > >();
minitl::format< 1024u > format = minitl::format< 1024u >("%s.%s") | v.type().name().c_str()
| toString->doCall(&self_->value, 1).as< const istring >();
if(s_library->getVersion() >= 30)
{
return s_library->m_PyUnicode_FromFormat(format);
}
else
{
return s_library->m_PyString_FromFormat(format);
}
}
PyObject* PyBugEnum::toint(PyObject* self)
{
PyBugObject* self_ = static_cast< PyBugObject* >(self);
raw< const Meta::Method > toInt = self_->value[s_toInt].as< raw< const Meta::Method > >();
long value = (long)toInt->doCall(&self_->value, 1).as< u32 >();
return s_library->m_PyInt_FromLong(value);
}
PyObject* PyBugEnum::tolong(PyObject* self)
{
PyBugObject* self_ = static_cast< PyBugObject* >(self);
raw< const Meta::Method > toInt = self_->value[s_toInt].as< raw< const Meta::Method > >();
unsigned long long value = (unsigned long long)toInt->doCall(&self_->value, 1).as< u32 >();
return s_library->m_PyLong_FromUnsignedLongLong(value);
}
PyObject* PyBugEnum::tofloat(PyObject* self)
{
PyBugObject* self_ = static_cast< PyBugObject* >(self);
raw< const Meta::Method > toInt = self_->value[s_toInt].as< raw< const Meta::Method > >();
double value = (double)toInt->doCall(&self_->value, 1).as< u32 >();
return s_library->m_PyFloat_FromDouble(value);
}
int PyBugEnum::nonZero(PyObject* self)
{
PyBugObject* self_ = static_cast< PyBugObject* >(self);
raw< const Meta::Method > toInt = self_->value[s_toInt].as< raw< const Meta::Method > >();
return toInt->doCall(&self_->value, 1).as< u32 >() != 0;
}
void PyBugEnum::registerType(PyObject* module)
{
if(s_library->getVersion() >= 30)
s_pyType.tp_as_number = &s_py3EnumNumber.nb_common;
else
s_pyType.tp_as_number = &s_py2EnumNumber.nb_common;
s_pyType.tp_alloc = s_library->m_PyType_GenericAlloc;
int result = s_library->m_PyType_Ready(&s_pyType);
be_assert(result >= 0, "unable to register type");
be_forceuse(module);
be_forceuse(result);
Py_INCREF(&s_pyType);
}
}} // namespace BugEngine::Python