ScummVM API documentation
scriptarray.h
1 #ifndef SCRIPTARRAY_H
2 #define SCRIPTARRAY_H
3 
4 #ifndef ANGELSCRIPT_H
5 // Avoid having to inform include path if header is already include before
6 #include "hpl1/engine/libraries/angelscript/angelscript.h"
7 #endif
8 
9 // Sometimes it may be desired to use the same method names as used by C++ STL.
10 // This may for example reduce time when converting code from script to C++ or
11 // back.
12 //
13 // 0 = off
14 // 1 = on
15 #ifndef AS_USE_STLNAMES
16 #define AS_USE_STLNAMES 0
17 #endif
18 
19 // Some prefer to use property accessors to get/set the length of the array
20 // This option registers the accessors instead of the method length()
21 #ifndef AS_USE_ACCESSORS
22 #define AS_USE_ACCESSORS 0
23 #endif
24 
25 BEGIN_AS_NAMESPACE
26 
27 struct SArrayBuffer;
28 struct SArrayCache;
29 
30 class CScriptArray {
31 public:
32  // Set the memory functions that should be used by all CScriptArrays
33  static void SetMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc);
34 
35  // Factory functions
36  static CScriptArray *Create(asITypeInfo *ot);
37  static CScriptArray *Create(asITypeInfo *ot, asUINT length);
38  static CScriptArray *Create(asITypeInfo *ot, asUINT length, void *defaultValue);
39  static CScriptArray *Create(asITypeInfo *ot, void *listBuffer);
40 
41  // Memory management
42  void AddRef() const;
43  void Release() const;
44 
45  // Type information
46  asITypeInfo *GetArrayObjectType() const;
47  int GetArrayTypeId() const;
48  int GetElementTypeId() const;
49 
50  // Get the current size
51  asUINT GetSize() const;
52 
53  // Returns true if the array is empty
54  bool IsEmpty() const;
55 
56  // Pre-allocates memory for elements
57  void Reserve(asUINT maxElements);
58 
59  // Resize the array
60  void Resize(asUINT numElements);
61 
62  // Get a pointer to an element. Returns 0 if out of bounds
63  void *At(asUINT index);
64  const void *At(asUINT index) const;
65 
66  // Set value of an element.
67  // The value arg should be a pointer to the value that will be copied to the element.
68  // Remember, if the array holds handles the value parameter should be the
69  // address of the handle. The refCount of the object will also be incremented
70  void SetValue(asUINT index, void *value);
71 
72  // Copy the contents of one array to another (only if the types are the same)
73  CScriptArray &operator=(const CScriptArray &);
74 
75  // Compare two arrays
76  bool operator==(const CScriptArray &) const;
77 
78  // Array manipulation
79  void InsertAt(asUINT index, void *value);
80  void InsertAt(asUINT index, const CScriptArray &arr);
81  void InsertLast(void *value);
82  void RemoveAt(asUINT index);
83  void RemoveLast();
84  void RemoveRange(asUINT start, asUINT count);
85  void SortAsc();
86  void SortDesc();
87  void SortAsc(asUINT startAt, asUINT count);
88  void SortDesc(asUINT startAt, asUINT count);
89  void Sort(asUINT startAt, asUINT count, bool asc);
90  void Sort(asIScriptFunction *less, asUINT startAt, asUINT count);
91  void Reverse();
92  int Find(void *value) const;
93  int Find(asUINT startAt, void *value) const;
94  int FindByRef(void *ref) const;
95  int FindByRef(asUINT startAt, void *ref) const;
96 
97  // Return the address of internal buffer for direct manipulation of elements
98  void *GetBuffer();
99 
100  // GC methods
101  int GetRefCount();
102  void SetFlag();
103  bool GetFlag();
104  void EnumReferences(asIScriptEngine *engine);
105  void ReleaseAllHandles(asIScriptEngine *engine);
106 
107 protected:
108  mutable int refCount;
109  mutable bool gcFlag;
110  asITypeInfo *objType;
111  SArrayBuffer *buffer;
112  int elementSize;
113  int subTypeId;
114 
115  // Constructors
116  CScriptArray(asITypeInfo *ot, void *initBuf); // Called from script when initialized with list
117  CScriptArray(asUINT length, asITypeInfo *ot);
118  CScriptArray(asUINT length, void *defVal, asITypeInfo *ot);
119  CScriptArray(const CScriptArray &other);
120  virtual ~CScriptArray();
121 
122  bool Less(const void *a, const void *b, bool asc);
123  void *GetArrayItemPointer(int index);
124  void *GetDataPointer(void *buffer);
125  void Copy(void *dst, void *src);
126  void Swap(void *a, void *b);
127  void Precache();
128  bool CheckMaxSize(asUINT numElements);
129  void Resize(int delta, asUINT at);
130  void CreateBuffer(SArrayBuffer **buf, asUINT numElements);
131  void DeleteBuffer(SArrayBuffer *buf);
132  void CopyBuffer(SArrayBuffer *dst, SArrayBuffer *src);
133  void Construct(SArrayBuffer *buf, asUINT start, asUINT end);
134  void Destruct(SArrayBuffer *buf, asUINT start, asUINT end);
135  bool Equals(const void *a, const void *b, asIScriptContext *ctx, SArrayCache *cache) const;
136 };
137 
138 void RegisterScriptArray(asIScriptEngine *engine, bool defaultArray);
139 
140 END_AS_NAMESPACE
141 
142 #endif
Definition: angelscript.h:1083
Definition: angelscript.h:863
Definition: scriptarray.h:30
Definition: angelscript.h:639
Definition: angelscript.h:1011