Addons.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #ifdef URHO3D_IS_BUILDING
  24. #include "Urho3D.h"
  25. #else
  26. #include <Urho3D/Urho3D.h>
  27. #endif
  28. #include "../Container/HashMap.h"
  29. #include "../Container/Str.h"
  30. #include "../Math/StringHash.h"
  31. #include <AngelScript/angelscript.h>
  32. // Adapted from Angelscript's scriptarray, scriptdictionary & scriptstdstring add-ons, but with garbage collection disabled
  33. namespace Urho3D
  34. {
  35. struct SArrayBuffer;
  36. struct SArrayCache;
  37. /// %Script array class.
  38. class URHO3D_API CScriptArray
  39. {
  40. public:
  41. // Set the memory functions that should be used by all CScriptArrays
  42. static void SetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
  43. // Factory functions
  44. static CScriptArray *Create(asITypeInfo *ot);
  45. static CScriptArray *Create(asITypeInfo *ot, asUINT length);
  46. static CScriptArray *Create(asITypeInfo *ot, asUINT length, void *defVal);
  47. static CScriptArray *Create(asITypeInfo *ot, void *initList);
  48. // Memory management
  49. void AddRef() const;
  50. void Release() const;
  51. // Type information
  52. asITypeInfo *GetArrayObjectType() const;
  53. int GetArrayTypeId() const;
  54. int GetElementTypeId() const;
  55. // Get the current size
  56. asUINT GetSize() const;
  57. // Returns true if the array is empty
  58. bool IsEmpty() const;
  59. // Pre-allocates memory for elements
  60. void Reserve(asUINT maxElements);
  61. // Resize the array
  62. void Resize(asUINT numElements);
  63. // Get a pointer to an element. Returns 0 if out of bounds
  64. void *At(asUINT index);
  65. const void *At(asUINT index) const;
  66. // Set value of an element.
  67. // The value arg should be a pointer to the value that will be copied to the element.
  68. // Remember, if the array holds handles the value parameter should be the
  69. // address of the handle. The refCount of the object will also be incremented
  70. void SetValue(asUINT index, void *value);
  71. // Copy the contents of one array to another (only if the types are the same)
  72. CScriptArray &operator=(const CScriptArray&);
  73. // Compare two arrays
  74. bool operator==(const CScriptArray &) const;
  75. // Array manipulation
  76. void InsertAt(asUINT index, void *value);
  77. void RemoveAt(asUINT index);
  78. void InsertLast(void *value);
  79. void RemoveLast();
  80. void SortAsc();
  81. void SortDesc();
  82. void SortAsc(asUINT startAt, asUINT count);
  83. void SortDesc(asUINT startAt, asUINT count);
  84. void Sort(asUINT startAt, asUINT count, bool asc);
  85. void Reverse();
  86. int Find(void *value) const;
  87. int Find(asUINT startAt, void *value) const;
  88. int FindByRef(void *ref) const;
  89. int FindByRef(asUINT startAt, void *ref) const;
  90. // Swap content of two arrays for avoid copy
  91. bool Swap(CScriptArray& other);
  92. // GC methods
  93. int GetRefCount();
  94. void SetFlag();
  95. bool GetFlag();
  96. void EnumReferences(asIScriptEngine *engine);
  97. void ReleaseAllHandles(asIScriptEngine *engine);
  98. protected:
  99. mutable int refCount;
  100. mutable bool gcFlag;
  101. asITypeInfo *objType;
  102. SArrayBuffer *buffer;
  103. size_t elementSize;
  104. int subTypeId{};
  105. // Constructors
  106. CScriptArray(asITypeInfo *ot, void *buf); // Called from script when initialized with list
  107. CScriptArray(asUINT length, asITypeInfo *ot);
  108. CScriptArray(asUINT length, void *defVal, asITypeInfo *ot);
  109. CScriptArray(const CScriptArray &other);
  110. virtual ~CScriptArray();
  111. bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
  112. void *GetArrayItemPointer(int index);
  113. void *GetDataPointer(void *buffer);
  114. void Copy(void *dst, void *src);
  115. void Precache();
  116. bool CheckMaxSize(asUINT numElements);
  117. void Resize(int delta, asUINT at);
  118. void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
  119. void DeleteBuffer(SArrayBuffer *buf);
  120. void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
  121. void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
  122. void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
  123. bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
  124. };
  125. class CScriptDictionary;
  126. /// %Script dictionary value.
  127. class URHO3D_API CScriptDictValue
  128. {
  129. public:
  130. // This class must not be declared as local variable in C++, because it needs
  131. // to receive the script engine pointer in all operations. The engine pointer
  132. // is not kept as member in order to keep the size down
  133. CScriptDictValue();
  134. CScriptDictValue(asIScriptEngine *engine, void *value, int typeId);
  135. // Destructor must not be called without first calling FreeValue, otherwise a memory leak will occur
  136. ~CScriptDictValue();
  137. // Replace the stored value
  138. void Set(asIScriptEngine *engine, void *value, int typeId);
  139. void Set(asIScriptEngine *engine, const asINT64 &value);
  140. void Set(asIScriptEngine *engine, const double &value);
  141. // Gets the stored value. Returns false if the value isn't compatible with the informed typeId
  142. bool Get(asIScriptEngine *engine, void *value, int typeId) const;
  143. bool Get(asIScriptEngine *engine, asINT64 &value) const;
  144. bool Get(asIScriptEngine *engine, double &value) const;
  145. // Returns the type id of the stored value
  146. int GetTypeId() const;
  147. // Free the stored value
  148. void FreeValue(asIScriptEngine *engine);
  149. protected:
  150. friend class CScriptDictionary;
  151. union
  152. {
  153. asINT64 m_valueInt;
  154. double m_valueFlt;
  155. void *m_valueObj;
  156. };
  157. int m_typeId;
  158. };
  159. /// %Script dictionary class.
  160. class URHO3D_API CScriptDictionary
  161. {
  162. public:
  163. // Factory functions
  164. static CScriptDictionary *Create(asIScriptEngine *engine);
  165. // Called from the script to instantiate a dictionary from an initialization list
  166. static CScriptDictionary *Create(asBYTE *buffer);
  167. // Reference counting
  168. void AddRef() const;
  169. void Release() const;
  170. // Reassign the dictionary
  171. CScriptDictionary &operator =(const CScriptDictionary &other);
  172. // Sets a key/value pair
  173. void Set(const String &key, void *value, int typeId);
  174. void Set(const String &key, const asINT64 &value);
  175. void Set(const String &key, const double &value);
  176. // Gets the stored value. Returns false if the value isn't compatible with the informed typeId
  177. bool Get(const String &key, void *value, int typeId) const;
  178. bool Get(const String &key, asINT64 &value) const;
  179. bool Get(const String &key, double &value) const;
  180. // Index accessors. If the dictionary is not const it inserts the value if it doesn't already exist
  181. // If the dictionary is const then a script exception is set if it doesn't exist and a null pointer is returned
  182. CScriptDictValue *operator[](const String &key);
  183. const CScriptDictValue *operator[](const String &key) const;
  184. // Returns the type id of the stored value, or negative if it doesn't exist
  185. int GetTypeId(const String &key) const;
  186. // Returns true if the key is set
  187. bool Exists(const String &key) const;
  188. // Returns true if there are no key/value pairs in the dictionary
  189. bool IsEmpty() const;
  190. // Returns the number of key/value pairs in the dictionary
  191. asUINT GetSize() const;
  192. // Deletes the key
  193. void Delete(const String &key);
  194. // Deletes all keys
  195. void DeleteAll();
  196. // Get an array of all keys
  197. CScriptArray *GetKeys() const;
  198. public:
  199. /// STL style iterator for %Script dictionary class.
  200. class CIterator
  201. {
  202. public:
  203. void operator++(); // Pre-increment
  204. void operator++(int); // Post-increment
  205. // This is needed to support C++11 range-for
  206. CIterator &operator*();
  207. bool operator==(const CIterator &other) const;
  208. bool operator!=(const CIterator &other) const;
  209. // Accessors
  210. const String &GetKey() const;
  211. int GetTypeId() const;
  212. bool GetValue(asINT64 &value) const;
  213. bool GetValue(double &value) const;
  214. bool GetValue(void *value, int typeId) const;
  215. protected:
  216. friend class CScriptDictionary;
  217. CIterator();
  218. CIterator(const CScriptDictionary &dict,
  219. HashMap<String, CScriptDictValue>::ConstIterator it);
  220. CIterator &operator=(const CIterator &) {return *this;} // Not used
  221. HashMap<String, CScriptDictValue>::ConstIterator m_it;
  222. const CScriptDictionary &m_dict;
  223. };
  224. CIterator begin() const;
  225. CIterator end() const;
  226. // Garbage collections behaviours
  227. int GetRefCount();
  228. void SetGCFlag();
  229. bool GetGCFlag();
  230. void EnumReferences(asIScriptEngine *engine);
  231. void ReleaseAllReferences(asIScriptEngine *engine);
  232. protected:
  233. // Since the dictionary uses the asAllocMem and asFreeMem functions to allocate memory
  234. // the constructors are made protected so that the application cannot allocate it
  235. // manually in a different way
  236. explicit CScriptDictionary(asIScriptEngine *engine);
  237. explicit CScriptDictionary(asBYTE *buffer);
  238. // We don't want anyone to call the destructor directly, it should be called through the Release method
  239. virtual ~CScriptDictionary();
  240. // Our properties
  241. asIScriptEngine *engine;
  242. mutable int refCount;
  243. mutable bool gcFlag;
  244. // TODO: memory: The allocator should use the asAllocMem and asFreeMem
  245. HashMap<String, CScriptDictValue> dict;
  246. };
  247. /// %String factory class that manages the string constants the script engine uses.
  248. class URHO3D_API StringFactory : public asIStringFactory
  249. {
  250. public:
  251. const void* GetStringConstant(const char* data, asUINT length) override;
  252. int ReleaseStringConstant(const void* str) override;
  253. int GetRawStringData(const void* str, char* data, asUINT* length) const override;
  254. private:
  255. StringMap map_;
  256. };
  257. }