| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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 {
27
28 protected:
29
30 ToolHandleInfo(const IInterface* parent = nullptr, bool createIf = true )
31 : m_parent(parent), m_createIf(createIf)
32 {}
33
34 public:
35
36 virtual ~ToolHandleInfo() = default;
37
38 bool isPublic() const noexcept { return !m_parent; }
39
40 bool createIf() const noexcept { return m_createIf; }
41
42 const IInterface* parent() const noexcept { return m_parent; }
43
44 //
45 // Some helper functions
46 //
47
48 static std::string toolComponentType(const IInterface* parent)
49 {
50 return parent ? "PrivateTool" : "PublicTool";
51 }
52
53 static std::string toolParentName(const IInterface* parent)
54 {
55 auto * pNamed = ( parent ? dynamic_cast<const INamedInterface*>(parent) : nullptr );
56 return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
57 }
58
59 protected:
60
61 const IInterface* m_parent = nullptr;
62 bool m_createIf{true};
63
64 };
65
73 class BaseToolHandle: public ToolHandleInfo
74 {
75
76 protected:
77
78 BaseToolHandle(const IInterface* parent = nullptr, bool createIf = true )
79 : ToolHandleInfo(parent, createIf)
80 {}
81
82 virtual StatusCode i_retrieve(IAlgTool*&) const = 0;
83
84 public:
85
86 StatusCode retrieve(IAlgTool*& tool) const {
87 return i_retrieve(tool);
88 }
89
90 const IAlgTool * get() const { return getAsIAlgTool(); }
91
92 IAlgTool * get() { return getAsIAlgTool(); }
93
94 virtual std::string typeAndName() const = 0;
95
96 protected:
97
98 virtual const IAlgTool * getAsIAlgTool() const = 0;
99
100 virtual IAlgTool * getAsIAlgTool() = 0;
101
102 };
103
114 template< class T >
115 class ToolHandle : public BaseToolHandle, public GaudiHandle<T>
116 {
117
118 friend class Algorithm;
119 friend class AlgTool;
120 friend class Service;
121
122 public:
123
127 ToolHandle(const IInterface* parent = nullptr, bool createIf = true)
128 : BaseToolHandle(parent,createIf),
129 GaudiHandle<T>( GaudiHandle<T>::getDefaultType(),
130 toolComponentType(parent),
131 toolParentName(parent) ),
132 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
133 { }
134
136 template< typename CT = T,
137 typename NCT = typename std::remove_const<T>::type >
138 ToolHandle( const ToolHandle<NCT>& other,
139 typename std::enable_if< std::is_const<CT>::value &&
140 !std::is_same<CT,NCT>::value >::type * = nullptr )
141 : BaseToolHandle( other.parent(), other.createIf() ),
142 GaudiHandle<CT>( other ),
143 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
144 { }
145
146 public:
147 //
148 // Constructors etc.
149 //
150
170 #if defined(TOOLHANDLE_DEPR_WARN)
171 //warn about using deprecated explicit ToolHandle construction
172 #pragma message("Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property")
173
174 __attribute__ ((deprecated))
175
176 #endif
177 ToolHandle(const std::string& toolTypeAndName,
178 const IInterface* parent = nullptr, bool createIf = true )
179 : BaseToolHandle(parent,createIf),
180 GaudiHandle<T>( toolTypeAndName,
181 toolComponentType(parent),
182 toolParentName(parent) ),
183 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
184 { }
185
186 public:
187
188 StatusCode initialize(const std::string& toolTypeAndName,
189 const IInterface* parent = nullptr, bool createIf = true)
190 {
191
192 GaudiHandleBase::setTypeAndName(toolTypeAndName);
193 GaudiHandleBase::setComponentType(toolComponentType(parent));
194 GaudiHandleBase::setParentName(toolParentName(parent));
195
196 m_parent = parent;
197 m_createIf = createIf;
198
199 return m_pToolSvc.initialize("ToolSvc", GaudiHandleBase::parentName());
200 }
201
204 StatusCode retrieve() const { // not really const, because it updates m_pObject
205 return GaudiHandle<T>::retrieve();
206 }
207
210 StatusCode release() const { // not really const, because it updates m_pObject
211 return GaudiHandle<T>::release();
212 }
213
215 StatusCode retrieve( T*& algTool ) const override {
216 IAlgTool* iface = nullptr;
217 algTool = i_retrieve(iface) ? dynamic_cast<T*>(iface) : nullptr;
218 return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
219 }
220
222 StatusCode release( T* algTool ) const override
223 {
224 return m_pToolSvc->releaseTool( this->nonConst(algTool) );
225 }
226
227 std::string typeAndName() const override {
228 return GaudiHandleBase::typeAndName();
229 }
230
231 typename std::add_const<T>::type * get() const { return GaudiHandle<T>::get(); }
232
233 T * get() { return GaudiHandle<T>::get(); }
234
235 // Allow access to non-const Tool methods of const ToolHandle
236 #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
237 T * operator->() {
238 return GaudiHandle<T>::operator->();
239 }
240 T & operator*() {
241 return * ( GaudiHandle<T>::operator->() );
242 }
243
244 T * operator->() const {
245 return GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() );
246 }
247 T & operator*() const {
248 return * ( GaudiHandle<T>::nonConst(GaudiHandle<T>::operator->()) );
249 }
250 #endif
251
252 #ifdef ATLAS
253 [[deprecated("FIXME!! should not call non-const method from a const ToolHandle")]]
254 ToolHandle<T>& unConst() const {
255 return const_cast<ToolHandle<T>&> (*this);
256 }
257 #endif
258
259 protected:
260
261 const IAlgTool * getAsIAlgTool() const override
262 {
263 // const cast to support T being const
264 return GaudiHandle<T>::get();
265 }
266
267 IAlgTool * getAsIAlgTool() override
268 {
269 // const cast to support T being const
270 return this->nonConst( GaudiHandle<T>::get() );
271 }
272
273 StatusCode i_retrieve(IAlgTool*& algTool) const override
274 {
275 return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(),
276 algTool,
277 ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
278 }
279
280 private:
281 //
282 // Private data members
283 //
284 mutable ServiceHandle<IToolSvc> m_pToolSvc;
285 };
286
287 //-------------------------------------------------------------------------//
288
289
290
301 template < class T >
302 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
303 public:
304 //
305 // Constructors
306 //
313 ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
314 const IInterface* parent = 0, bool createIf = true )
315 : ToolHandleInfo( parent, createIf ),
316 GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
317 ToolHandleInfo::toolComponentType(parent),
318 ToolHandleInfo::toolParentName(parent) )
319 {}
320
325 ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
326 : ToolHandleInfo( parent, createIf ),
327 GaudiHandleArray< ToolHandle<T> >( ToolHandleInfo::toolComponentType(parent),
328 ToolHandleInfo::toolParentName(parent) )
329 { }
330
335 bool push_back( const std::string& toolTypeAndName ) override {
336 ToolHandle<T> handle( toolTypeAndName,
337 ToolHandleInfo::parent(),
338 ToolHandleInfo::createIf() );
339 GaudiHandleArray< ToolHandle<T> >::push_back( handle );
340 return true;
341 }
342
344 bool push_back( const ToolHandle<T>& myHandle ) override {
345 return push_back( myHandle.typeAndName() );
346 }
347
348 };
349
350
351 template <class T>
352 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
353 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
354 }
355
356
357 template <class T>
358 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
359 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
360 }
361
362 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
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:127
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
StatusCode::SUCCESS
Definition: StatusCode.h:30
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:172
__attribute__
#define __attribute__(x)
Definition: System.cpp:72
ToolHandleInfo::parent
const IInterface * parent() const noexcept
Definition: ToolHandle.h:42
GaudiHandle::nonConst
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:321
ToolHandle::m_pToolSvc
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:284
StatusCode::FAILURE
Definition: StatusCode.h:29
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
ToolHandle::release
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition: ToolHandle.h:222
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:38
GaudiHandleInfo::setComponentType
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:68
ToolHandleInfo::toolParentName
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:53
std::is_same
Gaudi::Functional::deprecated
struct deprecated("use MergingTransformer instead")]] ListTransformer
Definition: ListTransformer.h:9
BaseToolHandle
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:73
ToolHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyHolder.h:34
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:50
BaseToolHandle::BaseToolHandle
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:78
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:73
ToolHandleArray::push_back
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:335
ToolHandleInfo::toolComponentType
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:48
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
Gaudi::Functional::details::details2::push_back
void push_back(Container &c, const Value &v, std::true_type)
Definition: FunctionalDetails.h:154
IToolSvc.h
ToolHandle::ToolHandle
ToolHandle(const ToolHandle< NCT > &other, typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type *=nullptr)
Copy constructor from a non const T to const T tool handle.
Definition: ToolHandle.h:138
ToolHandle::i_retrieve
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:273
std::enable_if
std::string
STL class.
INamedInterface.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:227
IInterface
Definition of the basic interface.
Definition: IInterface.h:234
gaudirun.type
type
Definition: gaudirun.py:125
GaudiHandle::get
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:262
IAlgTool.h
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:228
BaseToolHandle::retrieve
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:86
ServiceHandle.h
ToolHandle::getAsIAlgTool
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:267
std::add_const
GaudiHandle::operator->
T * operator->()
Definition: GaudiHandle.h:279
std::is_const
ToolHandle::retrieve
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:204
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyHolder.h:30
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:188
Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
ToolHandleArray::ToolHandleArray
ToolHandleArray(const IInterface *parent=0, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:325
INamedInterface
IInterface compliant class extending IInterface with the name() method.
Definition: INamedInterface.h:15
ToolHandleInfo::m_parent
const IInterface * m_parent
Definition: ToolHandle.h:61
std::vector< std::string >
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
ToolHandle::getAsIAlgTool
const IAlgTool * getAsIAlgTool() const override
Definition: ToolHandle.h:261
ToolHandleInfo::~ToolHandleInfo
virtual ~ToolHandleInfo()=default
ToolHandle::retrieve
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:215
ToolHandleInfo::m_createIf
bool m_createIf
Definition: ToolHandle.h:62
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:94
std::remove_const
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:121
ToolHandleArray::ToolHandleArray
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=0, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:313
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
ToolHandle::ToolHandle
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Create a handle (&#39;smart pointer&#39;) to a tool.
Definition: ToolHandle.h:177
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
ToolHandleArray::push_back
bool push_back(const ToolHandle< T > &myHandle) override
Ensure that for added handles the parent and creatIf are taken from this array.
Definition: ToolHandle.h:344
ToolHandle::release
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:210
ToolHandleInfo::ToolHandleInfo
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:30
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:428
ToolHandleInfo::createIf
bool createIf() const noexcept
Definition: ToolHandle.h:40