Psi4
liboptions.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2019 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #ifndef _psi_src_lib_liboptions_liboptions_hpp
30 #define _psi_src_lib_liboptions_liboptions_hpp
31 
32 #ifdef _POSIX_C_SOURCE
33 #undef _POSIX_C_SOURCE
34 #endif
35 #ifdef _XOPEN_SOURCE
36 #undef _XOPEN_SOURCE
37 #endif
38 
39 #include <map>
40 #include <memory>
41 #include <string>
42 #include <vector>
43 
45 
46 namespace psi {
47 
49  public:
50  DataTypeException(const std::string& message) : PSIEXCEPTION(message) {}
51 };
52 
53 class IndexException : public PsiException {
54  public:
55  IndexException(const std::string& message) : PSIEXCEPTION(message + " is not a valid option.") {}
56  IndexException(const std::string& message, const std::string& module)
57  : PSIEXCEPTION(message + " is not a valid option for module " + module) {}
58 };
59 
61  public:
62  DuplicateKeyException(const std::string& key, const std::string& type1, const std::string& type2, const char* lfile,
63  int lline)
64  : PsiException("Option " + key + " has been declared as a " + type1 + " and a " + type2, lfile, lline) {}
65 };
66 
68  public:
69  OptionsException(const std::string& message) : PSIEXCEPTION("Options Exception: " + message) {}
70 };
71 
72 class PSI_API Data;
73 class DataType {
74  bool changed_;
75 
76  public:
77  DataType();
78  virtual ~DataType();
79 
80  bool has_changed() const;
81  void changed();
82  void dechanged();
83 
84  void to_upper(std::string& str);
85 
86  virtual void add_choices(std::string str);
87  virtual std::string type() const;
88 
89  virtual bool is_array() const;
90  virtual size_t size() const;
91  virtual void add(DataType*);
92  virtual void add(std::string, DataType*);
93  virtual void add(bool);
94  virtual void add(int);
95  virtual void add(double);
96  virtual void add(std::string, bool);
97  virtual void add(std::string, std::string);
98  virtual void add(std::string, int);
99  virtual void add(std::string, double);
100  virtual void add(std::string, std::string, std::string);
101 
102  virtual bool exists(std::string);
103 
104  virtual std::string to_string() const;
105  virtual int to_integer() const;
106  virtual double to_double() const;
107 
108  virtual void assign(DataType*);
109  virtual void assign(bool);
110  virtual void assign(int);
111  virtual void assign(double);
112  virtual void assign(std::string);
113 
114  virtual void reset();
115 
116  virtual Data& operator[](std::string);
117  virtual Data& operator[](size_t);
118 };
119 
120 #ifdef __INTEL_COMPILER
121 #pragma warning disable 654
122 #endif
123 class BooleanDataType : public DataType {
124  bool boolean_;
125 
126  public:
127  BooleanDataType();
128  BooleanDataType(bool b);
129  ~BooleanDataType() override;
130 
131  std::string type() const override;
132 
133  std::string to_string() const override;
134  int to_integer() const override;
135  double to_double() const override;
136 
137  void assign(bool b) override;
138  void assign(int i) override;
139  void assign(double d) override;
140  void assign(std::string s) override;
141 };
142 
143 #ifdef __INTEL_COMPILER
144 #pragma warning disable 654
145 #endif
146 class IntDataType : public DataType {
147  int integer_;
148 
149  public:
150  IntDataType();
151  IntDataType(int i);
152  ~IntDataType() override;
153 
154  std::string type() const override;
155 
156  std::string to_string() const override;
157  int to_integer() const override;
158  double to_double() const override;
159 
160  void assign(bool b) override;
161  void assign(int i) override;
162  void assign(double d) override;
163  void assign(std::string s) override;
164 };
165 
166 #ifdef __INTEL_COMPILER
167 #pragma warning disable 654
168 #endif
169 class DoubleDataType : public DataType {
170  double double_;
171 
172  public:
173  DoubleDataType();
174  DoubleDataType(double d);
175  ~DoubleDataType() override;
176 
177  std::string type() const override;
178 
179  std::string to_string() const override;
180  int to_integer() const override;
181  double to_double() const override;
182 
183  void assign(bool b) override;
184  void assign(int i) override;
185  void assign(double d) override;
186  void assign(std::string s) override;
187 };
188 
189 #ifdef __INTEL_COMPILER
190 #pragma warning disable 654
191 #endif
192 class StringDataType : public DataType {
193  std::string str_;
194  std::vector<std::string> choices_;
195 
196  public:
197  StringDataType();
198  StringDataType(std::string s);
199  StringDataType(std::string s, std::string c);
200 
201  ~StringDataType() override;
202 
203  void add_choices(std::string str) override;
204 
205  std::string type() const override;
206 
207  std::string to_string() const override;
208  int to_integer() const override;
209  double to_double() const override;
210 
211  void assign(bool b) override;
212  void assign(int i) override;
213  void assign(double d) override;
214  void assign(std::string s) override;
215 };
216 
217 #ifdef __INTEL_COMPILER
218 #pragma warning disable 654
219 #endif
220 class IStringDataType : public DataType {
221  std::string str_;
222  std::vector<std::string> choices_;
223 
224  public:
225  IStringDataType();
226  IStringDataType(std::string s);
227  IStringDataType(std::string s, std::string c);
228  ~IStringDataType() override;
229 
230  void add_choices(std::string str) override;
231 
232  std::string type() const override;
233 
234  std::string to_string() const override;
235  int to_integer() const override;
236  double to_double() const override;
237 
238  void assign(bool b) override;
239  void assign(int i) override;
240  void assign(double d) override;
241  void assign(std::string s) override;
242 };
243 
244 class PSI_API Data {
245  std::shared_ptr<DataType> ptr_;
246 
247  public:
248  Data();
249  Data(DataType* t);
250  Data(const Data& copy);
251 
252  std::string to_string() const;
253  int to_integer() const;
254  double to_double() const;
255 
256  bool is_array() const;
257  size_t size() const;
258 
259  bool has_changed() const;
260 
261  void changed();
262  void dechanged();
263 
264  void add_choices(std::string str);
265 
266  std::string type() const;
267 
268  void add(DataType* data);
269  void add(std::string s, DataType* data);
270  void add(bool b);
271  void add(int i);
272  void add(double d);
273  void add(std::string s, std::string c);
274  void add(std::string key, bool b);
275  void add(std::string key, int i);
276  void add(std::string key, double d);
277  void add(std::string key, std::string s, std::string c);
278 
279  void assign(DataType* data);
280  void assign(bool b);
281  void assign(int i);
282  void assign(double d);
283  void assign(std::string s);
284 
285  void reset();
286 
287  DataType* get() const;
288 
289  Data& operator[](int i);
290  Data& operator[](std::string s);
291 };
292 
293 #ifdef __INTEL_COMPILER
294 #pragma warning disable 654
295 #endif
296 class PSI_API ArrayType : public DataType {
297  std::vector<Data> array_;
298 
299  public:
300  ArrayType();
301 
302  std::string type() const override;
303 
304  void add(DataType* data) override;
305  void add(bool b) override;
306  void add(int i) override;
307  void add(double d) override;
308  void add(std::string s, std::string c = "") override;
309  void assign(DataType* data) override;
310 
311  Data& operator[](size_t i) override;
312  Data& operator[](std::string s) override;
313  bool is_array() const override;
314 
315  size_t size() const override;
316 
317  std::string to_string() const override;
318 
319  void reset() override;
320  std::vector<Data> data() { return array_; }
321 };
322 
323 #ifdef __INTEL_COMPILER
324 #pragma warning disable 654
325 #endif
326 class MapType : public DataType {
327  std::map<std::string, Data> keyvals_;
328  typedef std::map<std::string, Data>::iterator iterator;
329  typedef std::map<std::string, Data>::const_iterator const_iterator;
330 
331  public:
332  MapType();
333 
334  std::string type() const override;
335 
336  void add(std::string key, DataType* data) override;
337  void add(std::string key, bool b) override;
338  void add(std::string key, int i) override;
339  void add(std::string key, double d) override;
340  void add(std::string key, std::string s, std::string c = "") override;
341 
342  bool exists(std::string key) override;
343 
344  Data& operator[](std::string s) override;
345  bool is_array() const override;
346 
347  size_t size() const override;
348 
349  std::string to_string() const override;
350 };
351 
354 
356  std::map<std::string, Data> all_local_options_;
358  std::string current_module_;
359 
361  std::map<std::string, std::map<std::string, Data> > locals_;
362 
364  std::map<std::string, Data> globals_;
365 
366  typedef std::map<std::string, Data>::iterator iterator;
367  typedef std::map<std::string, Data>::const_iterator const_iterator;
368  typedef std::map<std::string, std::map<std::string, Data> >::const_iterator const_mod_iterator;
369 
370  public:
371  Options();
372 
373  Options& operator=(const Options& rhs);
374  bool read_globals() const;
375  void set_read_globals(bool _b);
376  void set_current_module(const std::string s);
377  std::string get_current_module() const { return current_module_; }
378 
379  void to_upper(std::string& str);
380 
381  void validate_options();
382 
383  void add(std::string key, DataType* data);
384  void add(std::string key, bool b);
385  void add(std::string key, int i);
386  void add(std::string key, double d);
387  void add(std::string key, std::string s, std::string c = "");
388  void add_i(std::string key, std::string s, std::string c = "");
389 
390  void add_bool(std::string key, bool b);
391  void add_int(std::string key, int i);
392  void add_double(std::string key, double d);
393  void add_str(std::string key, std::string s, std::string c = "");
394  void add_str_i(std::string key, std::string s, std::string c = "");
395  void add_array(std::string key);
396  void set_bool(const std::string& module, const std::string& key, bool b);
397  void set_int(const std::string& module, const std::string& key, int i);
398  void set_double(const std::string& module, const std::string& key, double d);
399  void set_str(const std::string& module, const std::string& key, std::string s);
400  void set_str_i(const std::string& module, const std::string& key, std::string s);
401  void set_array(const std::string& module, const std::string& key);
402 
403  void set_global_bool(const std::string& key, bool b);
404  void set_global_int(const std::string& key, int i);
405  void set_global_double(const std::string& key, double d);
406  void set_global_str(const std::string& key, const std::string& s);
407  void set_global_array(const std::string& key);
408 
409  DataType* set_global_array_entry(const std::string& key, DataType* entry, DataType* loc);
410  void set_global_array_double(std::string key, double val, DataType* entry);
411  void set_global_array_string(std::string key, std::string val, DataType* entry);
412  void set_global_array_int(std::string key, int val, DataType* entry);
413  DataType* set_global_array_array(std::string key, DataType* entry);
414  DataType* set_local_array_entry(const std::string& module, const std::string& key, DataType* entry, DataType* loc);
415  void set_local_array_double(const std::string& module, const std::string& key, double val, DataType* entry);
416  void set_local_array_string(const std::string& module, const std::string& key, std::string val, DataType* entry);
417  void set_local_array_int(const std::string& module, const std::string& key, int val, DataType* entry);
418  DataType* set_local_array_array(const std::string& module, const std::string& key, DataType* entry);
419 
420  void clear();
421 
422  bool exists_in_active(std::string key);
423 
424  bool exists_in_global(std::string key);
425  bool exists(std::string key);
426 
427  Data& get(std::string key);
428 
429  Data& get(std::map<std::string, Data>& m, std::string& key);
430 
431  Data& get_global(std::string key);
432 
433  Data& get_local(std::string& key);
434 
435  Data& use(std::string& key);
436 
437  Data& use_local(std::string& key);
438 
439  bool get_bool(std::string key);
440  int get_int(std::string key);
441  double get_double(std::string key);
442  std::string get_str(std::string key);
443  int* get_int_array(std::string key);
444  void fill_int_array(std::string key, int* empty_array);
445  std::vector<int> get_int_vector(std::string key);
446  double* get_double_array(std::string key);
447 
448  std::vector<double> get_double_vector(std::string key);
449 
450  const char* get_cstr(std::string key);
451 
452  Data& operator[](std::string key);
453 
454  std::string to_string() const;
455  std::string globals_to_string() const;
456 
457  void print();
458  void print_globals();
459  std::vector<std::string> list_globals();
460 };
461 } // namespace psi
462 #endif
double to_double() const override
Definition: liboptions.cc:162
std::map< std::string, Data >::iterator iterator
Definition: liboptions.h:366
std::vector< Data > array_
Definition: liboptions.h:297
std::map< std::string, Data > keyvals_
Definition: liboptions.h:327
IndexException(const std::string &message, const std::string &module)
Definition: liboptions.h:56
std::string to_string() const override
Definition: liboptions.cc:154
std::string to_string(const int val)
Definition: stl_string.cc:180
std::string to_string() const override
Definition: liboptions.cc:474
~StringDataType() override
Definition: liboptions.cc:216
void add(std::string key, DataType *data) override
Definition: liboptions.cc:441
std::vector< Data > data()
Definition: liboptions.h:320
std::map< std::string, Data > globals_
&quot;Global&quot; set of options
Definition: liboptions.h:364
std::string type() const override
Definition: liboptions.cc:182
virtual std::string to_string() const
Definition: liboptions.cc:90
virtual Data & operator[](std::string)
Definition: liboptions.cc:108
std::shared_ptr< DataType > ptr_
Definition: liboptions.h:245
Definition: liboptions.h:244
std::vector< std::string > choices_
Definition: liboptions.h:222
OptionsException(const std::string &message)
Definition: liboptions.h:69
virtual bool is_array() const
Definition: liboptions.cc:64
std::map< std::string, Data >::const_iterator const_iterator
Definition: liboptions.h:329
double to_double() const override
Definition: liboptions.cc:132
Definition: liboptions.h:60
std::string type() const override
Definition: liboptions.cc:152
void add_choices(std::string str) override
Definition: liboptions.cc:218
Definition: liboptions.h:169
int to_integer() const override
Definition: liboptions.cc:190
class PSI_API Data
Definition: liboptions.h:72
void assign(bool b) override
Definition: liboptions.cc:134
std::string str_
Definition: liboptions.h:193
Definition: liboptions.h:146
std::map< std::string, Data > all_local_options_
A temporary map used for validation of local options.
Definition: liboptions.h:356
Definition: liboptions.h:220
Definition: pointgrp.h:104
void dechanged()
Definition: liboptions.cc:53
std::string type() const override
Definition: liboptions.cc:224
int to_integer() const override
Definition: liboptions.cc:286
DataTypeException(const std::string &message)
Definition: liboptions.h:50
int to_integer(const std::string inString)
Definition: stl_string.cc:192
std::string type() const override
Definition: liboptions.cc:282
std::map< std::string, Data >::iterator iterator
Definition: liboptions.h:328
virtual std::string type() const
Definition: liboptions.cc:62
bool changed_
Definition: liboptions.h:74
virtual void add(DataType *)
Definition: liboptions.cc:68
virtual size_t size() const
Definition: liboptions.cc:66
std::string to_string() const override
Definition: liboptions.cc:184
StringDataType()
Definition: liboptions.cc:206
int to_integer() const override
Definition: liboptions.cc:130
void to_upper(std::string &str)
Definition: stl_string.cc:170
Definition: exception.h:54
~BooleanDataType() override
Definition: liboptions.cc:117
~IntDataType() override
Definition: liboptions.cc:150
~IStringDataType() override
Definition: liboptions.cc:275
bool has_changed() const
Definition: liboptions.cc:49
bool boolean_
Definition: liboptions.h:124
DoubleDataType()
Definition: liboptions.cc:176
BooleanDataType()
Definition: liboptions.cc:113
IStringDataType()
Definition: liboptions.cc:267
std::map< std::string, Data >::const_iterator const_iterator
Definition: liboptions.h:367
bool exists(std::string key) override
Definition: liboptions.cc:457
virtual void reset()
Definition: liboptions.cc:106
void to_upper(std::string &str)
Definition: liboptions.cc:55
void assign(bool b) override
Definition: liboptions.cc:232
double to_double() const override
Definition: liboptions.cc:192
std::string to_string() const override
Definition: liboptions.cc:121
MapType()
Definition: liboptions.cc:437
Definition: liboptions.h:123
Definition: liboptions.h:67
Definition: liboptions.h:352
double to_double() const override
Definition: liboptions.cc:288
std::string type() const override
Definition: liboptions.cc:439
Definition: liboptions.h:326
int to_integer() const override
Definition: liboptions.cc:160
Definition: liboptions.h:48
std::string to_string() const override
Definition: liboptions.cc:226
std::string to_string() const override
Definition: liboptions.cc:284
Definition: liboptions.h:296
void add_choices(std::string str) override
Definition: liboptions.cc:277
double to_double() const override
Definition: liboptions.cc:230
void assign(bool b) override
Definition: liboptions.cc:164
std::string get_current_module() const
Definition: liboptions.h:377
bool edit_globals_
Definition: liboptions.h:353
#define PSI_API
Definition: pragma.h:155
Definition: liboptions.h:53
virtual int to_integer() const
Definition: liboptions.cc:92
int to_integer() const override
Definition: liboptions.cc:228
Definition: liboptions.h:73
std::string current_module_
The module that&#39;s active right now.
Definition: liboptions.h:358
virtual bool exists(std::string)
Definition: liboptions.cc:88
void assign(bool b) override
Definition: liboptions.cc:290
double to_double(const std::string str)
Definition: stl_string.cc:178
DuplicateKeyException(const std::string &key, const std::string &type1, const std::string &type2, const char *lfile, int lline)
Definition: liboptions.h:62
std::string str_
Definition: liboptions.h:221
Data & operator[](std::string s) override
Definition: liboptions.cc:464
size_t size() const override
Definition: liboptions.cc:472
std::map< std::string, std::map< std::string, Data > >::const_iterator const_mod_iterator
Definition: liboptions.h:368
virtual double to_double() const
Definition: liboptions.cc:94
~DoubleDataType() override
Definition: liboptions.cc:180
std::string type() const override
Definition: liboptions.cc:119
IntDataType()
Definition: liboptions.cc:146
virtual void add_choices(std::string str)
Definition: liboptions.cc:57
IndexException(const std::string &message)
Definition: liboptions.h:55
virtual ~DataType()
Definition: liboptions.cc:47
#define PSIEXCEPTION(message)
Definition: exception.h:48
virtual void assign(DataType *)
Definition: liboptions.cc:96
std::map< std::string, std::map< std::string, Data > > locals_
&quot;Active&quot; set of options
Definition: liboptions.h:361
int integer_
Definition: liboptions.h:147
std::vector< std::string > choices_
Definition: liboptions.h:194
void changed()
Definition: liboptions.cc:51
DataType()
Definition: liboptions.cc:45
bool is_array() const override
Definition: liboptions.cc:470
void assign(bool b) override
Definition: liboptions.cc:194
double double_
Definition: liboptions.h:170
Definition: liboptions.h:192