FML
Loading...
Searching...
No Matches
ParameterMap.h
Go to the documentation of this file.
1#ifndef PARAMETERMAP_HEADER
2#define PARAMETERMAP_HEADER
3
4#include <cmath>
5#include <iomanip>
6#include <iostream>
7#include <map>
8#include <stdexcept>
9#include <string>
10#include <variant>
11#include <vector>
12
13namespace FML {
14 namespace UTILS {
15
16 std::ostream & operator<<(std::ostream & s, std::vector<double> const & v);
17 std::ostream & operator<<(std::ostream & s, std::vector<int> const & v);
18 std::ostream & operator<<(std::ostream & s, std::vector<std::string> const & v);
19
20 /// cout for a variant
21 template <typename T0, typename... Ts>
22 std::ostream & operator<<(std::ostream & s, std::variant<T0, Ts...> const & v) {
23 std::visit([&](auto && arg) { s << arg; }, v);
24 return s;
25 }
26
27 /// All the types we can have in the parameter file: string, int, boolean, double and vectors of double and ints
28 /// Can be extended if needed. Only thing that is required is that the type you add have a cout-overload for
29 /// printing the value
30 using ParameterTypes = std::
31 variant<bool, int, double, std::string, std::vector<int>, std::vector<double>, std::vector<std::string>>;
32
33 //============================================================================
34 ///
35 /// For holding a map of parameters of different types with easy set and gets
36 /// allowing for fiducial values if the parameter is not in the map
37 ///
38 /// Uses std::variant which requires a C++17 compatible compiler.
39 ///
40 /// Errors handled via the throw_error function.
41 ///
42 /// Compile time defines:
43 ///
44 /// USE_MPI : Use MPI (only difference is in how errors are handled)
45 ///
46 //============================================================================
47
49 private:
50 std::map<std::string, ParameterTypes> parameters{};
51
52 void throw_error(std::string errormessage) const;
53
54 public:
55 ParameterMap() = default;
56 ParameterMap & operator=(const ParameterMap & rhs) = default;
57 ParameterMap & operator=(ParameterMap && other) = default;
58 ParameterMap(const ParameterMap & rhs) = default;
59 ParameterMap(ParameterMap && rhs) = default;
60 ~ParameterMap() = default;
61
62 ParameterTypes & operator[](std::string rhs) {
63 auto & x = parameters[rhs];
64 return x;
65 }
66
67 /// Show info about the parameter map
68 void info() const;
69
70 /// Check if a parameter map contains a value
71 bool contains(std::string name) const;
72
73 /// Get a reference to the underlying map
74 std::map<std::string, ParameterTypes> & get_map();
75
76 /// Fetch a value. If it doesn't exist or has another type we throw an error (std::runtime_error)
77 template <typename T>
78 T get(std::string name) const;
79
80 /// Fetch a value. If it doesn't exist we use the provided fiducial value. If it exists with another type
81 /// it is an error (std::runtime_error)
82 template <typename T>
83 T get(std::string name, T fiducial_value) const;
84 };
85 } // namespace UTILS
86} // namespace FML
87
88#endif
std::string name
std::function< std::string(const RunSettings &)> get
For holding a map of parameters of different types with easy set and gets allowing for fiducial value...
ParameterMap & operator=(const ParameterMap &rhs)=default
ParameterMap & operator=(ParameterMap &&other)=default
std::map< std::string, ParameterTypes > & get_map()
Get a reference to the underlying map.
ParameterMap(const ParameterMap &rhs)=default
void info() const
Show info about the parameter map.
ParameterTypes & operator[](std::string rhs)
bool contains(std::string name) const
Check if a parameter map contains a value.
ParameterMap(ParameterMap &&rhs)=default
std:: variant< bool, int, double, std::string, std::vector< int >, std::vector< double >, std::vector< std::string > > ParameterTypes
All the types we can have in the parameter file: string, int, boolean, double and vectors of double a...
std::ostream & operator<<(std::ostream &s, std::vector< double > const &v)