| The Gaudi Framework v27r1

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

- File List
- File Members

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/IAlgTool.h"
10
11 #include <string>
12 #include <vector>
13 #include <stdexcept>
14 #include <type_traits>
15
16 // forward declarations
17 class IInterface;
18 class IToolSvc;
19
20 class Algorithm;
21 class AlgTool;
22 class Service;
23
25 class ToolHandleInfo {
26 protected:
27 ToolHandleInfo(const IInterface* parent = nullptr, bool createIf = true )
28 : m_parent(parent), m_createIf(createIf)
29 {}
30
31 public:
32 virtual ~ToolHandleInfo() = default;
33
34 bool isPublic() const { return !m_parent; }
35
36 bool createIf() const { return m_createIf; }
37
38 const IInterface* parent() const { return m_parent; }
39
40 //
41 // Some helper functions
42 //
43 static std::string toolComponentType(const IInterface* parent) {
44 return parent ? "PrivateTool" : "PublicTool";
45 }
46
47 static std::string toolParentName(const IInterface* parent) {
48 if (!parent) return "ToolSvc";
49 const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
50 return pNamed ? pNamed->name() : "";
51 }
52
53 protected:
54 const IInterface* m_parent;
55 bool m_createIf;
56
57 };
58
66 class BaseToolHandle: public ToolHandleInfo {
67
68 protected:
69 BaseToolHandle(const IInterface* parent = nullptr, bool createIf = true )
70 : ToolHandleInfo(parent, createIf)
71 {}
72
73 virtual StatusCode i_retrieve(IAlgTool*&) const = 0;
74
75 public:
76 virtual ~BaseToolHandle() {}
77
78 StatusCode retrieve(IAlgTool*& tool) const {
79 return i_retrieve(tool);
80 }
81
82 virtual IAlgTool* get() const = 0;
83 virtual std::string typeAndName() const = 0;
84 };
85
96 template< class T >
97 class ToolHandle : public BaseToolHandle, public GaudiHandle<T> {
98
99 friend class Algorithm;
100 friend class AlgTool;
101 friend class Service;
102
103 public:
107 ToolHandle(const IInterface* parent = nullptr, bool createIf = true)
108 : BaseToolHandle(parent,createIf),
109 GaudiHandle<T>( GaudiHandle<T>::getDefaultType(),
110 toolComponentType(parent),
111 toolParentName(parent) ),
112 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
113 { }
114
115 public:
116 //
117 // Constructors etc.
118 //
119
139 #if defined(TOOLHANDLE_DEPR_WARN)
140 //warn about using deprecated explicit ToolHandle construction
141 #pragma message("Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property")
142
143 __attribute__ ((deprecated))
144
145 #endif
146 ToolHandle(const std::string& toolTypeAndName,
147 const IInterface* parent = nullptr, bool createIf = true )
148 : BaseToolHandle(parent,createIf),
149 GaudiHandle<T>( toolTypeAndName,
150 toolComponentType(parent),
151 toolParentName(parent) ),
152 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
153 { }
154
155 public:
156
157 StatusCode initialize(const std::string& toolTypeAndName,
158 const IInterface* parent = nullptr, bool createIf = true){
159
160 GaudiHandleBase::setTypeAndName(toolTypeAndName);
161 GaudiHandleBase::setComponentType(toolComponentType(parent));
162 GaudiHandleBase::setParentName(toolParentName(parent));
163
164 m_parent = parent;
165 m_createIf = createIf;
166
167 StatusCode sc = m_pToolSvc.initialize("ToolSvc", GaudiHandleBase::parentName());
168
169 return sc;
170 }
171
174 StatusCode retrieve() const { // not really const, because it updates m_pObject
175 return GaudiHandle<T>::retrieve();
176 }
177
180 StatusCode release() const { // not really const, because it updates m_pObject
181 return GaudiHandle<T>::release();
182 }
183
185 StatusCode retrieve( T*& algTool ) const override {
186 IAlgTool* iface = nullptr;
187 algTool = i_retrieve(iface) ? dynamic_cast<T*>(iface) : nullptr;
188 return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
189 }
190
192 StatusCode release( T* algTool ) const override {
193 return m_pToolSvc->releaseTool( algTool );
194 }
195
196 IAlgTool *get() const override {
197 return GaudiHandle<T>::get();
198 }
199 std::string typeAndName() const override {
200 return GaudiHandleBase::typeAndName();
201 }
202
203 protected:
204 StatusCode i_retrieve(IAlgTool*& algTool) const override {
205 return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(),
206 algTool,
207 ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
208 }
209
210 private:
211 //
212 // Private data members
213 //
214 mutable ServiceHandle<IToolSvc> m_pToolSvc;
215 };
216
217 //-------------------------------------------------------------------------//
218
219
220
231 template < class T >
232 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
233 public:
234 //
235 // Constructors
236 //
243 ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
244 const IInterface* parent = 0, bool createIf = true )
245 : ToolHandleInfo( parent, createIf ),
246 GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
247 ToolHandleInfo::toolComponentType(parent),
248 ToolHandleInfo::toolParentName(parent) )
249 {}
250
255 ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
256 : ToolHandleInfo( parent, createIf ),
257 GaudiHandleArray< ToolHandle<T> >( ToolHandleInfo::toolComponentType(parent),
258 ToolHandleInfo::toolParentName(parent) )
259 { }
260
265 virtual bool push_back( const std::string& toolTypeAndName ) {
266 ToolHandle<T> handle( toolTypeAndName,
267 ToolHandleInfo::parent(),
268 ToolHandleInfo::createIf() );
269 GaudiHandleArray< ToolHandle<T> >::push_back( handle );
270 return true;
271 }
272
274 virtual bool push_back( const ToolHandle<T>& myHandle ) {
275 return push_back( myHandle.typeAndName() );
276 }
277
278 };
279
280
281 template <class T>
282 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
283 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
284 }
285
286
287 template <class T>
288 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
289 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
290 }
291
292
293 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:199
ServiceHandle< IToolSvc >
ToolHandle::ToolHandle
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:107
ToolHandleArray::push_back
virtual bool push_back(const ToolHandle< T > &myHandle)
Ensure that for added handles the parent and creatIf are taken from this array.
Definition: ToolHandle.h:274
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:171
__attribute__
#define __attribute__(x)
Definition: System.cpp:70
GaudiHandle::getDefaultType
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:266
ToolHandle::m_pToolSvc
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:214
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
BaseToolHandle::i_retrieve
virtual StatusCode i_retrieve(IAlgTool *&) const =0
ToolHandle::retrieve
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:185
GaudiHandleInfo::setComponentType
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:67
ToolHandleInfo::toolParentName
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:47
BaseToolHandle::typeAndName
virtual std::string typeAndName() const =0
BaseToolHandle
Non-templated base class for actual ToolHandle.
Definition: ToolHandle.h:66
ToolHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:24
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:49
BaseToolHandle::BaseToolHandle
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:69
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:72
ToolHandleInfo::toolComponentType
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:43
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:215
ToolHandle::i_retrieve
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:204
merge_files.sc
int sc
Definition: merge_files.py:167
IToolSvc.h
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:46
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
std::string
STL class.
INamedInterface.h
ToolHandle::release
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition: ToolHandle.h:192
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
ToolHandleInfo::parent
const IInterface * parent() const
Definition: ToolHandle.h:38
IInterface
Definition of the basic interface.
Definition: IInterface.h:234
IAlgTool.h
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:205
ServiceHandle::retrieve
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:56
BaseToolHandle::retrieve
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:78
ServiceHandle.h
ToolHandleInfo::createIf
bool createIf() const
Definition: ToolHandle.h:36
ToolHandle::retrieve
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:174
ToolHandleInfo::isPublic
bool isPublic() const
Definition: ToolHandle.h:34
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:22
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:157
Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:74
ToolHandleArray::ToolHandleArray
ToolHandleArray(const IInterface *parent=0, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:255
INamedInterface
IInterface compliant class extending IInterface with the name() method.
Definition: INamedInterface.h:15
ToolHandleInfo::m_parent
const IInterface * m_parent
Definition: ToolHandle.h:54
std::vector< std::string >
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:45
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
ToolHandleInfo::~ToolHandleInfo
virtual ~ToolHandleInfo()=default
ToolHandleInfo::m_createIf
bool m_createIf
Definition: ToolHandle.h:55
ToolHandleArray::push_back
virtual bool push_back(const std::string &toolTypeAndName)
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:265
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:93
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:120
ToolHandleArray::ToolHandleArray
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=0, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:243
Service
Base class for all services.
Definition: Service.h:36
std::ostream
STL class.
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:243
GaudiHandle.h
BaseToolHandle::~BaseToolHandle
virtual ~BaseToolHandle()
Definition: ToolHandle.h:76
ToolHandle::ToolHandle
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Create a handle ('smart pointer') to a tool.
Definition: ToolHandle.h:146
GaudiHandle::get
T * get() const
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:236
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
ToolHandle::release
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:180
ToolHandleInfo::ToolHandleInfo
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:27
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:384