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 
4 // ======================================================================
5 // ServiceHandle
6 //
7 // Smart pointer used to give easy access to Services.
8 //
9 // Note invocation only requires one template argument, but the
10 // constructor will require zero or one arguments depending on the scope
11 // of the service (LEGACY, GLOBAL or PER_SCHEDULE).
12 //
13 // Technical notes:
14 //
15 // Since ServiceHelper<T> instantiations correspond to
16 // specializations created by macro calls, only a template argument
17 // 'T' that is non-const qualified will match any specialization.
18 // However, const-only access to a service can be provided via
19 // ServiceHandle<MyService const> as long as the const-ness of the
20 // template argument is stripped (via std::remove_const_t<T>)
21 // before serving as an argument to ServiceHelper.
22 // ======================================================================
23 
31 
32 #include <type_traits>
33 
34 namespace art {
35 
36  template <typename T,
37  ServiceScope SCOPE =
40  template <typename T>
42 }
43 
44 // General template.
45 template <typename T, art::ServiceScope SCOPE>
46 class art::ServiceHandle {
47 public:
48  static_assert(
49  detail::handle_allowed_v<T>,
50  "\n\nart-error: You cannot create a ServiceHandle for this type.\n"
51  " Please contact artists@fnal.gov for guidance.\n");
52 
54  &ServiceRegistry::instance().get<std::remove_const_t<T>>()
55  }
56  {}
57  catch (art::Exception const& x)
58  {
60  << "Unable to create ServiceHandle.\n"
61  << "Perhaps the FHiCL configuration does not specify the necessary "
62  "service?\n"
63  << "The class of the service is noted below...\n"
64  << x;
65  }
66 
67  T* operator->() const { return instance; }
68  T& operator*() const { return *instance; }
69  T*
70  get() const
71  {
72  return instance;
73  }
74 
75 private:
77 };
78 
79 // Per-schedule template. SFINAE wouldn't work here.
80 template <typename T>
82 public:
83  static_assert(
84  detail::handle_allowed_v<T>,
85  "\n\nart-error: You cannot create a ServiceHandle for this type.\n"
86  " Please contact artists@fnal.gov for guidance.\n");
87 
88  explicit ServiceHandle(ScheduleID const sID) try : instance {
89  &ServiceRegistry::instance().get<std::remove_const_t<T>>(sID)
90  }
91  {}
92  catch (art::Exception const& x)
93  {
95  << "Unable to create ServiceHandle.\n"
96  << "Perhaps the FHiCL configuration does not specify the necessary "
97  "service?\n"
98  << "The class of the service is noted below...\n"
99  << x;
100  }
101 
102  T* operator->() const { return instance; }
103  T& operator*() const { return *instance; }
104  T*
105  get() const
106  {
107  return instance;
108  }
109 
110 private:
112 };
113 
114 // ======================================================================
115 
116 #endif /* art_Framework_Services_Registry_ServiceHandle_h */
117 
118 // Local Variables:
119 // mode: c++
120 // End:
ServiceScope
Definition: ServiceScope.h:5
T & operator*() const
Definition: ServiceHandle.h:68
static ServiceRegistry & instance()
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:67