LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ServicesManager.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_ServicesManager_h
2 #define art_Framework_Services_Registry_ServicesManager_h
3 // vim: set sw=2 expandtab :
4 
13 #include "cetlib/HorizontalRule.h"
14 #include "cetlib/LibraryManager.h"
15 #include "cetlib/bold_fontify.h"
16 #include "cetlib_except/demangle.h"
17 #include "fhiclcpp/fwd.h"
19 
20 #include <map>
21 #include <memory>
22 #include <stack>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 namespace art {
28 
29  class ActivityRegistry;
30  class ProcessConfiguration;
31  class ProducingServiceSignals;
32  namespace detail {
33  class SharedResources;
34  }
35 
37  public:
38  explicit ServicesManager(fhicl::ParameterSet&& servicesPSet,
39  ActivityRegistry& actReg,
40  detail::SharedResources& resources);
41  ~ServicesManager();
42 
43  ServicesManager(ServicesManager const&) = delete;
44  ServicesManager(ServicesManager&&) = delete;
45  ServicesManager& operator=(ServicesManager const&) = delete;
46  ServicesManager& operator=(ServicesManager&&) = delete;
47 
48  template <class T>
49  bool
50  isAvailable() const
51  {
52  return services_.find(TypeID{typeid(T)}) != cend(services_);
53  }
54 
55  void getParameterSets(std::vector<fhicl::ParameterSet>& out) const;
56 
57  void forceCreation();
58 
59  // Returns vector of names corresponding to services that produce
60  // products.
61  std::vector<std::string> registerProducts(
62  ProductDescriptions& productsToProduce,
63  ProducingServiceSignals& signals,
64  ProcessConfiguration const& pc);
65 
66  template <typename T>
67  T& get();
68 
69  template <typename T>
70  void put(std::unique_ptr<T>&& premade_service);
71 
72  template <typename SERVICE, typename... ARGS>
73  void addSystemService(ARGS&&... args);
74 
75  private:
78  cet::LibraryManager lm_{Suffixes::service()};
79  std::map<TypeID, detail::ServiceCacheEntry> services_{};
80  std::vector<TypeID> requestedCreationOrder_{};
81  std::stack<std::shared_ptr<detail::ServiceWrapperBase>>
82  actualCreationOrder_{};
83  std::vector<std::string> configErrMsgs_{};
84  };
85 
86  template <typename T>
87  T&
89  {
90  auto it = services_.find(TypeID{typeid(T)});
91  if (it == services_.end()) {
93  << "ServicesManager unable to find the service of type '"
94  << cet::demangle_symbol(typeid(T).name()) << "'.\n";
95  }
96  return it->second.get<T>(actReg_, resources_, actualCreationOrder_);
97  }
98 
99  template <typename T>
100  void
101  ServicesManager::put(std::unique_ptr<T>&& premade_service)
102  {
103  std::unique_ptr<detail::ServiceHelperBase> service_helper(
105  TypeID const id{typeid(T)};
106  auto it = services_.find(id);
107  if (it != services_.end()) {
108  throw Exception(errors::LogicError, "Service")
109  << "The system has manually added service of type "
110  << cet::demangle_symbol(id.name())
111  << ", but the service system already has a configured service of that "
112  "type\n";
113  }
115  new detail::ServiceWrapper<T>(std::move(premade_service))};
116  actualCreationOrder_.push(swb);
117  services_.emplace(
118  id, detail::ServiceCacheEntry(std::move(swb), std::move(service_helper)));
119  }
120 
121  template <typename SERVICE, typename... ARGS>
122  void
124  try {
125  put(std::make_unique<SERVICE>(std::forward<ARGS>(args)...));
126  }
127  catch (fhicl::detail::validationException const& e) {
128  constexpr cet::HorizontalRule rule{100};
130  << '\n'
131  << rule('=') << "\n\n"
132  << "!! The following service has been misconfigured: !!"
133  << "\n\n"
134  << rule('-') << "\n\nservice_type: "
135  << cet::bold_fontify(cet::demangle_symbol(typeid(SERVICE).name()))
136  << "\n\n"
137  << e.what() << '\n'
138  << rule('=') << "\n\n";
139  }
140 
141 } // namespace art
142 
143 #endif /* art_Framework_Services_Registry_ServicesManager_h */
144 
145 // Local Variables:
146 // mode: c++
147 // End:
ActivityRegistry & actReg_
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
std::vector< BranchDescription > ProductDescriptions
void addSystemService(ARGS &&...args)
void put(std::unique_ptr< T > &&premade_service)
detail::SharedResources & resources_
bool isAvailable() const
std::shared_ptr< ServiceWrapperBase > WrapperBase_ptr
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
char const * what() const noexcept override
static std::string const & service()
Definition: MVAAlg.h:12
Float_t e
Definition: plot.C:35