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 <map>
6#include <optional>
7#include <set>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12#include "common.h"
13
22 public:
23 bool is_shooting = false;
24
25 FileContent() = default;
26 ~FileContent() = default;
27 FileContent(const FileContent&) = default;
28 FileContent(FileContent&&) = default;
29 FileContent& operator=(const FileContent&) = default;
30 FileContent& operator=(FileContent&&) = default;
31
34 static FileContent from_file(const std::string& filename);
35
37 void set(const std::string& name, const std::string& value);
38
42 friend FileContent operator+(FileContent lhs, const FileContent& rhs) {
43 return lhs += rhs;
44 }
45
47 int size() const {
48 return static_cast<int>(params_.size());
49 }
50
52 const std::string& get_filename() const {
53 return filename_;
54 }
55
59 template <class T>
60 std::optional<T> get(const std::string& name) const;
61
63 template <class T>
64 T get_or(const std::string& name, T fallback) const {
65 if (auto v = get<T>(name))
66 return *v;
67 return fallback;
68 }
70 std::string get_or(const std::string& name, const char* fallback) const;
71
73 void mark_all_unread() const {
74 read_params_.clear();
75 }
76
78 bool was_read(const std::string& name) const {
79 return read_params_.count(name) > 0;
80 }
81
86 std::vector<std::string> instances_with(const std::string& field, const std::string& value) const;
87
89 std::vector<std::string> unread_parameters() const;
90
91 private:
92 std::string filename_;
93 std::vector<std::string> keys_;
94 std::map<std::string, std::string> params_;
95 mutable std::set<std::string> read_params_;
96
97 public:
103 const std::function<void(const std::string& name, const std::string& value, bool read)>& fn)
104 const;
105
108 static bool parse_line(const std::string& line, std::string& name, std::string& value);
109
111 static std::vector<std::string> split_csv(const std::string& s);
112};
113
114// Explicit specialization declarations — definitions are in parser.cpp.
115// These prevent implicit instantiation from the primary template in any TU.
116template <>
117std::optional<int> FileContent::get<int>(const std::string& name) const;
118template <>
119std::optional<double> FileContent::get<double>(const std::string& name) const;
120template <>
121std::optional<std::string> FileContent::get<std::string>(const std::string& name) const;
122template <>
123std::optional<std::vector<double>> FileContent::get<std::vector<double>>(
124 const std::string& name) const;
125template <>
126std::optional<std::vector<int>> FileContent::get<std::vector<int>>(const std::string& name) const;
127template <>
128std::optional<std::vector<std::string>> FileContent::get<std::vector<std::string>>(
129 const std::string& name) const;
130
131/**************************************************************/
132/* Legacy C-style free functions. */
133/**************************************************************/
134
135#ifdef __cplusplus
136extern "C" {
137#endif
138
139void parser_read_file(const char* filename, FileContent* pfc);
140
141void parser_cat(const FileContent* pfc1, const FileContent* pfc2, FileContent* pfc3);
142
143#ifdef __cplusplus
144}
145#endif
146
147#endif
Definition parser.h:21
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:73
bool was_read(const std::string &name) const
Definition parser.h:78
int size() const
Definition parser.h:47
std::vector< std::string > unread_parameters() const
static FileContent from_file(const std::string &filename)
const std::string & get_filename() const
Definition parser.h:52
T get_or(const std::string &name, T fallback) const
Definition parser.h:64
void set(const std::string &name, const std::string &value)
std::optional< T > get(const std::string &name) 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)