| # 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
ToolHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TOOLHANDLE_H
2 #define GAUDIKERNEL_TOOLHANDLE_H
3
4 //Includes
5 #include "GaudiKernel/GaudiHandle.h"
6 #include "GaudiKernel/ServiceHandle.h"
7 #include "GaudiKernel/IToolSvc.h"
8 #include "GaudiKernel/INamedInterface.h"
9 //#include "GaudiKernel/SmartIF.h"
10
11 #include <string>
12 #include <vector>
13 #include <stdexcept>
14
15 // forward declarations
16 class IInterface;
17 class IAlgTool;
18 class IToolSvc;
19
21 class ToolHandleInfo {
22 protected:
23 ToolHandleInfo( const IInterface* parent = 0, bool createIf = true )
24 : m_parent(parent), m_createIf(createIf)
25 {}
26
27 public:
28 virtual ~ToolHandleInfo() {};
29
30 bool isPublic() const {
31 return !m_parent;
32 }
33
34 bool createIf() const {
35 return m_createIf;
36 }
37
38 const IInterface* parent() const {
39 return m_parent;
40 }
41
42 //
43 // Some helper functions
44 //
45 const std::string toolComponentType( const IInterface* parent ) const {
46 return parent ? "PrivateTool" : "PublicTool";
47 }
48
49 const std::string toolParentName( const IInterface* parent ) const {
50 if (parent) {
51 //SmartIF<INamedInterface> pNamed(const_cast<IInterface*>(parent));
52 //if (pNamed.isValid()) {
53 const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
54 if (pNamed) {
55 return pNamed->name();
56 } else {
57 return "";
58 }
59 } else {
60 return "ToolSvc";
61 }
62 }
63
64 private:
65 const IInterface* m_parent;
66 bool m_createIf;
67 };
68
79 template< class T >
80 class ToolHandle : public ToolHandleInfo, public GaudiHandle<T> {
81 public:
82 //
83 // Constructors etc.
84 //
88 ToolHandle( const IInterface* parent = 0, bool createIf = true )
89 : ToolHandleInfo(parent,createIf),
90 GaudiHandle<T>( GaudiHandle<T>::getDefaultType(),
91 ToolHandleInfo::toolComponentType(parent),
92 ToolHandleInfo::toolParentName(parent) ),
93 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
94 {}
95
113 ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = 0, bool createIf = true )
114 : ToolHandleInfo(parent,createIf),
115 GaudiHandle<T>( toolTypeAndName,
116 ToolHandleInfo::toolComponentType(parent),
117 ToolHandleInfo::toolParentName(parent) ),
118 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
119 {}
120
123 StatusCode retrieve() const { // not really const, because it updates m_pObject
124 return GaudiHandle<T>::retrieve();
125 }
126
129 StatusCode release() const { // not really const, because it updates m_pObject
130 return GaudiHandle<T>::release();
131 }
132
134 virtual StatusCode retrieve( T*& algTool ) const {
135 return m_pToolSvc->retrieve( GaudiHandleBase::typeAndName(), T::interfaceID(),
136 (IAlgTool*&)(algTool),
137 ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
138 }
139
141 virtual StatusCode release( T* algTool ) const {
142 return m_pToolSvc->releaseTool( algTool );
143 }
144
145 private:
146 //
147 // Private data members
148 //
149 mutable ServiceHandle<IToolSvc> m_pToolSvc;
150 };
151
162 template < class T >
163 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
164 public:
165 //
166 // Constructors
167 //
174 ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
175 const IInterface* parent = 0, bool createIf = true )
176 : ToolHandleInfo( parent, createIf ),
177 GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
178 ToolHandleInfo::toolComponentType(parent),
179 ToolHandleInfo::toolParentName(parent) )
180 {}
181
186 ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
187 : ToolHandleInfo( parent, createIf ),
188 GaudiHandleArray< ToolHandle<T> >( ToolHandleInfo::toolComponentType(parent),
189 ToolHandleInfo::toolParentName(parent) )
190 { }
191
196 virtual bool push_back( const std::string& toolTypeAndName ) {
197 ToolHandle<T> handle( toolTypeAndName,
198 ToolHandleInfo::parent(),
199 ToolHandleInfo::createIf() );
200 GaudiHandleArray< ToolHandle<T> >::push_back( handle );
201 return true;
202 }
203
205 virtual bool push_back( const ToolHandle<T>& myHandle ) {
206 return push_back( myHandle.typeAndName() );
207 }
208
209 };
210
211
212 template <class T>
213 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
214 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
215 }
216
217
218 template <class T>
219 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
220 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
221 }
222
223
224 #endif // ! GAUDIKERNEL_TOOLHANDLE_H