Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NamedVariableList Class Reference

hosts one or more VariableBlocks keeping only one name for all of them More...

#include <NamedVars.h>

Public Member Functions

DLLEXPORT NamedVariableList ()
 
DLLEXPORT NamedVariableList (const NamedVariableList &other)
 
DLLEXPORT NamedVariableList (const std::string &name)
 Creates an empty block with no values. More...
 
DLLEXPORT NamedVariableList (const std::string &name, VariableBlock *value1)
 
DLLEXPORT NamedVariableList (const std::string &name, const VariableBlock &val)
 
DLLEXPORT NamedVariableList (ScriptSafeVariableBlock *const data)
 
DLLEXPORT NamedVariableList (sf::Packet &packet)
 For receiving NamedVariableLists through the network. More...
 
DLLEXPORT void AddDataToPacket (sf::Packet &packet) const
 For passing NamedVariableLists to other instances through the network. More...
 
DLLEXPORT NamedVariableList (const std::string &name, std::vector< VariableBlock *> values_willclear)
 
DLLEXPORT NamedVariableList (const std::string &line, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined=NULL)
 
DLLEXPORT NamedVariableList (const std::string &name, const std::string &valuestr, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined=NULL)
 
DLLEXPORT bool ConstructValuesForObject (const std::string &variablestr, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined)
 Helper function for constructing values. More...
 
DLLEXPORT bool RecursiveParseList (std::vector< VariableBlock *> &resultvalues, std::unique_ptr< std::string > expression, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined)
 Handles a found bracket expression "[...]" parsing it recursively into values. More...
 
DLLEXPORT ~NamedVariableList ()
 
bool IsValid () const
 Returns true if this is valid. More...
 
DLLEXPORT void SetValue (const VariableBlock &value1)
 
DLLEXPORT void SetValue (VariableBlock *value1)
 
DLLEXPORT void SetValue (const std::vector< VariableBlock *> &values)
 
DLLEXPORT void SetValue (const int &nindex, const VariableBlock &valuetoset)
 
DLLEXPORT void SetValue (const int &nindex, VariableBlock *valuetoset)
 
DLLEXPORT void PushValue (std::unique_ptr< VariableBlock > &&value)
 Adds a new value. More...
 
DLLEXPORT VariableBlockGetValueDirect ()
 
DLLEXPORT VariableBlockGetValue ()
 
DLLEXPORT VariableBlockGetValueDirect (size_t nindex)
 
DLLEXPORT VariableBlockGetValue (size_t nindex)
 
DLLEXPORT std::vector< VariableBlock * > & GetValues ()
 
DLLEXPORT size_t GetVariableCount () const
 
DLLEXPORT int GetCommonType () const
 
template<class DBT >
bool CanAllBeCastedToType () const
 
template<class DBT >
bool CanAllBeCastedToType (const int &startindex, const int &endindex) const
 
DLLEXPORT int GetVariableType () const
 
DLLEXPORT int GetVariableType (const int &nindex) const
 
std::string GetName () const
 
DLLEXPORT void GetName (std::string &name) const
 
DLLEXPORT void SetName (const std::string &name)
 
DLLEXPORT bool CompareName (const std::string &name) const
 
DLLEXPORT std::string ToText (int WhichSeparator=0, bool AddAllBrackets=false) const
 
DLLEXPORT NamedVariableListoperator= (const NamedVariableList &other)
 
DLLEXPORT bool operator== (const NamedVariableList &other) const
 
DLLEXPORT bool operator!= (const NamedVariableList &other) const
 
DLLEXPORT VariableBlockoperator[] (const int &nindex)
 

Static Public Member Functions

static DLLEXPORT bool ProcessDataDump (const std::string &data, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined=nullptr)
 
static DLLEXPORT void SwitchValues (NamedVariableList &receiver, NamedVariableList &donator)
 Switches values to a new instance. More...
 

Detailed Description

hosts one or more VariableBlocks keeping only one name for all of them

Todo:

Make this reference counted

Make all methods throw exceptions on invalid operations

Definition at line 27 of file NamedVars.h.

Constructor & Destructor Documentation

◆ NamedVariableList() [1/10]

NamedVariableList::NamedVariableList ( )

Definition at line 12 of file NamedVars.cpp.

12 : Datas(0), Name("") {}

◆ NamedVariableList() [2/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const NamedVariableList other)

Definition at line 55 of file NamedVars.cpp.

55  :
56  Datas(other.Datas.size()), Name(other.Name)
57 {
58 
59  // copy value over //
60  for(size_t i = 0; i < other.Datas.size(); i++) {
61 
62  Datas[i] = new VariableBlock(*other.Datas[i]);
63  }
64 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425

◆ NamedVariableList() [3/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const std::string &  name)

Creates an empty block with no values.

Definition at line 14 of file NamedVars.cpp.

14  : Datas(0), Name(name)
15 {}

◆ NamedVariableList() [4/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const std::string &  name,
VariableBlock value1 
)

Definition at line 17 of file NamedVars.cpp.

18  :
19  Datas(1),
20  Name(name)
21 {
22  // set value //
23  Datas[0] = value1;
24 }

◆ NamedVariableList() [5/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const std::string &  name,
const VariableBlock val 
)

Definition at line 26 of file NamedVars.cpp.

27  :
28  Datas(1),
29  Name(name)
30 {
31  // set value //
32  Datas[0] = new VariableBlock(val);
33 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425

◆ NamedVariableList() [6/10]

DLLEXPORT NamedVariableList::NamedVariableList ( ScriptSafeVariableBlock *const  data)

Definition at line 36 of file NamedVars.cpp.

36  :
37  Datas(1), Name(data->GetName())
38 {
39  // Copy value //
40  Datas[0] = new VariableBlock(*data);
41 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
std::string GetName() const
Definition: DataBlock.h:813

◆ NamedVariableList() [7/10]

DLLEXPORT NamedVariableList::NamedVariableList ( sf::Packet &  packet)

For receiving NamedVariableLists through the network.

Definition at line 330 of file NamedVars.cpp.

331 {
332  // Unpack the data from the packet //
333  packet >> Name;
334 
335  // First get the size //
336  int tmpsize = 0;
337 
338  // Thousand is considered here the maximum number of elements //
339  if(!(packet >> tmpsize) || tmpsize > 1000 || tmpsize < 0) {
340 
341  throw InvalidArgument("invalid packet format");
342  }
343 
344  // Reserve enough space //
345  Datas.reserve((size_t)tmpsize);
346 
347  // Loop and get the data //
348  for(int i = 0; i < tmpsize; i++) {
349 
350  Datas.push_back(new VariableBlock(packet));
351  }
352 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425

◆ NamedVariableList() [8/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const std::string &  name,
std::vector< VariableBlock *>  values_willclear 
)
Warning
the vector will be wiped clean after creating new variable

Definition at line 44 of file NamedVars.cpp.

45  :
46  Datas(values_willclear.size()),
47  Name(name)
48 {
49  // set values //
50  for(size_t i = 0; i < values_willclear.size(); i++) {
51  Datas[i] = values_willclear[i];
52  }
53 }

◆ NamedVariableList() [9/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const std::string &  line,
LErrorReporter errorreport,
std::map< std::string, std::shared_ptr< VariableBlock >> *  predefined = NULL 
)

Definition at line 66 of file NamedVars.cpp.

69 {
70  // using StringIterator makes this shorter //
71  StringIterator itr(&line);
72 
73  auto name = itr.GetUntilEqualityAssignment<std::string>(EQUALITYCHARACTER_TYPE_ALL);
74 
75  if(!name) {
76  // no name //
77 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
78  errorreport->Error(std::string("invalid data on line (invalid name)"));
79  return;
80 #else
81  throw InvalidArgument("invalid data on line (invalid name)");
82 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
83  }
84 
85  Name = *name;
86 
87  // skip whitespace //
88  itr.SkipWhiteSpace();
89 
90  // get last part of it //
91  auto tempvar = itr.GetUntilNextCharacterOrAll<std::string>(L';');
92 
93  if(!tempvar || tempvar->size() < 1) {
94  // no variable //
95 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
96  Name = "";
97  errorreport->Error(std::string("invalid data on line (no variable data)"));
98  return;
99 #else
100  throw InvalidArgument("invalid data on line (no variable data)");
101 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
102  }
103 
104  if(!ConstructValuesForObject(*tempvar, errorreport, predefined)) {
105 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
106  Name = "";
107  errorreport->Error(std::string("invalid variable string, parsing failed"));
108  return;
109 #else
110  throw InvalidArgument("invalid variable string, parsing failed");
111 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
112  }
113 }
Iterator class for getting parts of a string.
virtual void Error(const std::string &Text)=0
DLLEXPORT bool ConstructValuesForObject(const std::string &variablestr, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined)
Helper function for constructing values.
Definition: NamedVars.cpp:235

◆ NamedVariableList() [10/10]

DLLEXPORT NamedVariableList::NamedVariableList ( const std::string &  name,
const std::string &  valuestr,
LErrorReporter errorreport,
std::map< std::string, std::shared_ptr< VariableBlock >> *  predefined = NULL 
)

Definition at line 115 of file NamedVars.cpp.

118 {
119  // We already have the name provided for us //
120  Name = name;
121 
122  // The value needs to be parsed //
123  if(!ConstructValuesForObject(valuestr, errorreport, predefined)) {
124 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
125  Name = "";
126  errorreport->Error(std::string("invalid variable string, parsing failed"));
127  return;
128 #else
129  throw InvalidArgument("invalid variable string, parsing failed");
130 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
131  }
132 }
virtual void Error(const std::string &Text)=0
DLLEXPORT bool ConstructValuesForObject(const std::string &variablestr, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined)
Helper function for constructing values.
Definition: NamedVars.cpp:235

◆ ~NamedVariableList()

DLLEXPORT NamedVariableList::~NamedVariableList ( )

Definition at line 383 of file NamedVars.cpp.

384 {
385 
386  SAFE_DELETE_VECTOR(Datas);
387 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

Member Function Documentation

◆ AddDataToPacket()

DLLEXPORT void NamedVariableList::AddDataToPacket ( sf::Packet &  packet) const

For passing NamedVariableLists to other instances through the network.

Definition at line 354 of file NamedVars.cpp.

355 {
356  // Start adding data to the packet //
357  packet << Name;
358 
359  // The vector passing //
360  int truncsize = (int)Datas.size();
361 
362  if(truncsize > 1000) {
363 
364  // That's an error //
365  Logger::Get()->Error("NamedVariableList: AddToPacket: too many elements (sane maximum "
366  "is 1000 values), got " +
367  Convert::ToString(truncsize) +
368  " values, truncated to first 1000");
369 
370  truncsize = 1000;
371  }
372 
373  packet << truncsize;
374 
375  // Pass that number of elements //
376  for(int i = 0; i < truncsize; i++) {
377 
378  Datas[i]->AddDataToPacket(packet);
379  }
380 }
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void Error(const std::string &data) override
Definition: Logger.cpp:177

◆ CanAllBeCastedToType() [1/2]

template<class DBT >
bool Leviathan::NamedVariableList::CanAllBeCastedToType ( ) const
inline

Definition at line 103 of file NamedVars.h.

104  {
105  if(Datas.size() == 0)
106  return false;
107 
108  for(size_t i = 0; i < Datas.size(); i++) {
109  // check this //
110  if(!Datas[i]->IsConversionAllowedNonPtr<DBT>()) {
111  return false;
112  }
113  }
114  // all passed, can be casted //
115  return true;
116  }

◆ CanAllBeCastedToType() [2/2]

template<class DBT >
bool Leviathan::NamedVariableList::CanAllBeCastedToType ( const int &  startindex,
const int &  endindex 
) const
inline

Definition at line 118 of file NamedVars.h.

119  {
120  if(Datas.size() == 0)
121  return false;
122  // check would it go over //
123  if((size_t)endindex >= Datas.size())
124  return false;
125 
126  for(int i = startindex; i < endindex + 1; i++) {
127  // check this //
128  if(!Datas[(size_t)i]->IsConversionAllowedNonPtr<DBT>()) {
129  return false;
130  }
131  }
132  // all passed, can be casted //
133  return true;
134  }

◆ CompareName()

bool NamedVariableList::CompareName ( const std::string &  name) const

Definition at line 485 of file NamedVars.cpp.

486 {
487  // just default comparison //
488  return Name.compare(name) == 0;
489 }

◆ ConstructValuesForObject()

DLLEXPORT bool NamedVariableList::ConstructValuesForObject ( const std::string &  variablestr,
LErrorReporter errorreport,
std::map< std::string, std::shared_ptr< VariableBlock >> *  predefined 
)

Helper function for constructing values.

Definition at line 235 of file NamedVars.cpp.

238 {
239  if(variablestr.size() == 0) {
240 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
241  throw InvalidArgument("invalid variable string, 0 length");
242 #else
243  errorreport->Error(std::string("invalid variable string, 0 length"));
244  return false;
245 #endif
246  }
247 
248  // check does it have brackets (and need to be processed like so) //
249  if(variablestr[0] == L'[') {
250 
251  // Needs to be split into values //
252  StringIterator itr(variablestr);
253 
254  auto firstlevel = itr.GetStringInBracketsRecursive<string>();
255 
256  std::vector<VariableBlock*> parsedvalues;
257 
258 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
259 
260  try {
261  if(!RecursiveParseList(
262  parsedvalues, std::move(firstlevel), errorreport, predefined)) {
263 
264  throw InvalidArgument("NamedVariableList could not parse top level bracket "
265  "expression");
266  }
267  } catch(const InvalidArgument&) {
268 
269  throw;
270  }
271 #else
272  if(!RecursiveParseList(parsedvalues, std::move(firstlevel), errorreport, predefined)) {
273 
274  errorreport->Error(
275  std::string("NamedVariableList could not parse top level bracket "
276  "expression"));
277  return false;
278  }
279 
280 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
281 
282  for(auto iter = Datas.begin(); iter != Datas.end(); ++iter) {
283 
284  SAFE_DELETE(*iter);
285  }
286 
287  Datas.resize(parsedvalues.size());
288 
289  // Add the final values //
290  for(size_t i = 0; i < Datas.size(); i++) {
291 
292  Datas[i] = parsedvalues[i];
293  }
294 
295  parsedvalues.clear();
296 
297  return true;
298  }
299 
300  // just one value //
301 
302  // try to create new VariableBlock //
303  // it should always have one element //
304  std::unique_ptr<VariableBlock> tmpcreated;
305 
306 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
307  try {
308  tmpcreated = std::make_unique<VariableBlock>(variablestr, predefined);
309  } catch(const InvalidArgument&) {
310 
311  // Rethrow the exception //
312  SAFE_DELETE_VECTOR(Datas);
313  throw;
314  }
315 #else
316  tmpcreated = std::make_unique<VariableBlock>(variablestr, predefined);
317 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
318 
319  if(!tmpcreated || !tmpcreated->IsValid()) {
320 
321  SAFE_DELETE_VECTOR(Datas);
322  return false;
323  }
324 
325  Datas.push_back(tmpcreated.release());
326  return true;
327 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123
Iterator class for getting parts of a string.
bool IsValid() const
Definition: DataBlock.h:511
virtual void Error(const std::string &Text)=0
#define SAFE_DELETE(x)
Definition: Define.h:116
DLLEXPORT bool RecursiveParseList(std::vector< VariableBlock *> &resultvalues, std::unique_ptr< std::string > expression, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined)
Handles a found bracket expression "[...]" parsing it recursively into values.
Definition: NamedVars.cpp:134

◆ GetCommonType()

DLLEXPORT int NamedVariableList::GetCommonType ( ) const

Definition at line 738 of file NamedVars.cpp.

739 {
740  // if all have a common type return it //
741  if(Datas.size() == 0)
742  // no common type //
743  return DATABLOCK_TYPE_ERROR;
744 
745  int lasttype = Datas[0]->GetBlock()->Type;
746 
747  for(size_t i = 1; i < Datas.size(); i++) {
748 
749  if(lasttype != Datas[i]->GetBlock()->Type) {
750  // not same type //
751  return DATABLOCK_TYPE_ERROR;
752  }
753  }
754  // there is a common type //
755  return lasttype;
756 }
#define DATABLOCK_TYPE_ERROR
Definition: DataBlock.h:31

◆ GetName() [1/2]

std::string Leviathan::NamedVariableList::GetName ( ) const
inline

Definition at line 139 of file NamedVars.h.

140  {
141  return Name;
142  }

◆ GetName() [2/2]

DLLEXPORT void NamedVariableList::GetName ( std::string &  name) const

Definition at line 474 of file NamedVars.cpp.

475 {
476  // return name in a reference //
477  name = Name;
478 }

◆ GetValue() [1/2]

DLLEXPORT VariableBlock & NamedVariableList::GetValue ( )

Definition at line 462 of file NamedVars.cpp.

463 {
464  // uses vector operator to get value, might throw something //
465  return *Datas[0];
466 }

◆ GetValue() [2/2]

DLLEXPORT VariableBlock & NamedVariableList::GetValue ( size_t  nindex)

Definition at line 468 of file NamedVars.cpp.

469 {
470  // uses vector operator to get value, might throw or something //
471  return *Datas[nindex];
472 }

◆ GetValueDirect() [1/2]

DLLEXPORT VariableBlock * NamedVariableList::GetValueDirect ( )

Definition at line 718 of file NamedVars.cpp.

719 {
720  // return first element //
721  return Datas.size() ? Datas[0] : NULL;
722 }

◆ GetValueDirect() [2/2]

DLLEXPORT VariableBlock * NamedVariableList::GetValueDirect ( size_t  nindex)

Definition at line 724 of file NamedVars.cpp.

725 {
726 
727  if(nindex >= Datas.size())
728  return nullptr;
729 
730  return Datas[nindex];
731 }

◆ GetValues()

DLLEXPORT vector< VariableBlock * > & NamedVariableList::GetValues ( )

Definition at line 776 of file NamedVars.cpp.

777 {
778  return Datas;
779 }

◆ GetVariableCount()

DLLEXPORT size_t NamedVariableList::GetVariableCount ( ) const

Definition at line 733 of file NamedVars.cpp.

734 {
735  return Datas.size();
736 }

◆ GetVariableType() [1/2]

DLLEXPORT int NamedVariableList::GetVariableType ( ) const

Definition at line 758 of file NamedVars.cpp.

759 {
760  // get variable type of first index //
761  return Datas.size() ? Datas[0]->GetBlock()->Type : DATABLOCK_TYPE_ERROR;
762 }
#define DATABLOCK_TYPE_ERROR
Definition: DataBlock.h:31

◆ GetVariableType() [2/2]

DLLEXPORT int NamedVariableList::GetVariableType ( const int &  nindex) const

Definition at line 764 of file NamedVars.cpp.

765 {
766 
767  return Datas[nindex]->GetBlock()->Type;
768 }

◆ IsValid()

bool Leviathan::NamedVariableList::IsValid ( ) const
inline

Returns true if this is valid.

Definition at line 76 of file NamedVars.h.

77  {
78  return Name.length() > 0;
79  }

◆ operator!=()

DLLEXPORT bool Leviathan::NamedVariableList::operator!= ( const NamedVariableList other) const
See also
operator==

Definition at line 606 of file NamedVars.cpp.

607 {
608 
609  return !(*this == other);
610 }

◆ operator=()

DLLEXPORT NamedVariableList & NamedVariableList::operator= ( const NamedVariableList other)

Definition at line 569 of file NamedVars.cpp.

570 {
571  // copy values //
572  Name = other.Name;
573 
574  SAFE_DELETE_VECTOR(Datas);
575  Datas.resize(other.Datas.size());
576  // copy values over //
577  for(size_t i = 0; i < other.Datas.size(); i++) {
578 
579  Datas[i] = new VariableBlock(*other.Datas[i]);
580  }
581 
582  // return this as result //
583  return *this;
584 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

◆ operator==()

DLLEXPORT bool NamedVariableList::operator== ( const NamedVariableList other) const

Compare values extensively

If this returns true then the values are the same and assignment would have no visible effect.

Definition at line 586 of file NamedVars.cpp.

587 {
588  // Make sure that names are the same //
589  if(Name != other.Name)
590  return false;
591 
592  // Check variables //
593  if(Datas.size() != other.Datas.size())
594  return false;
595 
596  // Compare data in the DataBlocks //
597  for(size_t i = 0; i < Datas.size(); i++) {
598 
599  if(*Datas[i] != *other.Datas[i])
600  return false;
601  }
602 
603  // They truly are the same //
604  return true;
605 }

◆ operator[]()

DLLEXPORT VariableBlock & NamedVariableList::operator[] ( const int &  nindex)

Definition at line 770 of file NamedVars.cpp.

771 {
772  // will allow to throw any exceptions the vector wants //
773  return *Datas[nindex];
774 }

◆ ProcessDataDump()

DLLEXPORT bool NamedVariableList::ProcessDataDump ( const std::string &  data,
std::vector< std::shared_ptr< NamedVariableList >> &  vec,
LErrorReporter errorreport,
std::map< std::string, std::shared_ptr< VariableBlock >> *  predefined = nullptr 
)
static

Definition at line 612 of file NamedVars.cpp.

615 {
616  // Split to lines //
617  std::vector<std::shared_ptr<std::string>> lines;
618 
619  StringIterator itr(data);
620 
621  // Use string iterator to get the lines that are separated by ; //
622  std::unique_ptr<std::string> curLine;
623  size_t lineLength = 0;
624 
625  do {
626  curLine = itr.GetUntilNextCharacterOrNothing<std::string>(';');
627 
628  if(!curLine)
629  break;
630 
631  lineLength = curLine->size();
632 
633  lines.push_back(std::shared_ptr<string>(curLine.release()));
634 
635  } while(lineLength != 0);
636 
637 
638  if(lines.empty()) {
639  // No lines //
640  return false;
641  }
642 
643  // Make space for values //
644  // let's reserve space //
645  vec.reserve(lines.size());
646 
647  // Fill values //
648  for(size_t i = 0; i < lines.size(); ++i) {
649  // Skip empty lines //
650  if(lines[i]->empty())
651  continue;
652 
653  // Create a named var //
654 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
655  try {
656  auto var =
657  std::make_shared<NamedVariableList>(*lines[i], Logger::Get(), predefined);
658 
659  if(!var || !var->IsValid()) {
660  // Invalid value //
661  continue;
662  }
663 
664  vec.push_back(var);
665 
666  } catch(const InvalidArgument& e) {
667  // exception throws, must be invalid line //
668 
669  errorreport->Error("NamedVar: ProcessDataDump: contains invalid line, "
670  "line (with only ASCII characters): " +
671  // This should remove null characters from the string //
672  Convert::ToString(*lines[i]) + "\nEND");
673 
674  // Print to log //
675  e.Print(errorreport);
676 
677  continue;
678  }
679 
680 #else
681 
682  std::shared_ptr<NamedVariableList> var(
683  new NamedVariableList(*lines[i], errorreport, predefined));
684 
685  if(!var || !var->IsValid()) {
686  // Invalid value //
687  continue;
688  }
689 
690  vec.push_back(var);
691 
692 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
693  }
694 
695  return true;
696 }
virtual DLLEXPORT void Print(LErrorReporter *errorreport) const noexcept
Definition: Exceptions.cpp:41
Iterator class for getting parts of a string.
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
virtual void Error(const std::string &Text)=0

◆ PushValue()

DLLEXPORT void NamedVariableList::PushValue ( std::unique_ptr< VariableBlock > &&  value)

Adds a new value.

Definition at line 457 of file NamedVars.cpp.

458 {
459  Datas.push_back(value.release());
460 }

◆ RecursiveParseList()

DLLEXPORT bool NamedVariableList::RecursiveParseList ( std::vector< VariableBlock *> &  resultvalues,
std::unique_ptr< std::string >  expression,
LErrorReporter errorreport,
std::map< std::string, std::shared_ptr< VariableBlock >> *  predefined 
)

Handles a found bracket expression "[...]" parsing it recursively into values.

Returns
false on parse error

Definition at line 134 of file NamedVars.cpp.

137 {
138  // Empty brackets //
139  if(!expression) {
140 
141  resultvalues.push_back(new VariableBlock(new StringBlock(new string())));
142  return true;
143  }
144 
145  StringIterator itr(expression.get());
146 
147  itr.SkipWhiteSpace();
148 
149  // TODO: allow commas inside brackets without quoting them
150  while(auto value = itr.GetUntilNextCharacterOrAll<string>(',')) {
151 
152  StringIterator itr2(value.get());
153 
154  itr2.SkipWhiteSpace();
155 
156  if(itr2.IsOutOfBounds()) {
157 
158  continue;
159  }
160 
161  // Parameter is wrapped in brackets //
162  if(itr2.GetCharacter() == '[') {
163 
164  auto firstvalue = itr2.GetStringInBracketsRecursive<string>();
165 
166  std::vector<VariableBlock*> morevalues;
167 
168  if(!RecursiveParseList(morevalues, move(firstvalue), errorreport, predefined)) {
169 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
170  throw InvalidArgument("Sub expression parsing failed");
171 #else
172  errorreport->Error(std::string("Sub expression parsing failed"));
173  return false;
174 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
175  }
176 
177  if(morevalues.size() > 1) {
178 
179  SAFE_DELETE_VECTOR(morevalues);
180  morevalues.clear();
181 
182 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
183  throw InvalidArgument("NamedVars recursive parsing is not done");
184 #else
185  LEVIATHAN_ASSERT(0, "NamedVars recursive parsing is not done");
186 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
187 
188  } else {
189 
190  // Just a single or no values where wrapped in extra brackets //
191  for(auto ptr : morevalues) {
192  resultvalues.push_back(ptr);
193  }
194 
195  morevalues.clear();
196  }
197 
198  continue;
199  }
200 
201  // Parse value //
202  auto valuestr = itr2.GetUntilEnd<string>();
203 
204  if(!valuestr)
205  continue;
206 
207  std::unique_ptr<VariableBlock> tmpcreated;
208 
209 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
210  try {
211  tmpcreated = std::make_unique<VariableBlock>(*valuestr, predefined);
212  } catch(const InvalidArgument&) {
213 
214  // Rethrow the exception //
215  SAFE_DELETE_VECTOR(resultvalues);
216  throw;
217  }
218 #else
219  tmpcreated = std::make_unique<VariableBlock>(*valuestr, predefined);
220 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
221 
222  if(!tmpcreated || !tmpcreated->IsValid()) {
223 
224  SAFE_DELETE_VECTOR(resultvalues);
225  errorreport->Error(std::string("VariableBlock invalid value: " + *valuestr));
226  return false;
227  }
228 
229  resultvalues.push_back(tmpcreated.release());
230  }
231 
232  return true;
233 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
void SkipWhiteSpace(int specialflags=0)
Skips until characters that are not whitespace are found.
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123
Iterator class for getting parts of a string.
bool IsValid() const
Definition: DataBlock.h:511
virtual void Error(const std::string &Text)=0
DataBlock< std::string > StringBlock
Definition: DataBlock.h:386
DLLEXPORT bool RecursiveParseList(std::vector< VariableBlock *> &resultvalues, std::unique_ptr< std::string > expression, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined)
Handles a found bracket expression "[...]" parsing it recursively into values.
Definition: NamedVars.cpp:134

◆ SetName()

void NamedVariableList::SetName ( const std::string &  name)

Definition at line 480 of file NamedVars.cpp.

481 {
482  Name = name;
483 }

◆ SetValue() [1/5]

DLLEXPORT void NamedVariableList::SetValue ( const VariableBlock value1)

Definition at line 389 of file NamedVars.cpp.

390 {
391  // clear old //
392  SAFE_DELETE_VECTOR(Datas);
393 
394  // assign value //
395 
396  // create new //
397  Datas.push_back(new VariableBlock(value1));
398 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

◆ SetValue() [2/5]

DLLEXPORT void NamedVariableList::SetValue ( VariableBlock value1)

Definition at line 400 of file NamedVars.cpp.

401 {
402  // clear old //
403  SAFE_DELETE_VECTOR(Datas);
404 
405  // put value to vector //
406  Datas.push_back(value1);
407 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

◆ SetValue() [3/5]

DLLEXPORT void NamedVariableList::SetValue ( const std::vector< VariableBlock *> &  values)

Definition at line 449 of file NamedVars.cpp.

450 {
451  // delete old //
452  SAFE_DELETE_VECTOR(Datas);
453 
454  // copy vector (will copy pointers and steal them) //
455  Datas = values;
456 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

◆ SetValue() [4/5]

DLLEXPORT void NamedVariableList::SetValue ( const int &  nindex,
const VariableBlock valuetoset 
)

Definition at line 409 of file NamedVars.cpp.

410 {
411  // check do we need to allocate new //
412  if(Datas.size() <= (size_t)nindex) {
413 
414  // resize to have enough space //
415  Datas.resize(nindex + 1, NULL);
416  Datas[nindex] = new VariableBlock(valuetoset);
417  } else {
418 
419  if(Datas[nindex] != NULL) {
420  // assign to existing value //
421  *Datas[nindex] = valuetoset;
422  } else {
423  // new value needed //
424  Datas[nindex] = new VariableBlock(valuetoset);
425  }
426  }
427 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425

◆ SetValue() [5/5]

DLLEXPORT void NamedVariableList::SetValue ( const int &  nindex,
VariableBlock valuetoset 
)

Definition at line 429 of file NamedVars.cpp.

430 {
431  // check do we need to allocate new //
432  if(Datas.size() <= (size_t)nindex) {
433 
434  // resize to have enough space //
435  Datas.resize(nindex + 1, NULL);
436  // just copy the pointer //
437  Datas[nindex] = valuetoset;
438  } else {
439 
440  if(Datas[nindex] != NULL) {
441  // existing value needs to be deleted //
442  SAFE_DELETE(Datas[nindex]);
443  }
444  // set pointer //
445  Datas[nindex] = valuetoset;
446  }
447 }
#define SAFE_DELETE(x)
Definition: Define.h:116

◆ SwitchValues()

DLLEXPORT void NamedVariableList::SwitchValues ( NamedVariableList receiver,
NamedVariableList donator 
)
static

Switches values to a new instance.

Todo:
Rewrite this as a move operation

Definition at line 698 of file NamedVars.cpp.

700 {
701  // only overwrite name if there is one //
702  if(donator.Name.size() > 0)
703  receiver.Name = donator.Name;
704 
705 
706  SAFE_DELETE_VECTOR(receiver.Datas);
707  // resize to match sizes to avoid excess resizing //
708  receiver.Datas.resize(donator.Datas.size());
709 
710  for(size_t i = 0; i < donator.Datas.size(); i++) {
711 
712  receiver.Datas[i] = donator.Datas[i];
713  }
714  // clear donator data //
715  donator.Datas.clear();
716 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

◆ ToText()

DLLEXPORT std::string Leviathan::NamedVariableList::ToText ( int  WhichSeparator = 0,
bool  AddAllBrackets = false 
) const

Definition at line 490 of file NamedVars.cpp.

492 {
493 
494  string stringifiedval = Name;
495 
496  switch(WhichSeparator) {
497  default:
498  case 0: stringifiedval += " = "; break;
499  case 1: stringifiedval += ": "; break;
500  }
501 
502  // convert value to string //
503 
504  const bool WrapInBrackets = AddAllBrackets ? true : Datas.size() != 1;
505 
506  // starting bracket //
507  if(WrapInBrackets)
508  stringifiedval += "[";
509 
510  // reserve some space //
511  stringifiedval.reserve(Datas.size() * 4);
512 
513  for(size_t i = 0; i < Datas.size(); i++) {
514 
515  if(i != 0)
516  stringifiedval += ", ";
517 
518  // Check if type is a string type //
519  int blocktype = Datas[i]->GetBlockConst()->Type;
520 
521  if(blocktype == DATABLOCK_TYPE_STRING || blocktype == DATABLOCK_TYPE_WSTRING ||
522  blocktype == DATABLOCK_TYPE_CHAR) {
523  // Output in quotes //
524  if(AddAllBrackets)
525  stringifiedval += "[\"" + Datas[i]->operator string() + "\"]";
526  else
527  stringifiedval += "\"" + Datas[i]->operator string() + "\"";
528 
529  } else if(blocktype == DATABLOCK_TYPE_BOOL) {
530 
531  // Use true/false for this //
532  if(AddAllBrackets) {
533 
534  stringifiedval +=
535  "[" + (Datas[i]->operator bool() ? string("true") : string("false")) + "]";
536 
537  } else {
538 
539  stringifiedval += Datas[i]->operator bool() ? string("true") : string("false");
540  }
541 
542  } else {
543 
544  // check is conversion allowed //
545  if(!Datas[i]->IsConversionAllowedNonPtr<string>()) {
546 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
547  // no choice but to throw exception //
548  throw InvalidType("value cannot be cast to string");
549 #else
550  LEVIATHAN_ASSERT(0, "value cannot be cast to string");
551 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
552  }
553  if(AddAllBrackets)
554  stringifiedval += "[" + Datas[i]->operator string() + "]";
555  else
556  stringifiedval += "" + Datas[i]->operator string() + "";
557  }
558  }
559 
560  // add ending bracket and done //
561  if(WrapInBrackets)
562  stringifiedval += "];";
563  else
564  stringifiedval += ";";
565 
566  return stringifiedval;
567 }
#define DATABLOCK_TYPE_WSTRING
Definition: DataBlock.h:25
#define DATABLOCK_TYPE_STRING
Definition: DataBlock.h:26
#define DATABLOCK_TYPE_BOOL
Definition: DataBlock.h:24
#define DATABLOCK_TYPE_CHAR
Definition: DataBlock.h:27

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