ServiceHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
2 #define GAUDIKERNEL_SERVICEHANDLE_H
3 
4 //Includes
10 #include "GaudiKernel/MsgStream.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 
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 {
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 ) {
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",
98  }
99  }
100  return m_pMessageSvc;
101  }
102  //
103  // private data members
104  //
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());
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
Handle to be used in lieu of naked pointers to services.
Definition: PropertyMgr.h:23
StatusCode retrieve(T *&service) const
Release the Service.
Definition: ServiceHandle.h:68
SmartIF< ISvcLocator > m_pSvcLocator
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
an helper to share the implementation of service() among the various kernel base classes ...
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:171
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:48
Define general base for Gaudi exception.
virtual ~ServiceHandleArray()
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
SmartIF< IMessageSvc > m_pMessageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:92
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:49
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:72
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:82
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:46
STL class.
GAUDI_API ISvcLocator * svcLocator()
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:205
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:25
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:56
virtual bool push_back(const ServiceHandle< T > &myHandle)
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle to the array with "type/name" given in .
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:120
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:42
STL class.
StatusCode getService(const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
ServiceHandleArray(const std::string &myParentName)
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:384