LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
ServiceUtil.h
Go to the documentation of this file.
1 
19 #ifndef LARCORE_COREUTILS_SERVICEUTIL_H
20 #define LARCORE_COREUTILS_SERVICEUTIL_H
21 
22 // LArSoft libraries
25 
26 // framework libraries
29 #include "cetlib_except/demangle.h"
30 
31 // C/C++ standard libraries
32 #include <type_traits> // std::decay<>, std::is_same<>
33 #include <typeinfo>
34 
35 
36 namespace lar {
37 
38  namespace details {
39  template <typename PROVIDER>
41 
42  template <typename... Services>
44 
45  } // namespace details
46 
47 
76  template <typename T>
77  typename T::provider_type const* providerFrom()
78  {
79  details::ServiceRequirementsChecker<T>(); // instantiate a temporary...
80 
81  // retrieve the provider
83  typename T::provider_type const* pProvider = h->provider();
84  if (!pProvider) {
86  << "Service <" << cet::demangle_symbol(typeid(T).name())
87  << "> offered a null provider";
88  }
89 
90  return pProvider;
91 
92  } // providerFrom()
93 
94 
122  template <typename... Services>
125 
126 
137  template <typename... Services>
138  using providersFrom_t
139  = lar::ProviderPack<typename Services::provider_type...>;
140 
141 
142 
143  //----------------------------------------------------------------------------
144  namespace details {
146  template <typename PROVIDER>
148 
149  using provider_type = PROVIDER;
150 
151  // static checks on provider class: not copiable nor movable
152  static_assert(
154  "Service provider classes must not be copiable"
155  );
156  static_assert(
158  "Service provider classes must not be copiable"
159  );
160  static_assert(
162  "Service provider classes must not be movable"
163  );
164  static_assert(
166  "Service provider classes must not be movable"
167  );
168 
169  }; // ServiceProviderRequirementsChecker
170 
171 
172  template <typename SERVICE>
174 
175  // require SERVICE::provider_type to be a type
176  using provider_type = typename SERVICE::provider_type;
177 
178  // expected type for SERVICE::provider() method
179  using provider_func_type = provider_type const* (SERVICE::*)() const;
180 
183 
184  // check the provider() method
185  static_assert(
186  std::is_same<decltype(&SERVICE::provider), provider_func_type>::value,
187  "provider() method has unsupported signature"
188  );
189 
190  }; // ServiceRequirementsChecker
191 
192 
193  //--------------------------------------------------------------------------
194  template <typename First, typename... Others>
195  struct ProviderPackExtractor<First, Others...> {
196  static ProviderPack<
197  typename First::provider_type, typename Others::provider_type...
198  >
200  {
201  return {
203  lar::providerFrom<First>()
204  };
205  }
206  };
207 
208 
209  template <typename Service>
210  struct ProviderPackExtractor<Service> {
211  static auto parameterPack()
212  { return lar::makeProviderPack(lar::providerFrom<Service>()); }
213  };
214 
215 
216  } // namespace details
217 
218 } // namespace lar
219 
220 #endif //#LARCORE_COREUTILS_SERVICEUTIL_H
221 
typename SERVICE::provider_type provider_type
Definition: ServiceUtil.h:176
T::provider_type const * providerFrom()
Returns a constant pointer to the provider of specified service.
Definition: ServiceUtil.h:77
Defines a class that can&#39;t be copied nor moved.
static ProviderPack< typename First::provider_type, typename Others::provider_type... > parameterPack()
Definition: ServiceUtil.h:199
ServiceProviderRequirementsChecker< provider_type > provider_checker
Checker for the provider.
Definition: ServiceUtil.h:182
ProviderPack< Providers... > makeProviderPack(Providers const *...providers)
Function to create a ProviderPack from the function arguments.
Definition: ProviderPack.h:272
Compiles only if PROVIDER class satisfied service provider requirements.
Definition: ServiceUtil.h:147
provider_type const *(SERVICE::*)() const provider_func_type
Definition: ServiceUtil.h:179
auto providersFrom()
Returns a lar::ProviderPack with providers from all services.
Definition: ServiceUtil.h:123
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string value(boost::any const &)
LArSoft-specific namespace.
Container for a list of pointers to providers.
Definition: ProviderPack.h:114
Data structure containing constant pointers to classes.