Greens-code
A modular quantum transport code
input_file.hpp
1 #ifndef INPUT_G_H
2 #define INPUT_G_H
3 
4 #include <iostream>
5 #include <fstream>
6 #include <map>
7 #include <set>
8 #include <list>
9 #include <memory>
10 
11 
12 #include "exceptions.hpp"
13 #include "singleton.hpp"
14 
15 #ifdef USE_ABSTRACT_FUNCTIONS
16 #define D_ABSTRACT(X) = 0
17 #else
18 #define D_ABSTRACT(X) { throw(E_real_virtual_function(" X ",class_Name()));}
19 #endif
20 
21 const char* const TRUE_TYPE_NAME = "true_type_name";
22 
24 /*!****************************
25  *
26  * Abstract base clase for observer objects,
27  * These are signaled by Loadable_Object (see below) to
28  * report a state change
29  *
30  *******************************/
31 
32 class Observer{
33 
34 public:
35  typedef STD_TR1::shared_ptr< Observer > ref;
36 
37  virtual ~Observer() throw() {};
38 
39  void notify(){ notified_();};
40 
41 private:
42  virtual void notified_() = 0;
43 
44 };
45 
46 
48 
80 public:
81  typedef STD_TR1::shared_ptr< loadable_Object > ref;
82  typedef STD_TR1::shared_ptr< const loadable_Object > const_ref;
83 
84  typedef std::pair<std::string, ref> ref_pair;
85 
86  typedef STD_TR1::weak_ptr< loadable_Object > link;
87  typedef STD_TR1::weak_ptr< const loadable_Object > const_link;
88 
90  void write_xml(std::ostream&) const;
91 
93  loadable_Object():ready(false), written(false) {};
94  virtual ~loadable_Object() throw();
95 
97  const char* class_Name() const { return name();};
98 
100  bool get_param(const std::string& s, double& v) const;
101  bool get_param(const std::string& s, int& v) const;
102  bool get_param(const std::string& s, std::string& v) const;
103 
105  double get_double_param(const std::string& s) const;
106  int get_int_param(const std::string& s) const;
107  std::string get_string_param(const std::string& s) const;
108 
110  void set_param(const std::string& s, double v);
111  void set_param(const std::string& s, int v);
112  void set_param(const std::string& s, std::string v);
114  bool get_bool(const std::string& s, const bool def) const;
115 
117  void acquire_params(const const_ref&);
118 
120  void insert_object(const std::string& s, ref o){
121  components[s] = o;
122  component_list.push_back(ref_pair(s,o));};
123 
125  void link_object(const std::string& s, ref o){ links[s] = o;};
126 
128  bool get_component(const std::string& s, const_ref&) const;
129  bool get_component(const std::string& s, ref&);
130 
132  const_ref get_component(const std::string& s) const;
133  ref get_component(const std::string& s);
134 
136 
142  template <typename T>
143  STD_TR1::shared_ptr<T> get_dynamic_component(const std::string& s);
144 
145  template <typename T>
146  STD_TR1::shared_ptr<const T> get_dynamic_component(const std::string& s) const;
147 
149 
158  ref get_ref(){ return ref(my_reference);};
159  const_ref get_ref() const { return const_ref(my_reference);};
160 
162 
169  template <typename T>
170  STD_TR1::shared_ptr<T> get_dynamic_ref();
171  template <typename T>
172  STD_TR1::shared_ptr<const T> get_dynamic_ref() const;
173 
175 
182  void initialize(ref me);
183 
185 
189  void set_correct_ref(ref me);
190 
192  bool is_initialized() const { return ready;};
193 
195  void require_initialized() const { if (!ready) throw (E_Not_ready(class_Name()));};
197  void force_reinit();
199  void register_Observer( Observer::ref );
200  void unregister_Observer( Observer::ref );
201 
203  size_t nparams() const {
204  return params.size();};
205 
207  ref clone() const;
208 
209  template <class T>
210  static
211  STD_TR1::shared_ptr<const T> clone( const STD_TR1::shared_ptr<const T> r) {
212  ref o(r->clone());
213  STD_TR1::shared_ptr<T> result =
214  STD_TR1::dynamic_pointer_cast<T>(o);
215  return result;
216  }
217 
218  template <class T>
219  static
220  STD_TR1::shared_ptr<T> clone( STD_TR1::shared_ptr<T> r) {
221  ref o(r->clone());
222  STD_TR1::shared_ptr<T> result =
223  STD_TR1::dynamic_pointer_cast<T>(o);
224  return result;
225  }
226 
227  virtual size_t verbosity() {return 10;}
228 private:
229  int ready;
230  mutable bool written;
231 
232  struct paramstruct{
233  std::string s;
234  int i;
235  double d;
236 
237  enum {a_str, an_int, a_double} status;
238  };
239 
240 
241  std::map<std::string, paramstruct> params;
242 
243  std::set< Observer::ref > observers;
244 
245  std::map<std::string, ref > components;
246  std::list< ref_pair > component_list;
247 
248  std::map<std::string, link> links;
249 
250  void notify_Observers();
251 
252  void finnish_writing() const;
253 
254  typedef std::map< const_ref, ref> ref_map;
255 
256  ref clone(ref_map& ) const;
257 
258  void clone_component( ref, const std::string&, ref, ref_map& ) const;
259  void clone_link( ref, const std::string&, ref, ref_map& ) const;
260 
261  void initialize_from_clone( ref , const_ref );
262 
263  virtual const char* name() const { return "root";};
264  virtual void init() D_ABSTRACT(init);
265  virtual void init_from_clone( const_ref );
266 
267 
268  STD_TR1::weak_ptr<loadable_Object> my_reference;
269 
270  void write_xml_(std::ostream& os, int indent) const;
271 
272 };
273 
278 public:
279  virtual ~Parameters() throw() {};
280 
281 private:
282  const char* name() const { return "Parameters";};
283  void init();
284 };
285 
286 template <typename T>
288 public:
289  static T exec(const std::string& name){
290  throw (E_unknown_typename(name));
291  };
292 };
293 
294 template <class T>
296 public:
297  static T exec(const std::string& name){
298  std::cerr << "Warning: " << name << " not registered. Reverting to Parameter\n";
299  T t(new typename T::element_type);
300  t->set_param(TRUE_TYPE_NAME,name);
301  return t;
302  };
303 };
304 
305 #include "input_file.tpp"
306 
307 typedef manage_Loadable<loadable_Object,
309  STD_TR1::shared_ptr<Parameters>
310  >
311  >
312 effective_manage_Loadable;
313 
314 #endif
Definition: exceptions.hpp:179
void require_initialized() const
throws if is_initialized returns false
Definition: input_file.hpp:195
Definition: exceptions.hpp:133
Definition: input_file.hpp:295
Simple collection of Parameters.
Definition: input_file.hpp:277
ref get_ref()
Get a shared_ptr to this object.
Definition: input_file.hpp:158
void insert_object(const std::string &s, ref o)
insert an object as parameter
Definition: input_file.hpp:120
An abstract base class supporting xml_file_reading of input parameters.
Definition: input_file.hpp:79
Definition: input_file.hpp:287
Abstract observer helper class.
Definition: input_file.hpp:32
const char * class_Name() const
Calls virtual const char* name(), which must be redefined for derived classes.
Definition: input_file.hpp:97
size_t nparams() const
returns total number of parameters
Definition: input_file.hpp:203
bool is_initialized() const
returns whether the object has been correctly initialized
Definition: input_file.hpp:192
void link_object(const std::string &s, ref o)
insert a link to an object as parameter, the refered object must be defined elsewhere ...
Definition: input_file.hpp:125
loadable_Object()
Empty construction, objects are initialized by init routine.
Definition: input_file.hpp:93