Addons.h 3.9 KB

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