-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathRegCDuiString.cpp
More file actions
463 lines (362 loc) · 17.6 KB
/
Copy pathRegCDuiString.cpp
File metadata and controls
463 lines (362 loc) · 17.6 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "StdAfx.h"
#include "RegCDuiString.h"
#include <map> // std::map
namespace DuiLib {
typedef std::map<CDuiString, int> map_t;
class RegCDuiStringFactory : public asIStringFactory
{
public:
RegCDuiStringFactory() {}
~RegCDuiStringFactory()
{
// The script engine must release each string
// constant that it has requested
assert(stringCache.size() == 0);
}
const void *GetStringConstant(const char *data, asUINT length)
{
// The string factory might be modified from multiple
// threads, so it is necessary to use a mutex.
asAcquireExclusiveLock();
#ifdef _UNICODE
CDuiString str((const wchar_t *)data, length/sizeof(wchar_t));
#else
CDuiString str((const char *)data, length);
#endif
map_t::iterator it = stringCache.find(str);
if (it != stringCache.end())
it->second++;
else
it = stringCache.insert(map_t::value_type(str, 1)).first;
asReleaseExclusiveLock();
return reinterpret_cast<const void*>(&it->first);
}
int ReleaseStringConstant(const void *str)
{
if (str == 0)
return asERROR;
int ret = asSUCCESS;
// The string factory might be modified from multiple
// threads, so it is necessary to use a mutex.
asAcquireExclusiveLock();
#ifdef _DEBUG
CDuiString *sTemp = (CDuiString *)str;
#endif
map_t::iterator it = stringCache.find(*reinterpret_cast<const CDuiString*>(str));
if (it == stringCache.end())
ret = asERROR;
else
{
it->second--;
if (it->second == 0)
stringCache.erase(it);
}
asReleaseExclusiveLock();
return ret;
}
int GetRawStringData(const void *str, char *data, asUINT *length) const
{
if (str == 0)
return asERROR;
if (length)
{
*length = (asUINT)reinterpret_cast<const CDuiString*>(str)->GetLength();
#ifdef _UNICODE
*length *= 2;
#endif
}
if (data)
{
#ifdef _DEBUG
CDuiString *sTemp = (CDuiString *)str;
#endif
#ifdef _UNICODE
memcpy(data, reinterpret_cast<const CDuiString*>(str)->GetData(), reinterpret_cast<const CDuiString*>(str)->GetLength()*sizeof(wchar_t));
#else
memcpy(data, reinterpret_cast<const CDuiString*>(str)->GetData(), reinterpret_cast<const CDuiString*>(str)->GetLength());
#endif
}
return asSUCCESS;
}
// THe access to the string cache is protected with the common mutex provided by AngelScript
map_t stringCache;
};
static RegCDuiStringFactory *stringFactory = 0;
// TODO: Make this public so the application can also use the string
// factory and share the string constants if so desired, or to
// monitor the size of the string factory cache.
RegCDuiStringFactory *GetStdStringFactorySingleton()
{
if( stringFactory == 0 )
{
// Make sure no other thread is creating the string factory at the same time
asAcquireExclusiveLock();
if (stringFactory == 0)
{
// The following instance will be destroyed by the global
// CStdStringFactoryCleaner instance upon application shutdown
stringFactory = new RegCDuiStringFactory();
}
asReleaseExclusiveLock();
}
return stringFactory;
}
class RegCDuiStringFactoryCleaner
{
public:
~RegCDuiStringFactoryCleaner()
{
if (stringFactory)
{
// Only delete the string factory if the stringCache is empty
// If it is not empty, it means that someone might still attempt
// to release string constants, so if we delete the string factory
// the application might crash. Not deleting the cache would
// lead to a memory leak, but since this is only happens when the
// application is shutting down anyway, it is not important.
if (stringFactory->stringCache.empty())
{
delete stringFactory;
stringFactory = 0;
}
}
}
};
static RegCDuiStringFactoryCleaner cleaner;
// CDuiString regCDuiString::StringFactory(asUINT length, LPCTSTR s)
// {
// #ifdef _UNICODE
// return CDuiString(s, length/2);
// #else
// return CDuiString(s, length);
// #endif
// }
void regCDuiString::String_Construct_DuiString(CDuiString *thisPointer)
{
new(thisPointer) CDuiString();
}
void regCDuiString::String_CopyConstruct(const CDuiString &other, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(other);
}
void regCDuiString::String_Construct_LPCTSTR(LPCTSTR str, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(str);
}
void regCDuiString::String_Construct_bool(bool val, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(val);
}
void regCDuiString::String_Construct_int(int val, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(val);
}
void regCDuiString::String_Construct_int64(uiInt64 val, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(val);
}
void regCDuiString::String_Construct_double(double val, int base, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(val, base);
}
void regCDuiString::String_Construct_float(float val, int base, CDuiString *thisPointer)
{
new(thisPointer) CDuiString(val, base);
}
void regCDuiString::String_Destruct(CDuiString *thisPointer)
{
thisPointer->~CDuiString();
}
CDuiString ®CDuiString::SetAt(int n, CDuiString ch, CDuiString &dest)
{
if(!ch.IsEmpty())
{
dest.SetAt(n, ch.GetAt(0));
}
return dest;
}
CDuiString regCDuiString::GetAt(int n, CDuiString &dest)
{
return dest.GetAt(n);
}
CDuiString regCDuiString::opAddString(const CDuiString &str, const CDuiString &dest)
{
return str + dest;
}
//////////////////////////////////////////////////////////////////////////
CDuiString regCDuiString::opAddString(bool val, const CDuiString &str)
{
return str + val;
}
//////////////////////////////////////////////////////////////////////////
CDuiString regCDuiString::opAddString(int val, const CDuiString &str)
{
return str + val;
}
//////////////////////////////////////////////////////////////////////////
CDuiString regCDuiString::opAddString(uiInt64 val, const CDuiString &str)
{
return str + val;
}
//////////////////////////////////////////////////////////////////////////
CDuiString ®CDuiString::opAssign(double val, CDuiString &dest)
{
dest = CDuiString(val,2);
return dest;
}
CDuiString ®CDuiString::opAddAssign(double val, CDuiString &dest)
{
dest += CDuiString(val,2);
return dest;
}
CDuiString regCDuiString::opAddString(double val, const CDuiString &str)
{
return str + val;
}
//////////////////////////////////////////////////////////////////////////
CDuiString ®CDuiString::opAssign(float val, CDuiString &dest)
{
dest = CDuiString(val,2);
return dest;
}
CDuiString ®CDuiString::opAddAssign(float val, CDuiString &dest)
{
dest += CDuiString(val,2);
return dest;
}
CDuiString regCDuiString::opAddString(float val, const CDuiString &str)
{
return str + val;
}
LPCTSTR regCDuiString::StringToLPCTSTR1(const CDuiString &b)
{
return b.GetData();
}
LPCTSTR regCDuiString::StringToLPCTSTR2(const CDuiString &b)
{
return b.GetData();
}
LPCTSTR regCDuiString::StringToLPCTSTR3(const CDuiString &b)
{
return b.GetData();
}
LPCTSTR regCDuiString::StringToLPCTSTR4(const CDuiString &b)
{
return b.GetData();
}
bool regCDuiString::Register(asIScriptEngine *engine)
{
int r =0;
//注册字符串类
r = engine->RegisterObjectType("string", sizeof(CDuiString), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert( r >= 0 );
//注册字符串类工厂
//r = engine->RegisterStringFactory("string", asFUNCTION(StringFactory), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterStringFactory("string", GetStdStringFactorySingleton());
//默认构造函数 CDuiString();
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(String_Construct_DuiString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//拷贝构造函数 CDuiString(const CDuiString& src);
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(const string &in)", asFUNCTION(String_CopyConstruct), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//构造函数 CDuiString(const LPCTSTR src);
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(LPCTSTR &in)", asFUNCTION(String_Construct_LPCTSTR), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(String_Construct_bool), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(String_Construct_int), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(int64)", asFUNCTION(String_Construct_int64), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(double, int)", asFUNCTION(String_Construct_double), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(float, int)", asFUNCTION(String_Construct_float), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//析构函数 ~CDuiString();
r = engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(String_Destruct), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
REG_METHOD_FUNPR2("string", CDuiString, void, SetBufferLength, (int iLength)); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, int, GetBufferLength, ()); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, void, Empty, ()); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, int, GetLength, () const); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, bool, IsEmpty, () const); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string &SetAt(int, string)", asFUNCTION(SetAt), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "const string opIndex(int) const", asFUNCTION(GetAt), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string GetAt(int)", asFUNCTION(GetAt), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
//赋值函数 operator =
r = engine->RegisterObjectMethod("string", "string &opAssign(const string &in)", asMETHODPR(CDuiString, Assign, (const CDuiString &), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +=
r = engine->RegisterObjectMethod("string", "string &opAddAssign(const string &in)", asMETHODPR(CDuiString, Append, (const CDuiString &), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +
r = engine->RegisterObjectMethod("string", "string opAdd(const string &in) const", asFUNCTIONPR(opAddString, (const CDuiString &, const CDuiString &), CDuiString), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
//赋值函数 operator =
r = engine->RegisterObjectMethod("string", "string &opAssign(bool)", asMETHODPR(CDuiString, Assign, (bool), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +=
r = engine->RegisterObjectMethod("string", "string &opAddAssign(bool)", asMETHODPR(CDuiString, Append, (bool), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +
r = engine->RegisterObjectMethod("string", "string opAdd(bool) const", asFUNCTIONPR(opAddString, (bool, const CDuiString &), CDuiString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
//赋值函数 operator =
r = engine->RegisterObjectMethod("string", "string &opAssign(int)", asMETHODPR(CDuiString, Assign, (int), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +=
r = engine->RegisterObjectMethod("string", "string &opAddAssign(int)", asMETHODPR(CDuiString, Append, (int), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +
r = engine->RegisterObjectMethod("string", "string opAdd(int) const", asFUNCTIONPR(opAddString, (int, const CDuiString &), CDuiString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
//赋值函数 operator =
r = engine->RegisterObjectMethod("string", "string &opAssign(int64)", asMETHODPR(CDuiString, Assign, (uiInt64), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +=
r = engine->RegisterObjectMethod("string", "string &opAddAssign(int64)", asMETHODPR(CDuiString, Append, (uiInt64), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//字符串连接 operator +
r = engine->RegisterObjectMethod("string", "string opAdd(int64) const", asFUNCTIONPR(opAddString, (uiInt64, const CDuiString &), CDuiString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
//赋值函数 operator =
r = engine->RegisterObjectMethod("string", "string &opAssign(double)", asFUNCTIONPR(opAssign, (double, CDuiString&), CDuiString&), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//字符串连接 operator +=
r = engine->RegisterObjectMethod("string", "string &opAddAssign(double)", asFUNCTIONPR(opAddAssign, (double, CDuiString&), CDuiString&), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//字符串连接 operator +
r = engine->RegisterObjectMethod("string", "string opAdd(double) const", asFUNCTIONPR(opAddString, (double, const CDuiString &), CDuiString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
//赋值函数 operator =
r = engine->RegisterObjectMethod("string", "string &opAssign(float)", asFUNCTIONPR(opAssign, (float, CDuiString&), CDuiString&), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//字符串连接 operator +=
r = engine->RegisterObjectMethod("string", "string &opAddAssign(float)", asFUNCTIONPR(opAddAssign, (float, CDuiString&), CDuiString&), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//字符串连接 operator +
r = engine->RegisterObjectMethod("string", "string opAdd(float) const", asFUNCTIONPR(opAddString, (float, const CDuiString &), CDuiString), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//为了兼容
r = engine->RegisterObjectMethod("string", "void setFloat(float,int)", asMETHODPR(CDuiString, Assign, (float,int), CDuiString&), asCALL_THISCALL); assert( r >= 0 );
//////////////////////////////////////////////////////////////////////////
REG_METHOD_FUNPR2("string", CDuiString, bool, toBool, (bool def) const); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, int, toInt, (int def) const); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int64 toInt64(int64 def) const", asMETHODPR(CDuiString, toInt64, (uiInt64 def) const, uiInt64), asCALL_THISCALL); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, double, toDouble, (double def) const); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, float, toFloat, (float def) const); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int64 HexToInt64()", asMETHOD(CDuiString, HexToInt64), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string toHex()", asMETHOD(CDuiString, toHex), asCALL_THISCALL); assert( r >= 0 );
//字符串是否相等
//bool operator == (LPCTSTR str) const;
//bool operator != (LPCTSTR str) const;
r = engine->RegisterObjectMethod("string", "bool opEquals(string) const", asMETHOD(CDuiString, IsEquals), asCALL_THISCALL); assert( r >= 0 );
//字符串比较大小
// bool operator <= (LPCTSTR str) const;
// bool operator < (LPCTSTR str) const;
// bool operator >= (LPCTSTR str) const;
// bool operator > (LPCTSTR str) const;
r = engine->RegisterObjectMethod("string", "int opCmp(const string &in) const", asMETHOD(CDuiString, Compare), asCALL_THISCALL); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, void, MakeUpper, ());
REG_METHOD_FUNPR2("string", CDuiString, void, MakeLower, ());
REG_METHOD_FUNPR2("string", CDuiString, CDuiString, Left, (int iLength) const); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string Mid(int iPos, int iLength=-1) const", asMETHOD(CDuiString, Mid), asCALL_THISCALL); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, CDuiString, Right, (int iLength) const); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string Section(string, int start, int end=-1) const", asMETHOD(CDuiString, Section), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "string ReverseSection(string, int start, int end=-1) const", asMETHOD(CDuiString, ReverseSection), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int Find(string, int iPos=0) const", asMETHODPR(CDuiString, Find, (LPCTSTR, int) const, int), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "int ReverseFind(string, int iPos=0) const", asMETHODPR(CDuiString, ReverseFind, (LPCTSTR, int) const, int), asCALL_THISCALL); assert( r >= 0 );
REG_METHOD_FUNPR2("string", CDuiString, int, Replace, (LPCTSTR pstrFrom, LPCTSTR pstrTo));
REG_METHOD_FUNPR2("string", CDuiString, CDuiString &, TrimLeft, ());
REG_METHOD_FUNPR2("string", CDuiString, CDuiString &, TrimRight, ());
REG_METHOD_FUNPR2("string", CDuiString, CDuiString &, Trim, ());
REG_METHOD_FUNPR2("string", CDuiString, CDuiString &, Remove, (LPCTSTR pstr));
REG_METHOD_FUNPR2("string", CDuiString, CDuiString &, Delete, (int nIndex, int nCount));
REG_METHOD_FUNPR2("string", CDuiString, CDuiString &, ReverseDelete, (int nIndex, int nCount));
//类型转换 CDuiString转为LPCTSTR
r = engine->RegisterObjectMethod("string", "LPCTSTR opImplConv() const", asFUNCTION(StringToLPCTSTR1), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "LPCTSTR opConv() const", asFUNCTION(StringToLPCTSTR2), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "LPCTSTR opCast() const", asFUNCTION(StringToLPCTSTR3), asCALL_CDECL_OBJLAST); assert( r >= 0 );
r = engine->RegisterObjectMethod("string", "LPCTSTR opImplCast() const", asFUNCTION(StringToLPCTSTR4), asCALL_CDECL_OBJLAST); assert( r >= 0 );
return r >= 0;
}
} //namespace DuiLib {