RegisterArray.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #ifndef SCRIPT_REGISTERARRAY_H
  24. #define SCRIPT_REGISTERARRAY_H
  25. #include <angelscript.h>
  26. // Adapted from Angelscript's scriptarray add-on
  27. struct SArrayBuffer;
  28. //! A script array type
  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 Resize(asUINT numElements);
  42. asUINT GetSize() const;
  43. // Get a pointer to an element. Returns 0 if out of bounds
  44. void* At(asUINT index);
  45. CScriptArray& operator = (const CScriptArray&);
  46. // TODO: Add methods Sort, Reverse, Find, etc
  47. void InsertAt(asUINT index, void *value);
  48. void RemoveAt(asUINT index);
  49. void InsertLast(void *value);
  50. void RemoveLast();
  51. // GC methods
  52. int GetRefCount();
  53. void SetFlag();
  54. bool GetFlag();
  55. void EnumReferences(asIScriptEngine* engine);
  56. void ReleaseAllHandles(asIScriptEngine* engine);
  57. protected:
  58. mutable int refCount;
  59. mutable bool gcFlag;
  60. asIObjectType* objType;
  61. SArrayBuffer* buffer;
  62. bool isArrayOfHandles;
  63. int elementSize;
  64. bool CheckMaxSize(asUINT numElements);
  65. void Resize(int delta, asUINT at);
  66. void SetValue(asUINT index, void *value);
  67. void CreateBuffer(SArrayBuffer** buf, asUINT numElements);
  68. void DeleteBuffer(SArrayBuffer* buf);
  69. void CopyBuffer(SArrayBuffer* dst, SArrayBuffer* src);
  70. void Construct(SArrayBuffer* buf, asUINT start, asUINT end);
  71. void Destruct(SArrayBuffer* buf, asUINT start, asUINT end);
  72. };
  73. //! Register the array type to script
  74. void registerArray(asIScriptEngine* engine);
  75. #endif // SCRIPT_REGISTERARRAY_H