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:
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  put(std::make_unique<SERVICE>(std::forward<ARGS>(args)...));
125  }
126  catch (fhicl::detail::validationException const& e) {
127  constexpr cet::HorizontalRule rule{100};
129  << "\n"
130  << rule('=') << "\n\n"
131  << "!! The following service has been misconfigured: !!"
132  << "\n\n"
133  << rule('-') << "\n\nservice_type: "
134  << cet::bold_fontify(cet::demangle_symbol(typeid(SERVICE).name()))
135  << "\n\n"
136  << e.what() << "\n"
137  << rule('=') << "\n\n";
138  }
139 
140 } // namespace art
141 
142 #endif /* art_Framework_Services_Registry_ServicesManager_h */
143 
144 // Local Variables:
145 // mode: c++
146 // End:
static QCString name
Definition: declinfo.cpp:673
ActivityRegistry & actReg_
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:87
std::vector< BranchDescription > ProductDescriptions
void addSystemService(ARGS &&...args)
static QCString args
Definition: declinfo.cpp:674
void put(std::unique_ptr< T > &&premade_service)
detail::SharedResources & resources_
const double e
def move(depos, offset)
Definition: depos.py:107
std::string bold_fontify(std::string const &s)
Definition: bold_fontify.h:8
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()