Addons.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <angelscript.h>
  25. // Adapted from Angelscript's scriptarray & scriptstdstring add-ons, but with garbage collection disabled
  26. struct SArrayBuffer;
  27. struct SArrayCache;
  28. /// %Script array class.
  29. class CScriptArray
  30. {
  31. public:
  32. CScriptArray(asUINT length, asIObjectType *ot);
  33. CScriptArray(asUINT length, void *defVal, asIObjectType *ot);
  34. virtual ~CScriptArray();
  35. void AddRef() const;
  36. void Release() const;
  37. // Type information
  38. asIObjectType *GetArrayObjectType() const;
  39. int GetArrayTypeId() const;
  40. int GetElementTypeId() const;
  41. void Reserve(asUINT maxElements);
  42. void Resize(asUINT numElements);
  43. asUINT GetSize() const;
  44. bool IsEmpty() const;
  45. // Get a pointer to an element. Returns 0 if out of bounds
  46. void *At(asUINT index);
  47. const void *At(asUINT index) const;
  48. // Set value of an element
  49. void SetValue(asUINT index, void *value);
  50. CScriptArray &operator=(const CScriptArray&);
  51. bool operator==(const CScriptArray &) const;
  52. void InsertAt(asUINT index, void *value);
  53. void RemoveAt(asUINT index);
  54. void InsertLast(void *value);
  55. void RemoveLast();
  56. void SortAsc();
  57. void SortDesc();
  58. void SortAsc(asUINT index, asUINT count);
  59. void SortDesc(asUINT index, asUINT count);
  60. void Sort(asUINT index, asUINT count, bool asc);
  61. void Reverse();
  62. int Find(void *value) const;
  63. int Find(asUINT index, void *value) const;
  64. // GC methods
  65. int GetRefCount();
  66. void SetFlag();
  67. bool GetFlag();
  68. void EnumReferences(asIScriptEngine *engine);
  69. void ReleaseAllHandles(asIScriptEngine *engine);
  70. protected:
  71. mutable int refCount;
  72. mutable bool gcFlag;
  73. asIObjectType *objType;
  74. SArrayBuffer *buffer;
  75. int elementSize;
  76. int subTypeId;
  77. bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx);
  78. void *GetArrayItemPointer(int index);
  79. void *GetDataPointer(void *buffer);
  80. void Copy(void *dst, void *src);
  81. void Precache();
  82. bool CheckMaxSize(asUINT numElements);
  83. void Resize(int delta, asUINT at);
  84. void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
  85. void DeleteBuffer(SArrayBuffer *buf);
  86. void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
  87. void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
  88. void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
  89. bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
  90. };
  91. /// Register the array type to script.
  92. void RegisterArray(asIScriptEngine* engine);
  93. /// Register String to script.
  94. void RegisterString(asIScriptEngine* engine);