RegisterStdString.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "Exception.h"
  25. #include "StringUtils.h"
  26. #include <angelscript.h>
  27. // Adapted from AngelScript's scriptstdstring add-on
  28. static std::string StringFactory(asUINT length, const char* s)
  29. {
  30. return std::string(s, length);
  31. }
  32. static void ConstructString(std::string* ptr)
  33. {
  34. new(ptr) std::string();
  35. }
  36. static void ConstructStringCopy(const std::string& str, std::string* ptr)
  37. {
  38. new(ptr) std::string(str);
  39. }
  40. static void DestructString(std::string* ptr)
  41. {
  42. using namespace std;
  43. ptr->~string();
  44. }
  45. static char* StringCharAt(unsigned int i, std::string& str)
  46. {
  47. if (i >= str.size())
  48. SAFE_EXCEPTION_RET("Index out of bounds", 0);
  49. return &str[i];
  50. }
  51. static int StringCmp(const std::string& lhs, const std::string& rhs)
  52. {
  53. int cmp = 0;
  54. if (lhs < rhs)
  55. cmp = -1;
  56. else if (lhs > rhs)
  57. cmp = 1;
  58. return cmp;
  59. }
  60. static int StringFind(const std::string& rhs, const std::string& str)
  61. {
  62. return str.find(rhs);
  63. }
  64. std::string StringSubString1Param(unsigned start, const std::string& str)
  65. {
  66. try
  67. {
  68. return str.substr(start);
  69. }
  70. catch (...)
  71. {
  72. return std::string();
  73. }
  74. }
  75. std::string StringSubString2Params(unsigned start, unsigned length, const std::string& str)
  76. {
  77. try
  78. {
  79. return str.substr(start, length);
  80. }
  81. catch (...)
  82. {
  83. return std::string();
  84. }
  85. }
  86. static void ConstructStringInt(int value, std::string* ptr)
  87. {
  88. new(ptr) std::string();
  89. *ptr = toString(value);
  90. }
  91. static void ConstructStringUInt(unsigned value, std::string* ptr)
  92. {
  93. new(ptr) std::string();
  94. *ptr = toString(value);
  95. }
  96. static void ConstructStringFloat(float value, std::string* ptr)
  97. {
  98. new(ptr) std::string();
  99. *ptr = toString(value);
  100. }
  101. static void ConstructStringBool(bool value, std::string* ptr)
  102. {
  103. new(ptr) std::string();
  104. *ptr = toString(value);
  105. }
  106. static std::string& StringAssignInt(int value, std::string& str)
  107. {
  108. str = toString(value);
  109. return str;
  110. }
  111. static std::string& StringAddAssignInt(int value, std::string& str)
  112. {
  113. str += toString(value);
  114. return str;
  115. }
  116. static std::string StringAddInt(int value, const std::string& str)
  117. {
  118. return str + toString(value);
  119. }
  120. static std::string StringAddIntReverse(int value, const std::string& str)
  121. {
  122. return toString(value) + str;
  123. }
  124. static std::string& StringAssignUInt(unsigned value, std::string& str)
  125. {
  126. str = toString(value);
  127. return str;
  128. }
  129. static std::string& StringAddAssignUInt(unsigned value, std::string& str)
  130. {
  131. str += toString(value);
  132. return str;
  133. }
  134. static std::string StringAddUInt(unsigned value, const std::string& str)
  135. {
  136. return str + toString(value);
  137. }
  138. static std::string StringAddUIntReverse(unsigned value, const std::string& str)
  139. {
  140. return toString(value) + str;
  141. }
  142. static std::string& StringAssignFloat(float value, std::string& str)
  143. {
  144. str = toString(value);
  145. return str;
  146. }
  147. static std::string& StringAddAssignFloat(float value, std::string& str)
  148. {
  149. str += toString(value);
  150. return str;
  151. }
  152. static std::string StringAddFloat(float value, const std::string& str)
  153. {
  154. return str + toString(value);
  155. }
  156. static std::string StringAddFloatReverse(float value, const std::string& str)
  157. {
  158. return toString(value) + str;
  159. }
  160. static std::string& StringAssignBool(bool value, std::string& str)
  161. {
  162. str = toString(value);
  163. return str;
  164. }
  165. static std::string& StringAddAssignBool(bool value, std::string& str)
  166. {
  167. str += toString(value);
  168. return str;
  169. }
  170. static std::string StringAddBool(bool value, const std::string& str)
  171. {
  172. return str + toString(value);
  173. }
  174. static std::string StringAddBoolReverse(bool value, const std::string& str)
  175. {
  176. return toString(value) + str;
  177. }
  178. void registerStdString(asIScriptEngine *engine)
  179. {
  180. engine->RegisterObjectType("string", sizeof(std::string), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  181. engine->RegisterStringFactory("string", asFUNCTION(StringFactory), asCALL_CDECL);
  182. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  183. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(const string& in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  184. engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  185. engine->RegisterObjectMethod("string", "string &opAssign(const string& in)", asMETHODPR(std::string, operator =, (const std::string&), std::string&), asCALL_THISCALL);
  186. engine->RegisterObjectMethod("string", "string &opAddAssign(const string& in)", asMETHODPR(std::string, operator+=, (const std::string&), std::string&), asCALL_THISCALL);
  187. engine->RegisterObjectMethod("string", "bool opEquals(const string& in) const", asFUNCTIONPR(std::operator ==, (const std::string&, const std::string&), bool), asCALL_CDECL_OBJFIRST);
  188. engine->RegisterObjectMethod("string", "int opCmp(const string& in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  189. engine->RegisterObjectMethod("string", "string opAdd(const string& in) const", asFUNCTIONPR(std::operator +, (const std::string&, const std::string&), std::string), asCALL_CDECL_OBJFIRST);
  190. engine->RegisterObjectMethod("string", "uint length() const", asMETHOD(std::string, length), asCALL_THISCALL);
  191. engine->RegisterObjectMethod("string", "uint size() const", asMETHOD(std::string, size), asCALL_THISCALL);
  192. engine->RegisterObjectMethod("string", "bool empty() const", asMETHOD(std::string, empty), asCALL_THISCALL);
  193. engine->RegisterObjectMethod("string", "void resize(uint)", asMETHODPR(std::string, resize, (size_t), void), asCALL_THISCALL);
  194. engine->RegisterObjectMethod("string", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  195. engine->RegisterObjectMethod("string", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  196. engine->RegisterObjectMethod("string", "int find(const string& in) const", asFUNCTION(StringFind), asCALL_CDECL_OBJLAST);
  197. engine->RegisterObjectMethod("string", "string substr(uint) const", asFUNCTION(StringSubString1Param), asCALL_CDECL_OBJLAST);
  198. engine->RegisterObjectMethod("string", "string substr(uint, uint) const", asFUNCTION(StringSubString2Params), asCALL_CDECL_OBJLAST);
  199. // Register automatic conversion functions for convenience
  200. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  201. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  202. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  203. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  204. engine->RegisterObjectMethod("string", "string &opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  205. engine->RegisterObjectMethod("string", "string &opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  206. engine->RegisterObjectMethod("string", "string opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  207. engine->RegisterObjectMethod("string", "string opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  208. engine->RegisterObjectMethod("string", "string &opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  209. engine->RegisterObjectMethod("string", "string &opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  210. engine->RegisterObjectMethod("string", "string opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  211. engine->RegisterObjectMethod("string", "string opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  212. engine->RegisterObjectMethod("string", "string &opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  213. engine->RegisterObjectMethod("string", "string &opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  214. engine->RegisterObjectMethod("string", "string opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  215. engine->RegisterObjectMethod("string", "string opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  216. engine->RegisterObjectMethod("string", "string &opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  217. engine->RegisterObjectMethod("string", "string &opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  218. engine->RegisterObjectMethod("string", "string opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  219. engine->RegisterObjectMethod("string", "string opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  220. }