Bayeux  3.4.1
Core Foundation library for SuperNEMO
utils.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2014-05-11
4  * Last modified : 2014-08-10
5  *
6  * Copyright (C) 2014 Francois Mauger <mauger@lpccaen.in2p3.fr>
7  *
8  * This program 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 (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  * Description:
24  *
25  * Some utilities for the description of configuration variants and parameters.
26  *
27  */
28 
29 #ifndef DATATOOLS_CONFIGURATION_UTILS_H
30 #define DATATOOLS_CONFIGURATION_UTILS_H
31 
32 // Standard library:
33 #include <string>
34 #include <map>
35 
36 // Third party:
37 #include <boost/variant.hpp>
38 
39 // This project (Bayeux/datatools):
40 #include <datatools/handle.h>
41 #include <datatools/properties.h>
43 
44 namespace datatools {
45 
47  namespace configuration {
48 
49  // Forward declarations:
50  class parameter_model;
51  class variant_repository;
52  class variant_model;
53  class model_item;
54 
57 
60 
62  typedef std::map<std::string, model_item> model_item_dict_type;
63 
66  bool validate_item_name(const std::string &);
67 
69  bool validate_instance_name(const std::string &);
70 
72  bool validate_model_name(const std::string &);
73 
75  bool validate_group_name(const std::string &);
76 
78  // class item {
79  class model_item {
80  public:
81 
83  enum model_type {
87  };
88 
90  static std::string label_from_model_type(model_type);
91 
93  static model_type model_type_from_label(const std::string &);
94 
96  static const std::string & variant_suffix();
97 
99  static const std::string & parameter_suffix();
100 
102  model_item();
103 
105  virtual ~model_item();
106 
108  void reset();
109 
111  const std::string & get_name() const;
112 
114  void set_name(const std::string & name_);
115 
117  bool is_parameter() const;
118 
120  bool is_variant() const;
121 
123  bool has_model() const;
124 
126  void set_model(model_type);
127 
129  model_type get_model() const;
130 
132  const properties & get_config() const;
133 
136 
138  const parameter_model & get_parameter_model() const;
139 
142 
144  const variant_model & get_variant_model() const;
145 
148 
150  void set_parameter_handle(const pm_handle_type &);
151 
153  void set_variant_handle(const vm_handle_type &);
154 
156  bool is_instantiated() const;
157 
160 
162  void destroy();
163 
165  const pm_handle_type & get_parameter_handle() const;
166 
169 
171  const vm_handle_type & get_variant_handle() const;
172 
175 
177  parameter_model & make_parameter(const std::string & name_ = "");
178 
180  variant_model & make_variant(const std::string & name_ = "");
181 
183  virtual void tree_dump(std::ostream & out_ = std::clog,
184  const std::string & title_ = "",
185  const std::string & indent_ = "",
186  bool inherit_ = false) const;
187 
188  // Methods to manipulate dictionaries of configuration items:
189 
192  const std::string & name_);
193 
196  const std::string & name_);
197 
199  static bool has_item(const model_item_dict_type & dict_,
200  const std::string & name_,
202 
204  static const parameter_model & get_parameter_model(const model_item_dict_type & dict_,
205  const std::string & name_);
206 
208  static const variant_model & get_variant_model(const model_item_dict_type & dict_,
209  const std::string & name_);
210 
212  static void remove_item(model_item_dict_type & dict_,
213  const std::string & name_);
214 
216  static void print_items(const model_item_dict_type & dict_,
217  std::ostream & out_ = std::clog,
218  const std::string & title_ = "",
219  const std::string & indent_ = "",
220  bool inherit_ = false);
221 
222  private:
223 
224  std::string _name_;
225  properties _config_;
226  model_type _model_;
227  pm_handle_type _parameter_handle_;
228  vm_handle_type _variant_handle_;
229 
230  };
231 
233 
250  std::string registry_key;
251  std::string param_key;
252  std::string param_value_str;
254  void reset();
255  command::returned_info parse(const std::string & variant_set_);
256  };
257 
260  public:
263  bool operator()(const std::string & vs1_, const std::string & vs2_) const;
264  private:
265  variant_repository * _repository_ = nullptr;
266  };
267 
268  typedef boost::variant<bool, int, double, std::string> parameter_value_type;
269 
270  } // end of namespace configuration
271 
272 } // end of namespace datatools
273 
274 #endif // DATATOOLS_CONFIGURATION_UTILS_H
275 
276 // Local Variables: --
277 // mode: c++ --
278 // c-file-style: "gnu" --
279 // tab-width: 2 --
280 // End: --
static model_type model_type_from_label(const std::string &)
Return the item type associated to label.
Model of a configuration variant.
Definition: variant_model.h:59
std::string param_key
The full path of the variant parameter of which the value should be modify.
Definition: utils.h:251
const parameter_model & get_parameter_model() const
Return a non mutable reference to the embedded parameter item.
model_type get_model() const
Return the type of the item.
variant_model & grab_variant_model()
Return a mutable reference to the embedded variant item.
handle< parameter_model > pm_handle_type
Handle on parameter model.
Definition: utils.h:59
const vm_handle_type & get_variant_handle() const
Return a non mutable handle to the embedded variant.
std::string param_value_str
The string to be parsed into the new value of the variant parameter.
Definition: utils.h:252
static void print_items(const model_item_dict_type &dict_, std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false)
Smart print of a dictionary.
pm_handle_type & grab_parameter_handle()
Return a mutable handle to the embedded parameter.
bool validate_model_name(const std::string &)
Validate the name of a model.
static const std::string & parameter_suffix()
Return the conventional parameter suffix.
void destroy()
Destroy the embedded item.
std::string registry_key
The registration key of the variant registry the parameter belongs to.
Definition: utils.h:250
bool is_variant() const
Check if item is a variant.
const pm_handle_type & get_parameter_handle() const
Return a non mutable handle to the embedded parameter.
void set_parameter_handle(const pm_handle_type &)
Set the parameter item by handle.
variant_model & make_variant(const std::string &name_="")
Create a new embedded variant item.
parameter_model & grab_parameter_model()
Return a mutable reference to the embedded parameter item.
Parameter model.
Definition: utils.h:85
Command returned information.
Definition: command_utils.h:78
vm_handle_type & grab_variant_handle()
Return a mutable handle to the embedded variant.
static std::string label_from_model_type(model_type)
Return the label associated to a item type.
static variant_model & add_variant_item(model_item_dict_type &dict_, const std::string &name_)
Add a variant item in a dictionary.
static bool has_item(const model_item_dict_type &dict_, const std::string &name_, model_item::model_type t_=MODEL_UNDEFINED)
Check if item with given name exists in a dictionary.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
void set_name(const std::string &name_)
Set the name.
void set_variant_handle(const vm_handle_type &)
Set the variant item by handle.
std::map< std::string, model_item > model_item_dict_type
Dictionary of items.
Definition: utils.h:62
model_item()
Default constructor.
command::returned_info parse(const std::string &variant_set_)
bool is_instantiated() const
Check if the item is instantiated.
Undefined model type.
Definition: utils.h:84
boost::variant< bool, int, double, std::string > parameter_value_type
Definition: utils.h:268
Comparator functor used to sort variant parameters by registry and path.
Definition: utils.h:259
const std::string & get_name() const
Return the name.
bool operator()(const std::string &vs1_, const std::string &vs2_) const
void set_model(model_type)
Set the type of the item.
properties & grab_config()
Return a mutable reference to the list of configuration parameters.
static parameter_model & add_parameter_item(model_item_dict_type &dict_, const std::string &name_)
Add a parameter item in a dictionary.
bool validate_group_name(const std::string &)
Validate the name of a group.
static void remove_item(model_item_dict_type &dict_, const std::string &name_)
Remove an item from a dictionary.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
handle< variant_model > vm_handle_type
Handle on variant model.
Definition: utils.h:53
bool validate_item_name(const std::string &)
bool validate_instance_name(const std::string &)
Validate the name of an instance.
Configuration item with an embedded parameter or variant model handle through a smart pointer.
Definition: utils.h:79
const variant_model & get_variant_model() const
Return a non mutable reference to the embedded variant item.
Variant model.
Definition: utils.h:86
static const std::string & variant_suffix()
Return the conventional variant suffix.
Variant repository.
Definition: variant_repository.h:57
bool has_model() const
Check if model is defined.
A triplet of character strings representing a variant parameter set directive.
Definition: utils.h:249
Description/model of a configuration parameter.
Definition: parameter_model.h:60
const properties & get_config() const
Return a non mutable reference to the list of configuration parameters.
bool is_parameter() const
Check if item is a parameter.
void instantiate(model_item_dict_type &)
Instantiate the embedded item.
model_type
Type of item (parameter or variant)
Definition: utils.h:83
A dictionary of arbitrary properties.
Definition: properties.h:125
parameter_model & make_parameter(const std::string &name_="")
Create a new embedded parameter item.