LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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<>, std::add_const_t<>
33 #include <typeinfo>
34 
35 namespace lar {
36 
37  namespace details {
38  template <typename PROVIDER>
40 
41  template <typename... Services>
43 
44  } // namespace details
45 
74  template <typename T>
75  typename T::provider_type const* providerFrom()
76  {
77  using Service_t = std::add_const_t<T>;
78  using Provider_t = typename Service_t::provider_type;
79 
81 
82  // retrieve the provider
84  Provider_t const* const pProvider{h->provider()};
85  if (!pProvider) {
87  << "ServiceHandle <" << cet::demangle_symbol(typeid(Service_t).name())
88  << "> offered a null provider";
89  }
90 
91  return pProvider;
92 
93  } // providerFrom()
94 
122  template <typename... Services>
124  {
126  }
127 
138  template <typename... Services>
139  using providersFrom_t = lar::ProviderPack<typename Services::provider_type...>;
140 
141  //----------------------------------------------------------------------------
142  namespace details {
144  template <typename PROVIDER>
146 
147  using provider_type = PROVIDER;
148 
149  // static checks on provider class: not copiable nor movable
151  "Service provider classes must not be copiable");
153  "Service provider classes must not be copiable");
155  "Service provider classes must not be movable");
157  "Service provider classes must not be movable");
158 
159  }; // ServiceProviderRequirementsChecker
160 
161  template <typename SERVICE>
163 
164  // require SERVICE::provider_type to be a type
165  using provider_type = typename SERVICE::provider_type;
166 
167  // expected type for SERVICE::provider() method
168  using provider_func_type = provider_type const* (SERVICE::*)() const;
169 
172 
173  // check the provider() method
174  static_assert(std::is_same<decltype(&SERVICE::provider), provider_func_type>::value,
175  "provider() method has unsupported signature");
176 
177  }; // ServiceRequirementsChecker
178 
179  //--------------------------------------------------------------------------
180  template <typename First, typename... Others>
181  struct ProviderPackExtractor<First, Others...> {
182  static ProviderPack<typename First::provider_type, typename Others::provider_type...>
184  {
185  return {ProviderPackExtractor<Others...>::parameterPack(), lar::providerFrom<First>()};
186  }
187  };
188 
189  template <typename Service>
190  struct ProviderPackExtractor<Service> {
191  static auto parameterPack() { return lar::makeProviderPack(lar::providerFrom<Service>()); }
192  };
193 
194  } // namespace details
195 
196 } // namespace lar
197 
198 #endif //#LARCORE_COREUTILS_SERVICEUTIL_H
typename SERVICE::provider_type provider_type
Definition: ServiceUtil.h:165
T::provider_type const * providerFrom()
Returns a constant pointer to the provider of specified service.
Definition: ServiceUtil.h:75
Defines classes that can&#39;t be copied nor moved.
ServiceProviderRequirementsChecker< provider_type > provider_checker
Checker for the provider.
Definition: ServiceUtil.h:171
static ProviderPack< typename First::provider_type, typename Others::provider_type... > parameterPack()
Definition: ServiceUtil.h:183
ProviderPack< Providers... > makeProviderPack(Providers const *...providers)
Function to create a ProviderPack from the function arguments.
Definition: ProviderPack.h:256
Compiles only if PROVIDER class satisfied service provider requirements.
Definition: ServiceUtil.h:145
provider_type const *(SERVICE::*)() const provider_func_type
Definition: ServiceUtil.h:168
double value
Definition: spectrum.C:18
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
LArSoft-specific namespace.
Container for a list of pointers to providers.
Definition: ProviderPack.h:111
Data structure containing constant pointers to classes.