LArSoft
v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
25
#include "
art/Framework/Services/Registry/ServiceRegistry.h
"
26
#include "
art/Framework/Services/Registry/ServiceScope.h
"
27
#include "
art/Framework/Services/Registry/detail/ServiceHandleAllowed.h
"
28
#include "
art/Framework/Services/Registry/detail/ServiceHelper.h
"
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
44
ServiceHandle
()
45
try
:
instance
{&
ServiceRegistry::instance
().
get
<std::remove_const_t<T>>()} {
46
}
47
catch
(
Exception
const
&
x
) {
48
throw
Exception
(
errors::ServiceNotFound
)
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
:
75
T*
instance
;
76
};
77
78
}
// namespace art
79
80
#endif
/* art_Framework_Services_Registry_ServiceHandle_h */
81
82
// Local Variables:
83
// mode: c++
84
// End:
x
Float_t x
Definition:
compare.C:6
art::ServiceHandle
Definition:
ServiceHandle.h:37
art::ServiceScope
ServiceScope
Definition:
ServiceScope.h:7
ServiceRegistry.h
ServiceHandleAllowed.h
ServiceScope.h
ServiceHelper.h
art::ServiceRegistry::instance
static ServiceRegistry & instance() noexcept
Definition:
ServiceRegistry.cc:10
art::ServiceHandle::instance
T * instance
Definition:
ServiceHandle.h:75
art::errors::ServiceNotFound
Definition:
Exception.h:53
art::ServiceHandle::ServiceHandle
ServiceHandle()
Definition:
ServiceHandle.h:44
art::ServiceHandle::operator*
T & operator*() const
Definition:
ServiceHandle.h:63
art::Exception
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition:
Exception.h:66
art::ServiceRegistry::get
T & get() const
Definition:
ServiceRegistry.h:46
art
Definition:
MVAAlg.h:12
art::ServiceHandle::operator->
T * operator->() const
Definition:
ServiceHandle.h:57
art
Framework
Services
Registry
ServiceHandle.h
Generated on Thu May 2 2024 20:59:30 for LArSoft by
1.8.11