KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Param.cpp
Go to the documentation of this file.
1#include "kids/Param.h"
2
3#include <cstring>
4#include <fstream>
5#include <iomanip>
6#include <memory>
7#include <sstream>
8#include <string>
9
10#include "kids/Exception.h"
11#include "kids/Types.h"
12
13namespace PROJECT_NS {
14
15Param::Param(const std::string &input, LoadOption option) {
16 pj = std::shared_ptr<JSON>(new JSON());
17 // clang-format off
18 switch (option) {
19 case fromFile: {
20 std::ifstream ifs(input);
21 try {
22 ifs >> *pj;
23 } catch (const JSON_Exception& e) {
24 std::cout << "Invalid file: " << input;
25 }
26 ifs.close();
27 break;
28 }
29 case fromString: {
30 std::stringstream sstr(input);
31 try {
32 sstr >> *pj;
33 } catch (const JSON_Exception& e) {
34 std::cout << "Invalid string: " << input;
35 }
36 break;
37 }
38 }
39 // clang-format on
40}
41
42bool Param::has_key(const std::string &key) { return !(pj->count(key) == 0); }
43
44std::shared_ptr<configor::json> Param::pjson() { return pj; }
45
46std::string Param::repr() { return pj->dump(4, ' '); }
47
48
49template <typename T, bool Require = false>
50T Param::get(const std::string &key, const std::string &loc, const phys::dimension7 &qdim, const T &default_value) {
51 if (!has_key(key)) {
52 if (Require) {
53 throw kids_error( //
54 utils::concat(loc, //
55 " Type<", as_str<T>(), ">", //
56 " Key/", key, "/", //
57 " : Illegal default") //
58 );
59 return T();
60 } else {
61 try {
62 throw param_warning( //
63 utils::concat(loc, //
64 " Type<", as_str<T>(), ">", //
65 " Key/", key, "/", //
66 " : Use default ", //
67 default_value) //
68 );
69 } catch (param_warning &w) { std::cerr << w.what() << "\n"; }
70 return default_value;
71 }
72 }
73 // the case find the key
74 switch ((*pj)[key].type()) {
75 case configor::config_value_type::string: {
76 if (std::is_same<T, std::string>::value) {
77 return (*pj)[key].get<T>();
78 } else if (std::is_same<T, double>::value) {
79 // parse unit
80 phys::uval uv = phys::us::parse((*pj)[key].as_string());
81 double qval = phys::au::as(qdim, uv);
82
83 // conversion by stringstream (stupid)
84 T q;
85 std::stringstream ss;
86 ss << std::setiosflags(std::ios::scientific) //
87 << std::setprecision(32) << qval; //
88 ss >> q;
89 return q;
90 }
91 break;
92 }
93 case configor::config_value_type::boolean: {
94 if (std::is_same<T, bool>::value) return (*pj)[key].get<T>();
95 break;
96 }
97 case configor::config_value_type::number_float: {
98 T q;
99 if (std::is_same<T, double>::value) {
100 q = (*pj)[key].as_float();
101 return q;
102 }
103 break;
104 }
105 case configor::config_value_type::number_integer: {
106 T q;
107 if (std::is_same<T, double>::value) {
108 q = (*pj)[key].as_float();
109 return q;
110 } else if (std::is_same<T, int>::value) {
111 return (*pj)[key].get<T>();
112 }
113 break;
114 }
115 }
116 // cannot be adapted to existing conversions
117 throw kids_error( //
118 utils::concat(loc, //
119 " Type<", as_str<T>(), ">", //
120 " Key/", key, "/", //
121 " Data{", //
122 (*pj)[key].dump(4, ' '), "}", //
123 " : Converting fatal") //
124 );
125 return T();
126}
127
129bool Param::get_bool(const std::string &key, const std::string &loc, const bool &default_value) {
130 return get<bool, false>(key, loc, phys::none_d, default_value);
131}
132bool Param::get_bool(const std::string &key, const std::string &loc) {
133 return get<bool, true>(key, loc, phys::none_d, bool());
134}
135
136int Param::get_int(const std::string &key, const std::string &loc, const int &default_value) {
137 return get<int, false>(key, loc, phys::none_d, default_value);
138}
139int Param::get_int(const std::string &key, const std::string &loc) {
140 return get<int, true>(key, loc, phys::none_d, int());
141}
142
143std::string Param::get_string(const std::string &key, const std::string &loc, const std::string &default_value) {
144 return get<std::string, false>(key, loc, phys::none_d, default_value);
145}
146std::string Param::get_string(const std::string &key, const std::string &loc) {
147 return get<std::string, true>(key, loc, phys::none_d, std::string());
148}
149
150double Param::get_double(const std::string &key, const std::string &loc, const phys::dimension7 &qdim,
151 const double &default_value) {
152 return get<double, false>(key, loc, qdim, default_value);
153}
154double Param::get_double(const std::string &key, const std::string &loc, const double &default_value) {
155 return get<double, false>(key, loc, phys::none_d, default_value);
156}
157double Param::get_double(const std::string &key, const std::string &loc) {
158 return get<double, true>(key, loc, phys::none_d, double());
159}
161
162}; // namespace PROJECT_NS
provide Exception structs
Provide struct and interfaces for input parameters.
definition of types in the project and some utiles for types
T get(const std::string &key, const std::string &loc, const phys::dimension7 &qdim, const T &default_value)
get parameter
Definition Param.cpp:50
configor::json JSON
Definition Param.h:84
@ fromString
construct Param from string
Definition Param.h:90
@ fromFile
construct Param from file
Definition Param.h:91
Param(const std::string &input, LoadOption option)
Definition Param.cpp:15
double get_double(const std::string &key, const std::string &loc, const phys::dimension7 &qdim, const double &default_value=double())
Definition Param.cpp:150
configor::configor_exception JSON_Exception
Definition Param.h:85
std::string repr()
Definition Param.cpp:46
std::shared_ptr< JSON > pjson()
Definition Param.cpp:44
std::string get_string(const std::string &key, const std::string &loc, const std::string &default_value)
Definition Param.cpp:143
int get_int(const std::string &key, const std::string &loc, const int &default_value)
Definition Param.cpp:136
bool get_bool(const std::string &key, const std::string &loc, const bool &default_value)
Definition Param.cpp:129
std::shared_ptr< JSON > pj
Definition Param.h:148
bool has_key(const std::string &key)
Param &operator[](const std::string &key) { if (!has_key(key)) { throw param_illegal_key_error(key); ...
Definition Param.cpp:42
static uval parse(const std::string &str)
Definition phys.h:790
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
constexpr dimension7 none_d
Definition phys.h:197
std::basic_string< CharT > concat(const separator_t< CharT > &sep, Args &&... seq)
Definition concat.h:242