ServiceRegistry.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_ServiceRegistry_h
2 #define art_Framework_Services_Registry_ServiceRegistry_h
3 
4 // ======================================================================
5 //
6 // ServiceRegistry - Manages the 'thread specific' instance of Services
7 //
8 // ======================================================================
9 
16 #include "cetlib/LibraryManager.h"
17 #include "fhiclcpp/ParameterSet.h"
18 
19 #include <memory>
20 
21 // ----------------------------------------------------------------------
22 
23 namespace art {
24  class ActivityRegistry;
25 
27  // non-copyable:
28  ServiceRegistry(ServiceRegistry const&) = delete;
29  ServiceRegistry& operator=(ServiceRegistry const&) = delete;
30 
31  public:
32  class Operate {
33  // non-copyable:
34  Operate(Operate const&) = delete;
35  Operate& operator=(Operate const&) = delete;
36  // override operator new to stop use on heap?
37 
38  public:
39  Operate(ServiceToken const& iToken)
41  {}
42 
44 
45  private:
47  }; // Operate
48 
49  friend class Operate;
50  template <typename T, art::ServiceScope>
51  friend class ServiceHandle;
52 
53  template <typename T>
54  static bool
56  {
57  if (auto& mgr = instance().manager_) {
58  return mgr->isAvailable<T>();
59  }
60 
62  << " no ServiceRegistry has been set for this thread";
63  }
64 
65  using ParameterSets = std::vector<fhicl::ParameterSet>;
67 
68  private:
69  // The token can be passed to another thread in order to have the
70  // same services available in the other thread.
71  ServiceToken presentToken() const;
72 
73  static ServiceRegistry& instance();
74 
75  // returns old token
76  ServiceToken setContext(ServiceToken const& iNewToken);
77  void unsetContext(ServiceToken const& iOldToken);
78 
79  template <typename T,
80  typename = std::enable_if_t<detail::ServiceHelper<T>::scope_val !=
82  T&
83  get() const
84  {
85  if (!manager_) {
87  << " no ServiceRegistry has been set for this thread";
88  }
89  return manager_->get<T>();
90  }
91 
92  template <typename T,
93  typename = std::enable_if_t<detail::ServiceHelper<T>::scope_val ==
95  T&
96  get(ScheduleID sID) const
97  {
98  if (!manager_) {
100  << " no ServiceRegistry has been set for this thread";
101  }
102  return manager_->get<T>(sID);
103  }
104 
105  ServiceRegistry() = default;
106 
107  // ---------- member data --------------------------------
109  std::shared_ptr<ServicesManager> manager_{nullptr};
110 
111  }; // ServiceRegistry
112 
113 } // art
114 
115 // ======================================================================
116 
117 #endif /* art_Framework_Services_Registry_ServiceRegistry_h */
118 
119 // Local Variables:
120 // mode: c++
121 // End:
Operate(Operate const &)=delete
void unsetContext(ServiceToken const &iOldToken)
static ServiceToken createSet(ParameterSets const &, ActivityRegistry &)
ServiceToken presentToken() const
ServiceToken setContext(ServiceToken const &iNewToken)
std::shared_ptr< ServicesManager > manager_
static ServiceRegistry & instance()
static bool isAvailable()
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< fhicl::ParameterSet > ParameterSets
ServiceRegistry()=default
ServiceRegistry & operator=(ServiceRegistry const &)=delete
static std::string const & service()
Operate(ServiceToken const &iToken)
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
Operate & operator=(Operate const &)=delete
cet::LibraryManager lm_