ServiceHandle.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Registry_ServiceHandle_h
2 #define art_Framework_Services_Registry_ServiceHandle_h
3 // vim: set sw=2 expandtab :
4 
5 // =======================================================================
6 // ServiceHandle
7 //
8 // Smart pointer used to give easy access to Services.
9 //
10 // Note invocation only requires one template argument, but the
11 // constructor will require zero or one arguments depending on the scope
12 // of the service (LEGACY, SHARED).
13 //
14 // Technical notes:
15 //
16 // Since ServiceHelper<T> instantiations correspond to
17 // specializations created by macro calls, only a template argument
18 // 'T' that is non-const qualified will match any specialization.
19 // However, const-only access to a service can be provided via
20 // ServiceHandle<MyService const> as long as the const-ness of the
21 // template argument is stripped (via std::remove_const_t<T>)
22 // before serving as an argument to ServiceHelper.
23 // =======================================================================
24 
29 
30 #include <type_traits>
31 
32 namespace art {
33 
34  template <typename T,
35  ServiceScope SCOPE =
36  detail::ServiceHelper<std::remove_const_t<T>>::scope_val>
37  class ServiceHandle {
38  public:
39  static_assert(
40  detail::handle_allowed_v<T>,
41  "\n\nart-error: You cannot create a ServiceHandle for this type.\n"
42  " Please contact artists@fnal.gov for guidance.\n");
43 
45  &ServiceRegistry::instance().get<std::remove_const_t<T>>()
46  }
47  {}
48  catch (Exception const& x)
49  {
51  << "Unable to create ServiceHandle.\n"
52  << "Perhaps the FHiCL configuration does not specify the necessary "
53  "service?\n"
54  << "The class of the service is noted below...\n"
55  << x;
56  }
57 
58  T* operator->() const { return instance; }
59 
60  T& operator*() const { return *instance; }
61 
62  T*
63  get() const
64  {
65  return instance;
66  }
67 
68  private:
70  };
71 
72 } // namespace art
73 
74 #endif /* art_Framework_Services_Registry_ServiceHandle_h */
75 
76 // Local Variables:
77 // mode: c++
78 // End:
ServiceScope
Definition: ServiceScope.h:7
static ServiceRegistry & instance() noexcept
T & operator*() const
Definition: ServiceHandle.h:60
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
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
T * operator->() const
Definition: ServiceHandle.h:58