Addons.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Addons.h"
  25. #include "StringUtils.h"
  26. #include <cstring>
  27. // Adapted from Angelscript's scriptarray & scriptstdstring add-ons, but with garbage collection disabled
  28. /// Script array buffer
  29. struct SArrayBuffer
  30. {
  31. asDWORD numElements;
  32. asBYTE data[1];
  33. };
  34. static CScriptArray* ScriptArrayFactory2(asIObjectType* ot, asUINT length)
  35. {
  36. CScriptArray* a = new CScriptArray(length, ot);
  37. // It's possible the constructor raised a script exception, in which case we
  38. // need to free the memory and return null instead, else we get a memory leak.
  39. asIScriptContext* context = asGetActiveContext();
  40. if (context && context->GetState() == asEXECUTION_EXCEPTION)
  41. {
  42. delete a;
  43. return 0;
  44. }
  45. return a;
  46. }
  47. static CScriptArray* ScriptArrayFactoryDefVal(asIObjectType* ot, asUINT length, void* defVal)
  48. {
  49. CScriptArray* a = new CScriptArray(length, defVal, ot);
  50. // It's possible the constructor raised a script exception, in which case we
  51. // need to free the memory and return null instead, else we get a memory leak.
  52. asIScriptContext* context = asGetActiveContext();
  53. if (context && context->GetState() == asEXECUTION_EXCEPTION)
  54. {
  55. delete a;
  56. return 0;
  57. }
  58. return a;
  59. }
  60. static CScriptArray* ScriptArrayFactory(asIObjectType* ot)
  61. {
  62. return ScriptArrayFactory2(ot, 0);
  63. }
  64. // This optional callback is called when the template type is first used by the compiler.
  65. // It allows the application to validate if the template can be instanciated for the requested
  66. // subtype at compile time, instead of at runtime.
  67. static bool ScriptArrayTemplateCallback(asIObjectType* ot)
  68. {
  69. // Make sure the subtype can be instanciated with a default factory/constructor,
  70. // otherwise we won't be able to instanciate the elements. Script classes always
  71. // have default factories, so we don't have to worry about those.
  72. int typeId = ot->GetSubTypeId();
  73. if ((typeId & asTYPEID_MASK_OBJECT) && !(typeId & asTYPEID_OBJHANDLE) && !(typeId & asTYPEID_SCRIPTOBJECT))
  74. {
  75. asIObjectType *subtype = ot->GetEngine()->GetObjectTypeById(typeId);
  76. asDWORD flags = subtype->GetFlags();
  77. if ((flags & asOBJ_VALUE) && !(flags & asOBJ_POD))
  78. {
  79. // Verify that there is a default constructor
  80. for (int n = 0; n < subtype->GetBehaviourCount(); ++n)
  81. {
  82. asEBehaviours beh;
  83. int funcId = subtype->GetBehaviourByIndex(n, &beh);
  84. if (beh != asBEHAVE_CONSTRUCT)
  85. continue;
  86. asIScriptFunction* func = ot->GetEngine()->GetFunctionDescriptorById(funcId);
  87. if (func->GetParamCount() == 0)
  88. {
  89. // Found the default constructor
  90. return true;
  91. }
  92. }
  93. // There is no default constructor
  94. return false;
  95. }
  96. else if ((flags & asOBJ_REF))
  97. {
  98. // Verify that there is a default factory
  99. for (int n = 0; n < subtype->GetFactoryCount(); ++n)
  100. {
  101. int funcId = subtype->GetFactoryIdByIndex(n);
  102. asIScriptFunction* func = ot->GetEngine()->GetFunctionDescriptorById(funcId);
  103. if (func->GetParamCount() == 0)
  104. {
  105. // Found the default factory
  106. return true;
  107. }
  108. }
  109. // No default factory
  110. return false;
  111. }
  112. }
  113. // The type is ok
  114. return true;
  115. }
  116. CScriptArray& CScriptArray::operator = (const CScriptArray &other)
  117. {
  118. // Only perform the copy if the array types are the same
  119. if (&other != this && other.GetArrayObjectType() == GetArrayObjectType())
  120. {
  121. if (buffer)
  122. {
  123. DeleteBuffer(buffer);
  124. buffer = 0;
  125. }
  126. // Copy all elements from the other array
  127. CreateBuffer(&buffer, other.buffer->numElements);
  128. CopyBuffer(buffer, other.buffer);
  129. }
  130. return *this;
  131. }
  132. CScriptArray::CScriptArray(asUINT length, asIObjectType* ot)
  133. {
  134. refCount = 1;
  135. gcFlag = false;
  136. objType = ot;
  137. objType->AddRef();
  138. buffer = 0;
  139. // Determine element size
  140. // TODO: Should probably store the template sub type id as well
  141. int typeId = objType->GetSubTypeId();
  142. if (typeId & asTYPEID_MASK_OBJECT)
  143. elementSize = sizeof(asPWORD);
  144. else
  145. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(typeId);
  146. isArrayOfHandles = typeId & asTYPEID_OBJHANDLE ? true : false;
  147. // Make sure the array size isn't too large for us to handle
  148. if (!CheckMaxSize(length))
  149. {
  150. // Don't continue with the initialization
  151. return;
  152. }
  153. CreateBuffer(&buffer, length);
  154. }
  155. CScriptArray::CScriptArray(asUINT length, void *defVal, asIObjectType *ot)
  156. {
  157. refCount = 1;
  158. gcFlag = false;
  159. objType = ot;
  160. objType->AddRef();
  161. buffer = 0;
  162. // Determine element size
  163. // TODO: Should probably store the template sub type id as well
  164. int typeId = objType->GetSubTypeId();
  165. if (typeId & asTYPEID_MASK_OBJECT)
  166. elementSize = sizeof(asPWORD);
  167. else
  168. elementSize = objType->GetEngine()->GetSizeOfPrimitiveType(typeId);
  169. isArrayOfHandles = typeId & asTYPEID_OBJHANDLE ? true : false;
  170. // Make sure the array size isn't too large for us to handle
  171. if (!CheckMaxSize(length))
  172. {
  173. // Don't continue with the initialization
  174. return;
  175. }
  176. CreateBuffer(&buffer, length);
  177. // Initialize the elements with the default value
  178. for (asUINT n = 0; n < GetSize(); ++n)
  179. SetValue(n, defVal);
  180. }
  181. // Internal
  182. void CScriptArray::SetValue(asUINT index, void *value)
  183. {
  184. int typeId = objType->GetSubTypeId();
  185. if ((typeId & ~0x03FFFFFF) && !(typeId & asTYPEID_OBJHANDLE))
  186. objType->GetEngine()->CopyScriptObject(At(index), value, typeId);
  187. else if (typeId & asTYPEID_OBJHANDLE)
  188. {
  189. *(void**)At(index) = *(void**)value;
  190. objType->GetEngine()->AddRefScriptObject(*(void**)value, typeId);
  191. }
  192. else if (typeId == asTYPEID_BOOL || typeId == asTYPEID_INT8 || typeId == asTYPEID_UINT8)
  193. *(char*)At(index) = *(char*)value;
  194. else if (typeId == asTYPEID_INT16 || typeId == asTYPEID_UINT16)
  195. *(short*)At(index) = *(short*)value;
  196. else if (typeId == asTYPEID_INT32 || typeId == asTYPEID_UINT32 || typeId == asTYPEID_FLOAT)
  197. *(int*)At(index) = *(int*)value;
  198. else if (typeId == asTYPEID_INT64 || typeId == asTYPEID_UINT64 || typeId == asTYPEID_DOUBLE)
  199. *(double*)At(index) = *(double*)value;
  200. }
  201. CScriptArray::~CScriptArray()
  202. {
  203. if (buffer)
  204. {
  205. DeleteBuffer(buffer);
  206. buffer = 0;
  207. }
  208. if (objType)
  209. objType->Release();
  210. }
  211. asUINT CScriptArray::GetSize() const
  212. {
  213. return buffer->numElements;
  214. }
  215. bool CScriptArray::IsEmpty() const
  216. {
  217. return buffer->numElements == 0;
  218. }
  219. void CScriptArray::Resize(asUINT numElements)
  220. {
  221. if (numElements & 0x80000000)
  222. {
  223. CheckMaxSize(numElements);
  224. return;
  225. }
  226. Resize((int)numElements - (int)buffer->numElements, -1);
  227. }
  228. // Internal
  229. void CScriptArray::Resize(int delta, asUINT at)
  230. {
  231. if (delta < 0)
  232. {
  233. if (-delta > (int)buffer->numElements)
  234. delta = -(int)buffer->numElements;
  235. if (at > buffer->numElements + delta)
  236. at = buffer->numElements + delta;
  237. }
  238. else if (delta > 0)
  239. {
  240. // Make sure the array size isn't too large for us to handle
  241. if (delta > 0 && !CheckMaxSize(buffer->numElements + delta))
  242. return;
  243. if (at > buffer->numElements)
  244. at = buffer->numElements;
  245. }
  246. if (delta == 0)
  247. return;
  248. // Allocate memory for the buffer
  249. SArrayBuffer *newBuffer;
  250. newBuffer = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1 + elementSize*(buffer->numElements + delta)];
  251. newBuffer->numElements = buffer->numElements + delta;
  252. int c = newBuffer->numElements > buffer->numElements ? buffer->numElements : newBuffer->numElements;
  253. memcpy(newBuffer->data, buffer->data, at*elementSize);
  254. if (delta > 0 && at < buffer->numElements)
  255. memcpy(newBuffer->data + (at+delta)*elementSize, buffer->data + at*elementSize, (buffer->numElements-at)*elementSize);
  256. else if (delta < 0 && at < buffer->numElements)
  257. memcpy(newBuffer->data + at*elementSize, buffer->data + (at-delta)*elementSize, (buffer->numElements-at+delta)*elementSize);
  258. int typeId = objType->GetSubTypeId();
  259. if (typeId & asTYPEID_MASK_OBJECT)
  260. {
  261. if (delta > 0)
  262. Construct(newBuffer, at, at+delta);
  263. else if (delta < 0)
  264. Destruct(buffer, at, at-delta);
  265. }
  266. // Release the old buffer
  267. delete[] (asBYTE*)buffer;
  268. buffer = newBuffer;
  269. }
  270. // internal
  271. bool CScriptArray::CheckMaxSize(asUINT numElements)
  272. {
  273. // This code makes sure the size of the buffer that is allocated
  274. // for the array doesn't overflow and becomes smaller than requested
  275. asUINT maxSize = 0xFFFFFFFFul - sizeof(SArrayBuffer) + 1;
  276. if (objType->GetSubTypeId() & asTYPEID_MASK_OBJECT)
  277. maxSize /= sizeof(void*);
  278. else
  279. maxSize /= elementSize;
  280. if (numElements > maxSize)
  281. {
  282. asIScriptContext* context = asGetActiveContext();
  283. if (context)
  284. context->SetException("Too large array size");
  285. return false;
  286. }
  287. // OK
  288. return true;
  289. }
  290. asIObjectType *CScriptArray::GetArrayObjectType() const
  291. {
  292. return objType;
  293. }
  294. int CScriptArray::GetArrayTypeId() const
  295. {
  296. return objType->GetTypeId();
  297. }
  298. int CScriptArray::GetElementTypeId() const
  299. {
  300. return objType->GetSubTypeId();
  301. }
  302. void CScriptArray::InsertAt(asUINT index, void *value)
  303. {
  304. if (index > buffer->numElements)
  305. {
  306. asIScriptContext* context = asGetActiveContext();
  307. if (context)
  308. context->SetException("Index out of bounds");
  309. return;
  310. }
  311. // Make room for the new element
  312. Resize(1, index);
  313. // Set the value of the new element
  314. SetValue(index, value);
  315. }
  316. void CScriptArray::InsertLast(void *value)
  317. {
  318. InsertAt(buffer->numElements, value);
  319. }
  320. void CScriptArray::RemoveAt(asUINT index)
  321. {
  322. if (index >= buffer->numElements)
  323. {
  324. asIScriptContext* context = asGetActiveContext();
  325. if (context)
  326. context->SetException("Index out of bounds");
  327. return;
  328. }
  329. // Remove the element
  330. Resize(-1, index);
  331. }
  332. void CScriptArray::RemoveLast()
  333. {
  334. RemoveAt(buffer->numElements-1);
  335. }
  336. // Return a pointer to the array element. Returns 0 if the index is out of bounds
  337. void *CScriptArray::At(asUINT index)
  338. {
  339. if (index >= buffer->numElements)
  340. {
  341. asIScriptContext* context = asGetActiveContext();
  342. if (context)
  343. context->SetException("Index out of bounds");
  344. return 0;
  345. }
  346. else
  347. {
  348. int typeId = objType->GetSubTypeId();
  349. if ((typeId & asTYPEID_MASK_OBJECT) && !isArrayOfHandles)
  350. return (void*)((size_t*)buffer->data)[index];
  351. else
  352. return buffer->data + elementSize*index;
  353. }
  354. }
  355. // internal
  356. void CScriptArray::CreateBuffer(SArrayBuffer **buf, asUINT numElements)
  357. {
  358. int typeId = objType->GetSubTypeId();
  359. if (typeId & asTYPEID_MASK_OBJECT)
  360. {
  361. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+sizeof(void*)*numElements];
  362. (*buf)->numElements = numElements;
  363. }
  364. else
  365. {
  366. *buf = (SArrayBuffer*)new asBYTE[sizeof(SArrayBuffer)-1+elementSize*numElements];
  367. (*buf)->numElements = numElements;
  368. }
  369. Construct(*buf, 0, numElements);
  370. }
  371. // internal
  372. void CScriptArray::DeleteBuffer(SArrayBuffer *buf)
  373. {
  374. Destruct(buf, 0, buf->numElements);
  375. // Free the buffer
  376. delete[] (asBYTE*)buf;
  377. }
  378. // internal
  379. void CScriptArray::Construct(SArrayBuffer *buf, asUINT start, asUINT end)
  380. {
  381. int typeId = objType->GetSubTypeId();
  382. if (isArrayOfHandles)
  383. {
  384. // Set all object handles to null
  385. void* d = (void*)(buf->data + start * sizeof(void*));
  386. memset(d, 0, (end-start)*sizeof(void*));
  387. }
  388. else if (typeId & asTYPEID_MASK_OBJECT)
  389. {
  390. void** max = (void**)(buf->data + end * sizeof(void*));
  391. void** d = (void**)(buf->data + start * sizeof(void*));
  392. asIScriptEngine* engine = objType->GetEngine();
  393. for (; d < max; d++)
  394. *d = (void*)engine->CreateScriptObject(typeId);
  395. }
  396. }
  397. // internal
  398. void CScriptArray::Destruct(SArrayBuffer *buf, asUINT start, asUINT end)
  399. {
  400. int typeId = objType->GetSubTypeId();
  401. if (typeId & asTYPEID_MASK_OBJECT)
  402. {
  403. asIScriptEngine* engine = objType->GetEngine();
  404. void** max = (void**)(buf->data + end * sizeof(void*));
  405. void** d = (void**)(buf->data + start * sizeof(void*));
  406. for(; d < max; d++ )
  407. {
  408. if( *d )
  409. engine->ReleaseScriptObject(*d, typeId);
  410. }
  411. }
  412. }
  413. // internal
  414. void CScriptArray::CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src)
  415. {
  416. asIScriptEngine* engine = objType->GetEngine();
  417. if(isArrayOfHandles)
  418. {
  419. // Copy the references and increase the reference counters
  420. if (dst->numElements > 0 && src->numElements > 0)
  421. {
  422. int typeId = objType->GetSubTypeId();
  423. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  424. void** max = (void**)(dst->data + count * sizeof(void*));
  425. void** d = (void**)dst->data;
  426. void** s = (void**)src->data;
  427. for (; d < max; d++, s++)
  428. {
  429. *d = *s;
  430. if (*d)
  431. engine->AddRefScriptObject(*d, typeId);
  432. }
  433. }
  434. }
  435. else
  436. {
  437. int typeId = objType->GetSubTypeId();
  438. if (dst->numElements > 0 && src->numElements > 0)
  439. {
  440. int count = dst->numElements > src->numElements ? src->numElements : dst->numElements;
  441. if (typeId & asTYPEID_MASK_OBJECT)
  442. {
  443. // Call the assignment operator on all of the objects
  444. void** max = (void**)(dst->data + count * sizeof(void*));
  445. void** d = (void**)dst->data;
  446. void** s = (void**)src->data;
  447. for(; d < max; d++, s++)
  448. engine->CopyScriptObject(*d, *s, typeId);
  449. }
  450. else
  451. {
  452. // Primitives are copied byte for byte
  453. memcpy(dst->data, src->data, count*elementSize);
  454. }
  455. }
  456. }
  457. }
  458. void CScriptArray::AddRef() const
  459. {
  460. // Clear the GC flag then increase the counter
  461. gcFlag = false;
  462. refCount++;
  463. }
  464. void CScriptArray::Release() const
  465. {
  466. // Now do the actual releasing (clearing the flag set by GC)
  467. gcFlag = false;
  468. if (--refCount == 0)
  469. delete this;
  470. }
  471. void RegisterArray(asIScriptEngine* engine)
  472. {
  473. engine->RegisterObjectType("Array<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE);
  474. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int&in)", asFUNCTION(ScriptArrayTemplateCallback), asCALL_CDECL);
  475. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in)", asFUNCTIONPR(ScriptArrayFactory, (asIObjectType*), CScriptArray*), asCALL_CDECL);
  476. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  477. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_FACTORY, "Array<T>@ f(int& in, uint, const T& in)", asFUNCTIONPR(ScriptArrayFactoryDefVal, (asIObjectType*, asUINT, void *), CScriptArray*), asCALL_CDECL);
  478. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_LIST_FACTORY, "Array<T>@ f(int& in, uint)", asFUNCTIONPR(ScriptArrayFactory2, (asIObjectType*, asUINT), CScriptArray*), asCALL_CDECL);
  479. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(CScriptArray,AddRef), asCALL_THISCALL);
  480. engine->RegisterObjectBehaviour("Array<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptArray,Release), asCALL_THISCALL);
  481. engine->RegisterObjectMethod("Array<T>", "T& opIndex(uint)", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  482. engine->RegisterObjectMethod("Array<T>", "const T& opIndex(uint) const", asMETHOD(CScriptArray, At), asCALL_THISCALL);
  483. engine->RegisterObjectMethod("Array<T>", "Array<T> &opAssign(const Array<T>& in)", asMETHOD(CScriptArray, operator=), asCALL_THISCALL);
  484. engine->RegisterObjectMethod("Array<T>", "void Insert(uint, const T& in)", asMETHOD(CScriptArray, InsertAt), asCALL_THISCALL);
  485. engine->RegisterObjectMethod("Array<T>", "void Remove(uint)", asMETHOD(CScriptArray, RemoveAt), asCALL_THISCALL);
  486. engine->RegisterObjectMethod("Array<T>", "void Push(const T& in)", asMETHOD(CScriptArray, InsertLast), asCALL_THISCALL);
  487. engine->RegisterObjectMethod("Array<T>", "void Pop()", asMETHOD(CScriptArray, RemoveLast), asCALL_THISCALL);
  488. engine->RegisterObjectMethod("Array<T>", "void Resize(uint)", asMETHODPR(CScriptArray, Resize, (asUINT), void), asCALL_THISCALL);
  489. engine->RegisterObjectMethod("Array<T>", "uint get_length() const", asMETHOD(CScriptArray, GetSize), asCALL_THISCALL);
  490. engine->RegisterObjectMethod("Array<T>", "bool get_empty() const", asMETHOD(CScriptArray, IsEmpty), asCALL_THISCALL);
  491. engine->RegisterDefaultArrayType("Array<T>");
  492. }
  493. // Adapted from AngelScript's scriptstdstring add-on
  494. static std::string StringFactory(asUINT length, const char* s)
  495. {
  496. return std::string(s, length);
  497. }
  498. static void ConstructString(std::string* ptr)
  499. {
  500. new(ptr) std::string();
  501. }
  502. static void ConstructStringCopy(const std::string& str, std::string* ptr)
  503. {
  504. new(ptr) std::string(str);
  505. }
  506. static void DestructString(std::string* ptr)
  507. {
  508. using namespace std;
  509. ptr->~string();
  510. }
  511. static char* StringCharAt(unsigned int i, std::string& str)
  512. {
  513. if (i >= str.size())
  514. {
  515. asIScriptContext* context = asGetActiveContext();
  516. if (context)
  517. context->SetException("Index out of bounds");
  518. return 0;
  519. }
  520. return &str[i];
  521. }
  522. static int StringCmp(const std::string& lhs, const std::string& rhs)
  523. {
  524. int cmp = 0;
  525. if (lhs < rhs)
  526. cmp = -1;
  527. else if (lhs > rhs)
  528. cmp = 1;
  529. return cmp;
  530. }
  531. static int StringFind(const std::string& rhs, const std::string& str)
  532. {
  533. return str.find(rhs);
  534. }
  535. void StringResize(unsigned newSize, std::string& str)
  536. {
  537. unsigned oldSize = str.length();
  538. str.resize(newSize);
  539. for (unsigned i = oldSize; i < newSize; ++i)
  540. str[i] = ' ';
  541. }
  542. std::string StringSubstring1Param(unsigned start, const std::string& str)
  543. {
  544. try
  545. {
  546. return str.substr(start);
  547. }
  548. catch (...)
  549. {
  550. return std::string();
  551. }
  552. }
  553. std::string StringSubstring2Params(unsigned start, unsigned length, const std::string& str)
  554. {
  555. try
  556. {
  557. return str.substr(start, length);
  558. }
  559. catch (...)
  560. {
  561. return std::string();
  562. }
  563. }
  564. std::string StringTrim(const std::string& str)
  565. {
  566. unsigned trimStart = 0;
  567. unsigned trimEnd = str.length();
  568. while (trimStart < trimEnd)
  569. {
  570. char c = str[trimStart];
  571. if ((c != ' ') && (c != 9))
  572. break;
  573. ++trimStart;
  574. }
  575. while (trimEnd > trimStart)
  576. {
  577. char c = str[trimEnd - 1];
  578. if ((c != ' ') && (c != 9))
  579. break;
  580. --trimEnd;
  581. }
  582. return str.substr(trimStart, trimEnd - trimStart);
  583. }
  584. static void ConstructStringInt(int value, std::string* ptr)
  585. {
  586. new(ptr) std::string();
  587. *ptr = ToString(value);
  588. }
  589. static void ConstructStringUInt(unsigned value, std::string* ptr)
  590. {
  591. new(ptr) std::string();
  592. *ptr = ToString(value);
  593. }
  594. static void ConstructStringFloat(float value, std::string* ptr)
  595. {
  596. new(ptr) std::string();
  597. *ptr = ToString(value);
  598. }
  599. static void ConstructStringBool(bool value, std::string* ptr)
  600. {
  601. new(ptr) std::string();
  602. *ptr = ToString(value);
  603. }
  604. static std::string& StringAssignInt(int value, std::string& str)
  605. {
  606. str = ToString(value);
  607. return str;
  608. }
  609. static std::string& StringAddAssignInt(int value, std::string& str)
  610. {
  611. str += ToString(value);
  612. return str;
  613. }
  614. static std::string StringAddInt(int value, const std::string& str)
  615. {
  616. return str + ToString(value);
  617. }
  618. static std::string StringAddIntReverse(int value, const std::string& str)
  619. {
  620. return ToString(value) + str;
  621. }
  622. static std::string& StringAssignUInt(unsigned value, std::string& str)
  623. {
  624. str = ToString(value);
  625. return str;
  626. }
  627. static std::string& StringAddAssignUInt(unsigned value, std::string& str)
  628. {
  629. str += ToString(value);
  630. return str;
  631. }
  632. static std::string StringAddUInt(unsigned value, const std::string& str)
  633. {
  634. return str + ToString(value);
  635. }
  636. static std::string StringAddUIntReverse(unsigned value, const std::string& str)
  637. {
  638. return ToString(value) + str;
  639. }
  640. static std::string& StringAssignFloat(float value, std::string& str)
  641. {
  642. str = ToString(value);
  643. return str;
  644. }
  645. static std::string& StringAddAssignFloat(float value, std::string& str)
  646. {
  647. str += ToString(value);
  648. return str;
  649. }
  650. static std::string StringAddFloat(float value, const std::string& str)
  651. {
  652. return str + ToString(value);
  653. }
  654. static std::string StringAddFloatReverse(float value, const std::string& str)
  655. {
  656. return ToString(value) + str;
  657. }
  658. static std::string& StringAssignBool(bool value, std::string& str)
  659. {
  660. str = ToString(value);
  661. return str;
  662. }
  663. static std::string& StringAddAssignBool(bool value, std::string& str)
  664. {
  665. str += ToString(value);
  666. return str;
  667. }
  668. static std::string StringAddBool(bool value, const std::string& str)
  669. {
  670. return str + ToString(value);
  671. }
  672. static std::string StringAddBoolReverse(bool value, const std::string& str)
  673. {
  674. return ToString(value) + str;
  675. }
  676. void RegisterString(asIScriptEngine *engine)
  677. {
  678. engine->RegisterObjectType("String", sizeof(std::string), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  679. engine->RegisterStringFactory("String", asFUNCTION(StringFactory), asCALL_CDECL);
  680. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  681. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(const String& in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  682. engine->RegisterObjectBehaviour("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  683. engine->RegisterObjectMethod("String", "String &opAssign(const String& in)", asMETHODPR(std::string, operator =, (const std::string&), std::string&), asCALL_THISCALL);
  684. engine->RegisterObjectMethod("String", "String &opAddAssign(const String& in)", asMETHODPR(std::string, operator+=, (const std::string&), std::string&), asCALL_THISCALL);
  685. engine->RegisterObjectMethod("String", "bool opEquals(const String& in) const", asFUNCTIONPR(std::operator ==, (const std::string&, const std::string&), bool), asCALL_CDECL_OBJFIRST);
  686. engine->RegisterObjectMethod("String", "int opCmp(const String& in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  687. engine->RegisterObjectMethod("String", "String opAdd(const String& in) const", asFUNCTIONPR(std::operator +, (const std::string&, const std::string&), std::string), asCALL_CDECL_OBJFIRST);
  688. engine->RegisterObjectMethod("String", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  689. engine->RegisterObjectMethod("String", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  690. engine->RegisterObjectMethod("String", "int Find(const String& in) const", asFUNCTION(StringFind), asCALL_CDECL_OBJLAST);
  691. engine->RegisterObjectMethod("String", "String Substring(uint) const", asFUNCTION(StringSubstring1Param), asCALL_CDECL_OBJLAST);
  692. engine->RegisterObjectMethod("String", "String Substring(uint, uint) const", asFUNCTION(StringSubstring2Params), asCALL_CDECL_OBJLAST);
  693. engine->RegisterObjectMethod("String", "String Trim() const", asFUNCTION(StringTrim), asCALL_CDECL_OBJLAST);
  694. engine->RegisterObjectMethod("String", "void Resize(uint)", asFUNCTION(StringResize), asCALL_CDECL_OBJLAST);
  695. engine->RegisterObjectMethod("String", "uint get_length() const", asMETHOD(std::string, length), asCALL_THISCALL);
  696. engine->RegisterObjectMethod("String", "bool get_empty() const", asMETHOD(std::string, empty), asCALL_THISCALL);
  697. // Register automatic conversion functions for convenience
  698. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  699. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  700. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  701. engine->RegisterObjectBehaviour("String", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  702. engine->RegisterObjectMethod("String", "String &opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  703. engine->RegisterObjectMethod("String", "String &opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  704. engine->RegisterObjectMethod("String", "String opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  705. engine->RegisterObjectMethod("String", "String opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  706. engine->RegisterObjectMethod("String", "String &opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  707. engine->RegisterObjectMethod("String", "String &opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  708. engine->RegisterObjectMethod("String", "String opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  709. engine->RegisterObjectMethod("String", "String opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  710. engine->RegisterObjectMethod("String", "String &opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  711. engine->RegisterObjectMethod("String", "String &opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  712. engine->RegisterObjectMethod("String", "String opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  713. engine->RegisterObjectMethod("String", "String opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  714. engine->RegisterObjectMethod("String", "String &opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  715. engine->RegisterObjectMethod("String", "String &opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  716. engine->RegisterObjectMethod("String", "String opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  717. engine->RegisterObjectMethod("String", "String opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  718. }