Addons.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. CScriptArray(asIObjectType *ot, void *buf);
  37. CScriptArray(asUINT length, asIObjectType *ot);
  38. CScriptArray(asUINT length, void *defVal, asIObjectType *ot);
  39. CScriptArray(const CScriptArray &other);
  40. virtual ~CScriptArray();
  41. void AddRef() const;
  42. void Release() const;
  43. // Type information
  44. asIObjectType *GetArrayObjectType() const;
  45. int GetArrayTypeId() const;
  46. int GetElementTypeId() const;
  47. void Reserve(asUINT maxElements);
  48. void Resize(asUINT numElements);
  49. asUINT GetSize() const;
  50. bool IsEmpty() const;
  51. // Get a pointer to an element. Returns 0 if out of bounds
  52. void *At(asUINT index);
  53. const void *At(asUINT index) const;
  54. // Set value of an element
  55. void SetValue(asUINT index, void *value);
  56. CScriptArray &operator=(const CScriptArray&);
  57. bool operator==(const CScriptArray &) const;
  58. void InsertAt(asUINT index, void *value);
  59. void RemoveAt(asUINT index);
  60. void InsertLast(void *value);
  61. void RemoveLast();
  62. void SortAsc();
  63. void SortDesc();
  64. void SortAsc(asUINT index, asUINT count);
  65. void SortDesc(asUINT index, asUINT count);
  66. void Sort(asUINT index, asUINT count, bool asc);
  67. void Reverse();
  68. int Find(void *value) const;
  69. int Find(asUINT index, void *value) const;
  70. // GC methods
  71. int GetRefCount();
  72. void SetFlag();
  73. bool GetFlag();
  74. void EnumReferences(asIScriptEngine *engine);
  75. void ReleaseAllHandles(asIScriptEngine *engine);
  76. protected:
  77. mutable int refCount;
  78. mutable bool gcFlag;
  79. asIObjectType *objType;
  80. SArrayBuffer *buffer;
  81. int elementSize;
  82. int subTypeId;
  83. bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
  84. void *GetArrayItemPointer(int index);
  85. void *GetDataPointer(void *buffer);
  86. void Copy(void *dst, void *src);
  87. void Precache();
  88. bool CheckMaxSize(asUINT numElements);
  89. void Resize(int delta, asUINT at);
  90. void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
  91. void DeleteBuffer(SArrayBuffer *buf);
  92. void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
  93. void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
  94. void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
  95. bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
  96. };
  97. /// Script dictionary class
  98. class CScriptDictionary
  99. {
  100. public:
  101. // Memory management
  102. CScriptDictionary(asIScriptEngine *engine);
  103. CScriptDictionary(asBYTE *buffer);
  104. void AddRef() const;
  105. void Release() const;
  106. CScriptDictionary &operator =(const CScriptDictionary &other);
  107. // Sets/Gets a variable type value for a key
  108. void Set(const String &key, void *value, int typeId);
  109. bool Get(const String &key, void *value, int typeId) const;
  110. // Sets/Gets an integer number value for a key
  111. void Set(const String &key, asINT64 &value);
  112. bool Get(const String &key, asINT64 &value) const;
  113. // Sets/Gets a real number value for a key
  114. void Set(const String &key, double &value);
  115. bool Get(const String &key, double &value) const;
  116. // Returns true if the key is set
  117. bool Exists(const String &key) const;
  118. bool IsEmpty() const;
  119. asUINT GetSize() const;
  120. // Deletes the key
  121. void Delete(const String &key);
  122. // Deletes all keys
  123. void DeleteAll();
  124. // Get an array of all keys
  125. CScriptArray *GetKeys() const;
  126. // Garbage collections behaviours
  127. int GetRefCount();
  128. void SetGCFlag();
  129. bool GetGCFlag();
  130. void EnumReferences(asIScriptEngine *engine);
  131. void ReleaseAllReferences(asIScriptEngine *engine);
  132. protected:
  133. // The structure for holding the values
  134. struct valueStruct
  135. {
  136. union
  137. {
  138. asINT64 valueInt;
  139. double valueFlt;
  140. void *valueObj;
  141. };
  142. int typeId;
  143. };
  144. // We don't want anyone to call the destructor directly, it should be called through the Release method
  145. virtual ~CScriptDictionary();
  146. // Helper methods
  147. void FreeValue(valueStruct &value);
  148. // Our properties
  149. asIScriptEngine *engine;
  150. mutable int refCount;
  151. mutable bool gcFlag;
  152. HashMap<String, valueStruct> dict;
  153. };
  154. /// Register the array type to script.
  155. void RegisterArray(asIScriptEngine* engine);
  156. /// Register the dictionary type to script.
  157. void RegisterDictionary(asIScriptEngine* engine);
  158. /// Register String to script.
  159. void RegisterString(asIScriptEngine* engine);
  160. }