Bayeux  3.4.1
Core Foundation library for SuperNEMO
factory.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2012-03-19
4  * Last modified : 2013-04-22
5  *
6  */
7 
8 #ifndef DATATOOLS_FACTORY_H
9 #define DATATOOLS_FACTORY_H
10 
11 // Standard Library:
12 #include <string>
13 #include <map>
14 #include <vector>
15 #include <iostream>
16 #include <sstream>
17 #include <stdexcept>
18 
19 // Third Party:
20 // - Boost:
21 #include <boost/function.hpp>
22 #include <boost/functional/factory.hpp>
23 #include <boost/scoped_ptr.hpp>
24 
25 // This project:
26 #include <datatools/i_tree_dump.h>
27 #include <datatools/exception.h>
28 #include <datatools/logger.h>
29 
30 namespace datatools {
34  {
35  public:
36  enum flag_type {
37  verbose = 0x1
38  };
39 
42 
44  virtual ~base_factory_register ();
45  };
46 
47 
50  template <class BaseType>
52  : public base_factory_register
54  {
55  public:
56 
57  typedef BaseType base_type;
58  typedef boost::function<base_type*() > factory_type;
59 
61  std::string type_id;
63  const std::type_info * tinfo = nullptr;
64  std::string description;
65  std::string category;
66  };
67 
69  typedef std::map<std::string, factory_record_type> factory_map_type;
70 
73 
75  factory_register(const std::string & label_, unsigned int flags_ = 0x0);
76 
78  virtual ~factory_register();
79 
82 
85 
87  const std::string & get_label() const;
88 
90  void set_label(const std::string & label_);
91 
93  void list_of_factories(std::vector<std::string> & ids_) const;
94 
96  bool has(const std::string & id_) const;
97 
99  bool is_group(const std::string & id_) const;
100 
102  void clear();
103 
105  void reset();
106 
108  factory_type & grab(const std::string & id_);
109 
111  const factory_type & get(const std::string & id_) const;
112 
114  const factory_record_type & get_record(const std::string & id_) const;
115 
117  void registration(const std::string & id_,
118  const factory_type & factory_,
119  const std::type_info & tinfo_,
120  const std::string & description_ = "",
121  const std::string & category_ = "");
122 
124  template<class DerivedType>
125  void registration(const std::string & id_,
126  const std::string & description_ = "",
127  const std::string & category_ = "");
128 
130  template<class DerivedType>
131  bool fetch_type_id(std::string & id_) const;
132 
134  bool fetch_type_id(const std::type_info & tinfo_, std::string & id_) const;
135 
140  void unregistration(const std::string & id_);
141 
143  void import(const factory_register & factory_register_);
144 
147  void import_some(const factory_register & factory_register_,
148  const std::vector<std::string> & imported_factories_);
149 
151  void print(std::ostream & out_, const std::string & indent_ = "") const;
152 
154  virtual void tree_dump(std::ostream & out_ = std::clog,
155  const std::string& title_ = "",
156  const std::string& indent_ = "",
157  bool inherit_ = false) const;
158 
159  private:
160 
161  std::string _label_;
162  datatools::logger::priority _logging_;
163  factory_map_type _registered_;
164 
165  };
166 
167 } // end of namespace datatools
168 
169 // Template definitions:
170 #include <datatools/factory-inl.h>
171 
172 // Boost:
173 #include <boost/type_traits/is_base_of.hpp>
174 
175 namespace datatools {
176 
179  template <class BaseType, class DerivedType>
181  {
182  public:
183 
184  // Constructor
185  _system_factory_registrator(const std::string & type_id_)
186  {
187  _type_info_ = &typeid(DerivedType);
188  _type_id_ = type_id_;
189  this->_trigger_factory_registration_();
190  return;
191  }
192 
193  // Destructor
195  {
196  this->_trigger_factory_unregistration_();
197  return;
198  }
199 
201  const std::string & get_type_id()const
202  {
203  return _type_id_;
204  }
205 
206  private:
207 
208  // Factory registration
209  void _trigger_factory_registration_()
210  {
211  bool fatal = !boost::is_base_of<BaseType, DerivedType>::value;
212  DT_THROW_IF(fatal,
213  std::logic_error,
214  "Class ID '" << _type_id_ << "' cannot be registered in register '"
215  << BaseType::grab_system_factory_register().get_label() << "' !");
216  BaseType::grab_system_factory_register().registration(_type_id_,
217  boost::factory<DerivedType*>(),
218  typeid(DerivedType));
219  return;
220  }
221 
222  // Factory unregistration
223  void _trigger_factory_unregistration_()
224  {
225  if (BaseType::grab_system_factory_register().has(_type_id_)) {
226  BaseType::grab_system_factory_register().unregistration(_type_id_);
227  }
228  return;
229  }
230 
231  private:
232 
233  const std::type_info * _type_info_ = nullptr;
234  std::string _type_id_;
235 
236 
237  };
238 
239 } // end of namespace datatools
240 
241 #endif // DATATOOLS_FACTORY_H
242 
243 // Local Variables: --
244 // mode: c++ --
245 // c-file-style: "gnu" --
246 // tab-width: 2 --
247 // End: --
datatools::logger::priority get_logging_priority() const
Returns logging priority.
Definition: factory-inl.h:49
BaseType base_type
Definition: factory.h:57
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
Definition: factory-inl.h:282
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
std::string type_id
Definition: factory.h:61
std::string category
Definition: factory.h:65
The base class for all specialized template factory registration classes.
Definition: factory.h:33
const factory_type & get(const std::string &id_) const
Return a const reference to a factory given by registration ID.
Definition: factory-inl.h:126
bool is_group(const std::string &id_) const
Return true if factory with given ID is registered with given group.
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
GENBB particle generator abstract base class.
Definition: i_genbb.h:59
std::string description
Definition: factory.h:64
void set_label(const std::string &label_)
Set the label associated to the factory.
Definition: factory-inl.h:68
Utility template class to enable auto-(un)registration of a derived class in a system factory registe...
Definition: factory.h:180
void set_logging_priority(datatools::logger::priority logging_)
Set logging priority.
Definition: factory-inl.h:55
bool has(const std::string &id_) const
Return true if factory with given ID is registered.
Definition: factory-inl.h:86
flag_type
Definition: factory.h:36
Utility macros for exception handling.
_system_factory_registrator(const std::string &type_id_)
Definition: factory.h:185
Template factory registration class.
Definition: factory.h:51
virtual ~base_factory_register()
Destructor.
base_factory_register()
Default constructor.
std::map< std::string, factory_record_type > factory_map_type
Dictionary of object factories.
Definition: factory.h:69
void list_of_factories(std::vector< std::string > &ids_) const
Copy factory IDs into supplied vector.
Definition: factory-inl.h:75
void registration(const std::string &id_, const factory_type &factory_, const std::type_info &tinfo_, const std::string &description_="", const std::string &category_="")
Register the supplied factory under the given ID.
Definition: factory-inl.h:197
~_system_factory_registrator()
Definition: factory.h:194
void print(std::ostream &out_, const std::string &indent_="") const
Simple print.
Definition: factory-inl.h:270
bool has(const service_dict_type &services_, const std::string &service_name_)
Definition: service_tools-inl.h:43
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
void clear()
Clear all registered factories.
Definition: factory-inl.h:92
void unregistration(const std::string &id_)
Definition: factory-inl.h:218
virtual ~factory_register()
Destructor.
Definition: factory-inl.h:40
factory_type fact
Definition: factory.h:62
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Utilities for logging information.
factory_type & grab(const std::string &id_)
Return a mutable reference to a factory given by registration ID.
Definition: factory-inl.h:116
void reset()
Reset.
Definition: factory-inl.h:106
const std::string & get_label() const
Get the label associated to the factory.
Definition: factory-inl.h:62
bool fetch_type_id(std::string &id_) const
Fetch the registration type ID associated to a given class.
Definition: factory-inl.h:161
void import_some(const factory_register &factory_register_, const std::vector< std::string > &imported_factories_)
Definition: factory-inl.h:247
boost::function< base_type *() > factory_type
Definition: factory.h:58
const std::string & get_type_id() const
Return registered type id.
Definition: factory.h:201
const std::type_info * tinfo
Definition: factory.h:63
factory_register()
Constructor.
Definition: factory-inl.h:16
const factory_record_type & get_record(const std::string &id_) const
Return a const reference to a factory record given by registration ID.
Definition: factory-inl.h:136