Bayeux  3.4.1
Core Foundation library for SuperNEMO
multi_properties.h
Go to the documentation of this file.
1 /* multi_properties.h
3  * Author(s): Francois Mauger <mauger@lpccaen.in2p3.fr>
4  * Creation date: 2009
5  * Last modified: 2019-01-29
6  *
7  * License:
8  *
9  * Copyright (C) 2011-2019 Francois Mauger <mauger@lpccaen.in2p3.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  *
26  * Description:
27  *
28  * A container of properties objects implemented as a dictionnary
29  *
30  */
31 
32 #ifndef DATATOOLS_MULTI_PROPERTIES_H
33 #define DATATOOLS_MULTI_PROPERTIES_H
34 
35 // Standard Library:
36 #include <map>
37 #include <list>
38 #include <string>
39 
40 // Third Party:
41 // - Boost:
42 #include <boost/cstdint.hpp>
43 #include <boost/serialization/access.hpp>
44 
45 // This project:
47 #include <datatools/i_clear.h>
48 #include <datatools/i_tree_dump.h>
49 #include <datatools/i_cloneable.h>
50 #include <datatools/properties.h>
51 #include <datatools/bit_mask.h>
52 #include <datatools/logger.h>
53 
54 namespace datatools {
55 
101  , public datatools::i_clear
103  , public datatools::i_cloneable
104  {
105  public:
106 
108  struct defaults {
110  static const std::string & key_label();
112  static const std::string & meta_label();
113  };
114 
115  public:
117  class entry
119  {
120  public:
121 
123  entry(const std::string & key_ = "",
124  const std::string & meta_ = "");
125 
127  virtual ~entry();
128 
130  const properties& get_properties() const;
131 
134 
136  const std::string & get_key() const;
137 
139  void set_key(const std::string &);
140 
142  const std::string & get_meta() const;
143 
145  void set_meta(const std::string &);
146 
148  bool has_meta() const;
149 
151  void print_tree(std::ostream & out_ = std::clog,
152  const boost::property_tree::ptree & options_ = empty_options()) const override;
153 
157  virtual void tree_dump(std::ostream & out_ = std::clog,
158  const std::string & title_ = "",
159  const std::string & indent_ = "",
160  bool inherit_ = false) const;
161 
162  private:
163  std::string _key_;
164  std::string _meta_;
165  properties _properties_;
166 
168 
169  }; // multi_properties::entry
170 
171 
172  public:
173 
175  typedef std::map<std::string, entry> entries_col_type;
176 
178  typedef std::list<entry*> entries_ordered_col_type;
179 
180  private:
181 
183  void _init_ (const std::string & key_label_,
184  const std::string & meta_label_,
185  const std::string & description_,
186  bool debug_);
187 
189  void _copy_impl_(const multi_properties &);
190 
191  public:
192 
195 
197  multi_properties(const std::string & key_label_,
198  const std::string & meta_label_);
199 
201  multi_properties(const std::string & key_label_,
202  const std::string & meta_label_,
203  const std::string & description_,
204  bool debug_ = false);
205 
207  virtual ~multi_properties();
208 
211 
214 
216  bool is_debug() const;
217 
219  void set_debug(bool = true);
220 
222  void set_description(const std::string & description_);
223 
225  bool has_description() const;
226 
228  const std::string & get_description() const;
229 
231  bool has_key_label() const;
232 
234  void set_key_label(const std::string & key_label_);
235 
237  void clear_key_label();
238 
240  const std::string & get_key_label() const;
241 
243  bool has_meta_label() const;
244 
246  void set_meta_label(const std::string & meta_label_);
247 
249  void clear_meta_label();
250 
252  const std::string & get_meta_label() const;
253 
255  uint32_t size() const;
256 
258  bool empty() const;
259 
261  void reset();
262 
264  virtual void clear();
265 
267  const entries_col_type& entries() const;
268 
271 
273  const entry& get(const std::string & key_) const;
274 
276  entry& grab(const std::string & key_);
277 
279  bool has_key(const std::string & key_) const;
280 
282  bool has_key_with_meta(const std::string & key_, const std::string & meta_) const;
283 
285  const std::string & key(int) const;
286 
288  const std::string & ordered_key(int) const;
289 
291  std::vector<std::string> keys() const;
292 
294  void keys(std::vector<std::string> & keys_) const;
295 
297  std::vector<std::string> ordered_keys() const;
298 
300  void ordered_keys(std::vector<std::string> & keys_) const;
301 
303  bool has_section(const std::string & key_) const;
304 
306  const properties& get_section(const std::string & key_) const;
307 
309  const properties& get_section_const(const std::string & key_) const;
310 
312  properties& grab_section(const std::string & key_);
313 
315  void add(const std::string & key_,
316  const std::string & meta_,
317  const properties & props_);
318 
320  void add(const std::string & key_,
321  const properties& props_);
322 
324  void add(const std::string & key_,
325  const std::string & meta_ = "");
326 
328  properties& add_section(const std::string & key_,
329  const std::string & meta_ = "");
330 
332  void remove(const std::string & key_);
333 
335  void write(const std::string & filename_,
336  uint32_t options_ = config::HEADER_FOOTER | config::SKIP_PRIVATE_SECTIONS) const;
337 
339  void read(const std::string & filename_, uint32_t options_ = 0);
340 
342  void dump(std::ostream & out_ = std::clog) const;
343 
345  void print_tree(std::ostream & out_ = std::clog,
346  const boost::property_tree::ptree & options_ = empty_options()) const override;
347 
351  virtual void tree_dump(std::ostream & out_ = std::clog,
352  const std::string & title_ = "",
353  const std::string & indent_ = "",
354  bool inherit_ = false) const;
355 
357  class config
358  {
359  public:
373  };
374 
376  config(uint32_t options_ = 0, const std::string & topic_ = "");
377 
380 
383 
385  void reset();
386 
388  void read(const std::string & filename, multi_properties & target_);
389 
391  void read(std::istream & in_, multi_properties & target_);
392 
394  void write(std::ostream& out_, const multi_properties & source_);
395 
397  void write(const std::string & filename_, const multi_properties & source_);
398 
400  bool has_topic() const;
401 
403  void set_topic(const std::string & topic_);
404 
406  const std::string & get_topic() const;
407 
408  protected:
409 
411  void _init_defaults();
412 
414  void _read(std::istream & in_, multi_properties & target_);
415 
417  void _write(std::ostream & out_, const multi_properties & target_);
418 
420  void _set_current_filename(const std::string & filename_);
421 
422  private:
423 
424  // Configuration:
425  datatools::logger::priority _logging_;
426  bool _skip_private_sections_;
427  bool _skip_private_properties_;
428  bool _forbid_variants_;
429  bool _header_footer_;
430  bool _requested_topic_;
431  std::string _topic_;
432  bool _resolve_path_;
433  bool _start_without_labels_;
434  bool _without_decoration_;
435 
436  // Working parsing data:
437  std::string _current_filename_;
438  int _current_line_number_;
439 
440  }; //----- end of class config
441 
442  private:
443 
445  void remove_impl(const std::string & key_);
446 
448  void add_impl(const std::string & key_,
449  const std::string & meta_ = "");
450 
452  properties& add_impl2(const std::string & key_,
453  const std::string & meta_ = "");
454 
456  void init_defaults();
457 
458  private:
459 
460  bool _debug_;
461  std::string _description_;
462  std::string _key_label_;
463  std::string _meta_label_;
464  entries_col_type _entries_;
465  entries_ordered_col_type _ordered_entries_;
466 
469 
470 
473 
474 #ifndef Q_MOC_RUN
475  DR_CLASS_RTTI()
477 #endif // Q_MOC_RUN
478 
479  };
480 
481 } // end of namespace datatools
482 
483 // Support for serialization tag :
485 // Support for old serialization tag :
487 
488 #include <boost/serialization/export.hpp>
489 BOOST_CLASS_EXPORT_KEY2(datatools::multi_properties, "datatools::multi_properties")
490 
491 #ifndef Q_MOC_RUN
492 // Activate reflection layer for the 'datatools::multi_properties' class:
494 #endif // Q_MOC_RUN
495 
496 // Explicit class version:
497 #include <boost/serialization/version.hpp>
498 BOOST_CLASS_VERSION(datatools::multi_properties, 2)
499 
500 #endif // DATATOOLS_MULTI_PROPERTIES_H
501 
502 // Local Variables: --
503 // mode: c++ --
504 // c-file-style: "gnu" --
505 // tab-width: 2 --
506 // End: --
static const boost::property_tree::ptree & empty_options()
#define DR_CLASS_INIT(Introspectable)
Inform Camp that class Introspectable exists and trigger the automatic registration of dedicated refl...
Definition: reflection_interface.h:149
virtual ~multi_properties()
Destructor.
bool has_description() const
Check if a description is available.
#define DATATOOLS_CLONEABLE_DECLARATION(Copyable)
Definition: i_cloneable.h:113
static const std::string & key_label()
Default label for primary keys.
void set_logging(datatools::logger::priority)
Set the logging priority threshold.
void dump(std::ostream &out_=std::clog) const
Basic print.
Use header/footer (write)
Definition: multi_properties.h:367
bool empty() const
Check if the collection of entries is empty.
static const uint32_t bit08
Definition: bit_mask.h:35
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
bool has_key(const std::string &key_) const
Check if a section with a given key exists.
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
A container of mapped properties objects.
Definition: multi_properties.h:99
options_flag
Definition: multi_properties.h:360
void reset()
Reset the reader/writer.
Resolve path for input filename (read/write)
Definition: multi_properties.h:370
virtual void clear()
Clear the dictionary of sections.
const entries_col_type & entries() const
Return the const reference to the collection of entries.
bool has_key_with_meta(const std::string &key_, const std::string &meta_) const
Check if a section with given key and meta exists.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
bool has_meta() const
Check if meta information text is not empty.
Skip private sections bit.
Definition: multi_properties.h:361
static const uint32_t bit07
Definition: bit_mask.h:34
static const std::string & meta_label()
Default label for meta information text.
const std::string & get_meta() const
Return the meta information text.
Mute mode activation bit.
Definition: multi_properties.h:363
void add(const std::string &key_, const std::string &meta_, const properties &props_)
Add a new section with primary key, meta information text and a collection of properties.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
#define DATATOOLS_SERIALIZATION_DECLARATION_ADVANCED(ClassName)
Definition: i_serializable.h:371
static const uint32_t bit01
Definition: bit_mask.h:28
properties & grab_properties()
Return a mutable reference to the collection of properties.
void print_tree(std::ostream &out_=std::clog, const boost::property_tree::ptree &options_=empty_options()) const override
Smart print.
static const uint32_t bit09
Definition: bit_mask.h:36
static const uint32_t bit03
Definition: bit_mask.h:30
const std::string & get_description() const
Get the description.
multi_properties()
Default constructor.
properties & add_section(const std::string &key_, const std::string &meta_="")
Add a new section with primary key and meta information text and return a mutable reference to the st...
const entry & get(const std::string &key_) const
Return a const reference to the stored entry.
datatools::logger::priority get_logging() const
Return the logging priority threshold.
bool has_section(const std::string &key_) const
Check if a section exists.
void _set_current_filename(const std::string &filename_)
Store the current filename.
void _init_defaults()
Default initialization.
void set_meta_label(const std::string &meta_label_)
Set the meta label.
Section entry handle internal data stored within the dictionary of the multi_properties class.
Definition: multi_properties.h:117
void set_debug(bool=true)
Set the debug flag.
multi_properties & operator=(const multi_properties &)
Assignment operator.
#define DATATOOLS_SERIALIZATION_EXT_SERIAL_TAG_DECLARATION(ClassName)
Template support for serializable type (backward compatibility support)
Definition: i_serializable.h:101
void read(const std::string &filename, multi_properties &target_)
Read from an input file.
static const uint32_t bit04
Definition: bit_mask.h:31
const std::string & key(int) const
Returns the ith key.
static const uint32_t bit06
Definition: bit_mask.h:33
const std::string & get_topic() const
Return the topic.
bool has_topic() const
Check if topic is set.
Reader/writer class for multi_properties objects.
Definition: multi_properties.h:357
Requested topic (read/write)
Definition: multi_properties.h:369
static const uint32_t bit11
Definition: bit_mask.h:38
properties & grab_section(const std::string &key_)
Return the mutable reference to the properties store in a section.
entry(const std::string &key_="", const std::string &meta_="")
Constructor.
#define DATATOOLS_SERIALIZATION_EXT_BACKWARD_SERIAL_TAG_DECLARATION(ClassName)
Definition: i_serializable.h:180
Start without key/meta labels and let the reader set them.
Definition: multi_properties.h:371
Default values.
Definition: multi_properties.h:108
void set_key(const std::string &)
Set the primary key.
const properties & get_section_const(const std::string &key_) const
Return the const reference to the properties store in a section.
entry & grab(const std::string &key_)
Return a mutable reference to the stored entry.
Debug mode activation bit.
Definition: multi_properties.h:364
static const uint32_t bit10
Definition: bit_mask.h:37
std::list< entry * > entries_ordered_col_type
List of handles on sections.
Definition: multi_properties.h:178
static const uint32_t bit02
Definition: bit_mask.h:29
void print_tree(std::ostream &out_=std::clog, const boost::property_tree::ptree &options_=empty_options()) const override
Smart print.
A pure abstract class (interface) for inherited clearable classes.
Definition: i_clear.h:9
static const uint32_t bit05
Definition: bit_mask.h:32
Forbid variant block directives bit.
Definition: multi_properties.h:362
void set_key_label(const std::string &key_label_)
Set the key label.
Skip private properties in sections bit.
Definition: multi_properties.h:366
const std::string & ordered_key(int) const
Returns the ith ordered key.
#define DATATOOLS_SERIALIZATION_BACKWARD_SERIAL_TAG_SUPPORT()
Definition: i_serializable.h:174
std::map< std::string, entry > entries_col_type
Dictionary of section.
Definition: multi_properties.h:175
const properties & get_properties() const
Return a const reference to the collection of properties.
Trace mode activation bit.
Definition: multi_properties.h:365
void write(std::ostream &out_, const multi_properties &source_)
Write to an output stream.
void remove(const std::string &key_)
Remove a section.
void set_meta(const std::string &)
Set the meta information text.
A pure abstract class (interface) for inherited cloneable classes.
Definition: i_cloneable.h:75
uint32_t size() const
Return the number of entries.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
void set_description(const std::string &description_)
Set the description.
Utilities for logging information.
void _write(std::ostream &out_, const multi_properties &target_)
Write to an output stream.
const properties & get_section(const std::string &key_) const
Return the const reference to the properties store in a section.
const std::string & get_meta_label() const
Return the meta label.
std::vector< std::string > keys() const
Return an array of keys.
bool has_meta_label() const
Check if the meta label is set.
bool is_debug() const
Check the debug flag.
static const uint32_t bit00
Definition: bit_mask.h:27
config(uint32_t options_=0, const std::string &topic_="")
Default constructor.
const std::string & get_key() const
Return the primary key.
#define DR_CLASS_RTTI()
Declare Camp RTTI within class declaration.
Definition: reflection_interface.h:46
void set_topic(const std::string &topic_)
Set the topic that should be matched.
bool has_key_label() const
Check if the key label is set.
std::vector< std::string > ordered_keys() const
Return an array of orderered keys.
const std::string & get_key_label() const
Return the key label.
const entries_ordered_col_type & ordered_entries() const
Return the const reference to the ordered collection of entries.
void clear_key_label()
Clear the key label.
void read(const std::string &filename_, uint32_t options_=0)
Read from a configuration file.
Do not use decoration.
Definition: multi_properties.h:372
Don't clear before parsing bit (read)
Definition: multi_properties.h:368
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
void clear_meta_label()
Clear the meta label.
void _read(std::istream &in_, multi_properties &target_)
Read from an input stream.
#define BOOST_SERIALIZATION_BASIC_DECLARATION()
Definition: serialization_macros.h:62
void write(const std::string &filename_, uint32_t options_=config::HEADER_FOOTER|config::SKIP_PRIVATE_SECTIONS) const
Write to a configuration file.
A dictionary of arbitrary properties.
Definition: properties.h:125