Addons.h 4.0 KB

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