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-2018 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 
40 #include "psi4/libpsi4util/libpsi4util.h" // Needed for Ref counting, string splitting, and conversions
41 
42 #include <memory>
43 #include <string>
44 #include <vector>
45 #include <map>
46 
47 namespace psi {
48 
50  public:
51  DataTypeException(const std::string& message) : PSIEXCEPTION(message) {}
52 };
53 
54 class IndexException : public PsiException {
55  public:
56  IndexException(const std::string& message) : PSIEXCEPTION(message + " is not a valid option.") {}
57  IndexException(const std::string& message, const std::string& module)
58  : PSIEXCEPTION(message + " is not a valid option for module " + module) {}
59 };
60 
62  public:
63  DuplicateKeyException(const std::string& key, const std::string& type1, const std::string& type2, const char* lfile,
64  int lline)
65  : PsiException("Option " + key + " has been declared as a " + type1 + " and a " + type2, lfile, lline) {}
66 };
67 
69  public:
70  OptionsException(const std::string& message) : PSIEXCEPTION("Options Exception: " + message) {}
71 };
72 
73 class PSI_API Data;
74 class DataType {
75  bool changed_;
76 
77  public:
78  DataType();
79  virtual ~DataType();
80 
81  bool has_changed() const;
82  void changed();
83  void dechanged();
84 
85  void to_upper(std::string& str);
86 
87  virtual void add_choices(std::string str);
88  virtual std::string type() const;
89 
90  virtual bool is_array() const;
91  virtual size_t size() const;
92  virtual void add(DataType*);
93  virtual void add(std::string, DataType*);
94  virtual void add(bool);
95  virtual void add(int);
96  virtual void add(double);
97  virtual void add(std::string, bool);
98  virtual void add(std::string, std::string);
99  virtual void add(std::string, int);
100  virtual void add(std::string, double);
101  virtual void add(std::string, std::string, std::string);
102 
103  virtual bool exists(std::string);
104 
105  virtual std::string to_string() const;
106  virtual int to_integer() const;
107  virtual double to_double() const;
108 
109  virtual void assign(DataType*);
110  virtual void assign(bool);
111  virtual void assign(int);
112  virtual void assign(double);
113  virtual void assign(std::string);
114 
115  virtual void reset();
116 
117  virtual Data& operator[](std::string);
118  virtual Data& operator[](size_t);
119 };
120 
121 #ifdef __INTEL_COMPILER
122 #pragma warning disable 654
123 #endif
124 class BooleanDataType : public DataType {
125  bool boolean_;
126 
127  public:
128  BooleanDataType();
129  BooleanDataType(bool b);
130  virtual ~BooleanDataType();
131 
132  virtual std::string type() const;
133 
134  virtual std::string to_string() const;
135  virtual int to_integer() const;
136  virtual double to_double() const;
137 
138  virtual void assign(bool b);
139  virtual void assign(int i);
140  virtual void assign(double d);
141  virtual void assign(std::string s);
142 };
143 
144 #ifdef __INTEL_COMPILER
145 #pragma warning disable 654
146 #endif
147 class IntDataType : public DataType {
148  int integer_;
149 
150  public:
151  IntDataType();
152  IntDataType(int i);
153  virtual ~IntDataType();
154 
155  virtual std::string type() const;
156 
157  virtual std::string to_string() const;
158  virtual int to_integer() const;
159  virtual double to_double() const;
160 
161  virtual void assign(bool b);
162  virtual void assign(int i);
163  virtual void assign(double d);
164  virtual void assign(std::string s);
165 };
166 
167 #ifdef __INTEL_COMPILER
168 #pragma warning disable 654
169 #endif
170 class DoubleDataType : public DataType {
171  double double_;
172 
173  public:
174  DoubleDataType();
175  DoubleDataType(double d);
176  virtual ~DoubleDataType();
177 
178  virtual std::string type() const;
179 
180  virtual std::string to_string() const;
181  virtual int to_integer() const;
182  virtual double to_double() const;
183 
184  virtual void assign(bool b);
185  virtual void assign(int i);
186  virtual void assign(double d);
187  virtual void assign(std::string s);
188 };
189 
190 #ifdef __INTEL_COMPILER
191 #pragma warning disable 654
192 #endif
193 class StringDataType : public DataType {
194  std::string str_;
195  std::vector<std::string> choices_;
196 
197  public:
198  StringDataType();
199  StringDataType(std::string s);
200  StringDataType(std::string s, std::string c);
201 
202  virtual ~StringDataType();
203 
204  virtual void add_choices(std::string str);
205 
206  virtual std::string type() const;
207 
208  virtual std::string to_string() const;
209  virtual int to_integer() const;
210  virtual double to_double() const;
211 
212  virtual void assign(bool b);
213  virtual void assign(int i);
214  virtual void assign(double d);
215  virtual void assign(std::string s);
216 };
217 
218 #ifdef __INTEL_COMPILER
219 #pragma warning disable 654
220 #endif
221 class IStringDataType : public DataType {
222  std::string str_;
223  std::vector<std::string> choices_;
224 
225  public:
226  IStringDataType();
227  IStringDataType(std::string s);
228  IStringDataType(std::string s, std::string c);
229  virtual ~IStringDataType();
230 
231  virtual void add_choices(std::string str);
232 
233  virtual std::string type() const;
234 
235  virtual std::string to_string() const;
236  virtual int to_integer() const;
237  virtual double to_double() const;
238 
239  virtual void assign(bool b);
240  virtual void assign(int i);
241  virtual void assign(double d);
242  virtual void assign(std::string s);
243 };
244 
245 class PSI_API Data {
246  std::shared_ptr<DataType> ptr_;
247 
248  public:
249  Data();
250  Data(DataType* t);
251  Data(const Data& copy);
252 
253  std::string to_string() const;
254  int to_integer() const;
255  double to_double() const;
256 
257  bool is_array() const;
258  size_t size() const;
259 
260  bool has_changed() const;
261 
262  void changed();
263  void dechanged();
264 
265  void add_choices(std::string str);
266 
267  std::string type() const;
268 
269  void add(DataType* data);
270  void add(std::string s, DataType* data);
271  void add(bool b);
272  void add(int i);
273  void add(double d);
274  void add(std::string s, std::string c);
275  void add(std::string key, bool b);
276  void add(std::string key, int i);
277  void add(std::string key, double d);
278  void add(std::string key, std::string s, std::string c);
279 
280  void assign(DataType* data);
281  void assign(bool b);
282  void assign(int i);
283  void assign(double d);
284  void assign(std::string s);
285 
286  void reset();
287 
288  DataType* get() const;
289 
290  Data& operator[](int i);
291  Data& operator[](std::string s);
292 };
293 
294 #ifdef __INTEL_COMPILER
295 #pragma warning disable 654
296 #endif
297 class PSI_API ArrayType : public DataType {
298  std::vector<Data> array_;
299 
300  public:
301  ArrayType();
302 
303  virtual std::string type() const;
304 
305  virtual void add(DataType* data);
306  virtual void add(bool b);
307  virtual void add(int i);
308  virtual void add(double d);
309  virtual void add(std::string s, std::string c = "");
310  virtual void assign(DataType* data);
311 
312  virtual Data& operator[](size_t i);
313  virtual Data& operator[](std::string s);
314  virtual bool is_array() const;
315 
316  virtual size_t size() const;
317 
318  virtual std::string to_string() const;
319 
320  virtual void reset();
321  std::vector<Data> data() { return array_; }
322 };
323 
324 #ifdef __INTEL_COMPILER
325 #pragma warning disable 654
326 #endif
327 class MapType : public DataType {
328  std::map<std::string, Data> keyvals_;
329  typedef std::map<std::string, Data>::iterator iterator;
330  typedef std::map<std::string, Data>::const_iterator const_iterator;
331 
332  public:
333  MapType();
334 
335  virtual std::string type() const;
336 
337  virtual void add(std::string key, DataType* data);
338  virtual void add(std::string key, bool b);
339  virtual void add(std::string key, int i);
340  virtual void add(std::string key, double d);
341  virtual void add(std::string key, std::string s, std::string c = "");
342 
343  virtual bool exists(std::string key);
344 
345  virtual Data& operator[](std::string s);
346  virtual bool is_array() const;
347 
348  virtual size_t size() const;
349 
350  virtual std::string to_string() const;
351 };
352 
355 
357  std::map<std::string, Data> all_local_options_;
359  std::string current_module_;
360 
362  std::map<std::string, std::map<std::string, Data> > locals_;
363 
365  std::map<std::string, Data> globals_;
366 
367  typedef std::map<std::string, Data>::iterator iterator;
368  typedef std::map<std::string, Data>::const_iterator const_iterator;
369  typedef std::map<std::string, std::map<std::string, Data> >::const_iterator const_mod_iterator;
370 
371  public:
372  Options();
373 
374  Options& operator=(const Options& rhs);
375  bool read_globals() const;
376  void set_read_globals(bool _b);
377  void set_current_module(const std::string s);
378  std::string get_current_module() const { return current_module_; }
379 
380  void to_upper(std::string& str);
381 
382  void validate_options();
383 
384  void add(std::string key, DataType* data);
385  void add(std::string key, bool b);
386  void add(std::string key, int i);
387  void add(std::string key, double d);
388  void add(std::string key, std::string s, std::string c = "");
389  void add_i(std::string key, std::string s, std::string c = "");
390 
391  void add_bool(std::string key, bool b);
392  void add_int(std::string key, int i);
393  void add_double(std::string key, double d);
394  void add_str(std::string key, std::string s, std::string c = "");
395  void add_str_i(std::string key, std::string s, std::string c = "");
396  void add_array(std::string key);
397  void set_bool(const std::string& module, const std::string& key, bool b);
398  void set_int(const std::string& module, const std::string& key, int i);
399  void set_double(const std::string& module, const std::string& key, double d);
400  void set_str(const std::string& module, const std::string& key, std::string s);
401  void set_str_i(const std::string& module, const std::string& key, std::string s);
402  void set_array(const std::string& module, const std::string& key);
403 
404  void set_global_bool(const std::string& key, bool b);
405  void set_global_int(const std::string& key, int i);
406  void set_global_double(const std::string& key, double d);
407  void set_global_str(const std::string& key, const std::string& s);
408  void set_global_array(const std::string& key);
409 
410  DataType* set_global_array_entry(const std::string& key, DataType* entry, DataType* loc);
411  void set_global_array_double(std::string key, double val, DataType* entry);
412  void set_global_array_string(std::string key, std::string val, DataType* entry);
413  void set_global_array_int(std::string key, int val, DataType* entry);
414  DataType* set_global_array_array(std::string key, DataType* entry);
415  DataType* set_local_array_entry(const std::string& module, const std::string& key, DataType* entry, DataType* loc);
416  void set_local_array_double(const std::string& module, const std::string& key, double val, DataType* entry);
417  void set_local_array_string(const std::string& module, const std::string& key, std::string val, DataType* entry);
418  void set_local_array_int(const std::string& module, const std::string& key, int val, DataType* entry);
419  DataType* set_local_array_array(const std::string& module, const std::string& key, DataType* entry);
420 
421  void clear(void);
422 
423  bool exists_in_active(std::string key);
424 
425  bool exists_in_global(std::string key);
426  bool exists(std::string key);
427 
428  Data& get(std::string key);
429 
430  Data& get(std::map<std::string, Data>& m, std::string& key);
431 
432  Data& get_global(std::string key);
433 
434  Data& get_local(std::string& key);
435 
436  Data& use(std::string& key);
437 
438  Data& use_local(std::string& key);
439 
440  bool get_bool(std::string key);
441  int get_int(std::string key);
442  double get_double(std::string key);
443  std::string get_str(std::string key);
444  int* get_int_array(std::string key);
445  void fill_int_array(std::string key, int* empty_array);
446  std::vector<int> get_int_vector(std::string key);
447  double* get_double_array(std::string key);
448 
449  std::vector<double> get_double_vector(std::string key);
450 
451  const char* get_cstr(std::string key);
452 
453  Data& operator[](std::string key);
454 
455  std::string to_string() const;
456  std::string globals_to_string() const;
457 
458  void print();
459  void print_globals();
460  std::vector<std::string> list_globals();
461 };
462 } // namespace psi
463 #endif
virtual void add(std::string key, DataType *data)
Definition: liboptions.cc:454
std::map< std::string, Data >::iterator iterator
Definition: liboptions.h:367
virtual void assign(bool b)
Definition: liboptions.cc:147
std::vector< Data > array_
Definition: liboptions.h:298
virtual int to_integer() const
Definition: liboptions.cc:203
virtual ~IStringDataType()
Definition: liboptions.cc:288
std::map< std::string, Data > keyvals_
Definition: liboptions.h:328
IndexException(const std::string &message, const std::string &module)
Definition: liboptions.h:57
std::string to_string(const int val)
Definition: stl_string.cc:180
virtual void assign(bool b)
Definition: liboptions.cc:207
std::vector< Data > data()
Definition: liboptions.h:321
std::map< std::string, Data > globals_
&quot;Global&quot; set of options
Definition: liboptions.h:365
virtual std::string to_string() const
Definition: liboptions.cc:103
virtual Data & operator[](std::string)
Definition: liboptions.cc:121
virtual std::string type() const
Definition: liboptions.cc:165
std::shared_ptr< DataType > ptr_
Definition: liboptions.h:246
Definition: liboptions.h:245
std::vector< std::string > choices_
Definition: liboptions.h:223
OptionsException(const std::string &message)
Definition: liboptions.h:70
virtual bool is_array() const
Definition: liboptions.cc:77
std::map< std::string, Data >::const_iterator const_iterator
Definition: liboptions.h:330
virtual int to_integer() const
Definition: liboptions.cc:299
Definition: liboptions.h:61
virtual void add_choices(std::string str)
Definition: liboptions.cc:290
Definition: liboptions.h:170
class PSI_API Data
Definition: liboptions.h:73
std::string str_
Definition: liboptions.h:194
virtual std::string type() const
Definition: liboptions.cc:452
virtual ~IntDataType()
Definition: liboptions.cc:163
Definition: liboptions.h:147
std::map< std::string, Data > all_local_options_
A temporary map used for validation of local options.
Definition: liboptions.h:357
Definition: liboptions.h:221
Definition: pointgrp.h:104
void dechanged()
Definition: liboptions.cc:66
DataTypeException(const std::string &message)
Definition: liboptions.h:51
int to_integer(const std::string inString)
Definition: stl_string.cc:192
std::map< std::string, Data >::iterator iterator
Definition: liboptions.h:329
virtual std::string type() const
Definition: liboptions.cc:75
bool changed_
Definition: liboptions.h:75
virtual void add(DataType *)
Definition: liboptions.cc:81
virtual double to_double() const
Definition: liboptions.cc:145
virtual size_t size() const
Definition: liboptions.cc:79
virtual std::string to_string() const
Definition: liboptions.cc:487
StringDataType()
Definition: liboptions.cc:219
virtual void assign(bool b)
Definition: liboptions.cc:245
virtual ~DoubleDataType()
Definition: liboptions.cc:193
virtual double to_double() const
Definition: liboptions.cc:243
void to_upper(std::string &str)
Definition: stl_string.cc:170
virtual void assign(bool b)
Definition: liboptions.cc:177
Definition: exception.h:56
bool has_changed() const
Definition: liboptions.cc:62
bool boolean_
Definition: liboptions.h:125
DoubleDataType()
Definition: liboptions.cc:189
BooleanDataType()
Definition: liboptions.cc:126
IStringDataType()
Definition: liboptions.cc:280
std::map< std::string, Data >::const_iterator const_iterator
Definition: liboptions.h:368
virtual std::string type() const
Definition: liboptions.cc:237
virtual std::string to_string() const
Definition: liboptions.cc:167
virtual void reset()
Definition: liboptions.cc:119
void to_upper(std::string &str)
Definition: liboptions.cc:68
MapType()
Definition: liboptions.cc:450
virtual bool is_array() const
Definition: liboptions.cc:483
virtual std::string to_string() const
Definition: liboptions.cc:134
Definition: liboptions.h:124
Definition: liboptions.h:68
Definition: liboptions.h:353
virtual double to_double() const
Definition: liboptions.cc:175
virtual std::string type() const
Definition: liboptions.cc:195
Definition: liboptions.h:327
Definition: liboptions.h:49
virtual void assign(bool b)
Definition: liboptions.cc:303
virtual void add_choices(std::string str)
Definition: liboptions.cc:231
Definition: liboptions.h:297
std::string get_current_module() const
Definition: liboptions.h:378
virtual int to_integer() const
Definition: liboptions.cc:173
bool edit_globals_
Definition: liboptions.h:354
#define PSI_API
Definition: pragma.h:155
Definition: liboptions.h:54
virtual std::string to_string() const
Definition: liboptions.cc:197
virtual int to_integer() const
Definition: liboptions.cc:105
Definition: liboptions.h:74
std::string current_module_
The module that&#39;s active right now.
Definition: liboptions.h:359
virtual bool exists(std::string)
Definition: liboptions.cc:101
virtual ~StringDataType()
Definition: liboptions.cc:229
virtual double to_double() const
Definition: liboptions.cc:205
virtual ~BooleanDataType()
Definition: liboptions.cc:130
double to_double(const std::string str)
Definition: stl_string.cc:178
virtual std::string to_string() const
Definition: liboptions.cc:297
DuplicateKeyException(const std::string &key, const std::string &type1, const std::string &type2, const char *lfile, int lline)
Definition: liboptions.h:63
std::string str_
Definition: liboptions.h:222
virtual std::string type() const
Definition: liboptions.cc:132
virtual std::string to_string() const
Definition: liboptions.cc:239
virtual Data & operator[](std::string s)
Definition: liboptions.cc:477
std::map< std::string, std::map< std::string, Data > >::const_iterator const_mod_iterator
Definition: liboptions.h:369
virtual double to_double() const
Definition: liboptions.cc:107
virtual bool exists(std::string key)
Definition: liboptions.cc:470
virtual std::string type() const
Definition: liboptions.cc:295
IntDataType()
Definition: liboptions.cc:159
virtual double to_double() const
Definition: liboptions.cc:301
virtual void add_choices(std::string str)
Definition: liboptions.cc:70
IndexException(const std::string &message)
Definition: liboptions.h:56
virtual ~DataType()
Definition: liboptions.cc:60
#define PSIEXCEPTION(message)
Definition: exception.h:50
virtual void assign(DataType *)
Definition: liboptions.cc:109
std::map< std::string, std::map< std::string, Data > > locals_
&quot;Active&quot; set of options
Definition: liboptions.h:362
int integer_
Definition: liboptions.h:148
std::vector< std::string > choices_
Definition: liboptions.h:195
virtual int to_integer() const
Definition: liboptions.cc:143
virtual size_t size() const
Definition: liboptions.cc:485
void changed()
Definition: liboptions.cc:64
DataType()
Definition: liboptions.cc:58
virtual int to_integer() const
Definition: liboptions.cc:241
double double_
Definition: liboptions.h:171
Definition: liboptions.h:193