Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarparse.h
Go to the documentation of this file.
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef COLVARPARSE_H
11#define COLVARPARSE_H
12
13#include <cstring>
14#include <list>
15#include <string>
16
17#include "colvarmodule.h"
18#include "colvarvalue.h"
19#include "colvarparams.h"
20
21
23
24
27class colvarparse : public colvarparams {
28
29protected:
30
31 colvarmodule *cvmodule = nullptr;
32
33public:
34
35 colvarmodule *get_cvmodule() { return cvmodule; }
36
38 // colvarparse();
39
41 colvarparse(colvarmodule *cvmodule_in);
42
43 // UNUSED
44 // /// Constructor that stores the object's config string
45 // colvarparse(colvarmodule *cvmodule_in, const std::string& conf);
46
48 void clear();
49
51 void set_string(std::string const &conf);
52
54 ~colvarparse() override;
55
57 inline std::string const & get_config() const
58 {
59 return config_string;
60 }
61
67 parse_echo = (1<<1),
75 parse_required = (1<<16),
78 parse_override = (1<<17),
80 parse_restart = (1<<18),
82 parse_normal = (1<<1) | (1<<2) | (1<<17),
84 parse_deprecated = (1<<1) | (1<<3) | (1<<17)
85 };
86
90 int check_keywords(std::string &conf, char const *key);
91
94
119
120 bool get_keyval(std::string const &conf,
121 char const *key,
122 int &value,
123 int const &def_value = 0,
124 Parse_Mode const parse_mode = parse_normal);
125 bool get_keyval(std::string const &conf,
126 char const *key,
127 size_t &value,
128 size_t const &def_value = 0,
129 Parse_Mode const parse_mode = parse_normal);
130 bool get_keyval(std::string const &conf,
131 char const *key,
132 long &value,
133 long const &def_value = 0,
134 Parse_Mode const parse_mode = parse_normal);
135 bool get_keyval(std::string const &conf,
136 char const *key,
137 cvm::step_number &value,
138 cvm::step_number const &def_value = 0,
139 Parse_Mode const parse_mode = parse_normal);
140 bool get_keyval(std::string const &conf,
141 char const *key,
142 std::string &value,
143 std::string const &def_value = std::string(""),
144 Parse_Mode const parse_mode = parse_normal);
145 bool get_keyval(std::string const &conf,
146 char const *key,
147 cvm::real &value,
148 cvm::real const &def_value = 0.0,
149 Parse_Mode const parse_mode = parse_normal);
150 bool get_keyval(std::string const &conf,
151 char const *key,
152 cvm::rvector &value,
153 cvm::rvector const &def_value = cvm::rvector(),
154 Parse_Mode const parse_mode = parse_normal);
155 bool get_keyval(std::string const &conf,
156 char const *key,
157 cvm::quaternion &value,
158 cvm::quaternion const &def_value = cvm::quaternion(),
159 Parse_Mode const parse_mode = parse_normal);
160 bool get_keyval(std::string const &conf,
161 char const *key,
162 colvarvalue &value,
164 Parse_Mode const parse_mode = parse_normal);
165 bool get_keyval(std::string const &conf,
166 char const *key,
167 bool &value,
168 bool const &def_value = false,
169 Parse_Mode const parse_mode = parse_normal);
170 bool get_keyval(std::string const &conf,
171 char const *key,
172 std::vector<int> &values,
173 std::vector<int> const &def_values = std::vector<int>(0, 0),
174 Parse_Mode const parse_mode = parse_normal);
175 bool get_keyval(std::string const &conf,
176 char const *key,
177 std::vector<size_t> &values,
178 std::vector<size_t> const &def_values = std::vector<size_t>(0, 0),
179 Parse_Mode const parse_mode = parse_normal);
180 bool get_keyval(std::string const &conf,
181 char const *key,
182 std::vector<long> &values,
183 std::vector<long> const &def_values = std::vector<long>(0, 0),
184 Parse_Mode const parse_mode = parse_normal);
185 bool get_keyval(std::string const &conf,
186 char const *key,
187 std::vector<std::string> &values,
188 std::vector<std::string> const &def_values = std::vector<std::string>(0, std::string("")),
189 Parse_Mode const parse_mode = parse_normal);
190 bool get_keyval(std::string const &conf,
191 char const *key,
192 std::vector<cvm::real> &values,
193 std::vector<cvm::real> const &def_values = std::vector<cvm::real>(0, 0.0),
194 Parse_Mode const parse_mode = parse_normal);
195 bool get_keyval(std::string const &conf,
196 char const *key,
197 std::vector<cvm::rvector> &values,
198 std::vector<cvm::rvector> const &def_values = std::vector<cvm::rvector>(0, cvm::rvector()),
199 Parse_Mode const parse_mode = parse_normal);
200 bool get_keyval(std::string const &conf,
201 char const *key,
202 std::vector<cvm::quaternion> &values,
203 std::vector<cvm::quaternion> const &def_values = std::vector<cvm::quaternion>(0, cvm::quaternion()),
204 Parse_Mode const parse_mode = parse_normal);
205 bool get_keyval(std::string const &conf,
206 char const *key,
207 std::vector<colvarvalue> &values,
208 std::vector<colvarvalue> const &def_values = std::vector<colvarvalue>(0, colvarvalue(colvarvalue::type_notset)),
209 Parse_Mode const parse_mode = parse_normal);
210
211protected:
212
214 bool get_key_string_value(std::string const &conf,
215 char const *key, std::string &data);
216
218 bool get_key_string_multi_value(std::string const &conf,
219 char const *key, std::vector<std::string>& data);
220
222 template<typename TYPE>
223 bool _get_keyval_scalar_(std::string const &conf,
224 char const *key,
225 TYPE &value,
226 TYPE const &def_value,
227 Parse_Mode const &parse_mode);
228
230 template<typename TYPE>
231 bool _get_keyval_vector_(std::string const &conf,
232 char const *key,
233 std::vector<TYPE> &values,
234 std::vector<TYPE> const &def_values,
235 Parse_Mode const &parse_mode);
236
238 template<typename TYPE>
239 int _get_keyval_scalar_value_(std::string const &key_str,
240 std::string const &data,
241 TYPE &value,
242 TYPE const &def_value);
243
245 template<typename TYPE>
246 int _get_keyval_scalar_novalue_(std::string const &key_str,
247 TYPE &value,
248 Parse_Mode const &parse_mode);
249
251 template<typename TYPE>
252 void mark_key_set_user(std::string const &key_str,
253 TYPE const &value,
254 Parse_Mode const &parse_mode);
255
257 template<typename TYPE>
258 void mark_key_set_default(std::string const &key_str,
259 TYPE const &def_value,
260 Parse_Mode const &parse_mode);
261
263 void error_key_required(std::string const &key_str,
264 Parse_Mode const &parse_mode);
265
267 bool key_already_set(std::string const &key_str);
268
269public:
270
272 static inline std::string to_lower_cppstr(std::string const &in)
273 {
274 std::string out = "";
275 for (size_t i = 0; i < in.size(); i++) {
276 out.append(1, static_cast<char>( ::tolower(in[i])) );
277 }
278 return out;
279 }
280
287 public:
288
289 read_block(std::string const &key, std::string *data = nullptr);
290
291 ~read_block();
292
294 friend std::istream & operator >> (std::istream &is, read_block const &rb);
295
298
299 private:
300
302 std::string const key;
303
305 std::string * const data;
306
310 std::istream & read_block_contents(std::istream &is, bool block_only = false) const;
311 };
312
313
315 static const char * const white_space;
316
324 bool key_lookup(std::string const &conf,
325 char const *key,
326 std::string *data = nullptr,
327 size_t *save_pos = nullptr);
328
332 std::istream & read_config_line(std::istream &is, std::string &line);
333
336 static std::istream & getline_nocomments(std::istream &is, std::string &s);
337
341 static int check_braces(std::string const &conf, size_t const start_pos);
342
345 int check_ascii(std::string const &conf);
346
351 static void split_string(const std::string& data, const std::string& delim, std::vector<std::string>& dest);
352
353protected:
354
356 std::string const keyword_delimiters_left;
357
359 std::string const keyword_delimiters_right;
360
364 std::list<std::string> allowed_keywords;
365
368 key_not_set = 0,
369 key_set_user = 1,
370 key_set_default = 2
371 };
372
374 std::map<std::string, key_set_mode> key_set_modes;
375
379 std::list<size_t> data_begin_pos;
380
384 std::list<size_t> data_end_pos;
385
387 void add_keyword(char const *key);
388
390 void strip_values(std::string &conf);
391
393 std::string config_string;
394
395};
396
397
400 colvarparse::Parse_Mode const &mode2)
401{
402 return static_cast<colvarparse::Parse_Mode>(static_cast<int>(mode1) |
403 static_cast<int>(mode2));
404}
405
406#endif
1-dimensional vector of real numbers with four components and a quaternion algebra
Definition: colvartypes.h:980
vector of real numbers with three components
Definition: colvartypes.h:728
Collective variables module (main class)
Definition: colvarmodule.h:72
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:99
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:96
Definition: colvarparams.h:18
Definition: colvarparse.h:286
std::string const key
Keyword that identifies the block.
Definition: colvarparse.h:302
friend std::istream & operator>>(std::istream &is, read_block const &rb)
Read block from stream, first check that key matches, then call read_contents()
Definition: colvarparse.cpp:875
std::string *const data
Where to keep the data.
Definition: colvarparse.h:305
std::istream & read_block_contents(std::istream &is, bool block_only=false) const
Definition: colvarparse.cpp:908
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
bool key_already_set(std::string const &key_str)
True if the keyword has been set already.
Definition: colvarparse.cpp:554
bool key_lookup(std::string const &conf, char const *key, std::string *data=nullptr, size_t *save_pos=nullptr)
Low-level function for parsing configuration strings; automatically adds the requested keyword to the...
Definition: colvarparse.cpp:670
static void split_string(const std::string &data, const std::string &delim, std::vector< std::string > &dest)
Split a string with a specified delimiter into a vector.
Definition: colvarparse.cpp:1015
int check_ascii(std::string const &conf)
Check that a config string contains non-ASCII characters.
Definition: colvarparse.cpp:996
~colvarparse() override
Default destructor.
Definition: colvarparse.cpp:74
std::string const keyword_delimiters_right
Characters allowed immediately to the right of a kewyord.
Definition: colvarparse.h:359
std::list< size_t > data_begin_pos
List of delimiters for the values of each keyword in the configuration string; all keywords will be s...
Definition: colvarparse.h:379
void set_string(std::string const &conf)
Set a new config string for this object.
Definition: colvarparse.cpp:65
std::string const keyword_delimiters_left
Characters allowed immediately to the left of a kewyord.
Definition: colvarparse.h:356
std::string config_string
Configuration string of the object (includes comments)
Definition: colvarparse.h:393
key_set_mode
How a keyword has been set.
Definition: colvarparse.h:367
bool _get_keyval_scalar_(std::string const &conf, char const *key, TYPE &value, TYPE const &def_value, Parse_Mode const &parse_mode)
Template for single-value keyword parsers.
Definition: colvarparse.cpp:247
void add_keyword(char const *key)
Add a new valid keyword to the list.
Definition: colvarparse.cpp:540
static int check_braces(std::string const &conf, size_t const start_pos)
Check if the content of a config string has matching braces.
Definition: colvarparse.cpp:982
bool get_key_string_multi_value(std::string const &conf, char const *key, std::vector< std::string > &data)
Get multiple strings from repeated instances of a same keyword.
Definition: colvarparse.cpp:106
void mark_key_set_user(std::string const &key_str, TYPE const &value, Parse_Mode const &parse_mode)
Record that the keyword has just been user-defined.
Definition: colvarparse.cpp:129
int check_keywords(std::string &conf, char const *key)
Check that all the keywords within "conf" are in the list of allowed keywords; this will invoke strip...
Definition: colvarparse.cpp:598
int _get_keyval_scalar_novalue_(std::string const &key_str, TYPE &value, Parse_Mode const &parse_mode)
Handle the case where the user provides a keyword without value.
Definition: colvarparse.cpp:227
static std::string to_lower_cppstr(std::string const &in)
Return a lowercased copy of the string.
Definition: colvarparse.h:272
std::istream & read_config_line(std::istream &is, std::string &line)
Reads a configuration line, adds it to config_string, and returns the stream.
Definition: colvarparse.cpp:645
static const char *const white_space
Accepted white space delimiters, used in key_lookup()
Definition: colvarparse.h:315
bool _get_keyval_vector_(std::string const &conf, char const *key, std::vector< TYPE > &values, std::vector< TYPE > const &def_values, Parse_Mode const &parse_mode)
Template for multiple-value keyword parsers.
Definition: colvarparse.cpp:293
std::list< size_t > data_end_pos
List of delimiters for the values of each keyword in the configuration string; all keywords will be s...
Definition: colvarparse.h:384
int _get_keyval_scalar_value_(std::string const &key_str, std::string const &data, TYPE &value, TYPE const &def_value)
Extract the value of a variable from a string.
Definition: colvarparse.cpp:175
void clear()
Set the object ready to parse a new configuration string.
Definition: colvarparse.cpp:59
std::list< std::string > allowed_keywords
List of legal keywords for this object: this is updated by each call to colvarparse::get_keyval() or ...
Definition: colvarparse.h:364
static std::istream & getline_nocomments(std::istream &is, std::string &s)
Works as std::getline() but also removes everything between a comment character and the following new...
Definition: colvarparse.cpp:658
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:63
@ parse_deprecation_warning
Print a deprecation warning if the keyword is given.
Definition: colvarparse.h:71
@ parse_echo_default
Print the default value of a keyword, if it is NOT given.
Definition: colvarparse.h:69
@ parse_echo
Print the value of a keyword if it is given.
Definition: colvarparse.h:67
@ parse_normal
Alias for old default behavior (should be phased out)
Definition: colvarparse.h:82
@ parse_deprecated
Settings for a deprecated keyword.
Definition: colvarparse.h:84
@ parse_restart
The call is being executed from a read_restart() function.
Definition: colvarparse.h:80
@ parse_null
Zero for all flags.
Definition: colvarparse.h:65
@ parse_override
Definition: colvarparse.h:78
@ parse_silent
Do not print the keyword.
Definition: colvarparse.h:73
@ parse_required
Raise error if the keyword is not provided.
Definition: colvarparse.h:75
void error_key_required(std::string const &key_str, Parse_Mode const &parse_mode)
Raise error condition due to the keyword being required!
Definition: colvarparse.cpp:158
void clear_keyword_registry()
Use this after parsing a config string (note that check_keywords() calls it already)
Definition: colvarparse.cpp:589
void strip_values(std::string &conf)
Remove all the values from the config string.
Definition: colvarparse.cpp:566
bool get_key_string_value(std::string const &conf, char const *key, std::string &data)
Get the string value of a keyword, and save it for later parsing.
Definition: colvarparse.cpp:81
std::map< std::string, key_set_mode > key_set_modes
Track which keywords have been already set, and how.
Definition: colvarparse.h:374
void mark_key_set_default(std::string const &key_str, TYPE const &def_value, Parse_Mode const &parse_mode)
Record that the keyword has just been set to its default value.
Definition: colvarparse.cpp:146
std::string const & get_config() const
Get the configuration string (includes comments)
Definition: colvarparse.h:57
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
@ type_notset
Undefined type.
Definition: colvarvalue.h:54
Definition: colvars_memstream.h:30
Collective variables main module.
Functions to handle scalar parameters used in objects.
colvarparse::Parse_Mode operator|(colvarparse::Parse_Mode const &mode1, colvarparse::Parse_Mode const &mode2)
Bitwise OR between two Parse_mode flags.
Definition: colvarparse.h:399