Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
garsoft
CoreUtils
ServiceUtil.h
Go to the documentation of this file.
1
/**
2
* @file ServiceUtil.h
3
* @brief Utilities related to art service access
4
* @author Jonathan Paley (jpaley@fnal.gov)
5
*
6
* This library is currently a pure header.
7
*
8
*/
9
10
#ifndef COREUTILS_SERVICEUTIL
11
#define COREUTILS_SERVICEUTIL
12
13
// GArSoft libraries
14
#include "CoreUtils/UncopiableAndUnmovableClass.h"
15
16
// framework libraries
17
#include "
art/Framework/Services/Registry/ServiceHandle.h
"
18
#include "
canvas/Utilities/Exception.h
"
19
#include "cetlib_except/demangle.h"
20
21
// C/C++ standard libraries
22
#include <type_traits>
23
#include <typeinfo>
24
25
26
namespace
gar
{
27
28
namespace
details
{
29
template
<
typename
PROVIDER>
30
struct
ServiceRequirementsChecker
;
31
}
// namespace details
32
33
34
/** **************************************************************************
35
* @brief Returns a constant pointer to the provider of specified service
36
* @tparam T type of the service
37
* @return a constant pointer to the provider of specified service
38
* @throws ::art::Exception (category ::art::errors::NotFound) if pointer is null
39
*
40
* This function relies on the following service and provider interface:
41
* - provider is not movable nor copiable
42
* - service contains a type "provider_type" defined as the class of the
43
* service provider
44
* - service contains a method "provider()" that returns a non-null pointer
45
* to a service provider; the service provider is owned and managed by
46
* the service, and the caller is not responsible of regulating the object
47
* lifetime, nor it should attempt to
48
*
49
* Violations of the protocol yield compilation errors (in case non-compliance
50
* can be statically detected), or throw of exceptions.
51
*
52
* Example of usage:
53
*
54
* auto const* geom = gar::providerFrom<geo::GeometryGAr>();
55
*
56
* retrieves the service provider for GArSoft geometry.
57
* This requires the inclusion of "Geometry/GeometryGAr.h" header, where the
58
* service is declared. Typically, both ServiceUtil.h and the header of the
59
* provider class are included in the service header.
60
*
61
*/
62
template
<
class
T>
63
typename
T::provider_type
const
*
providerFrom
()
64
{
65
details::ServiceRequirementsChecker<T>
();
// instantiate a temporary...
66
67
// retrieve the provider
68
::art::ServiceHandle<T>
h
;
69
typename
T::provider_type
const
* pProvider = h->provider();
70
if
(!pProvider) {
71
throw ::art::Exception
(::
art::errors::NotFound
)
72
<<
"Service <"
<< cet::demangle_symbol(
typeid
(
T
).
name
())
73
<<
"> offered a null provider"
;
74
}
75
76
return
pProvider;
77
78
}
// providerFrom()
79
80
81
//----------------------------------------------------------------------------
82
namespace
details
{
83
/// Compiles only if PROVIDER class satisfied service provider requirements
84
template
<
typename
PROVIDER>
85
struct
ServiceProviderRequirementsChecker
{
86
87
using
provider_type
= PROVIDER;
88
89
// static checks on provider class: not copiable nor movable
90
static_assert(
91
!
std::is_copy_constructible<provider_type>::value
,
92
"Service provider classes must not be copiable"
93
);
94
static_assert(
95
!
std::is_copy_assignable<provider_type>::value
,
96
"Service provider classes must not be copiable"
97
);
98
static_assert(
99
!
std::is_move_constructible<provider_type>::value
,
100
"Service provider classes must not be movable"
101
);
102
static_assert(
103
!
std::is_move_assignable<provider_type>::value
,
104
"Service provider classes must not be movable"
105
);
106
107
};
// ServiceProviderRequirementsChecker
108
109
110
template
<
typename
SERVICE>
111
struct
ServiceRequirementsChecker
{
112
113
// require SERVICE::provider_type to be a type
114
using
provider_type
=
typename
SERVICE::provider_type;
115
116
// expected type for SERVICE::provider() method
117
using
provider_func_type
=
provider_type
const
* (SERVICE::*)()
const
;
118
119
/// Checker for the provider
120
ServiceProviderRequirementsChecker<provider_type>
provider_checker
;
121
122
// check the provider() method
123
static_assert(
124
std::is_same<decltype(&
SERVICE::provider
),
provider_func_type
>::
value
,
125
"provider() method has unsupported signature"
126
);
127
128
};
// ServiceRequirementsChecker
129
130
}
// namespace details
131
132
}
// namespace gar
133
134
#endif //#COREUTILS_SERVICEUTIL
135
name
static QCString name
Definition:
declinfo.cpp:673
art::errors::NotFound
Definition:
Exception.h:52
gar::details::ServiceRequirementsChecker::provider_func_type
provider_type const *(SERVICE::*)() const provider_func_type
Definition:
ServiceUtil.h:117
art::ServiceHandle
Definition:
ServiceHandle.h:37
gar::details::ServiceProviderRequirementsChecker
Compiles only if PROVIDER class satisfied service provider requirements.
Definition:
ServiceUtil.h:85
RunHistoryService::provider
virtual const provider_type * provider() const override
Definition:
RunHistoryServiceStandard.h:30
ValidateOpDetSimulation.T
T
Definition:
ValidateOpDetSimulation.py:52
Exception.h
ServiceHandle.h
gar::details::ServiceProviderRequirementsChecker< provider_type >::provider_type
provider_type provider_type
Definition:
ServiceUtil.h:87
reco_momentum_tuples.h
h
Definition:
reco_momentum_tuples.py:66
details
Definition:
RawDataDrawer.cxx:105
gar::details::ServiceRequirementsChecker::provider_checker
ServiceProviderRequirementsChecker< provider_type > provider_checker
Checker for the provider.
Definition:
ServiceUtil.h:120
art::Exception
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition:
Exception.h:66
gar
General GArSoft Utilities.
Definition:
anatest_module.cc:57
gar::providerFrom
T::provider_type const * providerFrom()
Returns a constant pointer to the provider of specified service.
Definition:
ServiceUtil.h:63
gar::details::ServiceRequirementsChecker::provider_type
typename SERVICE::provider_type provider_type
Definition:
ServiceUtil.h:114
submit_mcc.value
value
Definition:
submit_mcc.py:159
gar::details::ServiceRequirementsChecker
Definition:
ServiceUtil.h:30
Generated by
1.8.11