Addons.h 3.9 KB

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