Leviathan  0.8.0.0
Leviathan game engine
NamedVars.h
Go to the documentation of this file.
1 #pragma once
2 // ------------------------------------ //
3 #include "Define.h"
4 // ------------------------------------ //
5 #include <string>
6 #include <vector>
7 
8 
9 #include "../ReferenceCounted.h"
11 #include "Common/ThreadSafe.h"
12 
13 
14 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
15 #include "Exceptions.h"
16 #endif
17 #ifdef SFML_PACKETS
18 #include "SFML/Network/Packet.hpp"
19 #endif // SFML_PACKETS
20 #include "ErrorReporter.h"
21 
22 namespace Leviathan {
23 
28  friend NamedVars;
29 
30 public:
34  DLLEXPORT NamedVariableList(const std::string& name);
35  DLLEXPORT NamedVariableList(const std::string& name, VariableBlock* value1);
36  DLLEXPORT NamedVariableList(const std::string& name, const VariableBlock& val);
37 #ifdef LEVIATHAN_USING_ANGELSCRIPT
39 #endif // LEVIATHAN_USING_ANGELSCRIPT
40 
41 #ifdef SFML_PACKETS
42  DLLEXPORT NamedVariableList(sf::Packet& packet);
44 
46  DLLEXPORT void AddDataToPacket(sf::Packet& packet) const;
47 
48 #endif // SFML_PACKETS
49 
52  const std::string& name, std::vector<VariableBlock*> values_willclear);
53 
54  DLLEXPORT NamedVariableList(const std::string& line, LErrorReporter* errorreport,
55  std::map<std::string, std::shared_ptr<VariableBlock>>* predefined = NULL);
56 
57  DLLEXPORT NamedVariableList(const std::string& name, const std::string& valuestr,
58  LErrorReporter* errorreport,
59  std::map<std::string, std::shared_ptr<VariableBlock>>* predefined = NULL);
60 
62  DLLEXPORT bool ConstructValuesForObject(const std::string& variablestr,
63  LErrorReporter* errorreport,
64  std::map<std::string, std::shared_ptr<VariableBlock>>* predefined);
65 
69  DLLEXPORT bool RecursiveParseList(std::vector<VariableBlock*>& resultvalues,
70  std::unique_ptr<std::string> expression, LErrorReporter* errorreport,
71  std::map<std::string, std::shared_ptr<VariableBlock>>* predefined);
72 
74 
76  inline bool IsValid() const
77  {
78  return Name.length() > 0;
79  }
80 
81 
82  // ------------------------------------ //
83  DLLEXPORT void SetValue(const VariableBlock& value1);
84  DLLEXPORT void SetValue(VariableBlock* value1);
85  DLLEXPORT void SetValue(const std::vector<VariableBlock*>& values);
86  DLLEXPORT void SetValue(const int& nindex, const VariableBlock& valuetoset);
87  DLLEXPORT void SetValue(const int& nindex, VariableBlock* valuetoset);
88 
90  DLLEXPORT void PushValue(std::unique_ptr<VariableBlock>&& value);
91 
94  DLLEXPORT VariableBlock* GetValueDirect(size_t nindex);
95  DLLEXPORT VariableBlock& GetValue(size_t nindex);
96  DLLEXPORT std::vector<VariableBlock*>& GetValues();
97 
98  DLLEXPORT size_t GetVariableCount() const;
99 
100 
101  DLLEXPORT int GetCommonType() const;
102  template<class DBT>
103  inline bool CanAllBeCastedToType() const
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  }
117  template<class DBT>
118  inline bool CanAllBeCastedToType(const int& startindex, const int& endindex) const
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  }
135 
136  DLLEXPORT int GetVariableType() const;
137  DLLEXPORT int GetVariableType(const int& nindex) const;
138 
139  inline std::string GetName() const
140  {
141  return Name;
142  }
143  DLLEXPORT void GetName(std::string& name) const;
144 
145  DLLEXPORT void SetName(const std::string& name);
146  DLLEXPORT bool CompareName(const std::string& name) const;
147  // ------------------------------------ //
148  DLLEXPORT std::string ToText(int WhichSeparator = 0, bool AddAllBrackets = false) const;
149 
150  // process functions //
151  DLLEXPORT static bool ProcessDataDump(const std::string& data,
152  std::vector<std::shared_ptr<NamedVariableList>>& vec, LErrorReporter* errorreport,
153  std::map<std::string, std::shared_ptr<VariableBlock>>* predefined = nullptr);
154 
155  // operators //
157 
162  DLLEXPORT bool operator==(const NamedVariableList& other) const;
163 
165  DLLEXPORT bool operator!=(const NamedVariableList& other) const;
166 
167  // element access operator //
168  DLLEXPORT VariableBlock& operator[](const int& nindex);
169 
172  DLLEXPORT static void SwitchValues(
173  NamedVariableList& receiver, NamedVariableList& donator);
174 
175 private:
177  std::vector<VariableBlock*> Datas;
178 
180  std::string Name;
181 };
182 
183 
184 // holds a vector of NamedVariableLists and provides searching functions //
186 class NamedVars : public ReferenceCounted, public ThreadSafe {
187 public:
189 
191 
194  DLLEXPORT NamedVars(NamedVars* stealfrom);
195 
196  DLLEXPORT NamedVars(const NamedVars& other);
198  DLLEXPORT NamedVars(const std::string& datadump, LErrorReporter* errorreport);
199  DLLEXPORT NamedVars(const std::vector<std::shared_ptr<NamedVariableList>>& variables);
200  DLLEXPORT NamedVars(std::shared_ptr<NamedVariableList> variable);
201 
202 
204  DLLEXPORT NamedVars(NamedVariableList* takevariable);
205 
206 #ifdef SFML_PACKETS
207 
209  DLLEXPORT NamedVars(sf::Packet& packet);
210 
212  DLLEXPORT void AddDataToPacket(sf::Packet& packet) const;
213 
214 #endif // SFML_PACKETS
215 
217  // ------------------------------------ //
218 
221  DLLEXPORT bool Add(std::shared_ptr<NamedVariableList> value);
222  DLLEXPORT bool SetValue(const std::string& name, const VariableBlock& value1);
223  DLLEXPORT bool SetValue(const std::string& name, VariableBlock* value1);
224  DLLEXPORT bool SetValue(
225  const std::string& name, const std::vector<VariableBlock*>& values);
226 
227  DLLEXPORT bool SetValue(NamedVariableList& nameandvalues);
228 
229  DLLEXPORT size_t GetValueCount(const std::string& name) const;
230 
232  DLLEXPORT inline bool IsIndexValid(size_t index) const
233  {
234  return index < Variables.size();
235  }
236 
237  DLLEXPORT VariableBlock& GetValueNonConst(const std::string& name);
238 
239  DLLEXPORT const VariableBlock* GetValue(const std::string& name) const;
240 
241  DLLEXPORT bool GetValue(const std::string& name, VariableBlock& receiver) const;
242 
244  DLLEXPORT bool GetValue(
245  const std::string& name, const int& index, VariableBlock& receiver) const;
246 
248  DLLEXPORT bool GetValue(const int& index, VariableBlock& receiver) const;
249  DLLEXPORT bool GetValues(
250  const std::string& name, std::vector<const VariableBlock*>& receiver) const;
251 
252 
253 
254  DLLEXPORT std::shared_ptr<NamedVariableList> GetValueDirect(const std::string& name) const;
255 
257  DLLEXPORT NamedVariableList* GetValueDirectRaw(const std::string& name) const;
258  DLLEXPORT NamedVariableList* GetValueDirectRaw(size_t index) const;
259 
260  template<class T>
261  bool GetValueAndConvertTo(const std::string& name, T& receiver) const
262  {
263  // use try block to catch all exceptions (not found and conversion fail //
264  GUARD_LOCK();
265  try {
266  auto index = Find(guard, name);
267 
268  if(index >= Variables.size())
269  return false;
270 
271  const VariableBlock* tmpblock = Variables[index]->GetValueDirect();
272  if(tmpblock == NULL) {
273  return false;
274  }
275  if(!tmpblock->ConvertAndAssingToVariable<T>(receiver)) {
276 
277  // Unallowed NamedVars block conversion
278  return false;
279  }
280  } catch(...) {
281  // variable not found / wrong type //
282  return false;
283  }
284  // correct variable has been set //
285  return true;
286  }
287 
288  DLLEXPORT std::vector<VariableBlock*>* GetValues(const std::string& name);
289 
292  DLLEXPORT std::string Serialize(const std::string& lineprefix = "") const;
293 
294 #ifdef LEVIATHAN_USING_ANGELSCRIPT
295  // Script accessible functions //
296 
299  ScriptSafeVariableBlock* GetScriptCompatibleValue(const std::string& name);
300 
305 
306 #endif // LEVIATHAN_USING_ANGELSCRIPT
307 
308  // ------------------------------------ //
309  DLLEXPORT int GetVariableType(const std::string& name) const;
310 
311  DLLEXPORT int GetVariableType(Lock& guard, size_t index) const;
312 
313  inline int GetVariableType(size_t index) const
314  {
315  GUARD_LOCK();
316  return GetVariableType(guard, index);
317  }
318 
319  DLLEXPORT int GetVariableTypeOfAll(const std::string& name) const;
320 
321  DLLEXPORT int GetVariableTypeOfAll(Lock& guard, size_t index) const;
322 
323  inline int GetVariableTypeOfAll(size_t index) const
324  {
325  GUARD_LOCK();
326  return GetVariableTypeOfAll(guard, index);
327  }
328 
329  DLLEXPORT std::string GetName(size_t index);
330  DLLEXPORT bool GetName(size_t index, std::string& name) const;
331 
332  DLLEXPORT void SetName(Lock& guard, size_t index, const std::string& name);
333 
334  inline void SetName(size_t index, const std::string& name)
335  {
336  GUARD_LOCK();
337  SetName(guard, index, name);
338  }
339 
340  DLLEXPORT void SetName(const std::string& oldname, const std::string& name);
341 
342  DLLEXPORT bool CompareName(size_t index, const std::string& name) const;
343  // ------------------------------------ //
344  DLLEXPORT void AddVar(NamedVariableList* newvaluetoadd);
345  DLLEXPORT void AddVar(std::shared_ptr<NamedVariableList> values);
346  DLLEXPORT void AddVar(const std::string& name, VariableBlock* valuetosteal);
347  DLLEXPORT void Remove(size_t index);
348  DLLEXPORT void Remove(const std::string& name);
350  DLLEXPORT void RemoveIfExists(const std::string& name, Lock& guard);
351  // ------------------------------------ //
352  DLLEXPORT bool LoadVarsFromFile(const std::string& file, LErrorReporter* errorreport);
353 
354  DLLEXPORT std::vector<std::shared_ptr<NamedVariableList>>* GetVec();
355  DLLEXPORT void SetVec(std::vector<std::shared_ptr<NamedVariableList>>& vec);
356 
358  DLLEXPORT size_t GetVariableCount() const;
359 
360  inline operator bool() const
361  {
362  return !StateIsInvalid;
363  }
364 
365  // ------------------------------------ //
366 
367  inline size_t Find(const std::string& name) const
368  {
369  GUARD_LOCK();
370  return Find(guard, name);
371  }
372 
373  DLLEXPORT size_t Find(Lock& guard, const std::string& name) const;
374  // ------------------------------------ //
375  template<class T>
376  bool ShouldAddValueIfNotFoundOrWrongType(const std::string& name)
377  {
378  size_t index = Find(name);
379 
380  if(index >= Variables.size()) {
381  // Add //
382  return true;
383  }
384  // Check is type correct //
385  if(!Variables[(size_t)index]->CanAllBeCastedToType<T>()) {
386  // Incorrect types in the variables //
387  return true;
388  }
389 
390  // No need to do anything //
391  return false;
392  }
393 
394 private:
395  std::vector<std::shared_ptr<NamedVariableList>> Variables;
396 
398  bool StateIsInvalid = false;
399 };
400 
401 } // namespace Leviathan
402 
403 #ifdef LEAK_INTO_GLOBAL
406 #endif
DLLEXPORT VariableBlock & GetValueNonConst(const std::string &name)
Definition: NamedVars.cpp:957
static DLLEXPORT void SwitchValues(NamedVariableList &receiver, NamedVariableList &donator)
Switches values to a new instance.
Definition: NamedVars.cpp:698
DLLEXPORT size_t GetValueCount(const std::string &name) const
Definition: NamedVars.cpp:1037
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)
Definition: NamedVars.cpp:612
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
bool GetValueAndConvertTo(const std::string &name, T &receiver) const
Definition: NamedVars.h:261
DLLEXPORT std::shared_ptr< NamedVariableList > GetValueDirect(const std::string &name) const
Definition: NamedVars.cpp:1084
Allows the inherited object to be locked.
Definition: ThreadSafe.h:123
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
bool ShouldAddValueIfNotFoundOrWrongType(const std::string &name)
Definition: NamedVars.h:376
DLLEXPORT bool SetValue(const std::string &name, const VariableBlock &value1)
Definition: NamedVars.cpp:899
DLLEXPORT NamedVars()
Definition: NamedVars.cpp:781
DLLEXPORT VariableBlock & GetValue()
Definition: NamedVars.cpp:462
DLLEXPORT bool IsIndexValid(size_t index) const
Definition: NamedVars.h:232
DLLEXPORT const VariableBlock * GetValue(const std::string &name) const
Definition: NamedVars.cpp:974
DLLEXPORT std::vector< VariableBlock * > & GetValues()
Definition: NamedVars.cpp:776
DLLEXPORT std::string Serialize(const std::string &lineprefix="") const
Serializes this object into a string representation.
Definition: NamedVars.cpp:1117
DLLEXPORT void Remove(size_t index)
Definition: NamedVars.cpp:1215
DLLEXPORT bool operator==(const NamedVariableList &other) const
Definition: NamedVars.cpp:586
bool CanAllBeCastedToType() const
Definition: NamedVars.h:103
DLLEXPORT void SetValue(const VariableBlock &value1)
Definition: NamedVars.cpp:389
DLLEXPORT int GetVariableType() const
Definition: NamedVars.cpp:758
REFERENCE_COUNTED_PTR_TYPE(NamedVars)
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
bool IsValid() const
Returns true if this is valid.
Definition: NamedVars.h:76
bool AddScriptCompatibleValue(ScriptSafeVariableBlock *value)
Definition: NamedVars.cpp:1283
void SetName(size_t index, const std::string &name)
Definition: NamedVars.h:334
DLLEXPORT size_t GetVariableCount() const
Returns the size of the internal variable vector.
Definition: NamedVars.cpp:1304
DLLEXPORT size_t GetVariableCount() const
Definition: NamedVars.cpp:733
DLLEXPORT std::string ToText(int WhichSeparator=0, bool AddAllBrackets=false) const
Definition: NamedVars.cpp:490
DLLEXPORT void SetVec(std::vector< std::shared_ptr< NamedVariableList >> &vec)
Definition: NamedVars.cpp:1250
DLLEXPORT bool CompareName(const std::string &name) const
Definition: NamedVars.cpp:485
DLLEXPORT VariableBlock * GetValueDirect()
Definition: NamedVars.cpp:718
DLLEXPORT bool LoadVarsFromFile(const std::string &file, LErrorReporter *errorreport)
Definition: NamedVars.cpp:1241
Reference counted version for scripts of VariableBlock.
Definition: DataBlock.h:840
DLLEXPORT bool Add(std::shared_ptr< NamedVariableList > value)
Adds a value.
Definition: NamedVars.cpp:883
bool ConvertAndAssingToVariable(ConvertT &var) const
Definition: DataBlock.h:728
DLLEXPORT std::string GetName(size_t index)
Definition: NamedVars.cpp:1155
DLLEXPORT int GetCommonType() const
Definition: NamedVars.cpp:738
ScriptSafeVariableBlock * GetScriptCompatibleValue(const std::string &name)
Finds and returns the first value in a list matching name.
Definition: NamedVars.cpp:1267
DLLEXPORT ~NamedVars()
Definition: NamedVars.cpp:832
DLLEXPORT void AddVar(NamedVariableList *newvaluetoadd)
Definition: NamedVars.cpp:1198
bool CanAllBeCastedToType(const int &startindex, const int &endindex) const
Definition: NamedVars.h:118
DLLEXPORT void AddDataToPacket(sf::Packet &packet) const
Writes this NamedVars to a packet.
Definition: NamedVars.cpp:866
int GetVariableTypeOfAll(size_t index) const
Definition: NamedVars.h:323
DLLEXPORT NamedVariableList & operator=(const NamedVariableList &other)
Definition: NamedVars.cpp:569
DLLEXPORT int GetVariableType(const std::string &name) const
Definition: NamedVars.cpp:1131
DLLEXPORT void RemoveIfExists(const std::string &name, Lock &guard)
Removes a value with the given name if it exists.
Definition: NamedVars.cpp:1229
DLLEXPORT bool GetValues(const std::string &name, std::vector< const VariableBlock *> &receiver) const
Definition: NamedVars.cpp:1062
DLLEXPORT void PushValue(std::unique_ptr< VariableBlock > &&value)
Adds a new value.
Definition: NamedVars.cpp:457
DLLEXPORT void AddDataToPacket(sf::Packet &packet) const
For passing NamedVariableLists to other instances through the network.
Definition: NamedVars.cpp:354
DLLEXPORT bool CompareName(size_t index, const std::string &name) const
Definition: NamedVars.cpp:1183
int GetVariableType(size_t index) const
Definition: NamedVars.h:313
DLLEXPORT VariableBlock & operator[](const int &nindex)
Definition: NamedVars.cpp:770
DLLEXPORT NamedVariableList * GetValueDirectRaw(const std::string &name) const
Definition: NamedVars.cpp:1096
DLLEXPORT void SetName(const std::string &name)
Definition: NamedVars.cpp:480
#define DLLEXPORT
Definition: Include.h:115
DLLEXPORT void SetName(Lock &guard, size_t index, const std::string &name)
Definition: NamedVars.cpp:1170
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
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
DLLEXPORT std::vector< std::shared_ptr< NamedVariableList > > * GetVec()
Definition: NamedVars.cpp:1246
std::string GetName() const
Definition: NamedVars.h:139
#define GUARD_LOCK()
Definition: ThreadSafe.h:97
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
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16
DLLEXPORT int GetVariableTypeOfAll(const std::string &name) const
Definition: NamedVars.cpp:1143
DLLEXPORT bool operator!=(const NamedVariableList &other) const
Definition: NamedVars.cpp:606