Bayeux  3.4.1
Core Foundation library for SuperNEMO
i_cut.h
Go to the documentation of this file.
1 /* Author(s) : Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date : 2011-06-07
4  * Last modified : 2015-11-09
5  *
6  * Copyright (C) 2011-2015 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  * Base cut.
26  *
27  * History:
28  *
29  */
30 
31 #ifndef CUTS_I_CUT_H
32 #define CUTS_I_CUT_H 1
33 
34 // Standard library:
35 #include <ostream>
36 #include <string>
37 #include <typeinfo>
38 #include <set>
39 
40 // Third party:
41 // - Boost:
42 #include <boost/cstdlib.hpp>
43 #include <boost/shared_ptr.hpp>
44 // - Bayeux/datatools:
45 #include <datatools/i_tree_dump.h>
47 #include <datatools/bit_mask.h>
48 #include <datatools/logger.h>
49 
50 // This project:
51 #include <cuts/cut_tools.h>
52 
53 namespace datatools {
54  class properties;
55  class service_manager;
56 }
57 
59 namespace cuts {
60 
63  {
64  public:
65 
68  virtual operator bool() const = 0;
69  virtual const std::type_info * get_typeinfo() const = 0;
70  virtual bool match(const std::type_info * tit_) const = 0;
71  virtual ~i_referenced_data() {}
72  };
73 
75  template<class T>
77  public:
78  const T & get() const {
79  DT_THROW_IF(_address == 0,
80  std::logic_error,
81  "No referenced data !"
82  );
83  return *_address;
84  }
85  virtual const std::type_info * get_typeinfo() const {
86  return _ti;
87  }
88  virtual bool match(const std::type_info * tit_) const {
89  return tit_ == _ti;
90  }
91  void set(const T & obj) {
92  _address = &obj;
93  _ti = &typeid(T);
94  }
95  virtual operator bool() const {
96  return _address != 0 && _ti != 0;
97  }
98  void reset() {
99  _address = 0;
100  _ti = 0;
101  }
103  _address = 0;
104  _ti = 0;
105  }
106  referenced_data(const T & obj) {
107  set(obj);
108  }
109  virtual ~referenced_data() {
110  _address = 0;
111  _ti = 0;
112  }
113  protected:
114  const T * _address;
115  const std::type_info * _ti;
116  };
117 
119  bool is_debug() const;
120 
122  bool has_name() const;
123 
125  void set_name(const std::string & a_name);
126 
128  const std::string & get_name() const;
129 
131  bool has_description() const;
132 
134  const std::string & get_description() const;
135 
137  void set_description(const std::string & a_description);
138 
140  bool has_version() const;
141 
143  const std::string & get_version() const;
144 
146  void set_version(const std::string & a_version);
147 
149  bool is_initialized() const;
150 
152  bool has_user_data() const;
153 
155  template<class T>
156  void set_user_data(const T & obj_)
157  {
159  "Cut '" << (has_name() ? get_name() : "?") << "' : "
160  << "adding user data of type '" << typeid(T).name() << "'...");
161  // DT_THROW_IF(!is_user_data_type_supported<T>(),
162  // std::logic_error,
163  // "User data type '" << typeid(T).name() << "' is not supported by cut '" << get_name() << "'!");
164  boost::shared_ptr<i_referenced_data> ud(new referenced_data<T>(obj_));
165  _set_user_data(ud);
166  }
167 
169  template<class T>
170  bool is_user_data() const
171  {
172  DT_THROW_IF(! _user_data_,
173  std::logic_error,
174  "Cut '" << (has_name()?get_name():"?") << "' does not reference any user data of type '" << typeid(T).name() << "' !");
175  const std::type_info & ti = typeid(T);
176  return _user_data_.get()->match(&ti);
177  }
178 
180  template<class T>
181  const T & get_user_data() const
182  {
183  DT_THROW_IF(! is_user_data<T>(),
184  std::logic_error,
185  "Invalid request on user data for cut named '" << (has_name()?get_name():"?") << "' !");
186  const referenced_data<T> * rd = dynamic_cast<const referenced_data<T> *>(_user_data_.get());
187  DT_THROW_IF(rd == 0,
188  std::logic_error,
189  "Invalid cast for reference data in cut named '" << (has_name()?get_name():"?") << "' !");
190  return rd->get();
191  }
192 
194  template<class T>
196  {
197  const std::type_info & ti = typeid(T);
198  DT_LOG_DEBUG(_logging, "Registering supported type '" << ti.name() << "' !!!");
199  _supported_types_.insert(&ti);
200  return;
201  }
202 
204  template<class T>
206  {
207  const std::type_info & ti = typeid(T);
208  DT_LOG_DEBUG(_logging, "Unregistering supported type '" << ti.name() << "' !!!");
209  _supported_types_.erase(&ti);
210  return;
211  }
212 
214  template<class T>
216  {
217  if (_supported_types_.size() == 0) {
218  DT_LOG_TRACE(_logging, "Supported type");
219  return true;
220  }
221  DT_LOG_TRACE(_logging, "Checking supported type...");
222  const std::type_info & ti = typeid(T);
223  if (_supported_types_.count(&ti) == 1) {
224  DT_LOG_TRACE(_logging, "Supported registered type.");
225  return true;
226  }
227  return false;
228  }
229 
231  bool is_user_data_type_supported(const std::type_info & tinfo_) const;
232 
234  void reset_user_data();
235 
237  virtual void initialize_simple();
238 
240  virtual void initialize_standalone(const datatools::properties & a_config);
241 
244  virtual void initialize_with_service_only(const datatools::properties & a_config,
245  datatools::service_manager & a_service_manager);
246 
249  virtual void initialize_without_service(const datatools::properties & a_config,
250  cut_handle_dict_type & a_cut_dictionary);
251 
259  virtual void initialize(const datatools::properties & a_config,
260  datatools::service_manager & a_service_manager,
261  cut_handle_dict_type & a_cut_dictionary) = 0;
262 
274  };
275 
277  virtual void export_to_config(datatools::properties & config_,
278  uint32_t flags_ = EXPORT_CONFIG_DEFAULT,
279  const std::string & prefix_ = "") const;
280 
284  virtual int process();
285 
287  int operator()();
288 
290  virtual void reset() = 0;
291 
294 
296  virtual ~i_cut();
297 
299  virtual void tree_dump(std::ostream & a_out = std::clog,
300  const std::string & a_title = "",
301  const std::string & a_indent = "",
302  bool a_inherit = false) const;
303 
305  void print(std::ostream & a_out = std::clog) const;
306 
309 
312 
315 
317  size_t get_number_of_accepted_entries() const;
318 
320  size_t get_number_of_rejected_entries() const;
321 
323  size_t get_number_of_processed_entries() const;
324 
326  void reset_counters();
327 
329  bool is_activated_counters() const;
330 
332  void set_activated_counters(bool);
333 
334  protected:
335 
337  virtual int _accept() = 0;
338 
340  void _set_user_data(const boost::shared_ptr<i_referenced_data> & hd_);
341 
343  void _common_initialize(const datatools::properties & a_config);
344 
346  void _import_user_data_from(const i_cut &);
347 
349  void _export_user_data_to(i_cut &) const;
350 
352  void _set_name(const std::string & a_name);
353 
355  void _set_initialized(bool a_initialized);
356 
358  virtual void _prepare_cut();
359 
361  virtual int _finish_cut(int a_selection_status);
362 
364  virtual void _increment_counters(int a_selection_status);
365 
367  virtual void _at_set_user_data();
368 
370  virtual void _at_reset_user_data();
371 
373  void _reset();
374 
376  void _set_defaults();
377 
378  protected:
379 
380  // Configuration parameters:
382  std::string _name;
383  std::string _description;
384  std::string _version;
385  std::set<const std::type_info *> _supported_types_;
386 
387  private:
388 
389  // Status:
390  bool _initialized_;
391 
392  // Working data:
393  bool _activated_counters_;
394  boost::shared_ptr<i_referenced_data> _user_data_;
395  size_t _number_of_accepted_entries_;
396  size_t _number_of_rejected_entries_;
397 
398  public:
399 
401  virtual std::string get_type_id() const = 0;
402 
403  // Factory stuff :
405 
406  };
407 
408 } // end of namespace cuts
409 
411 #define CUT_REGISTRATION_INTERFACE(T) \
412  public: \
413  virtual std::string get_type_id() const; \
414  private: \
415  DATATOOLS_FACTORY_SYSTEM_AUTO_REGISTRATION_INTERFACE(::cuts::i_cut, T) \
416 
417 
419 #define CUT_REGISTRATION_IMPLEMENT(T,CutID) \
420  std::string T::get_type_id() const { return CutID; } \
421  DATATOOLS_FACTORY_SYSTEM_AUTO_REGISTRATION_IMPLEMENTATION(::cuts::i_cut, T, CutID) \
422 
423 
424 #endif // CUTS_I_CUT_H
425 
426 /*
427 ** Local Variables: --
428 ** mode: c++ --
429 ** c-file-style: "gnu" --
430 ** tab-width: 2 --
431 ** End: --
432 */
datatools::logger::priority _logging
Logging priority threshold.
Definition: i_cut.h:381
Definition: i_cut.h:268
void _import_user_data_from(const i_cut &)
Import user data from another cut.
virtual ~referenced_data()
Definition: i_cut.h:109
referenced_data(const T &obj)
Definition: i_cut.h:106
virtual bool match(const std::type_info *tit_) const =0
void register_supported_user_data_type()
Register user data type is supported.
Definition: i_cut.h:195
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
void unregister_supported_user_data_type()
Unregister user data type is supported.
Definition: i_cut.h:205
Definition: i_cut.h:270
export_config_flags
Flags for export to a container of properties.
Definition: i_cut.h:264
Definition: i_cut.h:265
bool is_activated_counters() const
Check the flag to activate counters.
An abstract class for daughter templatized data wrapper classes.
Definition: i_cut.h:67
void reset_user_data()
Clear the referenced user data.
bool has_version() const
Check if the version of the cut is set.
void set_version(const std::string &a_version)
Return the version of the cut.
virtual void reset()=0
The main termination method.
void set_description(const std::string &a_description)
Set the description of the cut.
const T * _address
Address of the referenced object.
Definition: i_cut.h:114
An interface with utilities for printable objects.
Definition: i_tree_dump.h:36
A fatal error. The application will most likely terminate. This is the highest priority.
Definition: logger.h:85
The cut abstract base class (interface)
Definition: i_cut.h:62
void _set_initialized(bool a_initialized)
Set the initialization flag.
static const uint32_t bit01
Definition: bit_mask.h:28
std::string _name
The name of the cut.
Definition: i_cut.h:382
virtual int process()
static const uint32_t bit03
Definition: bit_mask.h:30
const std::string & get_version() const
Return the version of the cut.
const T & get_user_data() const
Get user data of given type.
Definition: i_cut.h:181
void _export_user_data_to(i_cut &) const
Export user data to another cut.
Definition: i_cut.h:266
const std::type_info * _ti
Definition: i_cut.h:115
virtual void tree_dump(std::ostream &a_out=std::clog, const std::string &a_title="", const std::string &a_indent="", bool a_inherit=false) const
Smart print.
virtual std::string get_type_id() const =0
Return the unique type identifier associated to the class of the cut object.
static const uint32_t bit04
Definition: bit_mask.h:31
void _set_user_data(const boost::shared_ptr< i_referenced_data > &hd_)
Set user data by shared pointer.
Top-level namespace of the Bayeux/cuts module library.
Definition: accept_cut.h:21
An object that describes the way an object of a given class can be configured through properties.
Definition: object_configuration_description.h:234
virtual void _at_reset_user_data()
Hook executed when user data is reset.
void _set_defaults()
Set default attributes values.
#define DT_LOG_DEBUG(Priority, Message)
Log Message if Priority is greater or equal to PRIO_DEBUG.
Definition: logger_macros.h:147
const std::string & get_description() const
Return the description of the cut.
virtual void _prepare_cut()
Hook invoked before the main selection method.
size_t get_number_of_accepted_entries() const
Return the number of entries accepted by the cut.
void set_activated_counters(bool)
Set the flag to activate statistics.
virtual void _increment_counters(int a_selection_status)
Increment statistics counters.
size_t get_number_of_processed_entries() const
Return the total number of entries processed by the cut.
std::string _version
The version of the cut.
Definition: i_cut.h:384
virtual int _finish_cut(int a_selection_status)
Hook invoked after the main selection method.
bool is_initialized() const
Check initialization status.
void reset_counters()
Reset the embedded counters.
virtual ~i_referenced_data()
Definition: i_cut.h:71
virtual ~i_cut()
Destructor.
void _reset()
Reset the cut.
virtual const std::type_info * get_typeinfo() const =0
virtual const std::type_info * get_typeinfo() const
Definition: i_cut.h:85
int operator()()
Function interface for the selection method.
static const uint32_t bit02
Definition: bit_mask.h:29
void _common_initialize(const datatools::properties &a_config)
Common initialization of all cuts.
Definition: i_cut.h:273
std::string _description
The description of the cut.
Definition: i_cut.h:383
bool has_user_data() const
Check if some user data is referenced.
virtual void initialize_without_service(const datatools::properties &a_config, cut_handle_dict_type &a_cut_dictionary)
referenced_data()
Definition: i_cut.h:102
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
bool is_user_data() const
Check user data type.
Definition: i_cut.h:170
virtual void initialize_with_service_only(const datatools::properties &a_config, datatools::service_manager &a_service_manager)
void set_name(const std::string &a_name)
Set the name of the cut.
bool is_debug() const
Check if logging priority is at least at debug level.
A weak reference to an arbitrary object with tracked type_info
Definition: i_cut.h:76
void set_logging_priority(datatools::logger::priority p)
Set the logging priority threshold.
void set_user_data(const T &obj_)
Set user data.
Definition: i_cut.h:156
#define DT_LOG_TRACE(Priority, Message)
Log Message if Priority is greater or equal to PRIO_TRACE.
Definition: logger_macros.h:227
void _set_name(const std::string &a_name)
Set the cut name.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Utilities for logging information.
void reset()
Definition: i_cut.h:98
virtual void initialize(const datatools::properties &a_config, datatools::service_manager &a_service_manager, cut_handle_dict_type &a_cut_dictionary)=0
std::map< std::string, cut_entry_type > cut_handle_dict_type
Alias type of a dictionary of cut entry.
Definition: cut_tools.h:175
static void common_ocd(datatools::object_configuration_description &ocd_)
Basic OCD support shared by all inherited modules.
void set(const T &obj)
Definition: i_cut.h:91
bool is_user_data_type_supported() const
Check if data type is supported.
Definition: i_cut.h:215
const T & get() const
Definition: i_cut.h:78
static const uint32_t bit00
Definition: bit_mask.h:27
bool has_description() const
Check if the description of the cut is set.
datatools::logger::priority get_logging_priority() const
Get the logging priority threshold.
virtual void initialize_standalone(const datatools::properties &a_config)
Initialization method through a container of configuration properties (default implementation,...
#define DATATOOLS_FACTORY_SYSTEM_REGISTER_INTERFACE(BaseType)
Declaration of a system (allocator/functor) factory register as a static member of a base class and s...
Definition: factory_macros.h:52
size_t get_number_of_rejected_entries() const
Return the number of entries rejected by the cut.
virtual void export_to_config(datatools::properties &config_, uint32_t flags_=EXPORT_CONFIG_DEFAULT, const std::string &prefix_="") const
Export to a container of properties.
void print(std::ostream &a_out=std::clog) const
Print shortcut.
virtual void initialize_simple()
Naked initialization method (default implementation,.
const std::string & get_name() const
Return the name of the cut.
bool has_name() const
Check if the name of the cut is set.
i_cut(datatools::logger::priority p=datatools::logger::PRIO_FATAL)
Constructor.
Service management class.
Definition: service_manager.h:57
std::set< const std::type_info * > _supported_types_
The set of supported user data types.
Definition: i_cut.h:385
virtual bool match(const std::type_info *tit_) const
Definition: i_cut.h:88
virtual int _accept()=0
The main selection method (pure virtual, invoked by the.
virtual void _at_set_user_data()
Hook executed when user data is set.
A dictionary of arbitrary properties.
Definition: properties.h:125