ServiceCacheEntry.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_detail_ServiceCacheEntry_h
2 #define art_Framework_Services_Registry_detail_ServiceCacheEntry_h
3 ////////////////////////////////////////////////////////////////////////
4 // ServiceCacheEntry
5 //
6 // Used by ServicesManager to handle creation and caching of services.
7 
12 #include "fhiclcpp/ParameterSet.h"
13 
14 #include <cassert>
15 #include <memory>
16 
17 namespace art {
18  namespace detail {
19  class ServiceCacheEntry;
20  }
21 
22  // Forward declarations.
23  class ActivityRegistry;
24  class MasterProductRegistry;
25  class ModuleDescription;
26  class ProducingServiceSignals;
27 }
28 
29 // A ServiceCacheEntry object encapsulates the (possibly delayed)
30 // creation of a Service object.
32 public:
33  // Service implementation.
35  std::unique_ptr<detail::ServiceHelperBase>&& helper);
36 
37  // Service interface implementation.
39  std::unique_ptr<detail::ServiceHelperBase>&& helper,
40  ServiceCacheEntry const& impl);
41 
42  // Pre-made service (1).
43  ServiceCacheEntry(WrapperBase_ptr premade_service,
44  std::unique_ptr<detail::ServiceHelperBase>&& helper);
45 
46  // Create the service if necessary, and return the WrapperBase_ptr
47  // that refers to it.
49  detail::ServiceStack& creationOrder) const;
50 
51  void forceCreation(ActivityRegistry& reg) const;
52 
53  fhicl::ParameterSet const& getParameterSet() const;
54 
55  template <typename T,
56  typename = std::enable_if_t<detail::ServiceHelper<T>::scope_val !=
58  T& get(ActivityRegistry& reg, detail::ServiceStack& creationOrder) const;
59 
60  template <typename T,
61  typename = std::enable_if_t<detail::ServiceHelper<T>::scope_val ==
63  T& get(ActivityRegistry& reg,
64  detail::ServiceStack& creationOrder,
65  ScheduleID sID) const;
66 
68  ProductDescriptions& productsToProduce,
69  ProducingServiceSignals& signals,
70  ModuleDescription const& md);
71 
72  static void setNSchedules(size_t nSched);
73 
74  bool is_impl() const;
75  bool is_interface() const;
76 
77  ServiceScope serviceScope() const;
78 
79 private:
80  void makeAndCacheService(ActivityRegistry& reg) const;
81 
83  detail::ServiceStack& creationOrder) const;
84 
85  void convertService(WrapperBase_ptr& swb) const;
86 
87  static size_t& nSchedules();
88 
90  std::unique_ptr<detail::ServiceHelperBase> helper_;
92  ServiceCacheEntry const* const interface_impl_{nullptr};
93 }; // ServiceCacheEntry
94 
95 inline void
97 {
98  assert(is_impl() &&
99  "ServiceCacheEntry::forceCreation called on a service interface!");
100  if (!service_) {
101  makeAndCacheService(reg);
102  }
103 }
104 
105 inline fhicl::ParameterSet const&
107 {
108  return config_;
109 }
110 
111 inline void
113  ActivityRegistry& reg,
114  detail::ServiceStack& creationOrder) const
115 {
116  assert(is_impl() &&
117  "ServiceCacheEntry::createService called on a service interface!");
118  // When we actually create the Service object, we have to
119  // remember the order of creation.
120  makeAndCacheService(reg);
121  creationOrder.push(service_);
122 }
123 
124 inline void
126 {
127  assert(is_impl() &&
128  "ServiceCacheEntry::convertService called on a service interface!");
129  swb = dynamic_cast<detail::ServiceInterfaceImplHelper&>(*helper_).convert(
130  service_);
131 }
132 
133 inline art::ServiceScope
135 {
136  return helper_->scope();
137 }
138 
139 template <typename T, typename>
140 inline T&
142  detail::ServiceStack& creationOrder) const
143 {
145  "Called wrong service get() function: need ScheduleID");
146  WrapperBase_ptr swb = getService(reg, creationOrder);
147  return *reinterpret_cast<T*>(
148  dynamic_cast<detail::ServiceLGRHelper&>(*helper_).retrieve(swb));
149 }
150 
151 template <typename T, typename>
152 inline T&
154  detail::ServiceStack& creationOrder,
155  ScheduleID const sID) const
156 {
158  "Called wrong service get() function!");
159  WrapperBase_ptr swb = getService(reg, creationOrder);
160  return *reinterpret_cast<T*>(
161  dynamic_cast<detail::ServicePSRHelper&>(*helper_).retrieve(swb, sID));
162 }
163 
164 inline void
166 {
167  nSchedules() = nSched;
168 }
169 
170 inline size_t&
172 {
173  static size_t s_ns{1};
174  return s_ns;
175 }
176 
177 inline bool
179 {
180  return !is_interface();
181 }
182 
183 inline bool
185 {
186  return helper_->is_interface();
187 }
188 #endif /* art_Framework_Services_Registry_detail_ServiceCacheEntry_h */
189 
190 // Local Variables:
191 // mode: c++
192 // End:
fhicl::ParameterSet const & getParameterSet() const
ServiceScope
Definition: ServiceScope.h:5
void convertService(WrapperBase_ptr &swb) const
virtual void * retrieve(std::shared_ptr< ServiceWrapperBase > &swb) const =0
std::vector< BranchDescription > ProductDescriptions
std::stack< WrapperBase_ptr > ServiceStack
Definition: ServiceStack.h:10
virtual void * retrieve(std::shared_ptr< ServiceWrapperBase > &swb, ScheduleID sID) const =0
void registerProducts(MasterProductRegistry &mpr, ProductDescriptions &productsToProduce, ProducingServiceSignals &signals, ModuleDescription const &md)
std::shared_ptr< detail::ServiceWrapperBase > WrapperBase_ptr
void forceCreation(ActivityRegistry &reg) const
ServiceScope serviceScope() const
void createService(ActivityRegistry &reg, detail::ServiceStack &creationOrder) const
WrapperBase_ptr getService(ActivityRegistry &reg, detail::ServiceStack &creationOrder) const
ServiceCacheEntry const *const interface_impl_
virtual std::unique_ptr< ServiceWrapperBase > convert(std::shared_ptr< ServiceWrapperBase > const &swb) const =0
T & get(ActivityRegistry &reg, detail::ServiceStack &creationOrder) const
assert(nhit_max >=nhit_nbins)
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
double T
Definition: Xdiff_gwt.C:5
ServiceCacheEntry(fhicl::ParameterSet const &pset, std::unique_ptr< detail::ServiceHelperBase > &&helper)
std::unique_ptr< detail::ServiceHelperBase > helper_
static void setNSchedules(size_t nSched)
void makeAndCacheService(ActivityRegistry &reg) const