All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
48  StatusCode retrieve() const { // not really const, because it updates m_pObject
49  return GaudiHandle<T>::retrieve();
50  }
51 
52 // /** Release the Service.
53 // Function must be repeated here to avoid hiding the function release( T*& ) */
54 // StatusCode release() const { // not really const, because it updates m_pObject
55 // return GaudiHandle<T>::release();
56 // }
57 
58 protected:
60  StatusCode retrieve( T*& service ) const {
62  return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
63  }
64 
65 // /** Do the real release of the Service */
66 // virtual StatusCode release( T* service ) const {
67 // return service->release();
68 // }
69 
70 private:
71  //
72  // Private helper functions
73  //
74  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
75  if ( !m_pSvcLocator.isValid() ) {
77  if ( !m_pSvcLocator.isValid() ) {
78  throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
79  }
80  }
81  return m_pSvcLocator;
82  }
83 
84  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
85  if ( !m_pMessageSvc.isValid() ) {
86  m_pMessageSvc = serviceLocator(); // default message service
87  if( !m_pMessageSvc.isValid() ) {
88  throw GaudiException("Service [MessageSvc] not found",
90  }
91  }
92  return m_pMessageSvc;
93  }
94  //
95  // private data members
96  //
99 };
100 
111 template < class T >
112 class ServiceHandleArray : public GaudiHandleArray< ServiceHandle<T> > {
113 public:
114  //
115  // Constructors
116  //
119  ServiceHandleArray( const std::vector< std::string >& myTypesAndNamesList,
120  const std::string& myComponentType, const std::string& myParentName ):
121  GaudiHandleArray< ServiceHandle<T> >( myTypesAndNamesList,
122  myComponentType,
123  myParentName)
124  { }
125 
126  virtual ~ServiceHandleArray() {}
127 
128  ServiceHandleArray( const std::string& myParentName )
129  : GaudiHandleArray< ServiceHandle<T> >( "Service", myParentName)
130  { }
131 
132  virtual bool push_back( const std::string& serviceTypeAndName ) {
133  ServiceHandle<T> handle( serviceTypeAndName,GaudiHandleInfo::parentName());
135  return true;
136  }
137 
138  virtual bool push_back( const ServiceHandle<T>& myHandle ) {
139  return push_back( myHandle.typeAndName() );
140  }
141 
142 };
143 
144 template <class T>
145 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
146  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
147 }
148 
149 template <class T>
150 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
151  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
152 }
153 
154 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
Handle to be used in lieu of naked pointers to services.
Definition: PropertyMgr.h:25
StatusCode retrieve(T *&service) const
Release the Service.
Definition: ServiceHandle.h:60
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:97
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:17
an helper to share the implementation of service() among the various kernel base classes $Id: ...
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:158
Define general base for Gaudi exception.
virtual ~ServiceHandleArray()
const std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:48
SmartIF< IMessageSvc > m_pMessageSvc
Definition: ServiceHandle.h:98
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:84
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:48
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:74
GAUDI_API ISvcLocator * svcLocator()
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:188
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:27
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:48
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:107
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:42
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:353