Addons.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. // GC methods
  90. int GetRefCount();
  91. void SetFlag();
  92. bool GetFlag();
  93. void EnumReferences(asIScriptEngine *engine);
  94. void ReleaseAllHandles(asIScriptEngine *engine);
  95. protected:
  96. mutable int refCount;
  97. mutable bool gcFlag;
  98. asITypeInfo *objType;
  99. SArrayBuffer *buffer;
  100. int elementSize;
  101. int subTypeId;
  102. // Constructors
  103. CScriptArray(asITypeInfo *ot, void *initBuf); // Called from script when initialized with list
  104. CScriptArray(asUINT length, asITypeInfo *ot);
  105. CScriptArray(asUINT length, void *defVal, asITypeInfo *ot);
  106. CScriptArray(const CScriptArray &other);
  107. virtual ~CScriptArray();
  108. bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
  109. void *GetArrayItemPointer(int index);
  110. void *GetDataPointer(void *buffer);
  111. void Copy(void *dst, void *src);
  112. void Precache();
  113. bool CheckMaxSize(asUINT numElements);
  114. void Resize(int delta, asUINT at);
  115. void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
  116. void DeleteBuffer(SArrayBuffer *buf);
  117. void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
  118. void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
  119. void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
  120. bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
  121. };
  122. class CScriptDictionary;
  123. /// %Script dictionary value.
  124. class URHO3D_API CScriptDictValue
  125. {
  126. public:
  127. // This class must not be declared as local variable in C++, because it needs
  128. // to receive the script engine pointer in all operations. The engine pointer
  129. // is not kept as member in order to keep the size down
  130. CScriptDictValue();
  131. CScriptDictValue(asIScriptEngine *engine, void *value, int typeId);
  132. // Destructor must not be called without first calling FreeValue, otherwise a memory leak will occur
  133. ~CScriptDictValue();
  134. // Replace the stored value
  135. void Set(asIScriptEngine *engine, void *value, int typeId);
  136. void Set(asIScriptEngine *engine, const asINT64 &value);
  137. void Set(asIScriptEngine *engine, const double &value);
  138. // Gets the stored value. Returns false if the value isn't compatible with the informed typeId
  139. bool Get(asIScriptEngine *engine, void *value, int typeId) const;
  140. bool Get(asIScriptEngine *engine, asINT64 &value) const;
  141. bool Get(asIScriptEngine *engine, double &value) const;
  142. // Returns the type id of the stored value
  143. int GetTypeId() const;
  144. // Free the stored value
  145. void FreeValue(asIScriptEngine *engine);
  146. protected:
  147. friend class CScriptDictionary;
  148. union
  149. {
  150. asINT64 m_valueInt;
  151. double m_valueFlt;
  152. void *m_valueObj;
  153. };
  154. int m_typeId;
  155. };
  156. /// %Script dictionary class.
  157. class URHO3D_API CScriptDictionary
  158. {
  159. public:
  160. // Factory functions
  161. static CScriptDictionary *Create(asIScriptEngine *engine);
  162. // Called from the script to instantiate a dictionary from an initialization list
  163. static CScriptDictionary *Create(asBYTE *buffer);
  164. // Reference counting
  165. void AddRef() const;
  166. void Release() const;
  167. // Reassign the dictionary
  168. CScriptDictionary &operator =(const CScriptDictionary &other);
  169. // Sets a key/value pair
  170. void Set(const String &key, void *value, int typeId);
  171. void Set(const String &key, const asINT64 &value);
  172. void Set(const String &key, const double &value);
  173. // Gets the stored value. Returns false if the value isn't compatible with the informed typeId
  174. bool Get(const String &key, void *value, int typeId) const;
  175. bool Get(const String &key, asINT64 &value) const;
  176. bool Get(const String &key, double &value) const;
  177. // Index accessors. If the dictionary is not const it inserts the value if it doesn't already exist
  178. // If the dictionary is const then a script exception is set if it doesn't exist and a null pointer is returned
  179. CScriptDictValue *operator[](const String &key);
  180. const CScriptDictValue *operator[](const String &key) const;
  181. // Returns the type id of the stored value, or negative if it doesn't exist
  182. int GetTypeId(const String &key) const;
  183. // Returns true if the key is set
  184. bool Exists(const String &key) const;
  185. // Returns true if there are no key/value pairs in the dictionary
  186. bool IsEmpty() const;
  187. // Returns the number of key/value pairs in the dictionary
  188. asUINT GetSize() const;
  189. // Deletes the key
  190. void Delete(const String &key);
  191. // Deletes all keys
  192. void DeleteAll();
  193. // Get an array of all keys
  194. CScriptArray *GetKeys() const;
  195. public:
  196. /// STL style iterator for %Script dictionary class.
  197. class CIterator
  198. {
  199. public:
  200. void operator++(); // Pre-increment
  201. void operator++(int); // Post-increment
  202. // This is needed to support C++11 range-for
  203. CIterator &operator*();
  204. bool operator==(const CIterator &other) const;
  205. bool operator!=(const CIterator &other) const;
  206. // Accessors
  207. const String &GetKey() const;
  208. int GetTypeId() const;
  209. bool GetValue(asINT64 &value) const;
  210. bool GetValue(double &value) const;
  211. bool GetValue(void *value, int typeId) const;
  212. protected:
  213. friend class CScriptDictionary;
  214. CIterator();
  215. CIterator(const CScriptDictionary &dict,
  216. HashMap<String, CScriptDictValue>::ConstIterator it);
  217. CIterator &operator=(const CIterator &) {return *this;} // Not used
  218. HashMap<String, CScriptDictValue>::ConstIterator m_it;
  219. const CScriptDictionary &m_dict;
  220. };
  221. CIterator begin() const;
  222. CIterator end() const;
  223. // Garbage collections behaviours
  224. int GetRefCount();
  225. void SetGCFlag();
  226. bool GetGCFlag();
  227. void EnumReferences(asIScriptEngine *engine);
  228. void ReleaseAllReferences(asIScriptEngine *engine);
  229. protected:
  230. // Since the dictionary uses the asAllocMem and asFreeMem functions to allocate memory
  231. // the constructors are made protected so that the application cannot allocate it
  232. // manually in a different way
  233. CScriptDictionary(asIScriptEngine *engine);
  234. CScriptDictionary(asBYTE *buffer);
  235. // We don't want anyone to call the destructor directly, it should be called through the Release method
  236. virtual ~CScriptDictionary();
  237. // Our properties
  238. asIScriptEngine *engine;
  239. mutable int refCount;
  240. mutable bool gcFlag;
  241. // TODO: memory: The allocator should use the asAllocMem and asFreeMem
  242. HashMap<String, CScriptDictValue> dict;
  243. };
  244. /// Register the array type to script.
  245. void RegisterArray(asIScriptEngine* engine);
  246. /// Register the dictionary type to script.
  247. void RegisterDictionary(asIScriptEngine* engine);
  248. /// Register String to script.
  249. void RegisterString(asIScriptEngine* engine);
  250. }