Bayeux  3.4.1
Core Foundation library for SuperNEMO
factory-inl.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_INL_H
9 #define DATATOOLS_FACTORY_INL_H
10 
11 // class factory_register implementation section
12 namespace datatools {
13 
14  // Constructor:
15  template <typename BaseType>
17  : _label_()
18  , _logging_(datatools::logger::PRIO_FATAL)
19  , _registered_()
20  {
21  return;
22  }
23 
24  // Constructor:
25  template <typename BaseType>
27  unsigned int flags_)
28  : _label_(label_),
29  _logging_(datatools::logger::PRIO_FATAL) {
30  if (flags_ & verbose) {
31  set_logging_priority(datatools::logger::PRIO_INFORMATION);
32  }
33  DT_LOG_INFORMATION(_logging_,"Construction (label='" << _label_ << "'...");
34  DT_LOG_INFORMATION(_logging_,"Construction is done (label='" << _label_ << "'...");
35  return;
36  }
37 
38  // Destructor:
39  template <typename BaseType>
41  {
42  DT_LOG_INFORMATION(_logging_,"Destruction (label='" << _label_ << "'...");
43  this->clear();
44  DT_LOG_INFORMATION(_logging_,"Destruction is done (label='" << _label_ << "'...");
45  return;
46  }
47 
48  template <typename BaseType>
50  {
51  return _logging_;
52  }
53 
54  template <typename BaseType>
56  {
57  _logging_ = logging_;
58  return;
59  }
60 
61  template <typename BaseType>
62  const std::string & factory_register<BaseType>::get_label() const
63  {
64  return _label_;
65  }
66 
67  template <typename BaseType>
68  void factory_register<BaseType>::set_label(const std::string & label_)
69  {
70  _label_ = label_;
71  return;
72  }
73 
74  template <typename BaseType>
75  void factory_register<BaseType>::list_of_factories(std::vector<std::string> & ids_) const
76  {
77  for (typename factory_map_type::const_iterator i = _registered_.begin();
78  i != _registered_.end();
79  ++i) {
80  ids_.push_back(i->first);
81  }
82  return;
83  }
84 
85  template <typename BaseType>
86  bool factory_register<BaseType>::has(const std::string & id_) const
87  {
88  return _registered_.find(id_) != _registered_.end();
89  }
90 
91  template <typename BaseType>
93  {
94  for (typename factory_map_type::iterator i = _registered_.begin();
95  i != _registered_.end();
96  ++i) {
97  if (i->second.fact != nullptr) {
98  DT_LOG_INFORMATION(_logging_,"Destroying registered allocator/functor '" << i->first << "'");
99  }
100  }
101  _registered_.clear();
102  return;
103  }
104 
105  template <typename BaseType>
107  {
108  this->clear();
109  _label_.clear();
110  _logging_ = datatools::logger::PRIO_FATAL;
111  return;
112  }
113 
114  template <typename BaseType>
116  factory_register<BaseType>::grab(const std::string & id_)
117  {
118  typename factory_map_type::iterator found = _registered_.find(id_);
119  DT_THROW_IF(found == _registered_.end(), std::logic_error,
120  "Class ID '" << id_ << "' is not registered !");
121  return found->second.fact;
122  }
123 
124  template <typename BaseType>
126  factory_register<BaseType>::get(const std::string & id_) const
127  {
128  typename factory_map_type::const_iterator found = _registered_.find(id_);
129  DT_THROW_IF(found == _registered_.end(), std::logic_error,
130  "Class ID '" << id_ << "' is not registered !");
131  return found->second.fact;
132  }
133 
134  template <typename BaseType>
136  factory_register<BaseType>::get_record(const std::string & id_) const
137  {
138  typename factory_map_type::const_iterator found = _registered_.find(id_);
139  DT_THROW_IF(found == _registered_.end(), std::logic_error,
140  "Class ID '" << id_ << "' is not registered !");
141  return found->second;
142  }
143 
144  template <typename BaseType>
145  bool factory_register<BaseType>::fetch_type_id(const std::type_info & tinfo_, std::string & id_) const
146  {
147  id_.clear();
148  for (typename factory_map_type::const_iterator i = _registered_.begin();
149  i != _registered_.end();
150  ++i) {
151  if (&tinfo_ == i->second.tinfo) {
152  id_ = i->first;
153  return true;
154  }
155  }
156  return false;
157  }
158 
159  template <typename BaseType>
160  template<class DerivedType>
161  bool factory_register<BaseType>::fetch_type_id(std::string & id_) const
162  {
163  id_.clear();
164  bool fatal = !boost::is_base_of<BaseType, DerivedType>::value;
165  DT_THROW_IF(fatal,
166  std::logic_error,
167  "Class ID '" << id_ << "' is not registrable!");
168  // if (fatal) {
169  // return false;
170  // }
171  for (typename factory_map_type::const_iterator i = _registered_.begin();
172  i != _registered_.end();
173  ++i) {
174  if (&typeid(DerivedType) == i->second.tinfo) {
175  id_ = i->first;
176  return true;
177  }
178  }
179  return false;
180  }
181 
182  template <typename BaseType>
183  template <typename DerivedType>
184  void factory_register<BaseType>::registration(const std::string & id_,
185  const std::string & description_,
186  const std::string & category_)
187  {
188  registration(id_,
189  boost::factory<DerivedType*>(),
190  typeid(DerivedType),
191  description_,
192  category_);
193  return;
194  }
195 
196  template <typename BaseType>
197  void factory_register<BaseType>::registration(const std::string & id_,
198  const factory_type & factory_,
199  const std::type_info & tinfo_,
200  const std::string & description_,
201  const std::string & category_)
202  {
203  DT_LOG_NOTICE(_logging_, "Registration of class with ID '" << id_ << "'");
204  typename factory_map_type::const_iterator found = _registered_.find(id_);
205  DT_THROW_IF(found != _registered_.end(), std::logic_error,
206  "Class ID '" << id_ << "' is already registered !");
207  factory_record_type record;
208  record.type_id = id_;
209  record.description = description_;
210  record.category = category_;
211  record.fact = factory_;
212  record.tinfo = &tinfo_;
213  _registered_[id_] = record;
214  return;
215  }
216 
217  template <typename BaseType>
218  void factory_register<BaseType>::unregistration(const std::string & id_)
219  {
220  DT_LOG_NOTICE(_logging_, "Unregistration of class with ID '" << id_ << "'");
221  typename factory_map_type::const_iterator found = _registered_.find(id_);
222  DT_THROW_IF(found == _registered_.end(), std::logic_error,
223  "Class ID '" << id_ << "' is not registered !");
224  _registered_.erase(id_);
225  return;
226  }
227 
228  template <typename BaseType>
230  {
231  DT_LOG_NOTICE(_logging_, "Importing registered factories from register '" << other_.get_label() << "'...");
232  for (typename factory_map_type::const_iterator i = other_._registered_.begin();
233  i != other_._registered_.end();
234  ++i) {
235  const factory_record_type & the_out_factory_record = i->second;
236  this->registration(i->first,
237  the_out_factory_record.fact,
238  *the_out_factory_record.tinfo,
239  the_out_factory_record.description,
240  the_out_factory_record.category);
241  }
242  DT_LOG_NOTICE(_logging_, "Done.");
243  return;
244  }
245 
246  template <typename BaseType>
248  const std::vector<std::string> & imported_factories_)
249  {
250  DT_LOG_NOTICE(_logging_, "Importing registered factories from register '" << other_.get_label() << "'...");
251  for (typename factory_map_type::const_iterator i = other_._registered_.begin();
252  i != other_._registered_.end();
253  ++i) {
254  if (std::find(imported_factories_.begin(),
255  imported_factories_.end(),
256  i->first) != imported_factories_.end()) {
257  const factory_record_type & the_out_factory_record = i->second;
258  this->registration(i->first,
259  the_out_factory_record.fact,
260  *the_out_factory_record.tinfo,
261  the_out_factory_record.description,
262  the_out_factory_record.category);
263  }
264  }
265  DT_LOG_NOTICE(_logging_, "Done.");
266  return;
267  }
268 
269  template <typename BaseType>
270  void factory_register<BaseType>::print(std::ostream & out_,
271  const std::string & indent_) const
272  {
273  std::ostringstream title_oss;
274  title_oss << "List of registered allocators/functors for label \""
275  << _label_
276  << "\" : ";
277  this->tree_dump(out_, title_oss.str(), indent_);
278  return;
279  }
280 
281  template <typename BaseType>
282  void factory_register<BaseType>::tree_dump(std::ostream & out_,
283  const std::string & title_,
284  const std::string & indent_,
285  bool inherit_) const
286  {
287  if (!title_.empty()) { out_ << indent_ << title_ << std::endl; }
288 
289  out_ << indent_ << i_tree_dumpable::tag
290  << "Label : '"
291  << _label_ << "'" << std::endl;
292 
293  out_ << indent_ << i_tree_dumpable::tag
294  << "Logging threshold : "
295  << datatools::logger::get_priority_label(_logging_) << std::endl;
296 
297  out_ << indent_ << i_tree_dumpable::inherit_tag(inherit_)
298  << "Registered factories : " << _registered_.size() << std::endl;
299 
300  for (typename factory_map_type::const_iterator i = _registered_.begin();
301  i != _registered_.end();
302  ++i) {
303  out_ << indent_;
304  out_ << i_tree_dumpable::inherit_skip_tag(inherit_);
305  typename factory_map_type::const_iterator j = i;
306  j++;
307  if (j == _registered_.end()) {
308  out_ << i_tree_dumpable::last_tag;
309  } else {
310  out_ << i_tree_dumpable::tag;
311  }
312  out_ << "ID: \"" << i->first << "\" @ " << &i->second.fact;
313  if (!i->second.description.empty()) {
314  out_ << ": " << i->second.description;
315  }
316  if (!i->second.category.empty()) {
317  out_ << " (" << i->second.category << ')';
318  }
319  out_ << std::endl;
320  }
321  return;
322  }
323 
324 } // namespace datatools
325 
326 #endif // DATATOOLS_FACTORY_INL_H
327 
328 // Local Variables: --
329 // mode: c++ --
330 // c-file-style: "gnu" --
331 // tab-width: 2 --
332 // End: --
Output stream manipulator.
Definition: i_tree_dump.h:140
Output stream manipulator.
Definition: i_tree_dump.h:124
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
#define DT_LOG_INFORMATION(Priority, Message)
Definition: logger_macros.h:136
An informational message, usually denoting the successful completion of an operation.
Definition: logger.h:90
A fatal error. The application will most likely terminate. This is the highest priority.
Definition: logger.h:85
const T & get(const service_dict_type &services_, const std::string &service_name_)
Definition: service_tools-inl.h:75
void reset(pg_entry_type &entry_)
Template factory registration class.
Definition: factory.h:51
Organise logging functionality under one roof.
Definition: logger.h:79
#define DT_LOG_NOTICE(Priority, Message)
Definition: logger_macros.h:118
void tree_dump(const rotation_3d &rot_, std::ostream &out_, const std::string &title_="", const std::string &indent_="")
Smart print for rotation 3D object.
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 print(const dependency_logic_ast_node &node_, std::ostream &out_, int indent_=0)
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
const std::string & get_label() const
Get the label associated to the factory.
Definition: factory-inl.h:62
boost::function< base_type *() > factory_type
Definition: factory.h:58
T & grab(service_dict_type &services_, const std::string &service_name_)
Definition: service_tools-inl.h:60
factory_register()
Constructor.
Definition: factory-inl.h:16
static std::string get_priority_label(priority p_)