| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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/GaudiHandle.h"
6 #include "GaudiKernel/IMessageSvc.h"
7 #include "GaudiKernel/ISvcLocator.h"
8 #include "GaudiKernel/Bootstrap.h"
9 #include "GaudiKernel/GaudiException.h"
10 #include "GaudiKernel/MsgStream.h"
11 #include "GaudiKernel/ServiceLocatorHelper.h"
12
13 #include <string>
14 #include <stdexcept>
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 public:
32 //
33 // Constructors etc.
34 //
42 ServiceHandle( const std::string& serviceName, const std::string& theParentName )
43 : GaudiHandle<T>(serviceName, "Service", theParentName)
44 { }
45
47 template< typename CT = T,
48 typename NCT = typename std::remove_const<T>::type >
49 ServiceHandle( const ServiceHandle<NCT>& other,
50 typename std::enable_if< std::is_const<CT>::value &&
51 !std::is_same<CT,NCT>::value >::type * = nullptr )
52 : GaudiHandle<CT>( other )
53 { }
54
55 StatusCode initialize(const std::string& serviceName, const std::string& theParentName){
56
57 GaudiHandleBase::setTypeAndName(serviceName);
58 GaudiHandleBase::setParentName(theParentName);
59
60 return StatusCode::SUCCESS;
61 }
62
65 StatusCode retrieve() const { // not really const, because it updates m_pObject
66 return GaudiHandle<T>::retrieve();
67 }
68
69 // /** Release the Service.
70 // Function must be repeated here to avoid hiding the function release( T*& ) */
71 // StatusCode release() const { // not really const, because it updates m_pObject
72 // return GaudiHandle<T>::release();
73 // }
74
76 T * get() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::get() ); }
77
79 T* operator->() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() ); }
80 T& operator*() const { return * GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() ); }
81
82 protected:
84 StatusCode retrieve( T*& service ) const override {
85 const ServiceLocatorHelper helper(*serviceLocator(), GaudiHandleBase::messageName(), this->parentName());
86 return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
87 }
88
89 // /** Do the real release of the Service */
90 // virtual StatusCode release( T* service ) const {
91 // return service->release();
92 // }
93
94 private:
95 //
96 // Private helper functions
97 //
98 SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
99 if ( !m_pSvcLocator ) {
100 m_pSvcLocator = Gaudi::svcLocator();
101 if ( !m_pSvcLocator ) {
102 throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
103 }
104 }
105 return m_pSvcLocator;
106 }
107
108 SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
109 if ( !m_pMessageSvc ) {
110 m_pMessageSvc = serviceLocator(); // default message service
111 if( !m_pMessageSvc ) {
112 throw GaudiException("Service [MessageSvc] not found",
113 this->parentName(), StatusCode::FAILURE);
114 }
115 }
116 return m_pMessageSvc;
117 }
118 //
119 // private data members
120 //
121 mutable SmartIF<ISvcLocator> m_pSvcLocator;
122 mutable SmartIF<IMessageSvc> m_pMessageSvc;
123 };
124
135 template < class T >
136 class ServiceHandleArray : public GaudiHandleArray< ServiceHandle<T> > {
137 public:
138 //
139 // Constructors
140 //
143 ServiceHandleArray( const std::vector< std::string >& myTypesAndNamesList,
144 const std::string& myComponentType, const std::string& myParentName ):
145 GaudiHandleArray< ServiceHandle<T> >( myTypesAndNamesList,
146 myComponentType,
147 myParentName)
148 { }
149
150 virtual ~ServiceHandleArray() {}
151
152 ServiceHandleArray( const std::string& myParentName )
153 : GaudiHandleArray< ServiceHandle<T> >( "Service", myParentName)
154 { }
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 ) {
163 return push_back( myHandle.typeAndName() );
164 }
165
166 };
167
168 template <class T>
169 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
170 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
171 }
172
173 template <class T>
174 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
175 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
176 }
177
178 #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:121
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes ...
Definition: ServiceLocatorHelper.h:17
StatusCode::SUCCESS
Definition: StatusCode.h:30
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:172
GaudiHandleBase::messageName
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:48
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
GaudiHandle::nonConst
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:321
ServiceHandleArray::~ServiceHandleArray
virtual ~ServiceHandleArray()
Definition: ServiceHandle.h:150
StatusCode::FAILURE
Definition: StatusCode.h:29
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:122
ServiceHandle::operator*
T & operator*() const
Definition: ServiceHandle.h:80
std::is_same
ServiceHandle::messageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:108
Bootstrap.h
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:50
ServiceHandle::operator->
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:79
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:73
Gaudi::Functional::details::details2::push_back
void push_back(Container &c, const Value &v, std::true_type)
Definition: FunctionalDetails.h:154
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:98
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:55
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:26
GaudiException.h
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:228
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:65
IMessageSvc.h
std::is_const
ServiceHandleArray::push_back
virtual bool push_back(const ServiceHandle< T > &myHandle)
Definition: ServiceHandle.h:162
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:13
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:156
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:49
std::remove_const
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:121
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle (&#39;smart pointer&#39;) to a service.
Definition: ServiceHandle.h:42
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:84
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:43
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: ServiceHandle.h:143
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:152
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:428