| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

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
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
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 {
61 const ServiceLocatorHelper helper(*serviceLocator(), GaudiHandleBase::messageName(), this->parentName());
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() ) {
76 m_pSvcLocator = Gaudi::svcLocator();
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",
89 this->parentName(), StatusCode::FAILURE);
90 }
91 }
92 return m_pMessageSvc;
93 }
94 //
95 // private data members
96 //
97 mutable SmartIF<ISvcLocator> m_pSvcLocator;
98 mutable SmartIF<IMessageSvc> m_pMessageSvc;
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());
134 GaudiHandleArray< ServiceHandle<T> >::push_back( handle );
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