OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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  enum { MaxSize = 1000000 }; // ye shalt not create arrays larger than that!
27 
28  C4ValueArray();
29  C4ValueArray(int32_t inSize);
30  C4ValueArray(const C4ValueArray &);
31 
32  ~C4ValueArray();
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;
86  int32_t iSize, iCapacity;
87  bool constant; // if true, this array is not changeable
88 };
89 
90 #endif
91 
void SetItem(int32_t iElemNr, const C4Value &Value)
bool SortByArrayElement(int32_t array_idx, bool descending=false)
void Denumerate(C4ValueNumbers *)
void SortStrings()
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
bool SortByProperty(C4String *prop_name, bool descending=false)
void Freeze()
Definition: C4ValueArray.h:61
void SetSize(int32_t inSize)
void SetSlice(int32_t startIndex, int32_t endIndex, const C4Value &Val)
int32_t GetSize() const
Definition: C4ValueArray.h:36
void Sort(class C4SortObject &rSort)
const C4Value C4VNull
Definition: C4Value.cpp:32
const C4Value & _GetItem(int32_t iElem) const
Definition: C4ValueArray.h:48
bool IsFrozen() const
Definition: C4ValueArray.h:63
C4ValueArray & operator=(const C4ValueArray &)
void CompileFunc(class StdCompiler *pComp, C4ValueNumbers *)
C4ValueArray * GetSlice(int32_t startIndex, int32_t endIndex)
bool operator==(const C4ValueArray &) const
C4Value operator[](int32_t iElem) const
Definition: C4ValueArray.h:53