| The Gaudi Framework v27r1

- 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
16 // class predeclarations
17 class IAlgTool;
18 class IToolSvc;
19 class ServiceHandleProperty;
20
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
46 StatusCode initialize(const std::string& serviceName, const std::string& theParentName){
47
48 GaudiHandleBase::setTypeAndName(serviceName);
49 GaudiHandleBase::setParentName(theParentName);
50
51 return StatusCode::SUCCESS;
52 }
53
56 StatusCode retrieve() const { // not really const, because it updates m_pObject
57 return GaudiHandle<T>::retrieve();
58 }
59
60 // /** Release the Service.
61 // Function must be repeated here to avoid hiding the function release( T*& ) */
62 // StatusCode release() const { // not really const, because it updates m_pObject
63 // return GaudiHandle<T>::release();
64 // }
65
66 protected:
68 StatusCode retrieve( T*& service ) const {
69 const ServiceLocatorHelper helper(*serviceLocator(), GaudiHandleBase::messageName(), this->parentName());
70 return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
71 }
72
73 // /** Do the real release of the Service */
74 // virtual StatusCode release( T* service ) const {
75 // return service->release();
76 // }
77
78 private:
79 //
80 // Private helper functions
81 //
82 SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
83 if ( !m_pSvcLocator ) {
84 m_pSvcLocator = Gaudi::svcLocator();
85 if ( !m_pSvcLocator ) {
86 throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
87 }
88 }
89 return m_pSvcLocator;
90 }
91
92 SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
93 if ( !m_pMessageSvc ) {
94 m_pMessageSvc = serviceLocator(); // default message service
95 if( !m_pMessageSvc ) {
96 throw GaudiException("Service [MessageSvc] not found",
97 this->parentName(), StatusCode::FAILURE);
98 }
99 }
100 return m_pMessageSvc;
101 }
102 //
103 // private data members
104 //
105 mutable SmartIF<ISvcLocator> m_pSvcLocator;
106 mutable SmartIF<IMessageSvc> m_pMessageSvc;
107 };
108
119 template < class T >
120 class ServiceHandleArray : public GaudiHandleArray< ServiceHandle<T> > {
121 public:
122 //
123 // Constructors
124 //
127 ServiceHandleArray( const std::vector< std::string >& myTypesAndNamesList,
128 const std::string& myComponentType, const std::string& myParentName ):
129 GaudiHandleArray< ServiceHandle<T> >( myTypesAndNamesList,
130 myComponentType,
131 myParentName)
132 { }
133
134 virtual ~ServiceHandleArray() {}
135
136 ServiceHandleArray( const std::string& myParentName )
137 : GaudiHandleArray< ServiceHandle<T> >( "Service", myParentName)
138 { }
139
140 virtual bool push_back( const std::string& serviceTypeAndName ) {
141 ServiceHandle<T> handle( serviceTypeAndName,GaudiHandleInfo::parentName());
142 GaudiHandleArray< ServiceHandle<T> >::push_back( handle );
143 return true;
144 }
145
146 virtual bool push_back( const ServiceHandle<T>& myHandle ) {
147 return push_back( myHandle.typeAndName() );
148 }
149
150 };
151
152 template <class T>
153 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
154 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
155 }
156
157 template <class T>
158 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
159 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
160 }
161
162 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
ServiceHandle
Handle to be used in lieu of naked pointers to services.
Definition: PropertyMgr.h:23
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const
Release the Service.
Definition: ServiceHandle.h:68
ServiceHandle::m_pSvcLocator
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:105
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
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:171
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
ServiceHandleArray::~ServiceHandleArray
virtual ~ServiceHandleArray()
Definition: ServiceHandle.h:134
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:106
ServiceHandle::messageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:92
Bootstrap.h
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:49
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:72
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:82
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:46
std::string
STL class.
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
StatusCode::FAILURE
Definition: StatusCode.h:29
StatusCode::SUCCESS
Definition: StatusCode.h:30
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:205
ServiceHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:25
ServiceHandle::retrieve
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:56
IMessageSvc.h
ServiceHandleArray::push_back
virtual bool push_back(const ServiceHandle< T > &myHandle)
Definition: ServiceHandle.h:146
std::vector< std::string >
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
ServiceLocatorHelper.h
ServiceHandleArray::push_back
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle to the array with "type/name" given in .
Definition: ServiceHandle.h:140
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:120
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:42
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:127
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:136
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:384