carl  24.04
Computer ARithmetic Library
Settings.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "settings_utils.h"
4 
5 #include <any>
6 #include <map>
7 #include <string>
8 
9 namespace carl::settings {
10 
11 /**
12  * Base class for central settings class.
13  *
14  * Wraps a map from a string identifier to some struct holding the actual settings, wrapped as `std::any`.
15  * Simply call `.get<SettingsData>("identifier")` to obtain a reference to the settings data, which is created (and thereby initialized) lazily.
16  */
17 struct Settings {
18 private:
19  /// Maps identifier to any object holding the actual settings data.
20  std::map<std::string,std::any> mSettings;
21 public:
22  /// Get settings data of type `T` from the identifier `name`. Constructs the data object if it does not exist yet.
23  template<typename T>
24  T& get(const std::string& name) {
25  auto res = mSettings.emplace(name, T{});
26  return std::any_cast<T&>(res.first->second);
27  }
28 };
29 
30 }
Base class for central settings class.
Definition: Settings.h:17
T & get(const std::string &name)
Get settings data of type T from the identifier name. Constructs the data object if it does not exist...
Definition: Settings.h:24
std::map< std::string, std::any > mSettings
Maps identifier to any object holding the actual settings data.
Definition: Settings.h:20