From 0c2430f3dce24fdb530d2ada338fe928589f73c4 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 8 Feb 2021 15:44:15 +0100 Subject: [PATCH] add std::any symboltable to Flags to store arbitrary objects --- libsrc/core/flags.cpp | 23 ++++++++++++++++++++++- libsrc/core/flags.hpp | 7 +++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/libsrc/core/flags.cpp b/libsrc/core/flags.cpp index cdf5a7e7..e3d845cf 100644 --- a/libsrc/core/flags.cpp +++ b/libsrc/core/flags.cpp @@ -51,6 +51,10 @@ namespace ngcore auto lflags = flags.GetFlagsFlag (i, name); SetFlag (name, lflags); } + for(auto i : Range(flags.anyflags.Size())) + { + SetFlag(flags.anyflags.GetName(i), flags.anyflags[i]); + } } Flags :: Flags (Flags && flags) @@ -178,7 +182,11 @@ namespace ngcore return *this; } - + Flags & Flags :: SetFlag (const string & name, const std::any & val) + { + anyflags.Set(name, val); + return *this; + } string Flags :: GetStringFlag (const string & name, const char * def) const { @@ -279,6 +287,14 @@ namespace ngcore } } + const std::any& Flags:: GetAnyFlag(const std::string& name) const + { + if(anyflags.Used(name)) + return anyflags[name]; + static std::any empty; + return empty; + } + bool Flags :: StringFlagDefined (const string & name) const { return strflags.Used (name); @@ -304,6 +320,11 @@ namespace ngcore return numlistflags.Used (name); } + bool Flags :: AnyFlagDefined (const string& name) const + { + return anyflags.Used(name); + } + void Flags :: SaveFlags (ostream & str) const { for (int i = 0; i < strflags.Size(); i++) diff --git a/libsrc/core/flags.hpp b/libsrc/core/flags.hpp index ea4a093c..cd4b8272 100644 --- a/libsrc/core/flags.hpp +++ b/libsrc/core/flags.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "array.hpp" #include "symboltable.hpp" @@ -38,6 +39,8 @@ namespace ngcore SymbolTable>> numlistflags; /// flags list flags SymbolTable flaglistflags; + /// any object can be stored as a flag + SymbolTable anyflags; public: /// no flags Flags (); @@ -94,6 +97,8 @@ namespace ngcore Flags & SetFlag (const std::string & name, const Array & val); /// Sets double array flag Flags & SetFlag (const std::string & name, const Array & val); + /// Sets any flag + Flags & SetFlag(const std::string& name, const std::any& val); Flags SetFlag (const char * name, bool b = true) &&; @@ -135,6 +140,7 @@ namespace ngcore const Array & GetNumListFlag (const std::string & name) const; /// Returns flag list flag, empty flag if not exist const Flags & GetFlagsFlag (const std::string & name) const; + const std::any& GetAnyFlag (const std::string& name) const; /// Test, if string flag is defined @@ -147,6 +153,7 @@ namespace ngcore bool StringListFlagDefined (const std::string & name) const; /// Test, if num list flag is defined bool NumListFlagDefined (const std::string & name) const; + bool AnyFlagDefined (const std::string& name) const; /// number of string flags int GetNStringFlags () const { return strflags.Size(); }