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) : service_ptr_{move(p)} {}
59 
60  T&
61  get()
62  {
63  return *service_ptr_;
64  }
65 
66  template <typename U,
67  typename = std::enable_if_t<std::is_base_of_v<U, T>>>
68  std::unique_ptr<ServiceWrapper<U>>
69  getAs() const
70  {
71  return std::make_unique<ServiceWrapper<U>>(
72  std::static_pointer_cast<U>(service_ptr_));
73  }
74 
75  private:
76  void
78  ProducingServiceSignals& signals,
79  ModuleDescription const& md) override
80  {
81  if constexpr (std::is_base_of_v<ProducingService, T>) {
82  service_ptr_->registerCallbacks(signals);
83  service_ptr_->setModuleDescription(md);
84  service_ptr_->registerProducts(productsToProduce, md);
85  }
86  }
87 
88  std::shared_ptr<T> service_ptr_;
89  };
90 
91  } // namespace detail
92 } // namespace art
93 
94 #endif /* art_Framework_Services_Registry_detail_ServiceWrapper_h */
95 
96 // Local Variables:
97 // mode: c++
98 // 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)
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
static constexpr double ps
Definition: Units.h:99
std::unique_ptr< ServiceWrapper< U > > getAs() const
void registerProducts(ProductDescriptions &productsToProduce, ProducingServiceSignals &signals, ModuleDescription const &md) override
ServiceWrapper(ServiceWrapper const &)=delete
ServiceWrapper(fhicl::ParameterSet const &ps, ActivityRegistry &areg)