LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
ServiceCacheEntry.cc
Go to the documentation of this file.
1 // ServiceCacheEntry
3 //
4 // Used by ServicesManager to handle creation and caching of services.
5 
7 
14 #include "cetlib/HorizontalRule.h"
15 #include "cetlib_except/demangle.h"
16 #include "fhiclcpp/ParameterSet.h"
18 
19 #include <cassert>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
25  fhicl::ParameterSet const& pset,
26  std::unique_ptr<detail::ServiceHelperBase>&& helper)
27  : config_{pset}, helper_{std::move(helper)}
28 {}
29 
30 // Service interface implementation.
32  fhicl::ParameterSet const& pset,
33  std::unique_ptr<detail::ServiceHelperBase>&& helper,
34  ServiceCacheEntry const& impl)
35  : config_{pset}, helper_{std::move(helper)}, interface_impl_{&impl}
36 {}
37 
38 // Pre-made service (1).
40  WrapperBase_ptr premade_service,
41  std::unique_ptr<detail::ServiceHelperBase>&& helper)
42  : helper_{std::move(helper)}, service_{premade_service}
43 {}
44 
45 // Create the service if necessary, and return the WrapperBase_ptr that
46 // refers to it.
49  ActivityRegistry& reg,
50  detail::ServiceStack& creationOrder) const
51 {
52  if (is_interface()) { // Service interface
53  if (!service_) {
54  // No cached instance, we need to make it.
55  if (!interface_impl_->service_) {
56  // The service provider has no cached instance, have it make one.
57  interface_impl_->createService(reg, creationOrder);
58  }
59  // Convert the service provider wrapper to a service interface wrapper,
60  // and use that as our cached instance.
62  }
63  } else { // Concrete service.
64  if (!service_) {
65  // No cached instance, we need to make it.
66  createService(reg, creationOrder);
67  }
68  }
69  return service_;
70 }
71 
72 void
74 {
75  assert(
76  is_impl() &&
77  "ServiceCacheEntry::makeAndCacheService called on a service interface!");
78  try {
80  service_ = dynamic_cast<detail::ServicePSMHelper&>(*helper_).make(
81  config_, reg, nSchedules());
82  } else {
83  service_ =
84  dynamic_cast<detail::ServiceLGMHelper&>(*helper_).make(config_, reg);
85  }
86  }
87  catch (fhicl::detail::validationException const& e) {
88  std::ostringstream err_stream;
89  constexpr cet::HorizontalRule rule{100};
90  err_stream << "\n"
91  << rule('=') << "\n\n"
92  << "!! The following service has been misconfigured: !!"
93  << "\n\n"
94  << rule('-') << "\n\nservice_type: "
96  config_.get<std::string>("service_type"))
97  << "\n\n"
98  << e.what() << "\n"
99  << rule('=') << "\n\n";
100  throw art::Exception(art::errors::Configuration) << err_stream.str();
101  }
102  catch (cet::exception& e) {
103  throw Exception(errors::OtherArt, "ServiceCreation", e)
104  << "cet::exception caught during construction of service type "
105  << cet::demangle_symbol(helper_->get_typeid().name()) << ":\n";
106  }
107  catch (std::exception& e) {
108  throw Exception(errors::StdException, "ServiceCreation")
109  << "std::exception caught during construction of service type "
110  << cet::demangle_symbol(helper_->get_typeid().name()) << ": " << e.what();
111  }
112  catch (std::string const& s) {
114  << "String exception during construction of service type "
115  << cet::demangle_symbol(helper_->get_typeid().name()) << ": " << s;
116  }
117  catch (...) {
119  << "String exception during construction of service type "
120  << cet::demangle_symbol(helper_->get_typeid().name()) << ":\n";
121  }
122 }
123 
124 void
127  ProductDescriptions& productsToProduce,
128  ProducingServiceSignals& signals,
129  ModuleDescription const& md)
130 {
131  service_->registerProducts(mpr, productsToProduce, signals, md);
132 }
std::string bold_fontify(std::string const &s)
Definition: bold_fontify.h:9
Float_t s
Definition: plot.C:23
virtual std::unique_ptr< ServiceWrapperBase > make(fhicl::ParameterSet const &cfg, ActivityRegistry &reg, size_t nSchedules) const =0
void convertService(WrapperBase_ptr &swb) const
std::vector< BranchDescription > ProductDescriptions
std::stack< WrapperBase_ptr > ServiceStack
Definition: ServiceStack.h:10
virtual std::unique_ptr< ServiceWrapperBase > make(fhicl::ParameterSet const &cfg, ActivityRegistry &reg) const =0
void registerProducts(MasterProductRegistry &mpr, ProductDescriptions &productsToProduce, ProducingServiceSignals &signals, ModuleDescription const &md)
std::shared_ptr< detail::ServiceWrapperBase > WrapperBase_ptr
T get(std::string const &key) const
Definition: ParameterSet.h:231
ServiceScope serviceScope() const
void createService(ActivityRegistry &reg, detail::ServiceStack &creationOrder) const
WrapperBase_ptr getService(ActivityRegistry &reg, detail::ServiceStack &creationOrder) const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
char const * what() const noexcept override
ServiceCacheEntry const *const interface_impl_
ServiceCacheEntry(fhicl::ParameterSet const &pset, std::unique_ptr< detail::ServiceHelperBase > &&helper)
std::unique_ptr< detail::ServiceHelperBase > helper_
Float_t e
Definition: plot.C:34
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void makeAndCacheService(ActivityRegistry &reg) const