Addons.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  27. {
  28. struct SArrayBuffer;
  29. struct SArrayCache;
  30. /// %Script array class.
  31. class CScriptArray
  32. {
  33. public:
  34. CScriptArray(asUINT length, asIObjectType *ot);
  35. CScriptArray(asUINT length, void *defVal, asIObjectType *ot);
  36. virtual ~CScriptArray();
  37. void AddRef() const;
  38. void Release() const;
  39. // Type information
  40. asIObjectType *GetArrayObjectType() const;
  41. int GetArrayTypeId() const;
  42. int GetElementTypeId() const;
  43. void Reserve(asUINT maxElements);
  44. void Resize(asUINT numElements);
  45. asUINT GetSize() const;
  46. bool IsEmpty() const;
  47. // Get a pointer to an element. Returns 0 if out of bounds
  48. void *At(asUINT index);
  49. const void *At(asUINT index) const;
  50. // Set value of an element
  51. void SetValue(asUINT index, void *value);
  52. CScriptArray &operator=(const CScriptArray&);
  53. bool operator==(const CScriptArray &) const;
  54. void InsertAt(asUINT index, void *value);
  55. void RemoveAt(asUINT index);
  56. void InsertLast(void *value);
  57. void RemoveLast();
  58. void SortAsc();
  59. void SortDesc();
  60. void SortAsc(asUINT index, asUINT count);
  61. void SortDesc(asUINT index, asUINT count);
  62. void Sort(asUINT index, asUINT count, bool asc);
  63. void Reverse();
  64. int Find(void *value) const;
  65. int Find(asUINT index, void *value) const;
  66. // GC methods
  67. int GetRefCount();
  68. void SetFlag();
  69. bool GetFlag();
  70. void EnumReferences(asIScriptEngine *engine);
  71. void ReleaseAllHandles(asIScriptEngine *engine);
  72. protected:
  73. mutable int refCount;
  74. mutable bool gcFlag;
  75. asIObjectType *objType;
  76. SArrayBuffer *buffer;
  77. int elementSize;
  78. int subTypeId;
  79. bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx);
  80. void *GetArrayItemPointer(int index);
  81. void *GetDataPointer(void *buffer);
  82. void Copy(void *dst, void *src);
  83. void Precache();
  84. bool CheckMaxSize(asUINT numElements);
  85. void Resize(int delta, asUINT at);
  86. void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
  87. void DeleteBuffer(SArrayBuffer *buf);
  88. void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
  89. void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
  90. void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
  91. bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
  92. };
  93. /// Register the array type to script.
  94. void RegisterArray(asIScriptEngine* engine);
  95. /// Register String to script.
  96. void RegisterString(asIScriptEngine* engine);
  97. }