SuperTuxKart
scriptarray.hpp
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2017 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 #ifndef SCRIPTARRAY_H
32 #define SCRIPTARRAY_H
33 
34 #ifndef ANGELSCRIPT_H
35 // Avoid having to inform include path if header is already include before
36 #include <angelscript.h>
37 #endif
38 
39 // Sometimes it may be desired to use the same method names as used by C++ STL.
40 // This may for example reduce time when converting code from script to C++ or
41 // back.
42 //
43 // 0 = off
44 // 1 = on
45 
46 #ifndef AS_USE_STLNAMES
47 #define AS_USE_STLNAMES 0
48 #endif
49 
50 BEGIN_AS_NAMESPACE
51 
52 struct SArrayBuffer;
53 struct SArrayCache;
54 
56 {
57 public:
58  // Set the memory functions that should be used by all CScriptArrays
59  static void SetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
60 
61  // Factory functions
62  static CScriptArray *Create(asITypeInfo *ot);
63  static CScriptArray *Create(asITypeInfo *ot, asUINT length);
64  static CScriptArray *Create(asITypeInfo *ot, asUINT length, void *defaultValue);
65  static CScriptArray *Create(asITypeInfo *ot, void *listBuffer);
66 
67  // Memory management
68  void AddRef() const;
69  void Release() const;
70 
71  // Type information
72  asITypeInfo *GetArrayObjectType() const;
73  int GetArrayTypeId() const;
74  int GetElementTypeId() const;
75 
76  // Get the current size
77  asUINT GetSize() const;
78 
79  // Returns true if the array is empty
80  bool IsEmpty() const;
81 
82  // Pre-allocates memory for elements
83  void Reserve(asUINT maxElements);
84 
85  // Resize the array
86  void Resize(asUINT numElements);
87 
88  // Get a pointer to an element. Returns 0 if out of bounds
89  void *At(asUINT index);
90  const void *At(asUINT index) const;
91 
92  // Set value of an element.
93  // The value arg should be a pointer to the value that will be copied to the element.
94  // Remember, if the array holds handles the value parameter should be the
95  // address of the handle. The refCount of the object will also be incremented
96  void SetValue(asUINT index, void *value);
97 
98  // Copy the contents of one array to another (only if the types are the same)
99  CScriptArray &operator=(const CScriptArray&);
100 
101  // Compare two arrays
102  bool operator==(const CScriptArray &) const;
103 
104  // Array manipulation
105  void InsertAt(asUINT index, void *value);
106  void InsertAt(asUINT index, const CScriptArray &arr);
107  void InsertLast(void *value);
108  void RemoveAt(asUINT index);
109  void RemoveLast();
110  void RemoveRange(asUINT start, asUINT count);
111  void SortAsc();
112  void SortDesc();
113  void SortAsc(asUINT startAt, asUINT count);
114  void SortDesc(asUINT startAt, asUINT count);
115  void Sort(asUINT startAt, asUINT count, bool asc);
116  void Sort(asIScriptFunction *less, asUINT startAt, asUINT count);
117  void Reverse();
118  int Find(void *value) const;
119  int Find(asUINT startAt, void *value) const;
120  int FindByRef(void *ref) const;
121  int FindByRef(asUINT startAt, void *ref) const;
122 
123  // Return the address of internal buffer for direct manipulation of elements
124  void *GetBuffer();
125 
126  // GC methods
127  int GetRefCount();
128  void SetFlag();
129  bool GetFlag();
130  void EnumReferences(asIScriptEngine *engine);
131  void ReleaseAllHandles(asIScriptEngine *engine);
132 
133 protected:
134  mutable int refCount;
135  mutable bool gcFlag;
136  asITypeInfo *objType;
137  SArrayBuffer *buffer;
138  int elementSize;
139  int subTypeId;
140 
141  // Constructors
142  CScriptArray(asITypeInfo *ot, void *initBuf); // Called from script when initialized with list
143  CScriptArray(asUINT length, asITypeInfo *ot);
144  CScriptArray(asUINT length, void *defVal, asITypeInfo *ot);
145  CScriptArray(const CScriptArray &other);
146  virtual ~CScriptArray();
147 
148  bool Less(const void *a, const void *b, bool asc, asIScriptContext *ctx, SArrayCache *cache);
149  void *GetArrayItemPointer(int index);
150  void *GetDataPointer(void *buffer);
151  void Copy(void *dst, void *src);
152  void Precache();
153  bool CheckMaxSize(asUINT numElements);
154  void Resize(int delta, asUINT at);
155  void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
156  void DeleteBuffer(SArrayBuffer *buf);
157  void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
158  void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
159  void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
160  bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
161 };
162 
163 void RegisterScriptArray(asIScriptEngine *engine, bool defaultArray);
164 
165 END_AS_NAMESPACE
166 
167 #endif
Definition: scriptarray.hpp:56
Definition: scriptarray.cpp:64
Definition: scriptarray.cpp:71