ALPSCore reference
params.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #ifndef ALPS_PARAMS_HPP_INCLUDED_00f672a032d949a7aa0e760a6b6f0602
8 #define ALPS_PARAMS_HPP_INCLUDED_00f672a032d949a7aa0e760a6b6f0602
9 
10 #include "alps/config.hpp"
11 #include "alps/hdf5/archive.hpp"
13 
14 
15 #ifdef ALPS_HAVE_MPI
16 #include <alps/utilities/mpi.hpp>
18 #endif
19 
20 #include <map>
21 #include <iosfwd>
22 
23 #include "./dictionary.hpp"
24 
25 namespace alps {
26  namespace params_ns {
27 
28  namespace detail {
30  struct make_typestr : public boost::static_visitor<std::string> {
31  template <typename T>
32  std::string operator()(const T&) const { return apply<T>(); }
33 
34  template <typename T>
35  static std::string apply() { return detail::type_info<T>::pretty_name(); }
36  };
37 
39  struct td_type {
40  std::string typestr_;
41  std::string descr_;
42  int defnumber_;
44  std::string& typestr() { return typestr_; }
46  const std::string& typestr() const { return typestr_; }
48  std::string& descr() { return descr_; }
50  const std::string& descr() const { return descr_; }
52  int defnumber() const { return defnumber_; }
54  int& defnumber() { return defnumber_; }
56  td_type(const std::string& t, const std::string& d, int n) : typestr_(t), descr_(d), defnumber_(n) {}
58  template <typename T>
59  static td_type make_pair(const std::string& d, int n) { return td_type(make_typestr::apply<T>(), d, n); }
61  td_type() : typestr_(), descr_(), defnumber_(-1) {}
63  bool operator==(const td_type& rhs) const {
64  return typestr_==rhs.typestr_ &&
65  descr_==rhs.descr_ &&
66  defnumber_==rhs.defnumber_;
67  }
68  };
69  } // detail::
70 
71 
73 
84  class params : public dictionary {
85  private:
86  typedef std::map<std::string,std::string> strmap;
87  typedef std::vector<std::string> strvec;
88 
89  // Small inner class to keep "origins" together
90  struct origins_type {
91  enum {
92  ARGV0=0,
93  ARCHNAME=1,
94  INIFILES=2
95  };
96 
97  strvec data_;
98  origins_type(): data_(INIFILES) {}
99  strvec& data() { return data_; }
100  const strvec& data() const { return data_; }
101  void check() {
102  if (data_.size()<INIFILES)
103  throw std::logic_error("params::origins_type invariants violation");
104  }
105  };
106 
107  typedef std::map<std::string, detail::td_type> td_map_type;
108 
109  strmap raw_kv_content_;
110  td_map_type td_map_;
111  strvec err_status_;
112  origins_type origins_;
113  std::string help_header_;
114 
115  void read_ini_file_(const std::string& inifile);
116  void initialize_(int argc, const char* const* argv, const char* hdf5_path);
117 
118  template <typename T>
119  bool assign_to_name_(const std::string& name, const std::string& strval);
120 
121  bool has_unused_(std::ostream& out, const std::string* prefix_ptr) const;
122 
124  template <typename T>
125  bool define_(const std::string& name, const std::string& descr);
126 
127  public:
129  params() : dictionary(), raw_kv_content_(), td_map_(), err_status_(), origins_(), help_header_() {}
130 
132 
136  params(const std::string& inifile);
137 
139 
186  params(int argc, const char* const* argv, const char* hdf5_path="/parameters");
187 
189  std::string get_argv0() const;
190 
192  std::string get_ini_name(int n) const;
193 
195  int get_ini_name_count() const;
196 
197 
199 
204  std::string get_origin_name() const ALPS_DEPRECATED;
205 
206  // FIXME: 1) Make these exception types derived from some `params::exception`;
207  // 2) Define them ouside and typedef here.
208 
210  struct not_restored : public std::runtime_error {
211  not_restored(const std::string& a_what)
212  : std::runtime_error(a_what) {}
213  };
214 
216  class archive_conflict : public std::runtime_error {
217  std::vector<std::string> fnames_;
218  public:
219  archive_conflict(const std::string& a_what, const std::string& fname1, const std::string& fname2)
220  : std::runtime_error(a_what+"; name1='"+fname1+"' name2='"+fname2+"'"),
221  fnames_({fname1, fname2})
222  { }
223 
224  const std::string& get_name(unsigned int i) const {
225  return fnames_[i % fnames_.size()];
226  }
227 
228  const std::vector<std::string>& get_names() const {
229  return fnames_;
230  }
231  };
232 
234  bool is_restored() const { return !origins_.data()[origins_type::ARCHNAME].empty(); }
235 
237  std::string get_archive_name() const;
238 
240  bool ok() const { return err_status_.empty(); }
241 
243  bool has_missing() const { return !ok(); }
244 
246  bool defaulted(const std::string& name) const;
247 
249  bool supplied(const std::string& name) const;
250 
252  bool has_unused(std::ostream& out) const;
253 
255 
260  bool has_unused(std::ostream& out, const std::string& subsection) const;
261 
263  bool has_missing(std::ostream& out) const;
264 
266  bool help_requested() const;
267 
269  bool help_requested(std::ostream&) const;
270 
272  std::ostream& print_help(std::ostream&) const;
273 
275  bool operator==(const params& rhs) const;
276 
278 
279  bool defined(const std::string& name) const;
280 
282  template<typename T>
283  params& define(const std::string& name, const std::string& descr);
284 
286  template<typename T>
287  params& define(const std::string& name, const T& defval, const std::string& descr);
288 
290  params& define(const std::string& name, const std::string& descr) {
291  return define<bool>(name, false, descr);
292  }
293 
295  params& description(const std::string& message);
296 
298  const std::string get_descr(const std::string& name) const;
299 
300  friend void swap(params& p1, params& p2);
301 
303  void save(alps::hdf5::archive&) const;
304 
306  void load(alps::hdf5::archive&);
307 
309  friend
310  std::ostream& operator<<(std::ostream&, const params&);
311 
312 #ifdef ALPS_HAVE_MPI
313  // FIXME: should it be virtual?
314  void broadcast(const alps::mpi::communicator& comm, int root);
315 
317 
328  params(int argc, const char* const* argv, const alps::mpi::communicator& comm, int root=0, const char* hdf5_path="/parameters")
329  : dictionary(),
330  raw_kv_content_(),
331  td_map_(),
332  err_status_(),
333  origins_(),
334  help_header_()
335 
336  {
337  initialize_(argc,argv,hdf5_path);
338  broadcast(comm, root);
339  }
340 #endif
341  };
342 
344 
355  std::string origin_name(const params& p);
356 
357  } // params_ns::
359 
360 #ifdef ALPS_HAVE_MPI
361  namespace mpi {
362 
363  inline void broadcast(const alps::mpi::communicator& comm, alps::params_ns::detail::td_type& td, int root) {
364  broadcast(comm, td.typestr_, root);
365  broadcast(comm, td.descr_, root);
366  broadcast(comm, td.defnumber_, root);
367  }
368 
369  inline void broadcast(const alps::mpi::communicator &comm, alps::params_ns::dictionary& dict, int root) {
370  dict.broadcast(comm, root);
371  }
372 
373  inline void broadcast(const alps::mpi::communicator &comm, alps::params_ns::params& p, int root) {
374  p.broadcast(comm, root);
375  }
376  } // mpi::
377 #endif
378 
379 } // alps::
380 
381 #include "./params/params_impl.hpp"
382 
383 
384 #endif /* ALPS_PARAMS_HPP_INCLUDED_00f672a032d949a7aa0e760a6b6f0602 */
const std::vector< std::string > & get_names() const
Definition: params.hpp:228
bool ok() const
No-errors status.
Definition: params.hpp:240
Parse sectioned INI file or HDF5 or command line, provide the results as dictionary.
Definition: params.hpp:84
void load(archive &ar, std::string const &path, T &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:309
void broadcast(C const &c, P &p, int r=0)
Definition: api.hpp:56
STL namespace.
void swap(params &p1, params &p2)
bool operator==(const dictionary &lhs, const dictionary &rhs)
Definition: dictionary.hpp:96
Header for object-oriented interface to MPI (similar to boost::mpi)
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
params()
Default ctor: creates an empty parameters object.
Definition: params.hpp:129
std::string origin_name(const params &p)
Convenience function to obtain the "origin" filename associated with the parameters object...
Definition: params.cpp:505
Exception type: attempt to restore from 2 archives.
Definition: params.hpp:216
bool has_missing() const
True if there are missing or wrong-type parameters.
Definition: params.hpp:243
params & define(const std::string &name, const std::string &descr)
Defines a flag (boolean option with default of false)
Definition: params.hpp:290
Exception type: the object was not restored from archive.
Definition: params.hpp:210
bool is_restored() const
Conveninece method: true if the object was restored from an archive.
Definition: params.hpp:234
const std::string & get_name(unsigned int i) const
Definition: params.hpp:224
archive_conflict(const std::string &a_what, const std::string &fname1, const std::string &fname2)
Definition: params.hpp:219
#define ALPS_DEPRECATED
Definition: deprecated.hpp:14
params_ns::params params
Definition: params.hpp:358
Python-like dictionary.
Definition: dictionary.hpp:18
not_restored(const std::string &a_what)
Definition: params.hpp:211
std::ostream & operator<<(std::ostream &os, const dict_value &dv)
Print the value together with type in some human-readable format.
Definition: dict_value.hpp:160
void save(archive &ar, std::string const &path, T const &value, std::vector< std::size_t >=std::vector< std::size_t >(), std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:292