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 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=NULL)
 
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 56 of file NamedVars.cpp.

56  :
57  Datas(other.Datas.size()), Name(other.Name)
58 {
59 
60  // copy value over //
61  for(size_t i = 0; i < other.Datas.size(); i++) {
62 
63  Datas[i] = new VariableBlock(*other.Datas[i]);
64  }
65 }
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 {
16 }

◆ NamedVariableList() [4/10]

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

Definition at line 18 of file NamedVars.cpp.

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

◆ NamedVariableList() [5/10]

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

Definition at line 27 of file NamedVars.cpp.

28  :
29  Datas(1),
30  Name(name)
31 {
32  // set value //
33  Datas[0] = new VariableBlock(val);
34 }
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 37 of file NamedVars.cpp.

37  :
38  Datas(1), Name(data->GetName())
39 {
40  // Copy value //
41  Datas[0] = new VariableBlock(*data);
42 }
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 331 of file NamedVars.cpp.

332 {
333  // Unpack the data from the packet //
334  packet >> Name;
335 
336  // First get the size //
337  int tmpsize = 0;
338 
339  // Thousand is considered here the maximum number of elements //
340  if(!(packet >> tmpsize) || tmpsize > 1000 || tmpsize < 0) {
341 
342  throw InvalidArgument("invalid packet format");
343  }
344 
345  // Reserve enough space //
346  Datas.reserve((size_t)tmpsize);
347 
348  // Loop and get the data //
349  for(int i = 0; i < tmpsize; i++) {
350 
351  Datas.push_back(new VariableBlock(packet));
352  }
353 }
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 45 of file NamedVars.cpp.

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

◆ 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 67 of file NamedVars.cpp.

70 {
71  // using StringIterator makes this shorter //
72  StringIterator itr(&line);
73 
74  auto name = itr.GetUntilEqualityAssignment<std::string>(EQUALITYCHARACTER_TYPE_ALL);
75 
76  if(!name) {
77  // no name //
78 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
79  errorreport->Error(std::string("invalid data on line (invalid name)"));
80  return;
81 #else
82  throw InvalidArgument("invalid data on line (invalid name)");
83 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
84  }
85 
86  Name = *name;
87 
88  // skip whitespace //
89  itr.SkipWhiteSpace();
90 
91  // get last part of it //
92  auto tempvar = itr.GetUntilNextCharacterOrAll<std::string>(L';');
93 
94  if(!tempvar || tempvar->size() < 1) {
95  // no variable //
96 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
97  Name = "";
98  errorreport->Error(std::string("invalid data on line (no variable data)"));
99  return;
100 #else
101  throw InvalidArgument("invalid data on line (no variable data)");
102 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
103  }
104 
105  if(!ConstructValuesForObject(*tempvar, errorreport, predefined)) {
106 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
107  Name = "";
108  errorreport->Error(std::string("invalid variable string, parsing failed"));
109  return;
110 #else
111  throw InvalidArgument("invalid variable string, parsing failed");
112 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
113  }
114 }
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:236

◆ 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 116 of file NamedVars.cpp.

119 {
120  // We already have the name provided for us //
121  Name = name;
122 
123  // The value needs to be parsed //
124  if(!ConstructValuesForObject(valuestr, errorreport, predefined)) {
125 #ifdef ALTERNATIVE_EXCEPTIONS_FATAL
126  Name = "";
127  errorreport->Error(std::string("invalid variable string, parsing failed"));
128  return;
129 #else
130  throw InvalidArgument("invalid variable string, parsing failed");
131 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
132  }
133 }
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:236

◆ ~NamedVariableList()

DLLEXPORT NamedVariableList::~NamedVariableList ( )

Definition at line 384 of file NamedVars.cpp.

385 {
386 
387  SAFE_DELETE_VECTOR(Datas);
388 }
#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 355 of file NamedVars.cpp.

356 {
357  // Start adding data to the packet //
358  packet << Name;
359 
360  // The vector passing //
361  int truncsize = (int)Datas.size();
362 
363  if(truncsize > 1000) {
364 
365  // That's an error //
366  Logger::Get()->Error("NamedVariableList: AddToPacket: too many elements (sane maximum "
367  "is 1000 values), got " +
368  Convert::ToString(truncsize) +
369  " values, truncated to first 1000");
370 
371  truncsize = 1000;
372  }
373 
374  packet << truncsize;
375 
376  // Pass that number of elements //
377  for(int i = 0; i < truncsize; i++) {
378 
379  Datas[i]->AddDataToPacket(packet);
380  }
381 }
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 100 of file NamedVars.h.

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

◆ CanAllBeCastedToType() [2/2]

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

Definition at line 115 of file NamedVars.h.

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

◆ CompareName()

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

Definition at line 482 of file NamedVars.cpp.

483 {
484  // just default comparison //
485  return Name.compare(name) == 0;
486 }

◆ 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 236 of file NamedVars.cpp.

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

◆ GetCommonType()

DLLEXPORT int NamedVariableList::GetCommonType ( ) const

Definition at line 734 of file NamedVars.cpp.

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

◆ GetName() [1/2]

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

Definition at line 136 of file NamedVars.h.

137  {
138  return Name;
139  }

◆ GetName() [2/2]

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

Definition at line 471 of file NamedVars.cpp.

472 {
473  // return name in a reference //
474  name = Name;
475 }

◆ GetValue() [1/2]

DLLEXPORT VariableBlock & NamedVariableList::GetValue ( )

Definition at line 459 of file NamedVars.cpp.

460 {
461  // uses vector operator to get value, might throw something //
462  return *Datas[0];
463 }

◆ GetValue() [2/2]

DLLEXPORT VariableBlock & NamedVariableList::GetValue ( size_t  nindex)

Definition at line 465 of file NamedVars.cpp.

466 {
467  // uses vector operator to get value, might throw or something //
468  return *Datas[nindex];
469 }

◆ GetValueDirect() [1/2]

DLLEXPORT VariableBlock * NamedVariableList::GetValueDirect ( )

Definition at line 714 of file NamedVars.cpp.

715 {
716  // return first element //
717  return Datas.size() ? Datas[0] : NULL;
718 }

◆ GetValueDirect() [2/2]

DLLEXPORT VariableBlock * NamedVariableList::GetValueDirect ( size_t  nindex)

Definition at line 720 of file NamedVars.cpp.

721 {
722 
723  if(nindex >= Datas.size())
724  return nullptr;
725 
726  return Datas[nindex];
727 }

◆ GetValues()

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

Definition at line 772 of file NamedVars.cpp.

773 {
774  return Datas;
775 }

◆ GetVariableCount()

DLLEXPORT size_t NamedVariableList::GetVariableCount ( ) const

Definition at line 729 of file NamedVars.cpp.

730 {
731  return Datas.size();
732 }

◆ GetVariableType() [1/2]

DLLEXPORT int NamedVariableList::GetVariableType ( ) const

Definition at line 754 of file NamedVars.cpp.

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

◆ GetVariableType() [2/2]

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

Definition at line 760 of file NamedVars.cpp.

761 {
762 
763  return Datas[nindex]->GetBlock()->Type;
764 }

◆ 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 603 of file NamedVars.cpp.

604 {
605 
606  return !(*this == other);
607 }

◆ operator=()

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

Definition at line 566 of file NamedVars.cpp.

567 {
568  // copy values //
569  Name = other.Name;
570 
571  SAFE_DELETE_VECTOR(Datas);
572  Datas.resize(other.Datas.size());
573  // copy values over //
574  for(size_t i = 0; i < other.Datas.size(); i++) {
575 
576  Datas[i] = new VariableBlock(*other.Datas[i]);
577  }
578 
579  // return this as result //
580  return *this;
581 }
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 583 of file NamedVars.cpp.

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

◆ operator[]()

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

Definition at line 766 of file NamedVars.cpp.

767 {
768  // will allow to throw any exceptions the vector wants //
769  return *Datas[nindex];
770 }

◆ 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 = NULL 
)
static

Definition at line 609 of file NamedVars.cpp.

612 {
613  // split to lines //
614  vector<shared_ptr<string>> Lines;
615 
616  StringIterator itr(data);
617 
618  // use string iterator to get the lines that are separated by ; //
619  unique_ptr<string> curline;
620  size_t linelength = 0;
621  do {
622  curline = itr.GetUntilNextCharacterOrNothing<string>(';');
623  if(!curline)
624  break;
625 
626  linelength = curline->size();
627 
628  string* tmp = curline.release();
629 
630  Lines.push_back(shared_ptr<string>(tmp));
631  } while(linelength != 0);
632 
633 
634  if(Lines.size() < 1) {
635  // no lines //
636  return false;
637  }
638  // make space for values //
639  // let's reserve space //
640  vec.reserve(Lines.size());
641 
642  // fill values //
643  for(size_t i = 0; i < Lines.size(); i++) {
644  // skip empty lines //
645  if(Lines[i]->size() == 0)
646  continue;
647 
648  // create a named var //
649 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
650  try {
651  auto var =
652  std::make_shared<NamedVariableList>(*Lines[i], Logger::Get(), predefined);
653 
654  if(!var || !var->IsValid()) {
655  // Invalid value //
656  continue;
657  }
658 
659  vec.push_back(var);
660  } catch(const InvalidArgument& e) {
661  // print to log //
662  e.PrintToLog();
663  // exception throws, must be invalid line //
664 
665  // This should remove null characters from the string //
666 
667  Logger::Get()->Info("NamedVar: ProcessDataDump: contains invalid line, "
668  "line (with only ASCII characters): " +
669  Convert::ToString(Lines[i]) + "\nEND");
670 
671  continue;
672  }
673 
674 #else
675 
676  shared_ptr<NamedVariableList> var(
677  new NamedVariableList(*Lines[i], errorreport, predefined));
678 
679  if(!var || !var->IsValid()) {
680  // Invalid value //
681  continue;
682  }
683 
684  vec.push_back(var);
685 
686 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
687 
688  continue;
689  }
690 
691  return true;
692 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
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

◆ 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 135 of file NamedVars.cpp.

138 {
139  // Empty brackets //
140  if(!expression) {
141 
142  resultvalues.push_back(new VariableBlock(new StringBlock(new string())));
143  return true;
144  }
145 
146  StringIterator itr(expression.get());
147 
148  itr.SkipWhiteSpace();
149 
150  // TODO: allow commas inside brackets without quoting them
151  while(auto value = itr.GetUntilNextCharacterOrAll<string>(',')) {
152 
153  StringIterator itr2(value.get());
154 
155  itr2.SkipWhiteSpace();
156 
157  if(itr2.IsOutOfBounds()) {
158 
159  continue;
160  }
161 
162  // Parameter is wrapped in brackets //
163  if(itr2.GetCharacter() == '[') {
164 
165  auto firstvalue = itr2.GetStringInBracketsRecursive<string>();
166 
167  std::vector<VariableBlock*> morevalues;
168 
169  if(!RecursiveParseList(morevalues, move(firstvalue), errorreport, predefined)) {
170 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
171  throw InvalidArgument("Sub expression parsing failed");
172 #else
173  errorreport->Error(std::string("Sub expression parsing failed"));
174  return false;
175 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
176  }
177 
178  if(morevalues.size() > 1) {
179 
180  SAFE_DELETE_VECTOR(morevalues);
181  morevalues.clear();
182 
183 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
184  throw InvalidArgument("NamedVars recursive parsing is not done");
185 #else
186  LEVIATHAN_ASSERT(0, "NamedVars recursive parsing is not done");
187 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
188 
189  } else {
190 
191  // Just a single or no values where wrapped in extra brackets //
192  for(auto ptr : morevalues) {
193  resultvalues.push_back(ptr);
194  }
195 
196  morevalues.clear();
197  }
198 
199  continue;
200  }
201 
202  // Parse value //
203  auto valuestr = itr2.GetUntilEnd<string>();
204 
205  if(!valuestr)
206  continue;
207 
208  std::unique_ptr<VariableBlock> tmpcreated;
209 
210 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
211  try {
212  tmpcreated = std::make_unique<VariableBlock>(*valuestr, predefined);
213  } catch(const InvalidArgument) {
214 
215  // Rethrow the exception //
216  SAFE_DELETE_VECTOR(resultvalues);
217  throw;
218  }
219 #else
220  tmpcreated = std::make_unique<VariableBlock>(*valuestr, predefined);
221 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
222 
223  if(!tmpcreated || !tmpcreated->IsValid()) {
224 
225  SAFE_DELETE_VECTOR(resultvalues);
226  errorreport->Error(std::string("VariableBlock invalid value: " + *valuestr));
227  return false;
228  }
229 
230  resultvalues.push_back(tmpcreated.release());
231  }
232 
233  return true;
234 }
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:135

◆ SetName()

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

Definition at line 477 of file NamedVars.cpp.

478 {
479  Name = name;
480 }

◆ SetValue() [1/5]

DLLEXPORT void NamedVariableList::SetValue ( const VariableBlock value1)

Definition at line 390 of file NamedVars.cpp.

391 {
392  // clear old //
393  SAFE_DELETE_VECTOR(Datas);
394 
395  // assign value //
396 
397  // create new //
398  Datas.push_back(new VariableBlock(value1));
399 }
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 401 of file NamedVars.cpp.

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

◆ SetValue() [3/5]

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

Definition at line 450 of file NamedVars.cpp.

451 {
452  // delete old //
453  SAFE_DELETE_VECTOR(Datas);
454 
455  // copy vector (will copy pointers and steal them) //
456  Datas = values;
457 }
#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 410 of file NamedVars.cpp.

411 {
412  // check do we need to allocate new //
413  if(Datas.size() <= (size_t)nindex) {
414 
415  // resize to have enough space //
416  Datas.resize(nindex + 1, NULL);
417  Datas[nindex] = new VariableBlock(valuetoset);
418  } else {
419 
420  if(Datas[nindex] != NULL) {
421  // assign to existing value //
422  *Datas[nindex] = valuetoset;
423  } else {
424  // new value needed //
425  Datas[nindex] = new VariableBlock(valuetoset);
426  }
427  }
428 }
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 430 of file NamedVars.cpp.

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

◆ SwitchValues()

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

Switches values to a new instance.

Definition at line 694 of file NamedVars.cpp.

696 {
697  // only overwrite name if there is one //
698  if(donator.Name.size() > 0)
699  receiver.Name = donator.Name;
700 
701 
702  SAFE_DELETE_VECTOR(receiver.Datas);
703  // resize to match sizes to avoid excess resizing //
704  receiver.Datas.resize(donator.Datas.size());
705 
706  for(size_t i = 0; i < donator.Datas.size(); i++) {
707 
708  receiver.Datas[i] = donator.Datas[i];
709  }
710  // clear donator data //
711  donator.Datas.clear();
712 }
#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 487 of file NamedVars.cpp.

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