OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ValueMap.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) 2013-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 #ifndef INC_C4ValueMap2
18 #define INC_C4ValueMap2
19 
20 // implements a list of C4Values associated with a name list.
21 // the list is split in the two components (data/names) to make it possible
22 // to have multiple data lists using a single name list
23 
24 class C4ValueMapNames;
25 
26 // the data list
28 {
29  friend class C4ValueMapNames;
30 public:
31 
32  // construction/destruction
34  C4ValueMapData(const C4ValueMapData &DataToCopy);
35  virtual ~C4ValueMapData();
36  C4ValueMapData& operator = (const C4ValueMapData &DataToCopy);
37 
38  bool operator == (const C4ValueMapData &Data) const;
39 
40  // data array
41  C4Value *pData{nullptr};
42 
43  // pointer on name list
45 
46  // using temporary name list?
47  // (delete when changing list)
48  bool bTempNameList{false};
49 
50 
51  // returns the item specified or 0 if it doesn't exist
52  C4Value *GetItem(const char* strName);
53  C4Value *GetItem(int32_t iNr);
54 
55 
56  // sets the name list
57  void SetNameList(C4ValueMapNames *pnNames);
58 
59  // creates a new (empty) temporary name list for this data class
61 
62  void Reset(); // resets content & unreg name list
63  void ResetContent(); // resets content
64 
65  int32_t GetAnzItems();
66 
67  C4Value& operator [] (int32_t iNr) { return *GetItem(iNr); }
68  C4Value& operator [] (const char *strName) { return *GetItem(strName); }
69 
70  void Denumerate(C4ValueNumbers *);
71 
72  void CompileFunc(StdCompiler *pComp, C4ValueNumbers *);
73 
74 private:
75 
76  // a list linking all data lists using the same name list together.
77  C4ValueMapData *pNext{nullptr};
78 
79  void Register(C4ValueMapNames *pnNames);
80  void UnRegister();
81 
82  // called by names list to tell us that the name list
83  // was changed and from SetNameList (the data list has to be rordered...)
84  void OnNameListChanged(const char **pOldNames, int32_t iOldSize);
85 
86  // (Re)Allocs data list
87  // old data will be deleted!
88  // (size taken from pNames->iSize)
89  void ReAllocList();
90 };
91 
92 // the names list
94 {
95  friend class C4ValueMapData;
96 public:
97 
98  // construction/destruction
100  C4ValueMapNames(const C4ValueMapNames& NamesToCopy);
101  C4ValueMapNames& operator = (const C4ValueMapNames &NamesToCopy);
102  virtual ~C4ValueMapNames();
103 
104 
105  // name array
106  char **pNames{nullptr};
107 
108  // item count
109  int32_t iSize{0};
110 
111 
112  // set name array
113  void SetNameArray(const char **pnNames, int32_t nSize);
114 
115  // add a name; return index of added element (or index of current if it's already in the list)
116  int32_t AddName(const char *pnName);
117 
118  // returns the nr of the given name
119  // (= nr of value in "child" data lists)
120  // returns -1 if no item with given name exists
121  int32_t GetItemNr(const char *strName) const;
122 
123  // get name by index; awway bounds not checked
124  const char *GetItemUnsafe(int32_t idx) const { return pNames[idx]; }
125 
126  void Reset();
127 
128 private:
129 
130  // points to first data list using this name list
131  C4ValueMapData *pFirst{nullptr};
132 
133  void Register(C4ValueMapData *pData);
134  void UnRegister(C4ValueMapData *pData);
135 
136  // changes the name list
137  void ChangeNameList(const char **pnNames, int32_t nSize);
138 };
139 
140 #endif
C4Value * GetItem(const char *strName)
Definition: C4ValueMap.cpp:235
bool bTempNameList
Definition: C4ValueMap.h:48
const char * GetItemUnsafe(int32_t idx) const
Definition: C4ValueMap.h:124
virtual ~C4ValueMapNames()
Definition: C4ValueMap.cpp:336
void Denumerate(C4ValueNumbers *)
Definition: C4ValueMap.cpp:254
C4Value & operator[](int32_t iNr)
Definition: C4ValueMap.h:67
int32_t AddName(const char *pnName)
Definition: C4ValueMap.cpp:429
void SetNameList(C4ValueMapNames *pnNames)
Definition: C4ValueMap.cpp:78
void ResetContent()
Definition: C4ValueMap.cpp:66
bool operator==(const C4ValueMapData &Data) const
Definition: C4ValueMap.cpp:41
void SetNameArray(const char **pnNames, int32_t nSize)
Definition: C4ValueMap.cpp:423
virtual ~C4ValueMapData()
Definition: C4ValueMap.cpp:51
C4ValueMapNames * pNames
Definition: C4ValueMap.h:44
C4ValueMapData & operator=(const C4ValueMapData &DataToCopy)
Definition: C4ValueMap.cpp:33
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459
C4ValueMapNames * CreateTempNameList()
Definition: C4ValueMap.cpp:146
int32_t GetAnzItems()
Definition: C4ValueMap.cpp:248
C4ValueMapNames & operator=(const C4ValueMapNames &NamesToCopy)
Definition: C4ValueMap.cpp:330
char ** pNames
Definition: C4ValueMap.h:106
void CompileFunc(StdCompiler *pComp, C4ValueNumbers *)
Definition: C4ValueMap.cpp:261
C4Value * pData
Definition: C4ValueMap.h:41