LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 // 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  try : instance{&ServiceRegistry::instance().get<std::remove_const_t<T>>()} {
46  }
47  catch (Exception const& x) {
49  << "Unable to create ServiceHandle.\n"
50  << "Perhaps the FHiCL configuration does not specify the necessary "
51  "service?\n"
52  << "The class of the service is noted below...\n"
53  << x;
54  }
55 
56  T*
57  operator->() const
58  {
59  return instance;
60  }
61 
62  T&
63  operator*() const
64  {
65  return *instance;
66  }
67 
68  T*
69  get() const
70  {
71  return instance;
72  }
73 
74  private:
76  };
77 
78 } // namespace art
79 
80 #endif /* art_Framework_Services_Registry_ServiceHandle_h */
81 
82 // Local Variables:
83 // mode: c++
84 // End:
Float_t x
Definition: compare.C:6
ServiceScope
Definition: ServiceScope.h:7
static ServiceRegistry & instance() noexcept
T & operator*() const
Definition: ServiceHandle.h:63
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Definition: MVAAlg.h:12
T * operator->() const
Definition: ServiceHandle.h:57