ABLATE Source Documentation  0.12.34
mapParameters.hpp
1 #ifndef ABLATELIBRARY_MAPPARAMETERS_HPP
2 #define ABLATELIBRARY_MAPPARAMETERS_HPP
3 
4 #include <initializer_list>
5 #include <iostream>
6 #include <map>
7 #include <memory>
8 #include <sstream>
9 #include "parameters.hpp"
10 
11 namespace ablate::parameters {
12 class MapParameters : public Parameters {
13  protected:
14  std::map<std::string, std::string> values;
15 
16  public:
20  struct Parameter {
21  template <typename T>
22  Parameter(std::string_view key, T value) : key{key} {
23  // convert to string
24  std::stringstream ss;
25  ss << value;
26  ss >> this->value;
27  }
28 
29  std::string key;
30  std::string value;
31  };
32 
36  MapParameters(std::initializer_list<Parameter>);
37 
38  /*
39  * Take a map directly
40  */
41  explicit MapParameters(std::map<std::string, std::string> values = {});
42 
48  [[nodiscard]] std::optional<std::string> GetString(std::string paramName) const override;
49 
54  [[nodiscard]] std::unordered_set<std::string> GetKeys() const override;
55 
60  [[nodiscard]] const std::map<std::string, std::string>& GetMap() const { return values; }
61 
68  template <class T>
69  void Insert(std::string key, T value) {
70  // convert to string
71  std::stringstream ss;
72  ss << value;
73  values[key] = ss.str();
74  }
75 
81  static std::shared_ptr<MapParameters> Create(std::initializer_list<Parameter>);
82 
87  static std::shared_ptr<MapParameters> Create(const std::map<std::string, std::string>& values);
88 };
89 } // namespace ablate::parameters
90 
91 #endif // ABLATELIBRARY_MAPPARAMETERS_HPP
Definition: mapParameters.hpp:12
static std::shared_ptr< MapParameters > Create(std::initializer_list< Parameter >)
Definition: mapParameters.cpp:28
const std::map< std::string, std::string > & GetMap() const
Definition: mapParameters.hpp:60
MapParameters(std::initializer_list< Parameter >)
Definition: mapParameters.cpp:22
void Insert(std::string key, T value)
Definition: mapParameters.hpp:69
std::unordered_set< std::string > GetKeys() const override
Definition: mapParameters.cpp:14
std::optional< std::string > GetString(std::string paramName) const override
Definition: mapParameters.cpp:6
Definition: parameters.hpp:15
Definition: mapParameters.hpp:20