LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ServiceWrapper.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_detail_ServiceWrapper_h
2 #define art_Framework_Services_Registry_detail_ServiceWrapper_h
3 // vim: set sw=2 expandtab :
4 
6 
7 #include <memory>
8 #include <type_traits>
9 
10 namespace art {
11 
12  class ActivityRegistry;
13  class ModuleDescription;
14  class ProducingService;
15 
16  namespace detail {
17 
18  template <typename T>
20 
21  // If we have a constructor taking fhicl::ParameterSet const& and
22  // ActivityRegistry&, use it. Otherwise, call a one-argument
23  // constructor taking fhicl::ParameterSet const& only.
24  template <typename T>
25  std::enable_if_t<
26  std::is_constructible_v<T, fhicl::ParameterSet const&, ActivityRegistry&>,
27  std::shared_ptr<T>>
29  {
30  static_assert(
31  !std::is_base_of_v<ProducingService, T>,
32  "\n\nart-error: A service that inherits from art::ProducingService\n"
33  " cannot have a constructor that takes an ActivityRegistry&\n"
34  " argument. Contact artists@fnal.gov for guidance.\n");
35  return std::make_shared<T>(ps, areg);
36  }
37 
38  template <typename T>
39  std::enable_if_t<!std::is_constructible_v<T,
40  fhicl::ParameterSet const&,
42  std::shared_ptr<T>>
43  makeServiceFrom(fhicl::ParameterSet const& ps, ActivityRegistry&)
44  {
45  return std::make_shared<T>(ps);
46  }
47 
48  template <typename T>
49  class ServiceWrapper : public ServiceWrapperBase {
50  public:
51  ServiceWrapper(ServiceWrapper const&) = delete;
52  ServiceWrapper& operator=(ServiceWrapper const&) = delete;
53 
54  ServiceWrapper(fhicl::ParameterSet const& ps, ActivityRegistry& areg)
55  : service_ptr_{makeServiceFrom<T>(ps, areg)}
56  {}
57 
58  explicit ServiceWrapper(std::shared_ptr<T>&& p)
59  : service_ptr_{std::move(p)}
60  {}
61 
62  T&
63  get()
64  {
65  return *service_ptr_;
66  }
67 
68  template <typename U,
69  typename = std::enable_if_t<std::is_base_of_v<U, T>>>
70  std::unique_ptr<ServiceWrapper<U>>
71  getAs() const
72  {
73  return std::make_unique<ServiceWrapper<U>>(
74  std::static_pointer_cast<U>(service_ptr_));
75  }
76 
77  private:
78  void
80  ProducingServiceSignals& signals,
81  ModuleDescription const& md) override
82  {
83  if constexpr (std::is_base_of_v<ProducingService, T>) {
84  service_ptr_->registerCallbacks(signals);
85  service_ptr_->setModuleDescription(md);
86  service_ptr_->registerProducts(productsToProduce, md);
87  }
88  }
89 
90  std::shared_ptr<T> service_ptr_;
91  };
92 
93  } // namespace detail
94 } // namespace art
95 
96 #endif /* art_Framework_Services_Registry_detail_ServiceWrapper_h */
97 
98 // Local Variables:
99 // mode: c++
100 // End:
std::shared_ptr< T > service_ptr_
std::enable_if_t< std::is_constructible_v< T, fhicl::ParameterSet const &, ActivityRegistry & >, std::shared_ptr< T > > makeServiceFrom(fhicl::ParameterSet const &ps, ActivityRegistry &areg)
ServiceWrapper & operator=(ServiceWrapper const &)=delete
std::vector< BranchDescription > ProductDescriptions
ServiceWrapper(std::shared_ptr< T > &&p)
std::unique_ptr< ServiceWrapper< U > > getAs() const
Definition: MVAAlg.h:12
void registerProducts(ProductDescriptions &productsToProduce, ProducingServiceSignals &signals, ModuleDescription const &md) override
ServiceWrapper(ServiceWrapper const &)=delete
ServiceWrapper(fhicl::ParameterSet const &ps, ActivityRegistry &areg)