| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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/ISvcLocator.h"
10 #include "GaudiKernel/MsgStream.h"
11 #include "GaudiKernel/ServiceLocatorHelper.h"
12
13 #include <stdexcept>
14 #include <string>
15 #include <type_traits>
16
17 // class predeclarations
18 class IAlgTool;
19 class IToolSvc;
20 class ServiceHandleProperty;
21
29 template <class T>
30 class ServiceHandle : public GaudiHandle<T>
31 {
32 public:
33 //
34 // Constructors etc.
35 //
43 ServiceHandle( const std::string& serviceName, const std::string& theParentName )
44 : GaudiHandle<T>( serviceName, "Service", theParentName )
45 {
46 }
47
49 template <typename CT = T, typename NCT = typename std::remove_const<T>::type>
50 ServiceHandle( const ServiceHandle<NCT>& other,
51 typename std::enable_if<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>::type* = nullptr )
52 : GaudiHandle<CT>( other )
53 {
54 }
55
58 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
59 inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
60 : ServiceHandle( svcName, owner->name() )
61 {
62 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
63 p->template setOwnerType<OWNER>();
64 }
65
66 StatusCode initialize( const std::string& serviceName, const std::string& theParentName )
67 {
68
69 GaudiHandleBase::setTypeAndName( serviceName );
70 GaudiHandleBase::setParentName( theParentName );
71
72 return StatusCode::SUCCESS;
73 }
74
77 StatusCode retrieve() const
78 { // not really const, because it updates m_pObject
79 return GaudiHandle<T>::retrieve();
80 }
81
82 // /** Release the Service.
83 // Function must be repeated here to avoid hiding the function release( T*& ) */
84 // StatusCode release() const { // not really const, because it updates m_pObject
85 // return GaudiHandle<T>::release();
86 // }
87
89 T* get() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::get() ); }
90
92 T* operator->() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() ); }
93 T& operator*() const { return *GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() ); }
94
95 protected:
97 StatusCode retrieve( T*& service ) const override
98 {
99 const ServiceLocatorHelper helper( *serviceLocator(), GaudiHandleBase::messageName(), this->parentName() );
100 return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
101 }
102
103 // /** Do the real release of the Service */
104 // virtual StatusCode release( T* service ) const {
105 // return service->release();
106 // }
107
108 private:
109 //
110 // Private helper functions
111 //
112 SmartIF<ISvcLocator>& serviceLocator() const
113 { // not really const, because it may change m_pSvcLocator
114 if ( !m_pSvcLocator ) {
115 m_pSvcLocator = Gaudi::svcLocator();
116 if ( !m_pSvcLocator ) {
117 throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
118 }
119 }
120 return m_pSvcLocator;
121 }
122
123 SmartIF<IMessageSvc>& messageSvc() const
124 { // not really const, because it may change m_pMessageSvc
125 if ( !m_pMessageSvc ) {
126 m_pMessageSvc = serviceLocator(); // default message service
127 if ( !m_pMessageSvc ) {
128 throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
129 }
130 }
131 return m_pMessageSvc;
132 }
133 //
134 // private data members
135 //
136 mutable SmartIF<ISvcLocator> m_pSvcLocator;
137 mutable SmartIF<IMessageSvc> m_pMessageSvc;
138 };
139
150 template <class T>
151 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>>
152 {
153 public:
154 //
155 // Constructors
156 //
159 ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
160 const std::string& myParentName )
161 : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName )
162 {
163 }
164
165 virtual ~ServiceHandleArray() {}
166
167 ServiceHandleArray( const std::string& myParentName ) : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName )
168 {
169 }
170
171 virtual bool push_back( const std::string& serviceTypeAndName )
172 {
173 ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
174 GaudiHandleArray<ServiceHandle<T>>::push_back( handle );
175 return true;
176 }
177
178 virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
179 };
180
181 template <class T>
182 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle )
183 {
184 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
185 }
186
187 template <class T>
188 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle )
189 {
190 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
191 }
192
193 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
ServiceHandle
Handle to be used in lieu of naked pointers to services.
Definition: PropertyHolder.h:32
ServiceHandle::m_pSvcLocator
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:136
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:59
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes ...
Definition: ServiceLocatorHelper.h:17
StatusCode::SUCCESS
Definition: StatusCode.h:31
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:157
GaudiHandleBase::messageName
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:44
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:22
GaudiHandle::nonConst
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:309
ServiceHandleArray::~ServiceHandleArray
virtual ~ServiceHandleArray()
Definition: ServiceHandle.h:165
StatusCode::FAILURE
Definition: StatusCode.h:31
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
MsgStream.h
ServiceHandle::m_pMessageSvc
SmartIF< IMessageSvc > m_pMessageSvc
Definition: ServiceHandle.h:137
ServiceHandle::operator*
T & operator*() const
Definition: ServiceHandle.h:93
std::is_same
ServiceHandle::messageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:123
Bootstrap.h
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:47
ServiceHandle::operator->
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:92
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:65
Gaudi::Functional::details::details2::push_back
void push_back(Container &c, const Value &v, std::true_type)
Definition: FunctionalDetails.h:217
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:112
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:66
std::enable_if
std::string
STL class.
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
GaudiException.h
gaudirun.type
type
Definition: gaudirun.py:133
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:213
ServiceHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyHolder.h:36
ServiceHandle::retrieve
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:77
IMessageSvc.h
std::move
T move(T...args)
std::is_const
ServiceHandleArray::push_back
virtual bool push_back(const ServiceHandle< T > &myHandle)
Definition: ServiceHandle.h:178
std::vector< std::string >
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
GaudiHandleBase::type
std::string type() const
The concrete component class name: the part before the &#39;/&#39;.
Definition: GaudiHandle.cpp:11
ServiceLocatorHelper.h
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:171
GaudiHandleBase::name
std::string name() const
The instance name: the part after the &#39;/&#39;.
Definition: GaudiHandle.cpp:23
ServiceHandle::ServiceHandle
ServiceHandle(const ServiceHandle< NCT > &other, typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type *=nullptr)
Copy constructor from a non const T to const T service handle.
Definition: ServiceHandle.h:50
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:111
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle (&#39;smart pointer&#39;) to a service.
Definition: ServiceHandle.h:43
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:97
std::ostream
STL class.
ISvcLocator.h
GaudiHandle.h
ServiceLocatorHelper::getService
StatusCode getService(const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:50
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: ServiceHandle.h:159
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:167
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:413