Addons.h 9.7 KB

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