Bayeux  3.4.1
Core Foundation library for SuperNEMO
things.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2011-03-08
4  * Last modified : 2014-06-18
5  *
6  * Copyright (C) 2011-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  * A serializable container for arbitrary serializable objects
26  *
27  * Storable objects must :
28  * - have a default constructor
29  * - inherit from the datatools::i_serializable interface
30  * - have a 'serialize' template method (ala Boost)
31  *
32  * History:
33  *
34  */
35 #ifndef DATATOOLS_THINGS_H
36 #define DATATOOLS_THINGS_H
37 
38 // Standard Library:
39 #include <exception>
40 #include <iostream>
41 #include <map>
42 #include <sstream>
43 #include <stdexcept>
44 #include <string>
45 #include <typeinfo>
46 #include <vector>
47 
48 // Third Party:
49 // - Boost:
50 #include <boost/cstdint.hpp>
51 #include <boost/utility.hpp>
52 #include <boost/serialization/access.hpp>
53 
54 // This Project:
57 #include <datatools/i_clear.h>
58 #include <datatools/i_tree_dump.h>
59 #include <datatools/bit_mask.h>
60 
61 namespace datatools {
62 
63  // Forward declaration:
64  class things;
65 
68  : public std::exception
69  {
70  public:
71 
72  explicit bad_things_cast (const std::string & msg_);
73 
74  virtual ~bad_things_cast() throw();
75 
76  virtual const char* what() const throw();
77 
78  private:
79 
80  std::string _message_;
81 
82  };
83 
85  class things
88  , public datatools::i_clear
89  , private boost::noncopyable
90  {
91  public:
92 
94  struct entry_type
96  {
97  static const char MASK_CONST = bit_mask::bit00;
98 
99  public:
100 
101  entry_type();
102 
103  virtual ~entry_type();
104 
105  bool is_not_const() const;
106 
107  bool is_const() const;
108 
109  void set_const(bool const_ = true);
110 
111  void set_description(const std::string &);
112 
113  const std::string & get_description() const;
114 
115  bool has_description() const;
116 
117  virtual void tree_dump(std::ostream & out_ = std::clog,
118  const std::string & title_ = "",
119  const std::string & indent_ = "",
120  bool inherit_ = false) const;
121 
122  public:
123 
124  std::string description;
125  uint8_t flags;
127 
129 
130  };
131 
132  // Begin the things declarations...
133  public:
134 
135  static const bool constant; // = true;
136  static const bool non_constant; // = !constant;
137  static const bool copyable; // = true;
138  static const bool noncopyable; // = !copyable;
139 
141  typedef std::map<std::string, entry_type> dict_type;
142 
144  things();
145 
147  things(const std::string & name_, const std::string & description_ = "");
148 
149  // Destructor
150  virtual ~things();
151 
153  const std::string & get_name() const;
154 
156  things & set_name(const std::string &);
157 
159  const std::string & get_name_impl() const;
160 
162  void set_name_impl(const std::string &);
163 
165  bool has_name() const;
166 
168  bool has_name_impl() const;
169 
171  const std::string & get_description() const;
172 
174  things & set_description(const std::string &);
175 
177  void set_description_impl(const std::string &);
178 
180  const std::string & get_description_impl() const;
181 
183  bool has_description_impl() const;
184 
186  void reset();
187 
189  virtual void clear();
190 
192  unsigned int size() const;
193 
195  bool empty() const;
196 
198  bool has(const std::string & name_) const;
199 
201  bool has_serial_tag(const std::string & name_,
202  const std::string & serial_tag_) const;
203 
205  bool is_constant(const std::string & name_) const;
206 
208  bool is_mutable(const std::string & name_) const;
209 
211  void set_constant(const std::string & name_, bool const_ = true);
212 
214  const std::string & get_description(const std::string & name_) const;
215 
217  const std::string & get_entry_description(const std::string & name_) const;
218 
220  void set_description(const std::string & name_, const std::string & desc_);
221 
223  void set_entry_description(const std::string & name_, const std::string & desc_);
224 
226  void get_names(std::vector<std::string> & names_) const;
227 
229  const std::string & get_entry_name(int index_) const;
230 
232  void remove(const std::string & name_);
233 
235  void erase(const std::string & name_);
236 
238  template<class T>
239  T& add(const std::string & name_, const std::string & desc_ = "",
240  bool const_ = false);
241 
244  template<class T>
245  T* pop(const std::string & name_);
246 
248  template<class T>
249  T& grab(const std::string & name_);
250 
252  template<class T>
253  bool is_a(const std::string & name_) const;
254 
256  template<class T>
257  const T& get(const std::string & name_) const;
258 
261  get_entry(const std::string & name_) const;
262 
265  grab_entry(const std::string & name_);
266 
269  add_entry(const std::string & name_,
270  const std::string & serial_tag_,
271  const std::string & description_ = "",
272  bool const_ = false);
273 
276  add_entry_impl(const std::string & name_,
277  const std::string & serial_tag_,
278  const std::string & description_,
279  bool const_);
280 
282  bool entry_is_a(const std::string & name_, const std::string &) const;
283 
285  bool entry_is_introspectable(const std::string & name_) const;
286 
288  std::string get_entry_serial_tag(const std::string & name_) const;
289 
291  std::string get_entry_introspection_id(const std::string & name_) const;
292 
294  virtual void tree_dump(std::ostream & out_ = std::clog,
295  const std::string & title_ = "",
296  const std::string & indent_ = "",
297  bool inherit_ = false) const;
298 
300  void dump(std::ostream & out_ = std::clog) const;
301 
304  grab(const std::string & name_, const std::string & serial_tag_ = "");
305 
308  get(const std::string & name_, const std::string & serial_tag_ = "") const;
309 
310  private:
311 
313  void add_impl(const std::string & name_,
315  const std::string & desc_ = "",
316  bool const_ = false);
317 
318  private:
319 
320  // Attributes :
321  std::string _name_;
322  std::string _description_;
323  dict_type _things_;
324 
328 
329 
330  DR_CLASS_RTTI()
331 
332  };
333 
334 } // end of namespace datatools
335 
336 // Template methods
337 #include <datatools/things-inl.h>
338 
339 // Support for serialization tag :
341 // Support for old serialization tag :
343 
344 #ifdef __clang__
345 #pragma clang diagnostic push
346 #pragma clang diagnostic ignored "-Wunused-local-typedef"
347 #endif
348 #include <boost/serialization/export.hpp>
349 #ifdef __clang__
350 #pragma clang diagnostic pop
351 #endif
352 
353 BOOST_CLASS_EXPORT_KEY2(datatools::things,"datatools::things")
354 
355 // Activate reflection layer for the 'datatools::things' class:
357 
358 // Explicit class version:
359 #include <boost/serialization/version.hpp>
360 BOOST_CLASS_VERSION(datatools::things, 1)
361 
362 #endif // DATATOOLS_THINGS_H
363 
364 // Local Variables: --
365 // mode: c++ --
366 // c-file-style: "gnu" --
367 // tab-width: 2 --
368 // End: --
A generic serializable and noncopyable container for arbitrary serializable objects.
Definition: things.h:85
T & grab(const std::string &name_)
Return a reference to a mutable object of given type and name.
Definition: things-inl.h:67
const std::string & get_entry_description(const std::string &name_) const
Return the description of the object stored with a given name.
Base abstract class of all serializable (and possibly introspectable) classes.
Definition: i_serializable.h:51
bad_things_cast(const std::string &msg_)
bool has_name() const
Check if the container has a name.
bool is_a(const std::string &name_) const
Check if a stored object with given name is of given type.
Definition: things-inl.h:90
std::string description
Definition: things.h:124
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
#define DATATOOLS_SERIALIZATION_DECLARATION_ADVANCED(ClassName)
Definition: i_serializable.h:371
void set_description(const std::string &)
static const bool constant
Definition: things.h:135
std::map< std::string, entry_type > dict_type
Embedded dictionary of arbitrary serializable objects.
Definition: things.h:141
bool has_serial_tag(const std::string &name_, const std::string &serial_tag_) const
Check if the container stores an object with a given name and a given serial tag.
void remove(const std::string &name_)
Remove the object stored in the container with a given name.
#define DR_CLASS_NONCOPYABLE_INIT(Introspectable)
Inform Camp that non copyable class Introspectable exists and trigger the automatic registration of d...
Definition: reflection_interface.h:158
virtual void clear()
Clear the container.
Internal entry for serializable object stored in the thing class.
Definition: things.h:94
const std::string & get_entry_name(int index_) const
Return the name of the object stored at a given index.
#define DATATOOLS_SERIALIZATION_EXT_SERIAL_TAG_DECLARATION(ClassName)
Template support for serializable type (backward compatibility support)
Definition: i_serializable.h:101
bool entry_is_introspectable(const std::string &name_) const
Check if a stored object with given name is introspectable through CAMP interface.
unsigned int size() const
Return the number of objects stored in the container.
const T & get(const std::string &name_) const
Return a reference to a non mutable object of given type and name.
Definition: things-inl.h:103
uint8_t flags
Definition: things.h:125
void dump(std::ostream &out_=std::clog) const
Basic print.
void erase(const std::string &name_)
Remove the object stored in the container with a given name.
void get_names(std::vector< std::string > &names_) const
Build the list of names of all objects stored in the container.
datatools::i_serializable & add_entry_impl(const std::string &name_, const std::string &serial_tag_, const std::string &description_, bool const_)
Instantiate and add an object of given type id with given name, description and constness flag (imple...
bool has(const std::string &name_) const
Check if the container stores an object with a given name.
#define DATATOOLS_SERIALIZATION_EXT_BACKWARD_SERIAL_TAG_DECLARATION(ClassName)
Definition: i_serializable.h:180
virtual const char * what() const
std::string get_entry_introspection_id(const std::string &name_) const
Return the introspection id of a stored object with given name.
std::string get_entry_serial_tag(const std::string &name_) const
Return the serial tag of a stored object with given name.
An exception for invalid cast operation within the things class.
Definition: things.h:67
const datatools::i_serializable & get_entry(const std::string &name_) const
Return a reference to a non mutable stored object of given name.
static const bool noncopyable
Definition: things.h:138
static const char MASK_CONST
Definition: things.h:97
void set_name_impl(const std::string &)
Set the name of the container (low-level implementation)
things()
Default constructor.
const std::string & get_description() const
Return the description of the container.
things & set_name(const std::string &)
Set the name of the container.
A pure abstract class (interface) for inherited clearable classes.
Definition: i_clear.h:9
static const bool copyable
Definition: things.h:137
void set_constant(const std::string &name_, bool const_=true)
not implemented :
bool has_description_impl() const
Check if the container has a description (low-level implementation)
const std::string & get_name_impl() const
Return the name of the container (low-level implementation)
#define DATATOOLS_SERIALIZATION_BACKWARD_SERIAL_TAG_SUPPORT()
Definition: i_serializable.h:174
const std::string & get_description_impl() const
Return the description of the container (low-level implementation)
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
bool empty() const
Check if the container is empty.
T * pop(const std::string &name_)
Definition: things-inl.h:44
datatools::i_serializable & add_entry(const std::string &name_, const std::string &serial_tag_, const std::string &description_="", bool const_=false)
Instantiate and add an object of given type id with given name, description and constness flag.
const std::string & get_name() const
Return the name of the container.
T & add(const std::string &name_, const std::string &desc_="", bool const_=false)
Instantiate and add an object of given type and name.
Definition: things-inl.h:15
datatools::i_serializable & grab_entry(const std::string &name_)
Return a reference to a mutable stored object of given name.
void set_entry_description(const std::string &name_, const std::string &desc_)
Set the description of the object stored with a given name.
void set_description_impl(const std::string &)
Set the description of the container (low-level implementation)
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
datatools::i_serializable * handle
Definition: things.h:126
const std::string & get_description() const
static const uint32_t bit00
Definition: bit_mask.h:27
bool is_mutable(const std::string &name_) const
Check if the object stored is the container with a given name is mutable.
#define DR_CLASS_RTTI()
Declare Camp RTTI within class declaration.
Definition: reflection_interface.h:46
void set_const(bool const_=true)
bool entry_is_a(const std::string &name_, const std::string &) const
Check if a stored object with given name has a type with given type id.
bool has_name_impl() const
Check if the container has a name (low-level implementation)
things & set_description(const std::string &)
Set the description of the container.
static const bool non_constant
Definition: things.h:136
void reset()
Reset the container.
#define BOOST_SERIALIZATION_BASIC_DECLARATION()
Definition: serialization_macros.h:62
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Main old interface method for printing.
bool is_constant(const std::string &name_) const
Check if the object stored is the container with a given name is constant.