Bayeux  3.4.1
Core Foundation library for SuperNEMO
parameter_store.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2015 by François Mauger <mauger@lpccaen.in2p3.fr>
5 //
6 // This file is part of Bayeux.
7 //
8 // Bayeux is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // Bayeux is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Bayeux. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef MYGSL_PARAMETER_STORE_H
22 #define MYGSL_PARAMETER_STORE_H
23 
24 // Standard library:
25 #include <string>
26 #include <map>
27 #include <vector>
28 #include <list>
29 
30 // Third party:
31 // - Boost :
32 #include <boost/variant.hpp>
33 // - Bayeux/datatools :
34 #include <datatools/i_tree_dump.h>
35 #include <datatools/types.h>
36 
37 namespace mygsl {
38 
41  {
42  public:
43 
44  static const int INVALID_INDEX = -1;
45 
47  typedef boost::variant<bool, int, double, std::string> param_type;
48 
51  public:
52 
55 
58 
61 
63  void set_index(int index_);
64 
66  int get_index() const;
67 
69  void set_name(const std::string & name_);
70 
72  const std::string & get_name() const;
73 
76 
79 
81  bool is_boolean() const;
82 
84  bool is_integer() const;
85 
87  bool is_real() const;
88 
90  bool is_string() const;
91 
93  void set_description(const std::string & description_);
94 
96  const std::string & get_description() const;
97 
99  void reset();
100 
102  bool is_value_set() const;
103 
104  void set_boolean_value(bool, bool update_ = true);
105 
106  void set_integer_value(int, bool update_ = true);
107 
108  void set_real_value(double, bool update_ = true);
109 
110  void set_string_value(const std::string &, bool update_ = true);
111 
112  void unset_value(bool update_ = true);
113 
114  bool to_boolean() const;
115 
116  int to_integer() const;
117 
118  double to_real() const;
119 
120  const std::string & to_string() const;
121 
122  private:
123 
124  parameter_store * _parent_;
125  std::string _name_;
126  datatools::basic_type _type_;
127  std::string _description_;
128  int _index_;
129  bool _value_set_;
130  param_type _value_;
131 
132  friend class parameter_store;
133 
134  };
135 
137  struct i_subscriber {
139  virtual void update_parameters() = 0;
140  };
141 
143  typedef std::map<std::string, parameter_record> parameter_dict_type;
144 
146  typedef std::vector<parameter_record *> indexed_parameter_col_type;
147 
149  parameter_store();
150 
152  virtual ~parameter_store();
153 
155  void reset();
156 
158  int add(const std::string & name_,
159  datatools::basic_type type_,
160  const std::string & description_);
161 
163  void remove(const std::string & name_);
164 
166  bool has(const std::string & name_) const;
167 
169  const parameter_record & get(const std::string & name_) const;
170 
172  parameter_record & grab(const std::string & name_);
173 
175  bool index_is_valid(int index_) const;
176 
178  const parameter_record & get(int index_, bool do_check_ = true) const;
179 
181  parameter_record & grab(int index_, bool do_check_ = true);
182 
184  int get_parameter_index(const std::string & parameter_name_) const;
185 
187  void update();
188 
190  virtual void tree_dump(std::ostream & out_ = std::clog,
191  const std::string & title_ = "",
192  const std::string & indent_ = "",
193  bool inherit_ = false) const;
194 
196  bool is_registered_subscriber(const i_subscriber & s_) const;
197 
200 
203 
204  protected:
205 
207  void _update_parameters();
208 
209  private:
210 
211  parameter_dict_type _parameters_;
212  indexed_parameter_col_type _indexed_parameters_;
213  std::list<i_subscriber*> _subscribers_;
214 
215  };
216 
217 } // end of namespace mygsl
218 
219 #endif // MYGSL_PARAMETER_STORE_H
220 
221 /* Local Variables: */
222 /* mode: c++ */
223 /* coding: utf-8 */
224 /* End: */
void set_real_value(double, bool update_=true)
parameter_store()
Default constructor.
const std::string & get_description() const
Return the parameter description.
bool has(const std::string &name_) const
Check if a parameter exists.
void register_subscriber(i_subscriber &)
Register a subscriber.
bool is_boolean() const
Check if the parameter is boolean.
int add(const std::string &name_, datatools::basic_type type_, const std::string &description_)
Add a parameter.
const std::string & to_string() const
parameter_record & grab(const std::string &name_)
Return a mutable parameter record by name.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
void unregister_subscriber(i_subscriber &)
Unregister a subscriber.
const parameter_record & get(const std::string &name_) const
Return a non mutable parameter record by name.
void reset()
Reset the store.
Subscriber.
Definition: parameter_store.h:137
virtual void update_parameters()=0
Action performed when parameters have changed.
void set_type(datatools::basic_type)
Set the parameter type.
void set_integer_value(int, bool update_=true)
Parameter record.
Definition: parameter_store.h:50
void set_name(const std::string &name_)
Set the parameter name.
basic_type
Basic type identifier.
Definition: types.h:28
void update()
Update.
std::map< std::string, parameter_record > parameter_dict_type
Type alias for a dictionary of parameter records.
Definition: parameter_store.h:143
static const int INVALID_INDEX
Definition: parameter_store.h:44
void set_string_value(const std::string &, bool update_=true)
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Main interface method for smart dump.
int get_parameter_index(const std::string &parameter_name_) const
Return the index associated to a parameter given its name.
bool is_registered_subscriber(const i_subscriber &s_) const
Check is a subscriber object is currently a subscriber of the store.
std::vector< parameter_record * > indexed_parameter_col_type
Type alias for an array of parameter record handles.
Definition: parameter_store.h:146
bool is_real() const
Check if the parameter is real.
const std::string & get_name() const
Return the parameter name.
int get_index() const
Return the parameter index.
void set_description(const std::string &description_)
Set the parameter description.
bool is_string() const
Check if the parameter is string.
boost::variant< bool, int, double, std::string > param_type
Type alias for parameter value.
Definition: parameter_store.h:47
Top-level namespace of the Bayeux/mygsl module library.
Definition: base_decay_driver.h:47
void set_boolean_value(bool, bool update_=true)
void set_parent(parameter_store &)
Set parent store.
bool index_is_valid(int index_) const
Check if a parameter idnex is valid.
virtual ~parameter_store()
Destructor.
void remove(const std::string &name_)
Remove parameter.
bool is_integer() const
Check if the parameter is integer.
bool is_value_set() const
Value is set.
void set_index(int index_)
Set the parameter index.
datatools::basic_type get_type() const
Return the parameter type.
Store of parameters.
Definition: parameter_store.h:40
void _update_parameters()
Update parameters action.