Addons.h 11 KB

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