FML
ParameterMap.cpp
Go to the documentation of this file.
1#include "ParameterMap.h"
2
3namespace FML {
4 namespace UTILS {
5
6 std::ostream & operator<<(std::ostream & s, std::vector<double> const & v) {
7 s << "[";
8 for (auto & e : v)
9 s << e << " ";
10 s << "]";
11 return s;
12 }
13
14 std::ostream & operator<<(std::ostream & s, std::vector<int> const & v) {
15 s << "[";
16 for (auto & e : v)
17 s << e << " ";
18 s << "]";
19 return s;
20 }
21
22 std::ostream & operator<<(std::ostream & s, std::vector<std::string> const & v) {
23 s << "[";
24 for (auto & e : v)
25 s << e << " ";
26 s << "]";
27 return s;
28 }
29
30 void ParameterMap::throw_error(std::string errormessage) const { throw std::runtime_error(errormessage); }
31
32 // Print a parameter map
33 void ParameterMap::info() const {
34 std::cout << std::boolalpha;
35 std::cout << "\n#=====================================================\n";
36 std::cout << "# Parameter map contains " << parameters.size() << " elements:\n";
37 std::cout << "#=====================================================\n";
38#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER + __INTEL_COMPILER_UPDATE < 1904)
39 std::cout << "Showing values is currently disabled for Intel compilers < 19.0.4.243 as old versions have a "
40 "compiler bug with std::variant\n";
41 std::cout << "You can try commening this test out in FML/ParameterMap/ParameterMap.cpp"
42 << " if you want to try to use it, but it will likely fail!\n";
43 for (auto && param : parameters) {
44 std::string name = param.first;
45 std::cout << " " << std::setw(30) << std::left << name << " : (showing value disabled)\n";
46 }
47#else
48 for (auto && param : parameters) {
49 std::string name = param.first;
50 ParameterTypes value = param.second;
51 std::cout << " " << std::setw(30) << std::left << name << " : ";
52 if(value.index() != 0)
53 std::cout << std::setw(15);
54 std::cout << value << "\n";
55 }
56#endif
57 std::cout << "#=====================================================\n\n";
58 }
59
60 std::map<std::string, ParameterTypes> & ParameterMap::get_map() { return parameters; }
61
62 // Fetch a parameter from the map
63 template <typename T>
64 T ParameterMap::get(std::string name) const {
65 T value{};
66 try {
67 value = std::get<T>(parameters.at(name));
68 } catch (const std::out_of_range & e) {
69 std::string errormessage =
70 "[ParameterMap::get] Required parameter [" + name + "] was not found in the parameter map\n";
71 throw_error(errormessage);
72 } catch (const std::bad_variant_access & e) {
73 std::string errormessage = "[ParameterMap::get] The type of the parameter [" + name + "] => [" +
74 typeid(value).name() + "] does not match that in the parameter map\n";
75 throw_error(errormessage);
76 }
77 return value;
78 }
79
80 template <typename T>
81 T ParameterMap::get(std::string name, T fiducial_value) const {
82 T value{};
83 try {
84 value = std::get<T>(parameters.at(name));
85 } catch (...) {
86 value = fiducial_value;
87 }
88 return value;
89 }
90
91 bool ParameterMap::contains(std::string name) const { return parameters.count(name) != 0; }
92
93 // Explicit template instantiation
94 template std::string ParameterMap::get<std::string>(std::string) const;
95 template double ParameterMap::get<double>(std::string) const;
96 template int ParameterMap::get<int>(std::string) const;
97 template bool ParameterMap::get<bool>(std::string) const;
98 template std::vector<double> ParameterMap::get<std::vector<double>>(std::string) const;
99 template std::vector<int> ParameterMap::get<std::vector<int>>(std::string) const;
100
101 template std::string ParameterMap::get<std::string>(std::string, std::string) const;
102 template double ParameterMap::get<double>(std::string, double) const;
103 template int ParameterMap::get<int>(std::string, int) const;
104 template bool ParameterMap::get<bool>(std::string, bool) const;
105 template std::vector<double> ParameterMap::get<std::vector<double>>(std::string, std::vector<double>) const;
106 template std::vector<int> ParameterMap::get<std::vector<int>>(std::string, std::vector<int>) const;
107 } // namespace UTILS
108} // namespace FML
std::map< std::string, ParameterTypes > & get_map()
Get a reference to the underlying map.
void info() const
Show info about the parameter map.
bool contains(std::string name) const
Check if a parameter map contains a value.
T get(std::string name) const
Fetch a value. If it doesn't exist we throw an error.
std::ostream & operator<<(std::ostream &s, std::vector< double > const &v)
Definition: ParameterMap.cpp:6
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...
Definition: ParameterMap.h:34
Definition: CAMBReader.h:10