Bayeux  3.4.1
Core Foundation library for SuperNEMO
service_tools-inl.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 : 2019-02-01
5  *
6  * Copyright (C) 2011-2019 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  * Service tools, template defs.
26  *
27  */
28 
29 #ifndef DATATOOLS_SERVICE_TOOLS_INL_H
30 #define DATATOOLS_SERVICE_TOOLS_INL_H
31 
32 // Standard Library:
33 #include <typeinfo>
34 
35 namespace datatools {
36 
42  template<class T>
43  bool has(const service_dict_type & services_, const std::string & service_name_)
44  {
45  if (!service_exists(services_, service_name_)) {
46  return false;
47  }
48  const base_service & srvc = get_service(services_, service_name_);
49  const std::type_info & ti = typeid(T);
50  const std::type_info & tf = typeid(srvc);
51  return ti == tf;
52  }
53 
59  template<class T>
60  T & grab(service_dict_type & services_, const std::string & service_name_)
61  {
62  base_service & srvc = grab_service(services_, service_name_);
63  const std::type_info & ti = typeid(T);
64  const std::type_info & tf = typeid(srvc);
65  DT_THROW_IF(ti != tf, std::logic_error, "Service '" << service_name_ << "' is not of requested type!");
66  return dynamic_cast<T &>(srvc);
67  }
68 
74  template<class T>
75  const T & get(const service_dict_type & services_, const std::string & service_name_)
76  {
77  const base_service & srvc = get_service(services_, service_name_);
78  const std::type_info & ti = typeid(T);
79  const std::type_info & tf = typeid(srvc);
80  DT_THROW_IF(ti != tf, std::logic_error, "Service '" << service_name_ << "' is not of requested type!");
81  return dynamic_cast<const T &>(srvc);
82  }
83 
84 } // end of namespace datatools
85 
86 #endif // DATATOOLS_SERVICE_TOOLS_INL_H
87 
88 // Local Variables: --
89 // mode: c++ --
90 // c-file-style: "gnu" --
91 // tab-width: 2 --
92 // End: --
const base_service & get_service(const service_dict_type &services_, const std::string &service_name_)
Return the const reference to a service given its name.
const T & get(const service_dict_type &services_, const std::string &service_name_)
Definition: service_tools-inl.h:75
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
base_service & grab_service(service_dict_type &services_, const std::string &service_name_)
Return the mutable reference to a service given its name.
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
bool service_exists(const service_dict_type &services_, const std::string &service_name_)
Check if a service with given name exists.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
T & grab(service_dict_type &services_, const std::string &service_name_)
Definition: service_tools-inl.h:60