Leviathan  0.8.0.0
Leviathan game engine
Leviathan::NamedVars Class Reference

#include <NamedVars.h>

+ Inheritance diagram for Leviathan::NamedVars:

Public Member Functions

 REFERENCE_COUNTED_PTR_TYPE (NamedVars)
 
DLLEXPORT NamedVars ()
 
DLLEXPORT NamedVars (NamedVars *stealfrom)
 Constructs a NamedVars by stealing variables from another. More...
 
DLLEXPORT NamedVars (const NamedVars &other)
 
DLLEXPORT NamedVars (const std::string &datadump, LErrorReporter *errorreport)
 
DLLEXPORT NamedVars (const std::vector< std::shared_ptr< NamedVariableList >> &variables)
 
DLLEXPORT NamedVars (std::shared_ptr< NamedVariableList > variable)
 
DLLEXPORT NamedVars (NamedVariableList *takevariable)
 
DLLEXPORT NamedVars (sf::Packet &packet)
 Loads a NamedVars object from a packet. More...
 
DLLEXPORT void AddDataToPacket (sf::Packet &packet) const
 Writes this NamedVars to a packet. More...
 
DLLEXPORT ~NamedVars ()
 
DLLEXPORT bool Add (std::shared_ptr< NamedVariableList > value)
 Adds a value. More...
 
DLLEXPORT bool SetValue (const std::string &name, const VariableBlock &value1)
 
DLLEXPORT bool SetValue (const std::string &name, VariableBlock *value1)
 
DLLEXPORT bool SetValue (const std::string &name, const std::vector< VariableBlock *> &values)
 
DLLEXPORT bool SetValue (NamedVariableList &nameandvalues)
 
DLLEXPORT size_t GetValueCount (const std::string &name) const
 
DLLEXPORT bool IsIndexValid (size_t index) const
 
DLLEXPORT VariableBlockGetValueNonConst (const std::string &name)
 
DLLEXPORT const VariableBlockGetValue (const std::string &name) const
 
DLLEXPORT bool GetValue (const std::string &name, VariableBlock &receiver) const
 
DLLEXPORT bool GetValue (const std::string &name, const int &index, VariableBlock &receiver) const
 Gets a VariableBlock from the specified index on the block matching name. More...
 
DLLEXPORT bool GetValue (const int &index, VariableBlock &receiver) const
 Gets constant value from vector index. More...
 
DLLEXPORT bool GetValues (const std::string &name, std::vector< const VariableBlock *> &receiver) const
 
DLLEXPORT std::shared_ptr< NamedVariableListGetValueDirect (const std::string &name) const
 
DLLEXPORT NamedVariableListGetValueDirectRaw (const std::string &name) const
 
DLLEXPORT NamedVariableListGetValueDirectRaw (size_t index) const
 
template<class T >
bool GetValueAndConvertTo (const std::string &name, T &receiver) const
 
DLLEXPORT std::vector< VariableBlock * > * GetValues (const std::string &name)
 
DLLEXPORT std::string Serialize (const std::string &lineprefix="") const
 Serializes this object into a string representation. More...
 
ScriptSafeVariableBlockGetScriptCompatibleValue (const std::string &name)
 Finds and returns the first value in a list matching name. More...
 
bool AddScriptCompatibleValue (ScriptSafeVariableBlock *value)
 
DLLEXPORT int GetVariableType (const std::string &name) const
 
DLLEXPORT int GetVariableType (Lock &guard, size_t index) const
 
int GetVariableType (size_t index) const
 
DLLEXPORT int GetVariableTypeOfAll (const std::string &name) const
 
DLLEXPORT int GetVariableTypeOfAll (Lock &guard, size_t index) const
 
int GetVariableTypeOfAll (size_t index) const
 
DLLEXPORT std::string GetName (size_t index)
 
DLLEXPORT bool GetName (size_t index, std::string &name) const
 
DLLEXPORT void SetName (Lock &guard, size_t index, const std::string &name)
 
void SetName (size_t index, const std::string &name)
 
DLLEXPORT void SetName (const std::string &oldname, const std::string &name)
 
DLLEXPORT bool CompareName (size_t index, const std::string &name) const
 
DLLEXPORT void AddVar (NamedVariableList *newvaluetoadd)
 
DLLEXPORT void AddVar (std::shared_ptr< NamedVariableList > values)
 
DLLEXPORT void AddVar (const std::string &name, VariableBlock *valuetosteal)
 
DLLEXPORT void Remove (size_t index)
 
DLLEXPORT void Remove (const std::string &name)
 
DLLEXPORT void RemoveIfExists (const std::string &name, Lock &guard)
 Removes a value with the given name if it exists. More...
 
DLLEXPORT bool LoadVarsFromFile (const std::string &file, LErrorReporter *errorreport)
 
DLLEXPORT std::vector< std::shared_ptr< NamedVariableList > > * GetVec ()
 
DLLEXPORT void SetVec (std::vector< std::shared_ptr< NamedVariableList >> &vec)
 
DLLEXPORT size_t GetVariableCount () const
 Returns the size of the internal variable vector. More...
 
 operator bool () const
 
size_t Find (const std::string &name) const
 
DLLEXPORT size_t Find (Lock &guard, const std::string &name) const
 
template<class T >
bool ShouldAddValueIfNotFoundOrWrongType (const std::string &name)
 
- Public Member Functions inherited from Leviathan::ReferenceCounted
 ReferenceCounted (const ReferenceCounted &other)=delete
 
ReferenceCountedoperator= (const ReferenceCounted &other)=delete
 
FORCE_INLINE void AddRef ()
 
FORCE_INLINE void Release ()
 removes a reference and deletes the object if reference count reaches zero More...
 
int32_t GetRefCount () const
 Returns the reference count. More...
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Additional Inherited Members

- Public Types inherited from Leviathan::ReferenceCounted
using basepointer = boost::intrusive_ptr< ReferenceCounted >
 
using refcountedpointer = boost::intrusive_ptr< ReferenceCounted >
 
- Static Public Member Functions inherited from Leviathan::ReferenceCounted
template<class ActualType >
static boost::intrusive_ptr< ActualType > WrapPtr (ActualType *ptr)
 Creates an intrusive_ptr from raw pointer. More...
 
template<class ActualType , class... Args>
static boost::intrusive_ptr< ActualType > MakeShared (Args &&... args)
 Constructs a new instance and wraps it. More...
 
- Protected Member Functions inherited from Leviathan::ReferenceCounted
DLLEXPORT ReferenceCounted ()
 
virtual DLLEXPORT ~ReferenceCounted ()
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Todo:
Make all methods throw exceptions on invalid operations

Definition at line 186 of file NamedVars.h.

Constructor & Destructor Documentation

◆ NamedVars() [1/8]

NamedVars::NamedVars ( )

Definition at line 781 of file NamedVars.cpp.

781  : Variables()
782 {
783  // nothing to initialize //
784 }

◆ NamedVars() [2/8]

DLLEXPORT NamedVars::NamedVars ( NamedVars stealfrom)

Constructs a NamedVars by stealing variables from another.

Note
The other object will be empty after this

Definition at line 795 of file NamedVars.cpp.

795  : Variables(stealfrom->Variables)
796 {
797  stealfrom->Variables.clear();
798 }

◆ NamedVars() [3/8]

NamedVars::NamedVars ( const NamedVars other)

Definition at line 785 of file NamedVars.cpp.

786 {
787  // deep copy is required here //
788  Variables.reserve(other.Variables.size());
789  for(size_t i = 0; i < other.Variables.size(); i++) {
790  Variables.push_back(
791  shared_ptr<NamedVariableList>(new NamedVariableList(*other.Variables[i])));
792  }
793 }
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27

◆ NamedVars() [4/8]

DLLEXPORT NamedVars::NamedVars ( const std::string &  datadump,
LErrorReporter errorreport 
)
Todo:
Allow predefined values

Definition at line 800 of file NamedVars.cpp.

800  :
801  Variables()
802 {
803 
804  // load data directly to vector //
805  if(!NamedVariableList::ProcessDataDump(datadump, Variables, errorreport, NULL)) {
806 
807  // error happened //
808 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
809  throw InvalidArgument("datadump processing failed");
810 #else
811  StateIsInvalid = true;
812  return;
813 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
814  }
815 }
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

◆ NamedVars() [5/8]

DLLEXPORT Leviathan::NamedVars::NamedVars ( const std::vector< std::shared_ptr< NamedVariableList >> &  variables)

◆ NamedVars() [6/8]

DLLEXPORT Leviathan::NamedVars::NamedVars ( std::shared_ptr< NamedVariableList variable)

◆ NamedVars() [7/8]

DLLEXPORT NamedVars::NamedVars ( NamedVariableList takevariable)
Parameters
takevariablenew allocated ptr that will be owned by this object

Definition at line 827 of file NamedVars.cpp.

827  : Variables(1)
828 {
829  Variables[0] = std::shared_ptr<NamedVariableList>(takevariable);
830 }

◆ NamedVars() [8/8]

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

Loads a NamedVars object from a packet.

Definition at line 838 of file NamedVars.cpp.

839 {
840  // First get the size //
841  int isize;
842 
843  if(!(packet >> isize)) {
844 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
845  throw InvalidArgument("packet has invalid format");
846 #else
847  StateIsInvalid = true;
848  return;
849 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
850  }
851 
852  // Reserve space //
853  Variables.reserve(isize);
854 
855  for(int i = 0; i < isize; i++) {
856 
857  shared_ptr<NamedVariableList> newvalue(new NamedVariableList(packet));
858 
859  if(!newvalue || !newvalue->IsValid())
860  continue;
861 
862  Variables.push_back(newvalue);
863  }
864 }
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27

◆ ~NamedVars()

NamedVars::~NamedVars ( )

Definition at line 832 of file NamedVars.cpp.

833 {
834  // no need to release due to smart pointers //
835 }

Member Function Documentation

◆ Add()

DLLEXPORT bool NamedVars::Add ( std::shared_ptr< NamedVariableList value)

Adds a value.

Returns
True if created new, false if updated an existing value

Definition at line 883 of file NamedVars.cpp.

884 {
885  GUARD_LOCK();
886 
887  auto index = Find(guard, value->Name);
888  // index check //
889  if(index >= Variables.size()) {
890 
891  Variables.push_back(value);
892  return true;
893  }
894 
895  Variables[index] = value;
896  return false;
897 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ AddDataToPacket()

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

Writes this NamedVars to a packet.

Definition at line 866 of file NamedVars.cpp.

867 {
868  GUARD_LOCK();
869 
870  // First write size //
871  int isize = (int)Variables.size();
872 
873  packet << isize;
874 
875  // Write each individual variable //
876  for(int i = 0; i < isize; i++) {
877 
878  Variables[i]->AddDataToPacket(packet);
879  }
880 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ AddScriptCompatibleValue()

bool NamedVars::AddScriptCompatibleValue ( ScriptSafeVariableBlock value)

For use from scripts

Decrements value refcount

Definition at line 1283 of file NamedVars.cpp.

1284 {
1285  GUARD_LOCK();
1286 
1287  RemoveIfExists(value->GetName(), guard);
1288 
1289  bool success = false;
1290 
1291  try {
1292 
1293  Variables.push_back(shared_ptr<NamedVariableList>(new NamedVariableList(value)));
1294  success = true;
1295 
1296  } catch(...) {
1297  }
1298 
1299  value->Release();
1300  return success;
1301 }
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
std::string GetName() const
Definition: DataBlock.h:813
FORCE_INLINE void Release()
removes a reference and deletes the object if reference count reaches zero
DLLEXPORT void RemoveIfExists(const std::string &name, Lock &guard)
Removes a value with the given name if it exists.
Definition: NamedVars.cpp:1229
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ AddVar() [1/3]

DLLEXPORT void NamedVars::AddVar ( NamedVariableList newvaluetoadd)

Definition at line 1198 of file NamedVars.cpp.

1199 {
1200  GUARD_LOCK();
1201  RemoveIfExists(newvaluetoadd->GetName(), guard);
1202  // create new smart pointer and push back //
1203  Variables.push_back(shared_ptr<NamedVariableList>(newvaluetoadd));
1204 }
DLLEXPORT void RemoveIfExists(const std::string &name, Lock &guard)
Removes a value with the given name if it exists.
Definition: NamedVars.cpp:1229
std::string GetName() const
Definition: NamedVars.h:139
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ AddVar() [2/3]

DLLEXPORT void Leviathan::NamedVars::AddVar ( std::shared_ptr< NamedVariableList values)

◆ AddVar() [3/3]

DLLEXPORT void NamedVars::AddVar ( const std::string &  name,
VariableBlock valuetosteal 
)

Definition at line 1206 of file NamedVars.cpp.

1207 {
1208  GUARD_LOCK();
1209  RemoveIfExists(name, guard);
1210  // create new smart pointer and push back //
1211  Variables.push_back(
1212  shared_ptr<NamedVariableList>(new NamedVariableList(name, valuetosteal)));
1213 }
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
DLLEXPORT void RemoveIfExists(const std::string &name, Lock &guard)
Removes a value with the given name if it exists.
Definition: NamedVars.cpp:1229
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ CompareName()

bool NamedVars::CompareName ( size_t  index,
const std::string &  name 
) const

Definition at line 1183 of file NamedVars.cpp.

1184 {
1185  GUARD_LOCK();
1186 
1187  return Variables[index]->CompareName(name);
1188 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ Find() [1/2]

size_t Leviathan::NamedVars::Find ( const std::string &  name) const
inline

Definition at line 367 of file NamedVars.h.

368  {
369  GUARD_LOCK();
370  return Find(guard, name);
371  }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ Find() [2/2]

DLLEXPORT size_t NamedVars::Find ( Lock guard,
const std::string &  name 
) const

Definition at line 1256 of file NamedVars.cpp.

1257 {
1258  for(size_t i = 0; i < Variables.size(); i++) {
1259  if(Variables[i]->CompareName(name))
1260  return i;
1261  }
1262 
1263  return std::numeric_limits<size_t>::max();
1264 }
DLLEXPORT bool CompareName(size_t index, const std::string &name) const
Definition: NamedVars.cpp:1183

◆ GetName() [1/2]

string NamedVars::GetName ( size_t  index)

Definition at line 1155 of file NamedVars.cpp.

1156 {
1157  GUARD_LOCK();
1158 
1159  return Variables[index]->GetName();
1160 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetName() [2/2]

DLLEXPORT bool NamedVars::GetName ( size_t  index,
std::string &  name 
) const

Definition at line 1162 of file NamedVars.cpp.

1163 {
1164  GUARD_LOCK();
1165 
1166  Variables[index]->GetName(name);
1167  return true;
1168 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetScriptCompatibleValue()

ScriptSafeVariableBlock * NamedVars::GetScriptCompatibleValue ( const std::string &  name)

Finds and returns the first value in a list matching name.

Warning
For use from scripts

Definition at line 1267 of file NamedVars.cpp.

1268 {
1269  // Use a try block to not throw exceptions to the script engine //
1270  try {
1271  VariableBlock& tmpblock = GetValueNonConst(name);
1272 
1273  // Create script safe version //
1274  return new ScriptSafeVariableBlock(&tmpblock, name);
1275 
1276 
1277  } catch(...) {
1278  // Something failed, return empty handle //
1279  return NULL;
1280  }
1281 }
DLLEXPORT VariableBlock & GetValueNonConst(const std::string &name)
Definition: NamedVars.cpp:957
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
Reference counted version for scripts of VariableBlock.
Definition: DataBlock.h:840

◆ GetValue() [1/4]

DLLEXPORT const VariableBlock * NamedVars::GetValue ( const std::string &  name) const

Definition at line 974 of file NamedVars.cpp.

975 {
976  GUARD_LOCK();
977 
978  auto index = Find(guard, name);
979 
980  if(index >= Variables.size()) {
981 
982 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
983  throw InvalidArgument("value not found");
984 #else
985  return nullptr;
986 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
987  }
988 
989  return Variables[index]->GetValueDirect();
990 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValue() [2/4]

DLLEXPORT bool NamedVars::GetValue ( const std::string &  name,
VariableBlock receiver 
) const

Definition at line 992 of file NamedVars.cpp.

993 {
994  GUARD_LOCK();
995 
996  auto index = Find(guard, name);
997  // index check //
998  if(index >= Variables.size()) {
999  return false;
1000  }
1001  // specific operator wanted here //
1002  receiver = const_cast<const VariableBlock&>(Variables[index]->GetValue());
1003  return true;
1004 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValue() [3/4]

DLLEXPORT bool NamedVars::GetValue ( const std::string &  name,
const int &  index,
VariableBlock receiver 
) const

Gets a VariableBlock from the specified index on the block matching name.

Definition at line 1006 of file NamedVars.cpp.

1008 {
1009  GUARD_LOCK();
1010 
1011  auto index = Find(guard, name);
1012 
1013  // index check //
1014  if(index >= Variables.size()) {
1015  return false;
1016  }
1017 
1018  // specific operator wanted here //
1019  receiver = const_cast<const VariableBlock&>(Variables[index]->GetValue(nindex));
1020  return true;
1021 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValue() [4/4]

DLLEXPORT bool NamedVars::GetValue ( const int &  index,
VariableBlock receiver 
) const

Gets constant value from vector index.

Definition at line 1023 of file NamedVars.cpp.

1024 {
1025  GUARD_LOCK();
1026 
1027  // index check //
1028  if(index >= static_cast<int>(Variables.size())) {
1029  return false;
1030  }
1031 
1032  // specific operator wanted here //
1033  receiver = const_cast<const VariableBlock&>(Variables[index]->GetValue(0));
1034  return true;
1035 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValueAndConvertTo()

template<class T >
bool Leviathan::NamedVars::GetValueAndConvertTo ( const std::string &  name,
T &  receiver 
) const
inline

Definition at line 261 of file NamedVars.h.

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  }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValueCount()

DLLEXPORT size_t NamedVars::GetValueCount ( const std::string &  name) const

Definition at line 1037 of file NamedVars.cpp.

1038 {
1039  GUARD_LOCK();
1040 
1041  auto index = Find(guard, name);
1042  // index check //
1043  if(index >= Variables.size()) {
1044  return 0;
1045  }
1046 
1047  return Variables[index]->GetVariableCount();
1048 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValueDirect()

DLLEXPORT std::shared_ptr< NamedVariableList > NamedVars::GetValueDirect ( const std::string &  name) const

Definition at line 1084 of file NamedVars.cpp.

1086 {
1087  GUARD_LOCK();
1088  auto index = Find(guard, name);
1089  // index check //
1090  if(index >= Variables.size()) {
1091  return NULL;
1092  }
1093  return Variables[index];
1094 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValueDirectRaw() [1/2]

DLLEXPORT NamedVariableList * NamedVars::GetValueDirectRaw ( const std::string &  name) const
Warning
You need to make sure that this is valid while the pointer is used

Definition at line 1096 of file NamedVars.cpp.

1097 {
1098  GUARD_LOCK();
1099  auto index = Find(guard, name);
1100  // index check //
1101  if(index >= Variables.size()) {
1102  return NULL;
1103  }
1104 
1105  return Variables[index].get();
1106 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValueDirectRaw() [2/2]

DLLEXPORT NamedVariableList * Leviathan::NamedVars::GetValueDirectRaw ( size_t  index) const

Definition at line 1108 of file NamedVars.cpp.

1109 {
1110  if(index >= Variables.size()) {
1111  return nullptr;
1112  }
1113 
1114  return Variables[index].get();
1115 }

◆ GetValueNonConst()

DLLEXPORT VariableBlock & NamedVars::GetValueNonConst ( const std::string &  name)

Definition at line 957 of file NamedVars.cpp.

958 {
959  GUARD_LOCK();
960  auto index = Find(guard, name);
961 
962  if(index >= Variables.size()) {
963 
964 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
965  throw InvalidArgument("value not found");
966 #else
967  LEVIATHAN_ASSERT(0, "NamedVars name not found");
968 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
969  }
970 
971  return Variables[index]->GetValue();
972 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValues() [1/2]

DLLEXPORT bool NamedVars::GetValues ( const std::string &  name,
std::vector< const VariableBlock *> &  receiver 
) const

Definition at line 1062 of file NamedVars.cpp.

1064 {
1065  GUARD_LOCK();
1066  auto index = Find(guard, name);
1067  // index check //
1068  if(index >= Variables.size()) {
1069  return false;
1070  }
1071  vector<VariableBlock*>& tmpvals = Variables[index]->GetValues();
1072 
1073  vector<const VariableBlock*> tmpconsted(tmpvals.size());
1074 
1075  for(size_t i = 0; i < tmpconsted.size(); i++) {
1076 
1077  tmpconsted[i] = const_cast<const VariableBlock*>(tmpvals[i]);
1078  }
1079 
1080  receiver = tmpconsted;
1081  return true;
1082 }
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetValues() [2/2]

DLLEXPORT vector< VariableBlock * > * NamedVars::GetValues ( const std::string &  name)

Definition at line 1050 of file NamedVars.cpp.

1051 {
1052  GUARD_LOCK();
1053  auto index = Find(guard, name);
1054  // index check //
1055  if(index >= Variables.size()) {
1056  return NULL;
1057  }
1058 
1059  return &Variables[index]->GetValues();
1060 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetVariableCount()

DLLEXPORT size_t NamedVars::GetVariableCount ( ) const

Returns the size of the internal variable vector.

Definition at line 1304 of file NamedVars.cpp.

1305 {
1306  return Variables.size();
1307 }

◆ GetVariableType() [1/3]

DLLEXPORT int NamedVars::GetVariableType ( const std::string &  name) const

Definition at line 1131 of file NamedVars.cpp.

1132 {
1133  GUARD_LOCK();
1134  // call overload //
1135  return GetVariableType(guard, Find(guard, name));
1136 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
DLLEXPORT int GetVariableType(const std::string &name) const
Definition: NamedVars.cpp:1131
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetVariableType() [2/3]

DLLEXPORT int NamedVars::GetVariableType ( Lock guard,
size_t  index 
) const

Definition at line 1138 of file NamedVars.cpp.

1139 {
1140  return Variables[index]->GetVariableType();
1141 }

◆ GetVariableType() [3/3]

int Leviathan::NamedVars::GetVariableType ( size_t  index) const
inline

Definition at line 313 of file NamedVars.h.

314  {
315  GUARD_LOCK();
316  return GetVariableType(guard, index);
317  }
DLLEXPORT int GetVariableType(const std::string &name) const
Definition: NamedVars.cpp:1131
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ GetVariableTypeOfAll() [1/3]

DLLEXPORT int NamedVars::GetVariableTypeOfAll ( const std::string &  name) const

Definition at line 1143 of file NamedVars.cpp.

1144 {
1145  GUARD_LOCK();
1146  // call overload //
1147  return GetVariableTypeOfAll(guard, Find(guard, name));
1148 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97
DLLEXPORT int GetVariableTypeOfAll(const std::string &name) const
Definition: NamedVars.cpp:1143

◆ GetVariableTypeOfAll() [2/3]

DLLEXPORT int NamedVars::GetVariableTypeOfAll ( Lock guard,
size_t  index 
) const

Definition at line 1150 of file NamedVars.cpp.

1151 {
1152  return Variables[index]->GetCommonType();
1153 }

◆ GetVariableTypeOfAll() [3/3]

int Leviathan::NamedVars::GetVariableTypeOfAll ( size_t  index) const
inline

Definition at line 323 of file NamedVars.h.

324  {
325  GUARD_LOCK();
326  return GetVariableTypeOfAll(guard, index);
327  }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97
DLLEXPORT int GetVariableTypeOfAll(const std::string &name) const
Definition: NamedVars.cpp:1143

◆ GetVec()

vector< shared_ptr< NamedVariableList > > * NamedVars::GetVec ( )

Definition at line 1246 of file NamedVars.cpp.

1247 {
1248  return &Variables;
1249 }

◆ IsIndexValid()

DLLEXPORT bool Leviathan::NamedVars::IsIndexValid ( size_t  index) const
inline
Returns
True if index is valid

Definition at line 232 of file NamedVars.h.

233  {
234  return index < Variables.size();
235  }

◆ LoadVarsFromFile()

bool NamedVars::LoadVarsFromFile ( const std::string &  file,
LErrorReporter errorreport 
)

Definition at line 1241 of file NamedVars.cpp.

1242 {
1243  // call datadump loaded with this object's vector //
1244  return FileSystem::LoadDataDump(file, Variables, errorreport);
1245 }
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:308

◆ operator bool()

Leviathan::NamedVars::operator bool ( ) const
inline

Definition at line 360 of file NamedVars.h.

361  {
362  return !StateIsInvalid;
363  }

◆ REFERENCE_COUNTED_PTR_TYPE()

Leviathan::NamedVars::REFERENCE_COUNTED_PTR_TYPE ( NamedVars  )

◆ Remove() [1/2]

void NamedVars::Remove ( size_t  index)

Definition at line 1215 of file NamedVars.cpp.

1216 {
1217  GUARD_LOCK();
1218 
1219  // smart pointers //
1220  Variables.erase(Variables.begin() + index);
1221 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ Remove() [2/2]

DLLEXPORT void NamedVars::Remove ( const std::string &  name)

Definition at line 1223 of file NamedVars.cpp.

1224 {
1225  // call overload //
1226  Remove(Find(name));
1227 }
DLLEXPORT void Remove(size_t index)
Definition: NamedVars.cpp:1215
size_t Find(const std::string &name) const
Definition: NamedVars.h:367

◆ RemoveIfExists()

DLLEXPORT void NamedVars::RemoveIfExists ( const std::string &  name,
Lock guard 
)

Removes a value with the given name if it exists.

Definition at line 1229 of file NamedVars.cpp.

1230 {
1231  // Try to find it //
1232  size_t index = Find(guard, name);
1233 
1234  if(index >= Variables.size())
1235  return;
1236 
1237 
1238  Variables.erase(Variables.begin() + index);
1239 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367

◆ Serialize()

DLLEXPORT std::string Leviathan::NamedVars::Serialize ( const std::string &  lineprefix = "") const

Serializes this object into a string representation.

Parameters
lineprefixAppended before each new line

Definition at line 1117 of file NamedVars.cpp.

1119 {
1120  std::string result;
1121 
1122  for(const auto& variable : Variables) {
1123 
1124  result += lineprefix + variable->ToText(0) + "\n";
1125  }
1126 
1127  return result;
1128 }

◆ SetName() [1/3]

void NamedVars::SetName ( Lock guard,
size_t  index,
const std::string &  name 
)

Definition at line 1170 of file NamedVars.cpp.

1171 {
1172 
1173  Variables[index]->SetName(name);
1174 }

◆ SetName() [2/3]

void Leviathan::NamedVars::SetName ( size_t  index,
const std::string &  name 
)
inline

Definition at line 334 of file NamedVars.h.

335  {
336  GUARD_LOCK();
337  SetName(guard, index, name);
338  }
DLLEXPORT void SetName(Lock &guard, size_t index, const std::string &name)
Definition: NamedVars.cpp:1170
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetName() [3/3]

void NamedVars::SetName ( const std::string &  oldname,
const std::string &  name 
)

Definition at line 1176 of file NamedVars.cpp.

1177 {
1178  GUARD_LOCK();
1179  // call overload //
1180  SetName(guard, Find(guard, oldname), name);
1181 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
DLLEXPORT void SetName(Lock &guard, size_t index, const std::string &name)
Definition: NamedVars.cpp:1170
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetValue() [1/4]

DLLEXPORT bool NamedVars::SetValue ( const std::string &  name,
const VariableBlock value1 
)

Definition at line 899 of file NamedVars.cpp.

900 {
901  GUARD_LOCK();
902  auto index = Find(name);
903 
904  if(index >= Variables.size())
905  return false;
906 
907 
908  Variables[index]->SetValue(value1);
909  return true;
910 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetValue() [2/4]

DLLEXPORT bool NamedVars::SetValue ( const std::string &  name,
VariableBlock value1 
)

Definition at line 912 of file NamedVars.cpp.

913 {
914  GUARD_LOCK();
915  auto index = Find(guard, name);
916 
917  if(index >= Variables.size())
918  return false;
919 
920 
921  Variables[index]->SetValue(value1);
922  return true;
923 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetValue() [3/4]

DLLEXPORT bool NamedVars::SetValue ( const std::string &  name,
const std::vector< VariableBlock *> &  values 
)

Definition at line 925 of file NamedVars.cpp.

927 {
928  GUARD_LOCK();
929  auto index = Find(name);
930 
931  if(index >= Variables.size())
932  return false;
933 
934 
935  Variables[index]->SetValue(values);
936  return true;
937 }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetValue() [4/4]

DLLEXPORT bool NamedVars::SetValue ( NamedVariableList nameandvalues)

Definition at line 939 of file NamedVars.cpp.

940 {
941  GUARD_LOCK();
942  auto index = Find(nameandvalues.Name);
943  // index check //
944  if(index >= Variables.size()) {
945 
946  Variables.push_back(
947  shared_ptr<NamedVariableList>(new NamedVariableList(nameandvalues)));
948  return true;
949  }
950 
951  nameandvalues.Name.clear();
952  // set values with "swap" //
953  NamedVariableList::SwitchValues(*Variables[index].get(), nameandvalues);
954  return true;
955 }
static DLLEXPORT void SwitchValues(NamedVariableList &receiver, NamedVariableList &donator)
Switches values to a new instance.
Definition: NamedVars.cpp:698
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
size_t Find(const std::string &name) const
Definition: NamedVars.h:367
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ SetVec()

void NamedVars::SetVec ( std::vector< std::shared_ptr< NamedVariableList >> &  vec)

Definition at line 1250 of file NamedVars.cpp.

1251 {
1252  GUARD_LOCK();
1253  Variables = vec;
1254 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:97

◆ ShouldAddValueIfNotFoundOrWrongType()

template<class T >
bool Leviathan::NamedVars::ShouldAddValueIfNotFoundOrWrongType ( const std::string &  name)
inline

Definition at line 376 of file NamedVars.h.

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  }
size_t Find(const std::string &name) const
Definition: NamedVars.h:367

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