replace const string& by string_view in Flags and SymbolTable

This commit is contained in:
Joachim Schoeberl 2024-07-16 12:44:04 +02:00
parent 63986a4e5f
commit 20e0b3efa5
3 changed files with 43 additions and 33 deletions

View File

@ -16,6 +16,7 @@
namespace ngcore namespace ngcore
{ {
using std::string; using std::string;
using std::string_view;
using std::endl; using std::endl;
Flags :: Flags () { ; } Flags :: Flags () { ; }
@ -209,18 +210,18 @@ namespace ngcore
} }
double Flags :: GetNumFlag (const string & name, double def) const double Flags :: GetNumFlag (string_view name, double def) const
{ {
if (numflags.Used (name)) if (numflags.Used (name))
return numflags[name]; return numflags[string(name)];
else else
return def; return def;
} }
const double * Flags :: GetNumFlagPtr (const string & name) const const double * Flags :: GetNumFlagPtr (string_view name) const
{ {
if (numflags.Used (name)) if (numflags.Used (name))
return & ((SymbolTable<double>&)numflags)[name]; return & ((SymbolTable<double>&)numflags)[string(name)];
else else
return NULL; return NULL;
} }
@ -239,16 +240,16 @@ namespace ngcore
return defflags.Used (name); return defflags.Used (name);
} }
*/ */
bool Flags :: GetDefineFlag (const string & name) const throw() bool Flags :: GetDefineFlag (string_view name) const throw()
{ {
if (!defflags.Used (name)) return false; if (!defflags.Used (string(name))) return false;
return defflags[name]; return defflags[string(name)];
} }
xbool Flags :: GetDefineFlagX (const string & name) const throw() xbool Flags :: GetDefineFlagX (string_view name) const throw()
{ {
if (!defflags.Used (name)) return maybe; if (!defflags.Used (string(name))) return maybe;
return bool(defflags[name]); return bool(defflags[string(name)]);
} }
@ -296,32 +297,32 @@ namespace ngcore
return empty; return empty;
} }
bool Flags :: StringFlagDefined (const string & name) const bool Flags :: StringFlagDefined (string_view name) const noexcept
{ {
return strflags.Used (name); return strflags.Used (name);
} }
bool Flags :: NumFlagDefined (const string &name) const bool Flags :: NumFlagDefined (string_view name) const noexcept
{ {
return numflags.Used (name); return numflags.Used (name);
} }
bool Flags :: FlagsFlagDefined (const string &name) const bool Flags :: FlagsFlagDefined (string_view name) const noexcept
{ {
return flaglistflags.Used (name); return flaglistflags.Used (name);
} }
bool Flags :: StringListFlagDefined (const string & name) const bool Flags :: StringListFlagDefined (string_view name) const noexcept
{ {
return strlistflags.Used (name); return strlistflags.Used (name);
} }
bool Flags :: NumListFlagDefined (const string & name) const bool Flags :: NumListFlagDefined (string_view name) const noexcept
{ {
return numlistflags.Used (name); return numlistflags.Used (name);
} }
bool Flags :: AnyFlagDefined (const string& name) const bool Flags :: AnyFlagDefined (string_view name) const noexcept
{ {
return anyflags.Used(name); return anyflags.Used(name);
} }

View File

@ -125,15 +125,15 @@ namespace ngcore
/// Returns std::string flag, default value if not exists /// Returns std::string flag, default value if not exists
std::string GetStringFlag (const std::string & name, std::string def = "") const; std::string GetStringFlag (const std::string & name, std::string def = "") const;
/// Returns numerical flag, default value if not exists /// Returns numerical flag, default value if not exists
double GetNumFlag (const std::string & name, double def) const; double GetNumFlag (std::string_view name, double def) const;
/// Returns address of numerical flag, null if not exists /// Returns address of numerical flag, null if not exists
const double * GetNumFlagPtr (const std::string & name) const; const double * GetNumFlagPtr (std::string_view name) const;
/// Returns address of numerical flag, null if not exists /// Returns address of numerical flag, null if not exists
double * GetNumFlagPtr (const std::string & name); double * GetNumFlagPtr (const std::string & name);
/// Returns boolean flag /// Returns boolean flag
// int GetDefineFlag (const char * name) const; // int GetDefineFlag (const char * name) const;
bool GetDefineFlag (const std::string & name) const throw(); bool GetDefineFlag (std::string_view name) const noexcept;
xbool GetDefineFlagX (const std::string & name) const throw(); xbool GetDefineFlagX (std::string_view name) const noexcept;
/// Returns string list flag, empty array if not exist /// Returns string list flag, empty array if not exist
const Array<std::string> & GetStringListFlag (const std::string & name) const; const Array<std::string> & GetStringListFlag (const std::string & name) const;
/// Returns num list flag, empty array if not exist /// Returns num list flag, empty array if not exist
@ -144,16 +144,16 @@ namespace ngcore
/// Test, if string flag is defined /// Test, if string flag is defined
bool StringFlagDefined (const std::string & name) const; bool StringFlagDefined (std::string_view name) const noexcept;
/// Test, if num flag is defined /// Test, if num flag is defined
bool NumFlagDefined (const std::string & name) const; bool NumFlagDefined (std::string_view name) const noexcept;
/// Test, if num flag is defined /// Test, if num flag is defined
bool FlagsFlagDefined (const std::string & name) const; bool FlagsFlagDefined (std::string_view name) const noexcept;
/// Test, if string list flag is defined /// Test, if string list flag is defined
bool StringListFlagDefined (const std::string & name) const; bool StringListFlagDefined (std::string_view name) const noexcept;
/// Test, if num list flag is defined /// Test, if num list flag is defined
bool NumListFlagDefined (const std::string & name) const; bool NumListFlagDefined (std::string_view name) const noexcept;
bool AnyFlagDefined (const std::string& name) const; bool AnyFlagDefined (std::string_view name) const noexcept;
/// number of string flags /// number of string flags
int GetNStringFlags () const { return strflags.Size(); } int GetNStringFlags () const { return strflags.Size(); }

View File

@ -45,7 +45,7 @@ namespace ngcore
} }
/// INDEX of symbol name, throws exception if unused /// INDEX of symbol name, throws exception if unused
size_t Index (const std::string & name) const size_t Index (std::string_view name) const
{ {
for (size_t i = 0; i < names.size(); i++) for (size_t i = 0; i < names.size(); i++)
if (names[i] == name) return i; if (names[i] == name) return i;
@ -53,7 +53,7 @@ namespace ngcore
} }
/// Index of symbol name, returns -1 if unused /// Index of symbol name, returns -1 if unused
int CheckIndex (const std::string & name) const int CheckIndex (std::string_view name) const
{ {
for (int i = 0; i < names.size(); i++) for (int i = 0; i < names.size(); i++)
if (names[i] == name) return i; if (names[i] == name) return i;
@ -67,12 +67,12 @@ namespace ngcore
} }
/// Returns reference to element. exception for unused identifier /// Returns reference to element. exception for unused identifier
reference operator[] (const std::string & name) reference operator[] (std::string_view name)
{ {
return data[Index (name)]; return data[Index (name)];
} }
const_reference operator[] (const std::string & name) const const_reference operator[] (std::string_view name) const
{ {
return data[Index (name)]; return data[Index (name)];
} }
@ -99,7 +99,7 @@ namespace ngcore
} }
/// Associates el to the string name, overrides if name is used /// Associates el to the string name, overrides if name is used
void Set (const std::string & name, const T & el) void Set (std::string_view name, const T & el)
{ {
int i = CheckIndex (name); int i = CheckIndex (name);
if (i >= 0) if (i >= 0)
@ -107,15 +107,24 @@ namespace ngcore
else else
{ {
data.push_back(el); data.push_back(el);
names.push_back(name); names.push_back(std::string(name));
} }
} }
/*
bool Used (const std::string & name) const bool Used (const std::string & name) const
{ {
return CheckIndex(name) >= 0; return CheckIndex(name) >= 0;
} }
*/
bool Used (std::string_view name) const
{
return CheckIndex(name) >= 0;
}
/// Deletes symboltable /// Deletes symboltable
inline void DeleteAll () inline void DeleteAll ()
{ {