| The Gaudi Framework v32r1 (f65d50dc)

ServiceHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
2 #define GAUDIKERNEL_SERVICEHANDLE_H
3
4 // Includes
5 #include "GaudiKernel/Bootstrap.h"
6 #include "GaudiKernel/GaudiException.h"
7 #include "GaudiKernel/GaudiHandle.h"
8 #include "GaudiKernel/IMessageSvc.h"
9 #include "GaudiKernel/IProperty.h"
10 #include "GaudiKernel/ISvcLocator.h"
11 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/ServiceLocatorHelper.h"
13
14 #include <stdexcept>
15 #include <string>
16 #include <type_traits>
17
18 // class predeclarations
19 class IAlgTool;
20 class IToolSvc;
21 class ServiceHandleProperty;
22
30 template <class T>
31 class ServiceHandle : public GaudiHandle<T> {
32 public:
33 //
34 // Constructors etc.
35 //
43 ServiceHandle( const std::string& serviceName, const std::string& theParentName )
44 : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
45
47 template <typename CT = T, typename NCT = std::remove_const_t<T>,
48 typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
49 ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
50
53 template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
54 inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
55 : ServiceHandle( svcName, owner->name() ) {
56 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
57 p->template setOwnerType<OWNER>();
58 }
59
60 StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
61
62 GaudiHandleBase::setTypeAndName( serviceName );
63 GaudiHandleBase::setParentName( theParentName );
64
65 return StatusCode::SUCCESS;
66 }
67
70 StatusCode retrieve() const { // not really const, because it updates m_pObject
71 return GaudiHandle<T>::retrieve();
72 }
73
74 // /** Release the Service.
75 // Function must be repeated here to avoid hiding the function release( T*& ) */
76 // StatusCode release() const { // not really const, because it updates m_pObject
77 // return GaudiHandle<T>::release();
78 // }
79
81 T* get() const { return ::details::nonConst( GaudiHandle<T>::get() ); }
82
84 T* operator->() const { return ::details::nonConst( GaudiHandle<T>::operator->() ); }
85 T& operator*() const { return *::details::nonConst( GaudiHandle<T>::operator->() ); }
86
87 protected:
89 StatusCode retrieve( T*& service ) const override {
90 const ServiceLocatorHelper helper( *serviceLocator(), GaudiHandleBase::messageName(), this->parentName() );
91 return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
92 }
93
94 // /** Do the real release of the Service */
95 // virtual StatusCode release( T* service ) const {
96 // return service->release();
97 // }
98
99 private:
100 //
101 // Private helper functions
102 //
103 SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
104 if ( !m_pSvcLocator ) {
105 m_pSvcLocator = Gaudi::svcLocator();
106 if ( !m_pSvcLocator ) {
107 throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
108 }
109 }
110 return m_pSvcLocator;
111 }
112
113 SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
114 if ( !m_pMessageSvc ) {
115 m_pMessageSvc = serviceLocator(); // default message service
116 if ( !m_pMessageSvc ) {
117 throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
118 }
119 }
120 return m_pMessageSvc;
121 }
122 //
123 // private data members
124 //
125 mutable SmartIF<ISvcLocator> m_pSvcLocator;
126 mutable SmartIF<IMessageSvc> m_pMessageSvc;
127 };
128
139 template <class T>
140 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
141 public:
142 //
143 // Constructors
144 //
147 ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
148 const std::string& myParentName )
149 : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
150
151 virtual ~ServiceHandleArray() {}
152
153 ServiceHandleArray( const std::string& myParentName )
154 : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
155
156 virtual bool push_back( const std::string& serviceTypeAndName ) {
157 ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
158 GaudiHandleArray<ServiceHandle<T>>::push_back( handle );
159 return true;
160 }
161
162 virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
163 };
164
165 template <class T>
166 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
167 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
168 }
169
170 template <class T>
171 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
172 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
173 }
174
175 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
ServiceHandle
Handle to be used in lieu of naked pointers to services.
Definition: ServiceHandle.h:31
ServiceHandle::m_pSvcLocator
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:125
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
ServiceHandle::ServiceHandle
ServiceHandle(OWNER *owner, std::string PropName, std::string svcName, std::string doc="")
Autodeclaring constructor with property name, service type/name and documentation.
Definition: ServiceHandle.h:54
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:17
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
ServiceHandleArray::~ServiceHandleArray
virtual ~ServiceHandleArray()
Definition: ServiceHandle.h:151
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
MsgStream.h
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:115
ServiceLocatorHelper::getService
StatusCode getService(const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:41
ServiceHandle::m_pMessageSvc
SmartIF< IMessageSvc > m_pMessageSvc
Definition: ServiceHandle.h:126
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Bootstrap.h
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:71
ServiceHandle::operator->
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:84
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:60
std::string
STL class.
GaudiHandleBase::name
std::string name() const
The instance name: the part after the '/'.
Definition: GaudiHandle.cpp:22
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
details::nonConst
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
GaudiException.h
ServiceHandle::retrieve
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:70
ServiceHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ServiceHandle.h:140
IMessageSvc.h
std::move
T move(T... args)
ServiceHandleArray::push_back
virtual bool push_back(const ServiceHandle< T > &myHandle)
Definition: ServiceHandle.h:162
ServiceHandle::get
T * get() const
Release the Service.
Definition: ServiceHandle.h:81
GaudiHandleBase::messageName
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:42
IProperty.h
std::vector< std::string >
ServiceHandle::operator *
T & operator *() const
Definition: ServiceHandle.h:85
operator<<
std::ostream & operator<<(std::ostream &os, const ServiceHandle< T > &handle)
Definition: ServiceHandle.h:166
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
ServiceLocatorHelper.h
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:89
ServiceHandleArray::push_back
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
Definition: ServiceHandle.h:156
ServiceHandle::ServiceHandle
ServiceHandle(const ServiceHandle< NCT > &other)
Copy constructor from a non const T to const T service handle.
Definition: ServiceHandle.h:49
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:43
std::ostream
STL class.
ISvcLocator.h
GaudiHandle.h
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: ServiceHandle.h:147
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:153
ServiceHandle::messageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:113
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:103
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:393