Addons.h 6.0 KB

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