OpenClonk
C4ValueMapData Class Reference

#include <C4ValueMap.h>

Collaboration diagram for C4ValueMapData:
[legend]

Public Member Functions

 C4ValueMapData ()
 
 C4ValueMapData (const C4ValueMapData &DataToCopy)
 
virtual ~C4ValueMapData ()
 
C4ValueMapDataoperator= (const C4ValueMapData &DataToCopy)
 
bool operator== (const C4ValueMapData &Data) const
 
C4ValueGetItem (const char *strName)
 
C4ValueGetItem (int32_t iNr)
 
void SetNameList (C4ValueMapNames *pnNames)
 
C4ValueMapNamesCreateTempNameList ()
 
void Reset ()
 
void ResetContent ()
 
int32_t GetAnzItems ()
 
C4Valueoperator[] (int32_t iNr)
 
C4Valueoperator[] (const char *strName)
 
void Denumerate (C4ValueNumbers *)
 
void CompileFunc (StdCompiler *pComp, C4ValueNumbers *)
 

Public Attributes

C4ValuepData {nullptr}
 
C4ValueMapNamespNames {nullptr}
 
bool bTempNameList {false}
 

Friends

class C4ValueMapNames
 

Detailed Description

Definition at line 27 of file C4ValueMap.h.

Constructor & Destructor Documentation

◆ C4ValueMapData() [1/2]

C4ValueMapData::C4ValueMapData ( )
default

◆ C4ValueMapData() [2/2]

C4ValueMapData::C4ValueMapData ( const C4ValueMapData DataToCopy)

Definition at line 26 of file C4ValueMap.cpp.

27 {
28  SetNameList(DataToCopy.pNames);
29  if (pNames) for (int32_t i = 0; i < pNames->iSize; i++)
30  pData[i].Set(DataToCopy.pData[i]);
31 }
C4ValueMapNames * pNames
Definition: C4ValueMap.h:44
C4Value * pData
Definition: C4ValueMap.h:41
void SetNameList(C4ValueMapNames *pnNames)
Definition: C4ValueMap.cpp:78

References C4ValueMapNames::iSize, pData, pNames, and SetNameList().

Here is the call graph for this function:

◆ ~C4ValueMapData()

C4ValueMapData::~C4ValueMapData ( )
virtual

Definition at line 51 of file C4ValueMap.cpp.

52 {
53  Reset();
54 }

References Reset().

Here is the call graph for this function:

Member Function Documentation

◆ CompileFunc()

void C4ValueMapData::CompileFunc ( StdCompiler pComp,
C4ValueNumbers numbers 
)

Definition at line 261 of file C4ValueMap.cpp.

262 {
263  bool deserializing = pComp->isDeserializer();
264  C4ValueMapNames *pOldNames = pNames;
265  if (deserializing) Reset();
266  // Compile item count
267  int32_t iValueCnt;
268  if (!deserializing) iValueCnt = pNames ? pNames->iSize : 0;
269  pComp->Value(mkDefaultAdapt(iValueCnt, 0));
270  // nuthing 2do for no items
271  if (!iValueCnt) return;
272  // Separator (';')
274  // Data
275  char **ppNames = !deserializing ? pNames->pNames : new char * [iValueCnt];
276  if (deserializing) for (int32_t i = 0; i < iValueCnt; i++) ppNames[i] = nullptr;
277  C4Value *pValues = !deserializing ? pData : new C4Value [iValueCnt];
278  // Compile
279  try
280  {
281  for (int32_t i = 0; i < iValueCnt; i++)
282  {
283  // Separate
284  if (i) pComp->Separator();
285  // Name
286  StdStrBuf Name;
287  if (!deserializing) Name.Ref(ppNames[i]);
288  pComp->Value(mkParAdapt(Name, StdCompiler::RCT_Idtf));
289  if (deserializing) ppNames[i] = Name.GrabPointer();
290  // Separator ('=')
292  // Value
293  pComp->Value(mkParAdapt(pValues[i], numbers));
294  }
295  }
296  catch (...)
297  {
298  // make sure no mem is leaked on compiler error in name list
299  if (deserializing)
300  {
301  for (int32_t i = 0; i < iValueCnt; i++) if (ppNames[i]) free(ppNames[i]);
302  delete [] ppNames;
303  delete [] pValues;
304  }
305  throw;
306  }
307  // Set
308  if (deserializing)
309  {
310  // Set
312  pNames->SetNameArray(const_cast<const char **>(ppNames), iValueCnt);
313  for (int32_t i = 0; i < iValueCnt; i++) free(ppNames[i]);
314  delete [] ppNames; delete [] pData;
315  pData = pValues;
316  // Assign old name list
317  if (pOldNames) SetNameList(pOldNames);
318  }
319 }
StdDefaultAdapt< T, D > mkDefaultAdapt(T &&rValue, const D &rDefault)
Definition: StdAdaptors.h:64
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:490
C4ValueMapNames * CreateTempNameList()
Definition: C4ValueMap.cpp:146
char ** pNames
Definition: C4ValueMap.h:106
void SetNameArray(const char **pnNames, int32_t nSize)
Definition: C4ValueMap.cpp:423
virtual bool Separator(Sep eSep=SEP_SEP)
Definition: StdCompiler.h:119
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
void Ref(const char *pnData)
Definition: StdBuf.h:455
char * GrabPointer()
Definition: StdBuf.h:459

References CreateTempNameList(), StdStrBuf::GrabPointer(), StdCompiler::isDeserializer(), C4ValueMapNames::iSize, mkDefaultAdapt(), mkParAdapt(), pData, pNames, C4ValueMapNames::pNames, StdCompiler::RCT_Idtf, StdStrBuf::Ref(), Reset(), StdCompiler::SEP_SEP2, StdCompiler::SEP_SET, StdCompiler::Separator(), C4ValueMapNames::SetNameArray(), SetNameList(), and StdCompiler::Value().

Here is the call graph for this function:

◆ CreateTempNameList()

C4ValueMapNames * C4ValueMapData::CreateTempNameList ( )

Definition at line 146 of file C4ValueMap.cpp.

147 {
148  // create new list
149  C4ValueMapNames *pTempNames = new C4ValueMapNames();
150 
151  // register (this func will unreg if necessary, too)
152  Register(pTempNames);
153 
154  // error?
155  if (pNames != pTempNames)
156  {
157  delete pTempNames;
158  return nullptr;
159  }
160 
161  // set flag
162  bTempNameList = true;
163 
164  return pTempNames;
165 }
bool bTempNameList
Definition: C4ValueMap.h:48
friend class C4ValueMapNames
Definition: C4ValueMap.h:29

References bTempNameList, C4ValueMapNames, and pNames.

Referenced by CompileFunc().

Here is the caller graph for this function:

◆ Denumerate()

void C4ValueMapData::Denumerate ( C4ValueNumbers numbers)

Definition at line 254 of file C4ValueMap.cpp.

255 {
256  if (!pNames) return;
257  for (int32_t i = 0; i < pNames->iSize; i++)
258  pData[i].Denumerate(numbers);
259 }
void Denumerate(C4ValueNumbers *)
Definition: C4ValueMap.cpp:254

References C4ValueMapNames::iSize, pData, and pNames.

Referenced by C4ObjectInfoCore::CompileFunc(), C4PlayerInfoCore::CompileFunc(), and C4AulScriptEngine::Denumerate().

Here is the caller graph for this function:

◆ GetAnzItems()

int32_t C4ValueMapData::GetAnzItems ( )

Definition at line 248 of file C4ValueMap.cpp.

249 {
250  if (!pNames) return 0;
251  return pNames->iSize;
252 }

References C4ValueMapNames::iSize, and pNames.

◆ GetItem() [1/2]

C4Value * C4ValueMapData::GetItem ( const char *  strName)

Definition at line 235 of file C4ValueMap.cpp.

236 {
237  assert(pNames);
238  if (!pNames) return nullptr;
239 
240  int32_t iNr = pNames->GetItemNr(strName);
241  assert(iNr != -1);
242 
243  if (iNr == -1) return nullptr;
244 
245  return &pData[iNr];
246 }
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459

References C4ValueMapNames::GetItemNr(), pData, and pNames.

Referenced by operator[]().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetItem() [2/2]

C4Value * C4ValueMapData::GetItem ( int32_t  iNr)

Definition at line 222 of file C4ValueMap.cpp.

223 {
224  assert(pNames);
225  assert(iNr < pNames->iSize);
226  assert(iNr >= 0);
227  // the list is nothing without name list...
228  if (!pNames) return nullptr;
229 
230  if (iNr >= pNames->iSize) return nullptr;
231 
232  return &pData[iNr];
233 }
int iSize
Definition: TstC4NetIO.cpp:32

References iSize, C4ValueMapNames::iSize, pData, and pNames.

◆ operator=()

C4ValueMapData & C4ValueMapData::operator= ( const C4ValueMapData DataToCopy)

Definition at line 33 of file C4ValueMap.cpp.

34 {
35  SetNameList(DataToCopy.pNames);
36  if (pNames) for (int32_t i = 0; i < pNames->iSize; i++)
37  pData[i].Set(DataToCopy.pData[i]);
38  return *this;
39 }

References C4ValueMapNames::iSize, pData, pNames, and SetNameList().

Here is the call graph for this function:

◆ operator==()

bool C4ValueMapData::operator== ( const C4ValueMapData Data) const

Definition at line 41 of file C4ValueMap.cpp.

42 {
43  if (pNames != Data.pNames) return false;
44  if (pNames)
45  for (int i = 0; i < pNames->iSize; i++)
46  if (pData[i] != Data.pData[i])
47  return false;
48  return true;
49 }

References C4ValueMapNames::iSize, pData, and pNames.

◆ operator[]() [1/2]

C4Value& C4ValueMapData::operator[] ( const char *  strName)
inline

Definition at line 68 of file C4ValueMap.h.

68 { return *GetItem(strName); }
C4Value * GetItem(const char *strName)
Definition: C4ValueMap.cpp:235

References GetItem().

Here is the call graph for this function:

◆ operator[]() [2/2]

C4Value& C4ValueMapData::operator[] ( int32_t  iNr)
inline

Definition at line 67 of file C4ValueMap.h.

67 { return *GetItem(iNr); }

References GetItem().

Here is the call graph for this function:

◆ Reset()

void C4ValueMapData::Reset ( )

Definition at line 56 of file C4ValueMap.cpp.

57 {
58  // unreg from name list (if using one)
59  if (pNames) UnRegister();
60  pNames = nullptr;
61  // free data
62  delete[] pData;
63  pData = nullptr;
64 }

References pData, and pNames.

Referenced by C4AulScriptEngine::Clear(), CompileFunc(), C4ObjectInfoCore::Default(), C4PlayerInfoCore::Default(), and ~C4ValueMapData().

Here is the caller graph for this function:

◆ ResetContent()

void C4ValueMapData::ResetContent ( )

Definition at line 66 of file C4ValueMap.cpp.

67 {
68  if (pNames)
69  // Realloc list (will destroy all data)
70  ReAllocList();
71  else
72  {
73  delete[] pData;
74  pData = nullptr;
75  }
76 }

References pData, and pNames.

◆ SetNameList()

void C4ValueMapData::SetNameList ( C4ValueMapNames pnNames)

Definition at line 78 of file C4ValueMap.cpp.

79 {
80  if (pNames == pnNames) return;
81  if (pNames)
82  {
83  // save name array from old name list
84  char **pOldNames = pNames->pNames;
85  int32_t iOldSize = pNames->iSize;
86 
87  // unreg from old name list
88  // (manually, because Data::UnRegister() would destroy content)
89  C4ValueMapNames *pNames = this->pNames;
90 
91  pNames->UnRegister(this);
92 
93  // register at new name list
94  pnNames->Register(this);
95 
96  // call OnNameListChanged to copy data and realloc data array
97  OnNameListChanged(const_cast<const char **>(pOldNames), iOldSize);
98 
99  // delete old names list, if it is temporary
100  if (bTempNameList)
101  delete pNames;
102  bTempNameList = false;
103 
104  // ok
105  }
106  else
107  {
108  // simply register...
109  Register(pnNames);
110  }
111 }

References bTempNameList, C4ValueMapNames::iSize, pNames, and C4ValueMapNames::pNames.

Referenced by C4ValueMapData(), C4AulScriptEngine::Clear(), C4AulScriptEngine::CompileFunc(), CompileFunc(), C4AulScriptEngine::Link(), and operator=().

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ C4ValueMapNames

friend class C4ValueMapNames
friend

Definition at line 29 of file C4ValueMap.h.

Referenced by CreateTempNameList().

Member Data Documentation

◆ bTempNameList

bool C4ValueMapData::bTempNameList {false}

Definition at line 48 of file C4ValueMap.h.

Referenced by CreateTempNameList(), and SetNameList().

◆ pData

C4Value* C4ValueMapData::pData {nullptr}

◆ pNames


The documentation for this class was generated from the following files: