OpenClonk
C4ValueArray.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 #include "script/C4Value.h"
18 
19 #ifndef INC_C4ValueList
20 #define INC_C4ValueList
21 
22 // reference counted array of C4Values
23 class C4ValueArray: public C4RefCnt
24 {
25 public:
26  static const int MaxSize = 1000000; // ye shalt not create arrays larger than that!
27 
29  C4ValueArray(int32_t inSize);
30  C4ValueArray(const C4ValueArray &);
31 
32  ~C4ValueArray() override;
33 
35 
36  int32_t GetSize() const { return iSize; }
37 
38  const C4Value &GetItem(int32_t iElem) const
39  {
40  if (-iSize <= iElem && iElem < 0)
41  return pData[iSize + iElem];
42  else if (0 <= iElem && iElem < iSize)
43  return pData[iElem];
44  else
45  return C4VNull;
46  }
47 
48  const C4Value &_GetItem(int32_t iElem) const // unchecked access; not auto-increasing array
49  {
50  return pData[iElem];
51  }
52 
53  C4Value operator[](int32_t iElem) const { return GetItem(iElem); }
54  C4Value &operator[](int32_t iElem); // interface for the engine, asserts that 0 <= index < MaxSize
55 
56  void Reset();
57  void SetItem(int32_t iElemNr, const C4Value &Value); // interface for script
58  void SetSize(int32_t inSize); // (enlarge only!)
59 
60  // for arrays declared in script constants
61  void Freeze() { constant = true; }
62  void Thaw() { constant = false; }
63  bool IsFrozen() const { return constant; }
64 
65  void Denumerate(C4ValueNumbers *);
66 
67  // comparison
68  bool operator==(const C4ValueArray&) const;
69 
70  // Compilation
71  void CompileFunc(class StdCompiler *pComp, C4ValueNumbers *);
72 
73  // Return sub-array [startIndex, endIndex). Throws C4AulExecError.
74  C4ValueArray * GetSlice(int32_t startIndex, int32_t endIndex);
75  // Sets sub-array [startIndex, endIndex). Might resize the array.
76  void SetSlice(int32_t startIndex, int32_t endIndex, const C4Value &Val);
77 
78  void Sort(class C4SortObject &rSort); // assume array of objects and sort by object sorting function
79  void SortStrings(); // sort by values as strings
80  void Sort(bool descending=false); // sort by values as integers or strings
81  bool SortByProperty(C4String *prop_name, bool descending=false); // checks that this is an array of all proplists and sorts by values of given property. return false if an element is not a proplist.
82  bool SortByArrayElement(int32_t array_idx, bool descending=false); // checks that this is an array of all arrays and sorts by array elements at index. returns false if an element is not an array or smaller than array_idx+1
83 
84 private:
85  C4Value* pData{nullptr};
86  int32_t iSize{0}, iCapacity{0};
87  bool constant{false}; // if true, this array is not changeable
88 };
89 
90 #endif
91 
const C4Value C4VNull
Definition: C4Value.cpp:30
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
void Denumerate(C4ValueNumbers *)
bool SortByProperty(C4String *prop_name, bool descending=false)
~C4ValueArray() override
void Sort(class C4SortObject &rSort)
C4ValueArray & operator=(const C4ValueArray &)
const C4Value & _GetItem(int32_t iElem) const
Definition: C4ValueArray.h:48
bool IsFrozen() const
Definition: C4ValueArray.h:63
void CompileFunc(class StdCompiler *pComp, C4ValueNumbers *)
void SetSlice(int32_t startIndex, int32_t endIndex, const C4Value &Val)
void SetSize(int32_t inSize)
C4Value operator[](int32_t iElem) const
Definition: C4ValueArray.h:53
void SetItem(int32_t iElemNr, const C4Value &Value)
C4ValueArray * GetSlice(int32_t startIndex, int32_t endIndex)
bool SortByArrayElement(int32_t array_idx, bool descending=false)
void SortStrings()
void Freeze()
Definition: C4ValueArray.h:61
static const int MaxSize
Definition: C4ValueArray.h:26
int32_t GetSize() const
Definition: C4ValueArray.h:36
bool operator==(const C4ValueArray &) const