CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
parser.h
1#ifndef __PARSER__
2#define __PARSER__
3
4#include <functional>
5#include <initializer_list>
6#include <map>
7#include <optional>
8#include <set>
9#include <stdexcept>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include "common.h"
15
24 public:
25 bool is_shooting = false;
26
27 FileContent() = default;
28 ~FileContent() = default;
29 FileContent(const FileContent&) = default;
30 FileContent(FileContent&&) = default;
31 FileContent& operator=(const FileContent&) = default;
32 FileContent& operator=(FileContent&&) = default;
33
36 static FileContent from_file(const std::string& filename);
37
39 void set(const std::string& name, const std::string& value);
40
44 friend FileContent operator+(FileContent lhs, const FileContent& rhs) {
45 return lhs += rhs;
46 }
47
49 int size() const {
50 return static_cast<int>(params_.size());
51 }
52
54 const std::string& get_filename() const {
55 return filename_;
56 }
57
61 template <class T>
62 std::optional<T> get(const std::string& name) const;
63
65 template <class T>
66 T get_or(const std::string& name, T fallback) const {
67 if (auto v = get<T>(name)) {
68 return *v;
69 }
70 return fallback;
71 }
73 std::string get_or(const std::string& name, const char* fallback) const;
74
77 bool get_yes(const std::string& name, bool fallback = false) const;
78
81 std::optional<bool> get_yes_no(const std::string& name) const;
82
86 template <class E>
87 void read_choice(const std::string& name,
88 E& choice,
89 std::initializer_list<std::pair<const char*, E>> spellings) const {
90 const auto value = get<std::string>(name);
91 if (!value) {
92 return;
93 }
94 std::string allowed;
95 for (const auto& [spelling, option] : spellings) {
96 if (*value == spelling) {
97 choice = option;
98 return;
99 }
100 allowed += (allowed.empty() ? "'" : ", '") + std::string(spelling) + "'";
101 }
102 class_stop_severe("%s = '%s' is not one of %s", name.c_str(), value->c_str(), allowed.c_str());
103 }
104
106 void mark_all_unread() const {
107 read_params_.clear();
108 }
109
111 bool was_read(const std::string& name) const {
112 return read_params_.count(name) > 0;
113 }
114
126 return legacy_ncdm_transmuted_;
127 }
128 void set_legacy_ncdm_transmuted() {
129 legacy_ncdm_transmuted_ = true;
130 }
131
139 return single_instance_translated_;
140 }
141 void set_single_instance_translated() {
142 single_instance_translated_ = true;
143 }
144
149 std::vector<std::string> instances_with(const std::string& field, const std::string& value) const;
150
152 std::vector<std::string> unread_parameters() const;
153
154 private:
155 std::string filename_;
156 std::vector<std::string> keys_;
157 std::map<std::string, std::string> params_;
158 mutable std::set<std::string> read_params_;
159 bool legacy_ncdm_transmuted_ = false;
160 bool single_instance_translated_ = false;
161
162 public:
168 const std::function<void(const std::string& name, const std::string& value, bool read)>& fn)
169 const;
170
173 static bool parse_line(const std::string& line, std::string& name, std::string& value);
174
176 static std::vector<std::string> split_csv(const std::string& s);
177
179 static bool is_instance_name(const std::string& name);
180};
181
182// Explicit specialization declarations — definitions are in parser.cpp.
183// These prevent implicit instantiation from the primary template in any TU.
184template <>
185std::optional<int> FileContent::get<int>(const std::string& name) const;
186template <>
187std::optional<double> FileContent::get<double>(const std::string& name) const;
188template <>
189std::optional<std::string> FileContent::get<std::string>(const std::string& name) const;
190template <>
191std::optional<std::vector<double>> FileContent::get<std::vector<double>>(
192 const std::string& name) const;
193template <>
194std::optional<std::vector<int>> FileContent::get<std::vector<int>>(const std::string& name) const;
195template <>
196std::optional<std::vector<std::string>> FileContent::get<std::vector<std::string>>(
197 const std::string& name) const;
198
199/**************************************************************/
200/* Legacy C-style free functions. */
201/**************************************************************/
202
203#ifdef __cplusplus
204extern "C" {
205#endif
206
207void parser_read_file(const char* filename, FileContent* pfc);
208
209void parser_cat(const FileContent* pfc1, const FileContent* pfc2, FileContent* pfc3);
210
211#ifdef __cplusplus
212}
213#endif
214
215#endif
Definition parser.h:23
void for_each(const std::function< void(const std::string &name, const std::string &value, bool read)> &fn) const
std::vector< std::string > instances_with(const std::string &field, const std::string &value) const
static std::vector< std::string > split_csv(const std::string &s)
void mark_all_unread() const
Definition parser.h:106
void read_choice(const std::string &name, E &choice, std::initializer_list< std::pair< const char *, E > > spellings) const
Definition parser.h:87
bool was_read(const std::string &name) const
Definition parser.h:111
std::optional< bool > get_yes_no(const std::string &name) const
int size() const
Definition parser.h:49
std::vector< std::string > unread_parameters() const
static FileContent from_file(const std::string &filename)
bool legacy_ncdm_transmuted() const
Definition parser.h:125
const std::string & get_filename() const
Definition parser.h:54
T get_or(const std::string &name, T fallback) const
Definition parser.h:66
void set(const std::string &name, const std::string &value)
bool single_instance_translated() const
Definition parser.h:138
std::optional< T > get(const std::string &name) const
static bool is_instance_name(const std::string &name)
bool get_yes(const std::string &name, bool fallback=false) const
std::string get_or(const std::string &name, const char *fallback) const
static bool parse_line(const std::string &line, std::string &name, std::string &value)
FileContent & operator+=(const FileContent &other)