RegisterStdString.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static void ConstructStringInt(int value, std::string* ptr)
  65. {
  66. new(ptr) std::string();
  67. *ptr = toString(value);
  68. }
  69. static void ConstructStringUInt(unsigned value, std::string* ptr)
  70. {
  71. new(ptr) std::string();
  72. *ptr = toString(value);
  73. }
  74. static void ConstructStringFloat(float value, std::string* ptr)
  75. {
  76. new(ptr) std::string();
  77. *ptr = toString(value);
  78. }
  79. static void ConstructStringBool(bool value, std::string* ptr)
  80. {
  81. new(ptr) std::string();
  82. *ptr = toString(value);
  83. }
  84. static std::string& StringAssignInt(int value, std::string& str)
  85. {
  86. str = toString(value);
  87. return str;
  88. }
  89. static std::string& StringAddAssignInt(int value, std::string& str)
  90. {
  91. str += toString(value);
  92. return str;
  93. }
  94. static std::string StringAddInt(int value, const std::string& str)
  95. {
  96. return str + toString(value);
  97. }
  98. static std::string StringAddIntReverse(int value, const std::string& str)
  99. {
  100. return toString(value) + str;
  101. }
  102. static std::string& StringAssignUInt(unsigned value, std::string& str)
  103. {
  104. str = toString(value);
  105. return str;
  106. }
  107. static std::string& StringAddAssignUInt(unsigned value, std::string& str)
  108. {
  109. str += toString(value);
  110. return str;
  111. }
  112. static std::string StringAddUInt(unsigned value, const std::string& str)
  113. {
  114. return str + toString(value);
  115. }
  116. static std::string StringAddUIntReverse(unsigned value, const std::string& str)
  117. {
  118. return toString(value) + str;
  119. }
  120. static std::string& StringAssignFloat(float value, std::string& str)
  121. {
  122. str = toString(value);
  123. return str;
  124. }
  125. static std::string& StringAddAssignFloat(float value, std::string& str)
  126. {
  127. str += toString(value);
  128. return str;
  129. }
  130. static std::string StringAddFloat(float value, const std::string& str)
  131. {
  132. return str + toString(value);
  133. }
  134. static std::string StringAddFloatReverse(float value, const std::string& str)
  135. {
  136. return toString(value) + str;
  137. }
  138. static std::string& StringAssignBool(bool value, std::string& str)
  139. {
  140. str = toString(value);
  141. return str;
  142. }
  143. static std::string& StringAddAssignBool(bool value, std::string& str)
  144. {
  145. str += toString(value);
  146. return str;
  147. }
  148. static std::string StringAddBool(bool value, const std::string& str)
  149. {
  150. return str + toString(value);
  151. }
  152. static std::string StringAddBoolReverse(bool value, const std::string& str)
  153. {
  154. return toString(value) + str;
  155. }
  156. void registerStdString(asIScriptEngine *engine)
  157. {
  158. engine->RegisterObjectType("string", sizeof(std::string), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
  159. engine->RegisterStringFactory("string", asFUNCTION(StringFactory), asCALL_CDECL);
  160. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
  161. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(const string& in)", asFUNCTION(ConstructStringCopy), asCALL_CDECL_OBJLAST);
  162. engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
  163. engine->RegisterObjectMethod("string", "string &opAssign(const string& in)", asMETHODPR(std::string, operator =, (const std::string&), std::string&), asCALL_THISCALL);
  164. engine->RegisterObjectMethod("string", "string &opAddAssign(const string& in)", asMETHODPR(std::string, operator+=, (const std::string&), std::string&), asCALL_THISCALL);
  165. engine->RegisterObjectMethod("string", "bool opEquals(const string& in) const", asFUNCTIONPR(std::operator ==, (const std::string&, const std::string&), bool), asCALL_CDECL_OBJFIRST);
  166. engine->RegisterObjectMethod("string", "int opCmp(const string& in) const", asFUNCTION(StringCmp), asCALL_CDECL_OBJFIRST);
  167. engine->RegisterObjectMethod("string", "string opAdd(const string& in) const", asFUNCTIONPR(std::operator +, (const std::string&, const std::string&), std::string), asCALL_CDECL_OBJFIRST);
  168. engine->RegisterObjectMethod("string", "uint length() const", asMETHOD(std::string, size), asCALL_THISCALL);
  169. engine->RegisterObjectMethod("string", "void resize(uint)", asMETHODPR(std::string, resize, (size_t), void), asCALL_THISCALL);
  170. engine->RegisterObjectMethod("string", "uint8 &opIndex(uint)", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  171. engine->RegisterObjectMethod("string", "const uint8 &opIndex(uint) const", asFUNCTION(StringCharAt), asCALL_CDECL_OBJLAST);
  172. engine->RegisterObjectMethod("string", "int find(const string& in) const", asFUNCTION(StringFind), asCALL_CDECL_OBJLAST);
  173. // Register automatic conversion functions for convenience
  174. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTION(ConstructStringInt), asCALL_CDECL_OBJLAST);
  175. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(uint)", asFUNCTION(ConstructStringUInt), asCALL_CDECL_OBJLAST);
  176. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(float)", asFUNCTION(ConstructStringFloat), asCALL_CDECL_OBJLAST);
  177. engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructStringBool), asCALL_CDECL_OBJLAST);
  178. engine->RegisterObjectMethod("string", "string &opAssign(int)", asFUNCTION(StringAssignInt), asCALL_CDECL_OBJLAST);
  179. engine->RegisterObjectMethod("string", "string &opAddAssign(int)", asFUNCTION(StringAddAssignInt), asCALL_CDECL_OBJLAST);
  180. engine->RegisterObjectMethod("string", "string opAdd(int) const", asFUNCTION(StringAddInt), asCALL_CDECL_OBJLAST);
  181. engine->RegisterObjectMethod("string", "string opAdd_r(int) const", asFUNCTION(StringAddIntReverse), asCALL_CDECL_OBJLAST);
  182. engine->RegisterObjectMethod("string", "string &opAssign(uint)", asFUNCTION(StringAssignUInt), asCALL_CDECL_OBJLAST);
  183. engine->RegisterObjectMethod("string", "string &opAddAssign(uint)", asFUNCTION(StringAddAssignUInt), asCALL_CDECL_OBJLAST);
  184. engine->RegisterObjectMethod("string", "string opAdd(uint) const", asFUNCTION(StringAddUInt), asCALL_CDECL_OBJLAST);
  185. engine->RegisterObjectMethod("string", "string opAdd_r(uint) const", asFUNCTION(StringAddUIntReverse), asCALL_CDECL_OBJLAST);
  186. engine->RegisterObjectMethod("string", "string &opAssign(float)", asFUNCTION(StringAssignFloat), asCALL_CDECL_OBJLAST);
  187. engine->RegisterObjectMethod("string", "string &opAddAssign(float)", asFUNCTION(StringAddAssignFloat), asCALL_CDECL_OBJLAST);
  188. engine->RegisterObjectMethod("string", "string opAdd(float) const", asFUNCTION(StringAddFloat), asCALL_CDECL_OBJLAST);
  189. engine->RegisterObjectMethod("string", "string opAdd_r(float) const", asFUNCTION(StringAddFloatReverse), asCALL_CDECL_OBJLAST);
  190. engine->RegisterObjectMethod("string", "string &opAssign(bool)", asFUNCTION(StringAssignBool), asCALL_CDECL_OBJLAST);
  191. engine->RegisterObjectMethod("string", "string &opAddAssign(bool)", asFUNCTION(StringAddAssignBool), asCALL_CDECL_OBJLAST);
  192. engine->RegisterObjectMethod("string", "string opAdd(bool) const", asFUNCTION(StringAddBool), asCALL_CDECL_OBJLAST);
  193. engine->RegisterObjectMethod("string", "string opAdd_r(bool) const", asFUNCTION(StringAddBoolReverse), asCALL_CDECL_OBJLAST);
  194. }