Repository navigation
Expand file tree
/
Copy pathfixeddict.cpp
More file actions
386 lines (317 loc) · 12 KB
/
Copy pathfixeddict.cpp
File metadata and controls
386 lines (317 loc) · 12 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fixeddict.h"
#include "datatypes.h"
#include "pyscript/py_gc.h"
namespace KBEngine{
/** python map操作所需要的方法表 */
PyMappingMethods FixedDict::mappingMethods =
{
(lenfunc)FixedDict::mp_length, // mp_length
(binaryfunc)FixedDict::mp_subscript, // mp_subscript
(objobjargproc)FixedDict::mp_ass_subscript // mp_ass_subscript
};
// 参考 objects/dictobject.c
// Hack to implement "key in dict"
PySequenceMethods FixedDict::mappingSequenceMethods =
{
0, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
0, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
PyMapping_HasKey, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};
SCRIPT_METHOD_DECLARE_BEGIN(FixedDict)
SCRIPT_METHOD_DECLARE("__reduce_ex__", reduce_ex__, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("has_key", has_key, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("keys", keys, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("values", values, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("items", items, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE_END()
SCRIPT_MEMBER_DECLARE_BEGIN(FixedDict)
SCRIPT_MEMBER_DECLARE_END()
SCRIPT_GETSET_DECLARE_BEGIN(FixedDict)
SCRIPT_GETSET_DECLARE_END()
SCRIPT_INIT(FixedDict, 0, &FixedDict::mappingSequenceMethods, &FixedDict::mappingMethods, 0, 0)
//-------------------------------------------------------------------------------------
FixedDict::FixedDict(DataType* dataType, std::string& strDictInitData):
Map(getScriptType(), false)
{
_dataType = static_cast<FixedDictType*>(dataType);
_dataType->incRef();
initialize(strDictInitData);
script::PyGC::incTracing("FixedDict");
// DEBUG_MSG(fmt::format("FixedDict::FixedDict(1): {:p}---{}\n", (void*)this,
// wchar2char(PyUnicode_AsWideCharString(PyObject_Str(getDictObject()), NULL))));
}
//-------------------------------------------------------------------------------------
FixedDict::FixedDict(DataType* dataType, PyObject* pyDictInitData):
Map(getScriptType(), false)
{
_dataType = static_cast<FixedDictType*>(dataType);
_dataType->incRef();
initialize(pyDictInitData);
script::PyGC::incTracing("FixedDict");
// DEBUG_MSG(fmt::format("FixedDict::FixedDict(2): {:p}---{}\n", (void*)this,
// wchar2char(PyUnicode_AsWideCharString(PyObject_Str(getDictObject()), NULL))));
}
//-------------------------------------------------------------------------------------
FixedDict::FixedDict(DataType* dataType, MemoryStream* streamInitData, bool isPersistentsStream):
Map(getScriptType(), false)
{
_dataType = static_cast<FixedDictType*>(dataType);
_dataType->incRef();
initialize(streamInitData, isPersistentsStream);
script::PyGC::incTracing("FixedDict");
// DEBUG_MSG(fmt::format("FixedDict::FixedDict(3): {:p}---{}\n", (void*)this,
// wchar2char(PyUnicode_AsWideCharString(PyObject_Str(getDictObject()), NULL))));
}
//-------------------------------------------------------------------------------------
FixedDict::FixedDict(DataType* dataType):
Map(getScriptType(), false)
{
_dataType = static_cast<FixedDictType*>(dataType);
_dataType->incRef();
initialize("");
script::PyGC::incTracing("FixedDict");
// DEBUG_MSG(fmt::format("FixedDict::FixedDict(4): {:p}---{}\n", (void*)this,
// wchar2char(PyUnicode_AsWideCharString(PyObject_Str(getDictObject()), NULL))));
}
//-------------------------------------------------------------------------------------
FixedDict::~FixedDict()
{
_dataType->decRef();
script::PyGC::decTracing("FixedDict");
// DEBUG_MSG(fmt::format("FixedDict::~FixedDict(): {:p}\n", (void*)this);
}
//-------------------------------------------------------------------------------------
void FixedDict::initialize(std::string strDictInitData)
{
FixedDictType::FIXEDDICT_KEYTYPE_MAP& keyTypes = _dataType->getKeyTypes();
FixedDictType::FIXEDDICT_KEYTYPE_MAP::iterator iter = keyTypes.begin();
for(; iter != keyTypes.end(); ++iter)
{
PyObject* pyobj = iter->second->dataType->parseDefaultStr("");
if(pyobj)
{
PyObject* pykey = PyUnicode_FromString(iter->first.c_str());
PyDict_SetItem(pyDict_, pykey, pyobj);
Py_DECREF(pykey);
Py_DECREF(pyobj);
}
else
{
ERROR_MSG(fmt::format("FixedDict::initialize: error! strDictInitData={}.\n",
strDictInitData.c_str()));
}
}
}
//-------------------------------------------------------------------------------------
void FixedDict::initialize(PyObject* pyDictInitData)
{
if(pyDictInitData)
{
update(pyDictInitData);
}
}
//-------------------------------------------------------------------------------------
void FixedDict::initialize(MemoryStream* streamInitData, bool isPersistentsStream)
{
FixedDictType::FIXEDDICT_KEYTYPE_MAP& keyTypes = _dataType->getKeyTypes();
FixedDictType::FIXEDDICT_KEYTYPE_MAP::const_iterator iter = keyTypes.begin();
for(; iter != keyTypes.end(); ++iter)
{
if(isPersistentsStream && !iter->second->persistent)
{
PyObject* val1 = iter->second->dataType->parseDefaultStr("");
PyDict_SetItemString(pyDict_, iter->first.c_str(), val1);
// 由于PyDict_SetItem会增加引用因此需要减
Py_DECREF(val1);
}
else
{
PyObject* val1 = NULL;
if(iter->second->dataType->type() == DATA_TYPE_FIXEDDICT)
val1 = ((FixedDictType*)iter->second->dataType)->createFromStreamEx(streamInitData, isPersistentsStream);
else if(iter->second->dataType->type() == DATA_TYPE_FIXEDARRAY)
val1 = ((FixedArrayType*)iter->second->dataType)->createFromStreamEx(streamInitData, isPersistentsStream);
else
val1 = iter->second->dataType->createFromStream(streamInitData);
PyDict_SetItemString(pyDict_, iter->first.c_str(), val1);
// 由于PyDict_SetItem会增加引用因此需要减
Py_DECREF(val1);
}
}
}
//-------------------------------------------------------------------------------------
PyObject* FixedDict::__py_reduce_ex__(PyObject* self, PyObject* protocol)
{
FixedDict* fixedDict = static_cast<FixedDict*>(self);
PyObject* args = PyTuple_New(2);
PyObject* unpickleMethod = script::Pickler::getUnpickleFunc("FixedDict");
PyTuple_SET_ITEM(args, 0, unpickleMethod);
PyObject* args1 = PyTuple_New(2);
PyTuple_SET_ITEM(args1, 0, PyLong_FromLongLong(fixedDict->getDataType()->id()));
PyTuple_SET_ITEM(args1, 1, PyDict_Copy(fixedDict->getDictObject()));
PyTuple_SET_ITEM(args, 1, args1);
if(unpickleMethod == NULL){
Py_DECREF(args);
return NULL;
}
return args;
}
//-------------------------------------------------------------------------------------
PyObject* FixedDict::__unpickle__(PyObject* self, PyObject* args)
{
Py_ssize_t size = PyTuple_Size(args);
if(size != 2)
{
ERROR_MSG("FixedDict::__unpickle__: args is wrong! (size != 2)");
S_Return;
}
PyObject* pyDatatypeUID = PyTuple_GET_ITEM(args, 0);
DATATYPE_UID uid = (DATATYPE_UID)PyLong_AsUnsignedLong(pyDatatypeUID);
PyObject* dict = PyTuple_GET_ITEM(args, 1);
if(dict == NULL)
{
ERROR_MSG("FixedDict::__unpickle__: args is wrong!");
S_Return;
}
return new FixedDict(DataTypes::getDataType(uid), dict);
}
//-------------------------------------------------------------------------------------
void FixedDict::onInstallScript(PyObject* mod)
{
static PyMethodDef __unpickle__Method =
{"FixedDict", (PyCFunction)&FixedDict::__unpickle__, METH_VARARGS, 0};
PyObject* pyFunc = PyCFunction_New(&__unpickle__Method, NULL);
script::Pickler::registerUnpickleFunc(pyFunc, "FixedDict");
Py_DECREF(pyFunc);
}
//-------------------------------------------------------------------------------------
int FixedDict::mp_length(PyObject* self)
{
return PyDict_Size(static_cast<FixedDict*>(self)->pyDict_);
}
//-------------------------------------------------------------------------------------
int FixedDict::mp_ass_subscript(PyObject* self, PyObject* key, PyObject* value)
{
wchar_t* PyUnicode_AsWideCharStringRet0 = PyUnicode_AsWideCharString(key, NULL);
char* dictKeyName = strutil::wchar2char(PyUnicode_AsWideCharStringRet0);
PyMem_Free(PyUnicode_AsWideCharStringRet0);
FixedDict* fixedDict = static_cast<FixedDict*>(self);
if (value == NULL)
{
if(!fixedDict->checkDataChanged(dictKeyName, value, true))
{
free(dictKeyName);
return 0;
}
return PyDict_DelItem(fixedDict->pyDict_, key);
}
if(!fixedDict->checkDataChanged(dictKeyName, value))
{
free(dictKeyName);
return 0;
}
PyObject* val1 =
static_cast<FixedDictType*>(fixedDict->getDataType())->createNewItemFromObj(dictKeyName, value);
int ret = PyDict_SetItem(fixedDict->pyDict_, key, val1);
// 由于PyDict_SetItem会增加引用因此需要减
Py_DECREF(val1);
free(dictKeyName);
return ret;
}
//-------------------------------------------------------------------------------------
bool FixedDict::checkDataChanged(const char* keyName, PyObject* value, bool isDelete)
{
FixedDictType::FIXEDDICT_KEYTYPE_MAP& keyTypes = _dataType->getKeyTypes();
FixedDictType::FIXEDDICT_KEYTYPE_MAP::const_iterator iter = keyTypes.begin();
for(; iter != keyTypes.end(); ++iter)
{
if((*iter).first == keyName)
{
if(isDelete)
{
char err[255];
kbe_snprintf(err, 255, "can't delete from FIXED_DICT key[%s].\n", keyName);
PyErr_SetString(PyExc_TypeError, err);
PyErr_PrintEx(0);
return false;
}
else
{
DataType* dataType = (*iter).second->dataType;
if(!dataType->isSameType(value)){
return false;
}
}
return true;
}
}
char err[255];
kbe_snprintf(err, 255, "set FIXED_DICT to a unknown key[%s].\n", keyName);
PyErr_SetString(PyExc_TypeError, err);
PyErr_PrintEx(0);
return false;
}
//-------------------------------------------------------------------------------------
PyObject* FixedDict::mp_subscript(PyObject* self, PyObject* key)
{
FixedDict* fixedDict = static_cast<FixedDict*>(self);
PyObject* pyObj = PyDict_GetItem(fixedDict->pyDict_, key);
if (!pyObj)
PyErr_SetObject(PyExc_KeyError, key);
else
Py_INCREF(pyObj);
return pyObj;
}
//-------------------------------------------------------------------------------------
PyObject* FixedDict::update(PyObject* args)
{
FixedDictType::FIXEDDICT_KEYTYPE_MAP& keyTypes = _dataType->getKeyTypes();
FixedDictType::FIXEDDICT_KEYTYPE_MAP::const_iterator iter = keyTypes.begin();
for(; iter != keyTypes.end(); ++iter)
{
PyObject* val = PyDict_GetItemString(args, iter->first.c_str());
if(val)
{
PyObject* val1 =
static_cast<FixedDictType*>(getDataType())->createNewItemFromObj(iter->first.c_str(), val);
PyDict_SetItemString(pyDict_, iter->first.c_str(), val1);
// 由于PyDict_SetItem会增加引用因此需要减
Py_DECREF(val1);
}
}
S_Return;
}
//-------------------------------------------------------------------------------------
PyObject* FixedDict::tp_str()
{
return tp_repr();
}
//-------------------------------------------------------------------------------------
PyObject* FixedDict::tp_repr()
{
return PyObject_Repr(pyDict_);
}
//-------------------------------------------------------------------------------------
}