Addons.h 11 KB

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