Bayeux  3.4.1
Core Foundation library for SuperNEMO
service_manager.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 : 2017-11-02
5  *
6  * Copyright (C) 2011-2017 Francois Mauger <mauger@lpccaen.in2p3.fr>
7  * Copyright (C) 2012 Ben Morgan <Ben.Morgan@warwick.ac.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or (at
12  * your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  *
25  * Description:
26  *
27  * A service manager.
28  *
29  */
30 
31 #ifndef DATATOOLS_SERVICE_MANAGER_H
32 #define DATATOOLS_SERVICE_MANAGER_H
33 
34 // Standard Library:
35 #include <string>
36 #include <sstream>
37 #include <typeinfo>
38 #include <stdexcept>
39 
40 // Third Party:
41 // - Boost:
42 #include <boost/cstdint.hpp>
43 
44 // This project:
46 #include <datatools/base_service.h>
47 #include <datatools/i_tree_dump.h>
48 #include <datatools/properties.h>
49 #include <datatools/bit_mask.h>
50 
51 namespace datatools {
52 
53  class properties;
54  class multi_properties;
55 
59  {
60  public:
61 
62  enum flag_type {
63  BLANK = 0,
68  };
69 
70  /*
71  struct dependee_manager_handle
72  {
73  dependee_manager_handle();
74  ~dependee_manager_handle();
75  void initialize(const service_manager &);
76  void initialize(const datatools::properties & config_,
77  const std::string & name_ = "",
78  const std::string & description_ = "",
79  const uint32_t flags_ = 0);
80  void reset();
81  bool is_initialized() const;
82  bool is_owned() const;
83  const service_manager & get_manager() const;
84  private:
85  const service_manager * _manager_ = nullptr; //!< Handle to the dependee manager
86  bool _owned_ = false; //!< Dependee manager is managed by the handle itself
87  };
88  */
89 
90  // typedef std::map<std::string, dependee_manager_handle> mgr_dict_type;
91 
93  service_manager(const std::string & name_ = "",
94  const std::string & description_ = "",
95  uint32_t flag_ = BLANK);
96 
98  virtual ~service_manager();
99 
101  void set_name(const std::string& name_);
102 
104  void set_description(const std::string& description_);
105 
107  const std::string& get_name() const;
108 
110  const std::string& get_description() const;
111 
113  bool is_debug() const;
114 
116  void set_debug(bool debug_ = true);
117 
119  void set_allow_dynamic_services(bool);
120 
122  bool is_allow_dynamic_services() const;
123 
126 
128  bool is_force_initialization_at_load() const;
129 
131  bool is_initialized() const;
132 
134  void initialize();
135 
137  void initialize(const datatools::properties& config_);
138 
140  void reset();
141 
142  // //! Check if external dependee managers are mounted
143  // bool has_mounted_dependee_managers() const;
144 
145  // //! Check if external dependee manager can be mounted
146  // bool can_mount_dependee_manager() const;
147 
148  // //! Mount an external dependee manager
149  // void mount_dependee_manager(const service_manager &);
150 
151  // //! Unmount an external dependee managers
152  // void unmount_dependee_managers();
153 
155  bool has_service_type(const std::string& id_) const;
156 
158  template <class ServiceClass>
159  void register_service_type(const std::string& id_);
160 
162  void unregister_service_type(const std::string& id_);
163 
171  };
172 
174  void build_list_of_services(std::vector<std::string> & list_, uint32_t flags_ = 0);
175 
179  bool has(const std::string & name_) const;
180 
184  bool is_initialized(const std::string & name_) const;
185 
189  template <class T>
190  bool is_a(const std::string & name_) const;
191 
196  template<class T>
197  T& grab(const std::string & name_);
198 
202  template<class T>
203  const T& get(const std::string & name_) const;
204 
208  base_service & grab_service(const std::string & name_);
209 
213  const base_service & get_service(const std::string & name_) const;
214 
216  bool can_drop(const std::string & name_) const;
217 
219  void drop(const std::string & name_);
220 
222  void load(const std::string & name_,
223  const std::string & id_,
224  const datatools::properties & config_);
225 
231  base_service & load_no_init(const std::string & name_,
232  const std::string & id_);
233 
238  void configure_no_init(const std::string& name_, const datatools::properties& config_);
239 
241  void load(const datatools::multi_properties& config_);
242 
244  const service_dict_type& get_local_services() const;
245 
247  const service_dict_type& get_bus_of_services(const bool update_ = false) const;
248 
250  void dump_services(std::ostream & out_ = std::clog,
251  const std::string& title_ = "",
252  const std::string& indent_ = "") const;
253 
255  virtual void tree_dump(std::ostream& out_ = std::clog,
256  const std::string& title_ = "",
257  const std::string& indent_ = "",
258  bool inherit_ = false) const;
259 
262 
265 
267  void update_service_bus();
268 
270  void sync();
271 
272  protected:
273 
275  void load_service(const std::string& name_,
276  const std::string& id_,
277  const datatools::properties& config_);
278 
280  void load_service(const std::string& name_,
281  const std::string& id_,
282  const datatools::properties * config_ptr_);
283 
285  void preload_global_dict();
286 
288  void create_service(service_entry& entry_);
289 
291  void initialize_service(service_entry& entry_);
292 
294  void reset_service(service_entry& entry_);
295 
297  void destroy_service(service_entry& entry_);
298 
300  void at_service_creation(const std::string & service_name_, const std::string & service_type_id_);
301 
303  void at_service_initialization(const std::string & service_name_);
304 
306  void at_service_reset(const std::string & service_name_);
307 
309  void at_service_drop(const std::string & service_name_);
310 
311  private:
312 
314  void set_preload(bool preload_);
315 
316  private:
317 
319  bool _initialized_ = false;
320  std::string _name_;
321  std::string _description_;
322  bool _preload_ = false;
323  bool _force_initialization_at_load_ = false;
324  bool _allow_dynamic_services_ = false;
325 
326  // 2012-04-09 FM : support for datatools::factory system :
327  base_service::factory_register_type _factory_register_;
328  // mgr_dict_type _dependee_managers_; //!< Dictionary of dependee managers
329 
330  service_dict_type _local_services_;
331 
332  service_dict_type _service_bus_;
333 
334  friend class service_entry;
335  };
336 
337 } // end of namespace datatools
338 
340 
341 /***************
342  * OCD support *
343  ***************/
344 #include <datatools/ocd_macros.h>
346 
347 #endif // DATATOOLS_SERVICE_MANAGER_H
348 
349 // Local Variables: --
350 // mode: c++ --
351 // c-file-style: "gnu" --
352 // tab-width: 2 --
353 // End: --
void set_force_initialization_at_load(bool)
Set the flag to force initialization of services at load.
void dump_services(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="") const
Basic print of embedded services.
bool has(const std::string &name_) const
datatools::logger::priority get_logging_priority() const
Return the logging priority threshold.
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
bool is_allow_dynamic_services() const
Check if dynamic services are allowed.
A container of mapped properties objects.
Definition: multi_properties.h:99
void set_debug(bool debug_=true)
Set the debug flag.
Flag to take into account services from the bus.
Definition: service_manager.h:170
void preload_global_dict()
Preload the factory register from service factory system register.
const T & get(const std::string &name_) const
Definition: service_manager-inl.h:31
flag_type
Definition: service_manager.h:62
void destroy_service(service_entry &entry_)
Destroy a service from its entry.
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
static const uint32_t bit01
Definition: bit_mask.h:28
void sync()
Synchronize internal data (expert only)
std::map< std::string, service_entry_ptr > service_dict_type
Type alias for a flat dictionary of service entries.
Definition: service_tools.h:196
The base service class.
Definition: base_service.h:68
static const uint32_t bit03
Definition: bit_mask.h:30
No special filter flags.
Definition: service_manager.h:166
bool has_service_type(const std::string &id_) const
Check if a service identifier/type is supported.
void build_list_of_services(std::vector< std::string > &list_, uint32_t flags_=0)
Build the list of services names.
void set_name(const std::string &name_)
Set the name of the service.
Force initialization of service instance at load rather than at first use.
Definition: service_manager.h:65
void set_description(const std::string &description_)
Set the description of the service.
bool can_drop(const std::string &name_) const
Check if a service with given name can be dropped.
void at_service_drop(const std::string &service_name_)
Action done at service drop.
Do not preload service types from the system register.
Definition: service_manager.h:64
T & grab(const std::string &name_)
Definition: service_manager-inl.h:25
void drop(const std::string &name_)
Drop a service given its name.
base_service & grab_service(const std::string &name_)
void register_service_type(const std::string &id_)
Register a service factory.
Definition: service_manager-inl.h:38
void at_service_creation(const std::string &service_name_, const std::string &service_type_id_)
Action done at service creation.
virtual ~service_manager()
Destructor.
void initialize()
Initialize the manager.
bool is_initialized() const
Check the initialization flag.
Allow dynamic services.
Definition: service_manager.h:67
bool is_a(const std::string &name_) const
Definition: service_manager-inl.h:16
bool is_debug() const
Check the debug flag.
Internal entry for service objects stored in the service manager class.
Definition: service_tools.h:85
void at_service_initialization(const std::string &service_name_)
Action done at service initialization.
void at_service_reset(const std::string &service_name_)
Action done at service reset.
Flag to skip uninitialized services.
Definition: service_manager.h:168
DOCD_CLASS_DECLARATION(my::algo)
Declaration of the OCD support for the my::algo class.
bool is_force_initialization_at_load() const
Check if the flag to force initialization of services at load is set.
void create_service(service_entry &entry_)
Instantiate a service from its entry.
Activate factory verbosity (debug)
Definition: service_manager.h:66
void configure_no_init(const std::string &name_, const datatools::properties &config_)
Flag to skip initialized services.
Definition: service_manager.h:167
static const uint32_t bit02
Definition: bit_mask.h:29
void unregister_service_type(const std::string &id_)
Unregister a service factory.
const service_dict_type & get_local_services() const
Return the list of services locally hosted by this manager.
const service_dict_type & get_bus_of_services(const bool update_=false) const
Return the bus of services known in the context of this manager.
No special configuration flags.
Definition: service_manager.h:63
base_service & load_no_init(const std::string &name_, const std::string &id_)
void initialize_service(service_entry &entry_)
Initialize a service from its entry.
void load(const std::string &name_, const std::string &id_, const datatools::properties &config_)
Load a service with given name, identifier and configuration.
void reset()
Reset the manager.
Flag to skip clear the list.
Definition: service_manager.h:169
service_filter_flag
Special flag to build the list of service names.
Definition: service_manager.h:165
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
void load_service(const std::string &name_, const std::string &id_, const datatools::properties &config_)
Load a service.
const base_service & get_service(const std::string &name_) const
static const uint32_t bit00
Definition: bit_mask.h:27
void update_service_bus()
Update the bus of services (expert only)
service_manager(const std::string &name_="", const std::string &description_="", uint32_t flag_=BLANK)
Constructor.
void set_allow_dynamic_services(bool)
Set the flag to allow_dynamic_services.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
void reset_service(service_entry &entry_)
Reset a service from its entry.
void set_logging_priority(datatools::logger::priority)
Set the logging priority threshold.
Service management class.
Definition: service_manager.h:57
const std::string & get_name() const
Get the name of the service.
const std::string & get_description() const
Get the description of the service.
A dictionary of arbitrary properties.
Definition: properties.h:125