SettingsManager.hpp

A simple tool for managing configuration settings.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2025 Michigan State University MIT Software license; see doc/LICENSE.md

Settings can be either a string of a numerical value (size_t or double).

DEVELOPER NOTES:

  • Consider allowing types to be more dynamic, perhaps set in a template.

Note

An older version of this class became SettingCombos.hpp

Note

Status: Alpha

Defines

INCLUDE_EMP_CONFIG_SETTINGS_MANAGER_HPP_GUARD
class SettingsManager
#include <SettingsManager.hpp>

Public Functions

inline bool Has(const String &name) const
template<typename T>
inline T Get(const String &name) const
inline const String &GetDesc(const String &name) const
inline char GetFlag(const String &name) const
inline const String &GetOption(const String &name) const
template<typename T>
inline void Set(const String &name, T &&value)
inline bool HasError() const
inline const String &GetError() const
template<typename T>
inline SettingsManager &AddSetting(const String &name, T &value, String desc, char flag = '\0', String option = "")
inline void Save(String filename) const
inline bool Load(const std::string &filename)

Private Types

using val_t = std::variant<String, bool, int, double>

Private Functions

inline SettingInfo &GetInfo(const String &name)
inline const SettingInfo &GetInfo(const String &name) const

Private Members

std::map<String, SettingInfo> setting_map
String assign_op = ":"
String error_note
class SettingInfo

Class to manage a single setting.

Public Functions

SettingInfo() = delete
SettingInfo(const SettingInfo&) = default
SettingInfo(SettingInfo&&) = default
inline SettingInfo(String name, String &var, String desc, char flag = '\0', String option = "")
inline SettingInfo(String name, bool &var, String desc, char flag = '\0', String option = "")
inline SettingInfo(String name, int &var, String desc, char flag = '\0', String option = "")
inline SettingInfo(String name, double &var, String desc, char flag = '\0', String option = "")
template<typename T>
inline bool IsType() const
inline bool IsString() const
inline bool IsBool() const
inline bool IsInt() const
inline bool IsDouble() const
inline const String &GetName() const
inline const String &GetDescription() const
inline char GetFlag() const
inline const String &GetOption() const
template<typename T>
inline const T &GetValue() const
template<typename T>
inline void SetValue(T &&val)

Set the value; must maintain current type.

inline void SetFromString(const String &input)

If input is a string, convert it to the correct type.

inline String AsString() const

Convert to string for display.

inline String AsLiteral() const
inline std::string GetTypeName() const

Check the type.

Private Functions

inline val_t ParseFromString(String str) const

Action to take when set.

Private Members

String name

Label for this setting in config files.

val_t value

Current value of this setting.

String desc = ""

Description of setting.

char flag = '\0'

Command-line flag (‘\0’ for none)

String option = ""

Command-line longer option (”” for none)

std::function<void(const SettingInfo&)> action