QNANO
Parameter_Map.h
1 #ifndef QNANO_NEW_PARAMETER_LIST_DEFINED_H
2 #define QNANO_NEW_PARAMETER_LIST_DEFINED_H
3 
4 #include "tools/Reader.h"
5 #include "tools/Debug_Logger.h"
6 #include "tools/Vec3d.h"
7 #include <map>
8 #include <algorithm>
9 
10 typedef std::vector<std::vector<std::string> > Parameter_Values;
11 
13 //private:
14 public:
15  int *p_args; char ***p_argv;
21  std::map<std::string, Parameter_Values > map;
22 
23 
25  std::vector<std::string> requested;
26  bool use_check_requested_on_destruction;
27 
28 
31 
32  typedef std::map<std::string, std::vector<std::vector<std::string> > >::iterator map_iter;
33  typedef std::map<std::string, std::vector<std::vector<std::string> > >::const_iterator map_const_iter;
34  std::vector<std::vector<std::string> > empty_stringvecvec;
35 
36 public:
37 
38  void add(const std::string &key, const std::vector<std::string> & stringvec);
39  void add(const std::string &key, const std::string &str);
40 
41  void set_requested(const std::string &key);
42 
43  bool is_specified(const std::string &key, size_t row=0, size_t col=0);
44  bool is_double(const std::string &key);
45  bool is_bool(const std::string &key);
46  bool is_Vec3d(const std::string &key, size_t row=0, size_t col=0);
47  bool is_VecXd(const std::string &key, size_t X, size_t row=0, size_t col=0);
48  void complain_if_not_specified(const std::string &key, std::ostream &ofs=std::cerr);
49  void quit_if_not_specified(const std::string &key, size_t row=0, size_t col=0);
50  void override_parameter(const std::string &key, const std::vector<std::string> & stringvec);
51  void override_parameter(const std::string &key, const std::string &value){
52  override_parameter(key,std::vector<std::string>(1,value));
53  }
54  void override_parameter(const std::string &key, double value){
55  override_parameter(key,Reader::toString(value));
56  }
57 
58  const Parameter_Values & get(const std::string &key);
59 
60  size_t get_rows(const std::string &key);
61  size_t get_cols(const std::string &key, size_t row);
62 
63  //condense string_vec_vec to string_vec:
64  std::vector<std::string> get_all_strings(const std::string &key);
65 
66  std::string get_as_string(const std::string &key, const std::string default_="", size_t row=0, size_t col=0);
67 /* inline std::string get_as_string(const char* key, const char* default_="", size_t row=0, size_t col=0){
68  return get_as_string(std::string(key),std::string(default_),row,col);
69  }*/
70  std::string get_as_string_check(const std::string &key, const std::string default_="", size_t row=0, size_t col=0){
71  quit_if_not_specified(key,row,col);
72  return get_as_string(key,default_, row, col);
73  }
74 
75  double get_as_double(const std::string &key, double default_=0., size_t row=0, size_t col=0);
76  double get_as_double_check(const std::string &key, double default_=0., size_t row=0, size_t col=0){
77  quit_if_not_specified(key, row, col);
78  return get_as_double(key,default_,row,col);
79  }
80  size_t get_as_sizet(const std::string &key, size_t default_=0., size_t row=0, size_t col=0){
81  int i=get_as_double(key,default_,row,col);
82  if(i<0){ std::cerr<<"Parameter_Map::get_as_sizet: value less than zero!"<<std::endl; exit(1);}
83  return i;
84  }
85  double get_as_sizet_check(const std::string &key, size_t default_=0., size_t row=0, size_t col=0){
86  quit_if_not_specified(key, row, col);
87  return get_as_sizet(key,default_,row,col);
88  }
89 
90  bool get_as_bool(const std::string &key, bool default_=false, size_t row=0, size_t col=0);
91 
92 
93 
94  std::vector<double> get_as_VecXd(const std::string &key, size_t X, std::vector<double> default_=std::vector<double>(), size_t row=0, size_t col=0);
95 
96  Vec3d get_as_Vec3d(const std::string &key, Vec3d default_=Vec3d(), size_t row=0, size_t col=0){
97  std::vector<double> default_vec(3);
98  default_vec[0]=default_[0];
99  default_vec[1]=default_[1];
100  default_vec[2]=default_[2];
101  std::vector<double> v=get_as_VecXd(key, 3, default_vec, row, col);
102  return Vec3d(v[0],v[1],v[2]);
103  }
104  inline Vec3d get_as_Vec3d(const char* key, Vec3d default_=Vec3d(), size_t row=0, size_t col=0){
105  return get_as_Vec3d(std::string(key),default_,row,col);
106  }
107  inline Vec3d get_as_Vec3d_check(const std::string &key, Vec3d default_=Vec3d(), size_t row=0, size_t col=0){
108  quit_if_not_specified(key);
109  return get_as_Vec3d(key, default_, row, col);
110  }
111 
112 
113  void add_from_file(const std::string &filename);
114  void add_from_command_line(int args, char** argv);
115  void add_from_command_line_or_driver(int args, char** argv);
116 
117 
118  void print(std::ostream &ofs)const;
119  void print(const std::string filename)const;
120  void print(const char* filename)const;
121 
122  void check_requested()const;
123 
124  void setup_logger(){
125  logger.setup(get_as_string("debug_log"));
126  }
127 
128 
129  void copy(const Parameter_Map &other){
130  map=other.map;
131  use_check_requested_on_destruction=false;
132  logger=other.logger;
133  }
134  Parameter_Map &operator=(const Parameter_Map &other){
135  copy(other);
136  return *this;
137  }
138  Parameter_Map(const Parameter_Map &other){
139  copy(other);
140  }
141  Parameter_Map(const std::string &filename, bool use_check_requested=false){
142  use_check_requested_on_destruction=use_check_requested;
143  add_from_file(filename);
144  setup_logger();
145  }
146  Parameter_Map(const char* filename, bool use_check_requested=false){
147  use_check_requested_on_destruction=use_check_requested;
148  add_from_file(std::string(filename));
149  setup_logger();
150  }
151  Parameter_Map(int &args, char** &argv, bool use_check_requested=false){
152  p_args=&args; p_argv=&argv;
153  use_check_requested_on_destruction=use_check_requested;
154  add_from_command_line_or_driver(args,argv);
155  setup_logger();
156  }
157  Parameter_Map(){}
158  ~Parameter_Map(){
159  if(use_check_requested_on_destruction)check_requested();
160  setup_logger();
161  }
162 };
163 
164 #endif
Debug_Logger logger
It is very convenient to pass along a log file for debugging purposes with the Parameter_Map: ...
Definition: Parameter_Map.h:30
Definition: Parameter_Map.h:12
std::map< std::string, Parameter_Values > map
Definition: Parameter_Map.h:21
Class to store positions and to handle operations on 3d vectors.
Definition: Vec3d.h:9
Definition: Debug_Logger.h:7
std::vector< std::string > requested
Track which parameters have been requested to point out unused paramters:
Definition: Parameter_Map.h:25