Addons.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // Copyright (c) 2008-2017 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 <AngelScript/angelscript.h>
  31. // Adapted from Angelscript's scriptarray, scriptdictionary & scriptstdstring add-ons, but with garbage collection disabled
  32. namespace Urho3D
  33. {
  34. struct SArrayBuffer;
  35. struct SArrayCache;
  36. /// %Script array class.
  37. class URHO3D_API CScriptArray
  38. {
  39. public:
  40. // Set the memory functions that should be used by all CScriptArrays
  41. static void SetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
  42. // Factory functions
  43. static CScriptArray *Create(asITypeInfo *ot);
  44. static CScriptArray *Create(asITypeInfo *ot, asUINT length);
  45. static CScriptArray *Create(asITypeInfo *ot, asUINT length, void *defaultValue);
  46. static CScriptArray *Create(asITypeInfo *ot, void *listBuffer);
  47. // Memory management
  48. void AddRef() const;
  49. void Release() const;
  50. // Type information
  51. asITypeInfo *GetArrayObjectType() const;
  52. int GetArrayTypeId() const;
  53. int GetElementTypeId() const;
  54. // Get the current size
  55. asUINT GetSize() const;
  56. // Returns true if the array is empty
  57. bool IsEmpty() const;
  58. // Pre-allocates memory for elements
  59. void Reserve(asUINT maxElements);
  60. // Resize the array
  61. void Resize(asUINT numElements);
  62. // Get a pointer to an element. Returns 0 if out of bounds
  63. void *At(asUINT index);
  64. const void *At(asUINT index) const;
  65. // Set value of an element.
  66. // The value arg should be a pointer to the value that will be copied to the element.
  67. // Remember, if the array holds handles the value parameter should be the
  68. // address of the handle. The refCount of the object will also be incremented
  69. void SetValue(asUINT index, void *value);
  70. // Copy the contents of one array to another (only if the types are the same)
  71. CScriptArray &operator=(const CScriptArray&);
  72. // Compare two arrays
  73. bool operator==(const CScriptArray &) const;
  74. // Array manipulation
  75. void InsertAt(asUINT index, void *value);
  76. void RemoveAt(asUINT index);
  77. void InsertLast(void *value);
  78. void RemoveLast();
  79. void SortAsc();
  80. void SortDesc();
  81. void SortAsc(asUINT startAt, asUINT count);
  82. void SortDesc(asUINT startAt, asUINT count);
  83. void Sort(asUINT startAt, asUINT count, bool asc);
  84. void Reverse();
  85. int Find(void *value) const;
  86. int Find(asUINT startAt, void *value) const;
  87. int FindByRef(void *ref) const;
  88. int FindByRef(asUINT startAt, void *ref) const;
  89. // Swap content of two arrays for avoid copy
  90. bool Swap(CScriptArray& other);
  91. // GC methods
  92. int GetRefCount();
  93. void SetFlag();
  94. bool GetFlag();
  95. void EnumReferences(asIScriptEngine *engine);
  96. void ReleaseAllHandles(asIScriptEngine *engine);
  97. protected:
  98. mutable int refCount;
  99. mutable bool gcFlag;
  100. asITypeInfo *objType;
  101. SArrayBuffer *buffer;
  102. int elementSize;
  103. int subTypeId;
  104. // Constructors
  105. CScriptArray(asITypeInfo *ot, void *initBuf); // Called from script when initialized with list
  106. CScriptArray(asUINT length, asITypeInfo *ot);
  107. CScriptArray(asUINT length, void *defVal, asITypeInfo *ot);
  108. CScriptArray(const CScriptArray &other);
  109. virtual ~CScriptArray();
  110. bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
  111. void *GetArrayItemPointer(int index);
  112. void *GetDataPointer(void *buffer);
  113. void Copy(void *dst, void *src);
  114. void Precache();
  115. bool CheckMaxSize(asUINT numElements);
  116. void Resize(int delta, asUINT at);
  117. void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
  118. void DeleteBuffer(SArrayBuffer *buf);
  119. void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
  120. void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
  121. void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
  122. bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
  123. };
  124. class CScriptDictionary;
  125. /// %Script dictionary value.
  126. class URHO3D_API CScriptDictValue
  127. {
  128. public:
  129. // This class must not be declared as local variable in C++, because it needs
  130. // to receive the script engine pointer in all operations. The engine pointer
  131. // is not kept as member in order to keep the size down
  132. CScriptDictValue();
  133. CScriptDictValue(asIScriptEngine *engine, void *value, int typeId);
  134. // Destructor must not be called without first calling FreeValue, otherwise a memory leak will occur
  135. ~CScriptDictValue();
  136. // Replace the stored value
  137. void Set(asIScriptEngine *engine, void *value, int typeId);
  138. void Set(asIScriptEngine *engine, const asINT64 &value);
  139. void Set(asIScriptEngine *engine, const double &value);
  140. // Gets the stored value. Returns false if the value isn't compatible with the informed typeId
  141. bool Get(asIScriptEngine *engine, void *value, int typeId) const;
  142. bool Get(asIScriptEngine *engine, asINT64 &value) const;
  143. bool Get(asIScriptEngine *engine, double &value) const;
  144. // Returns the type id of the stored value
  145. int GetTypeId() const;
  146. // Free the stored value
  147. void FreeValue(asIScriptEngine *engine);
  148. protected:
  149. friend class CScriptDictionary;
  150. union
  151. {
  152. asINT64 m_valueInt;
  153. double m_valueFlt;
  154. void *m_valueObj;
  155. };
  156. int m_typeId;
  157. };
  158. /// %Script dictionary class.
  159. class URHO3D_API CScriptDictionary
  160. {
  161. public:
  162. // Factory functions
  163. static CScriptDictionary *Create(asIScriptEngine *engine);
  164. // Called from the script to instantiate a dictionary from an initialization list
  165. static CScriptDictionary *Create(asBYTE *buffer);
  166. // Reference counting
  167. void AddRef() const;
  168. void Release() const;
  169. // Reassign the dictionary
  170. CScriptDictionary &operator =(const CScriptDictionary &other);
  171. // Sets a key/value pair
  172. void Set(const String &key, void *value, int typeId);
  173. void Set(const String &key, const asINT64 &value);
  174. void Set(const String &key, const double &value);
  175. // Gets the stored value. Returns false if the value isn't compatible with the informed typeId
  176. bool Get(const String &key, void *value, int typeId) const;
  177. bool Get(const String &key, asINT64 &value) const;
  178. bool Get(const String &key, double &value) const;
  179. // Index accessors. If the dictionary is not const it inserts the value if it doesn't already exist
  180. // If the dictionary is const then a script exception is set if it doesn't exist and a null pointer is returned
  181. CScriptDictValue *operator[](const String &key);
  182. const CScriptDictValue *operator[](const String &key) const;
  183. // Returns the type id of the stored value, or negative if it doesn't exist
  184. int GetTypeId(const String &key) const;
  185. // Returns true if the key is set
  186. bool Exists(const String &key) const;
  187. // Returns true if there are no key/value pairs in the dictionary
  188. bool IsEmpty() const;
  189. // Returns the number of key/value pairs in the dictionary
  190. asUINT GetSize() const;
  191. // Deletes the key
  192. void Delete(const String &key);
  193. // Deletes all keys
  194. void DeleteAll();
  195. // Get an array of all keys
  196. CScriptArray *GetKeys() const;
  197. public:
  198. /// STL style iterator for %Script dictionary class.
  199. class CIterator
  200. {
  201. public:
  202. void operator++(); // Pre-increment
  203. void operator++(int); // Post-increment
  204. // This is needed to support C++11 range-for
  205. CIterator &operator*();
  206. bool operator==(const CIterator &other) const;
  207. bool operator!=(const CIterator &other) const;
  208. // Accessors
  209. const String &GetKey() const;
  210. int GetTypeId() const;
  211. bool GetValue(asINT64 &value) const;
  212. bool GetValue(double &value) const;
  213. bool GetValue(void *value, int typeId) const;
  214. protected:
  215. friend class CScriptDictionary;
  216. CIterator();
  217. CIterator(const CScriptDictionary &dict,
  218. HashMap<String, CScriptDictValue>::ConstIterator it);
  219. CIterator &operator=(const CIterator &) {return *this;} // Not used
  220. HashMap<String, CScriptDictValue>::ConstIterator m_it;
  221. const CScriptDictionary &m_dict;
  222. };
  223. CIterator begin() const;
  224. CIterator end() const;
  225. // Garbage collections behaviours
  226. int GetRefCount();
  227. void SetGCFlag();
  228. bool GetGCFlag();
  229. void EnumReferences(asIScriptEngine *engine);
  230. void ReleaseAllReferences(asIScriptEngine *engine);
  231. protected:
  232. // Since the dictionary uses the asAllocMem and asFreeMem functions to allocate memory
  233. // the constructors are made protected so that the application cannot allocate it
  234. // manually in a different way
  235. CScriptDictionary(asIScriptEngine *engine);
  236. CScriptDictionary(asBYTE *buffer);
  237. // We don't want anyone to call the destructor directly, it should be called through the Release method
  238. virtual ~CScriptDictionary();
  239. // Our properties
  240. asIScriptEngine *engine;
  241. mutable int refCount;
  242. mutable bool gcFlag;
  243. // TODO: memory: The allocator should use the asAllocMem and asFreeMem
  244. HashMap<String, CScriptDictValue> dict;
  245. };
  246. /// Register the array type to script.
  247. void RegisterArray(asIScriptEngine* engine);
  248. /// Register the dictionary type to script.
  249. void RegisterDictionary(asIScriptEngine* engine);
  250. /// Register String to script.
  251. void RegisterString(asIScriptEngine* engine);
  252. }