LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
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  }
58  catch (art::Exception const& x)
59  {
61  << "Unable to create ServiceHandle.\n"
62  << "Perhaps the FHiCL configuration does not specify the necessary "
63  "service?\n"
64  << "The class of the service is noted below...\n"
65  << x;
66  }
67 
68  T* operator->() const { return instance; }
69  T& operator*() const { return *instance; }
70  T*
71  get() const
72  {
73  return instance;
74  }
75 
76 private:
78 };
79 
80 // Per-schedule template. SFINAE wouldn't work here.
81 template <typename T>
83 public:
84  static_assert(
85  detail::handle_allowed_v<T>,
86  "\n\nart-error: You cannot create a ServiceHandle for this type.\n"
87  " Please contact artists@fnal.gov for guidance.\n");
88 
89  explicit ServiceHandle(ScheduleID const sID) try : instance {
90  &ServiceRegistry::instance().get<std::remove_const_t<T>>(sID)
91  }
92  {
93  }
94  catch (art::Exception const& x)
95  {
97  << "Unable to create ServiceHandle.\n"
98  << "Perhaps the FHiCL configuration does not specify the necessary "
99  "service?\n"
100  << "The class of the service is noted below...\n"
101  << x;
102  }
103 
104  T* operator->() const { return instance; }
105  T& operator*() const { return *instance; }
106  T*
107  get() const
108  {
109  return instance;
110  }
111 
112 private:
114 };
115 
116 // ======================================================================
117 
118 #endif /* art_Framework_Services_Registry_ServiceHandle_h */
119 
120 // Local Variables:
121 // mode: c++
122 // End:
Float_t x
Definition: compare.C:6
ServiceScope
Definition: ServiceScope.h:5
static ServiceRegistry & instance()
T & operator*() const
Definition: ServiceHandle.h:69
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
HLT enums.
T * operator->() const
Definition: ServiceHandle.h:68