| The Gaudi Framework v30r0 (c919700c)

- 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/IAlgTool.h"
7 #include "GaudiKernel/INamedInterface.h"
8 #include "GaudiKernel/IToolSvc.h"
9 #include "GaudiKernel/ServiceHandle.h"
10
11 #include <stdexcept>
12 #include <string>
13 #include <type_traits>
14 #include <vector>
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 ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
30 : m_parent( parent ), m_createIf( createIf )
31 {
32 }
33
34 public:
35 virtual ~ToolHandleInfo() = default;
36
37 bool isPublic() const noexcept { return !m_parent; }
38
39 bool createIf() const noexcept { return m_createIf; }
40
41 const IInterface* parent() const noexcept { return m_parent; }
42
43 //
44 // Some helper functions
45 //
46
47 static std::string toolComponentType( const IInterface* parent ) { return parent ? "PrivateTool" : "PublicTool"; }
48
49 static std::string toolParentName( const IInterface* parent )
50 {
51 auto* pNamed = ( parent ? dynamic_cast<const INamedInterface*>( parent ) : nullptr );
52 return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
53 }
54
55 protected:
56 const IInterface* m_parent = nullptr;
57 bool m_createIf{true};
58 };
59
67 class BaseToolHandle : public ToolHandleInfo
68 {
69
70 protected:
71 BaseToolHandle( const IInterface* parent = nullptr, bool createIf = true ) : ToolHandleInfo( parent, createIf ) {}
72
73 virtual StatusCode i_retrieve( IAlgTool*& ) const = 0;
74
75 public:
76 StatusCode retrieve( IAlgTool*& tool ) const { return i_retrieve( tool ); }
77
78 virtual StatusCode retrieve() const = 0;
79
80 const IAlgTool* get() const { return getAsIAlgTool(); }
81
82 IAlgTool* get() { return getAsIAlgTool(); }
83
84 virtual std::string typeAndName() const = 0;
85
87 inline bool isEnabled() const
88 {
89 // the handle is considered enabled if the type/name is valid and the
90 // enabled flag was set to true, or it was already retrieved
91 return ( m_enabled && !typeAndName().empty() ) || get();
92 }
93
94 inline void enable() { m_enabled = true; }
95
96 inline void disable() { m_enabled = false; }
97
98 inline bool setEnabled( const bool flag )
99 {
100 auto old = m_enabled;
101 m_enabled = flag;
102 return old;
103 }
104
105 protected:
106 virtual const IAlgTool* getAsIAlgTool() const = 0;
107
108 virtual IAlgTool* getAsIAlgTool() = 0;
109
110 bool m_enabled = true;
111 };
112
123 template <class T>
124 class ToolHandle : public BaseToolHandle, public GaudiHandle<T>
125 {
126
127 friend class Algorithm;
128 friend class AlgTool;
129 friend class Service;
130
131 public:
135 ToolHandle( const IInterface* parent = nullptr, bool createIf = true )
136 : BaseToolHandle( parent, createIf )
137 , GaudiHandle<T>( "", toolComponentType( parent ), toolParentName( parent ) )
138 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
139 {
140 }
141
143 template <typename CT = T, typename NCT = typename std::remove_const<T>::type>
144 ToolHandle( const ToolHandle<NCT>& other,
145 typename std::enable_if<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>::type* = nullptr )
146 : BaseToolHandle( other.parent(), other.createIf() )
147 , GaudiHandle<CT>( other )
148 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
149 {
150 }
151
152 public:
153 //
154 // Constructors etc.
155 //
156
176 #if defined( TOOLHANDLE_DEPR_WARN )
177 // warn about using deprecated explicit ToolHandle construction
178 #pragma message( "Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property" )
179
180 __attribute__( ( deprecated ) )
181
182 #endif
183 ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
184 : BaseToolHandle( parent, createIf )
185 , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
186 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
187 {
188 }
189
192 template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
193 inline ToolHandle( OWNER* owner, std::string name, std::string toolType, std::string doc = "" ) : ToolHandle( owner )
194 {
195 // convert name and type to a valid type/name string
196 // - if type does not contain '/' use type/name
197 // - otherwise type is already a type/name string
198 if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
199 toolType += '/';
200 toolType += name;
201 }
202 owner->declareTool( *this, std::move( toolType ) ).ignore();
203 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
204 p->template setOwnerType<OWNER>();
205 }
206
207 public:
208 StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
209 {
210
211 GaudiHandleBase::setTypeAndName( toolTypeAndName );
212 GaudiHandleBase::setComponentType( toolComponentType( parent ) );
213 GaudiHandleBase::setParentName( toolParentName( parent ) );
214
215 m_parent = parent;
216 m_createIf = createIf;
217
218 return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
219 }
220
223 StatusCode retrieve() const override
224 { // not really const, because it updates m_pObject
225 return GaudiHandle<T>::retrieve();
226 }
227
230 StatusCode release() const
231 { // not really const, because it updates m_pObject
232 return GaudiHandle<T>::release();
233 }
234
236 StatusCode retrieve( T*& algTool ) const override
237 {
238 IAlgTool* iface = nullptr;
239 algTool = i_retrieve( iface ) ? dynamic_cast<T*>( iface ) : nullptr;
240 return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
241 }
242
244 StatusCode release( T* algTool ) const override { return m_pToolSvc->releaseTool( details::nonConst( algTool ) ); }
245
246 std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
247
248 typename std::add_const<T>::type* get() const { return GaudiHandle<T>::get(); }
249
250 T* get() { return GaudiHandle<T>::get(); }
251
252 // Allow access to non-const Tool methods of const ToolHandle
253 #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
254 T* operator->() { return GaudiHandle<T>::operator->(); }
255 T& operator*() { return *( GaudiHandle<T>::operator->() ); }
256
257 T* operator->() const { return details::nonConst( GaudiHandle<T>::operator->() ); }
258 T& operator*() const { return *( details::nonConst( GaudiHandle<T>::operator->() ) ); }
259 #endif
260
261 #ifdef ATLAS
262 [[deprecated( "FIXME!! should not call non-const method from a const ToolHandle" )]] ToolHandle<T>& unConst() const
263 {
264 return const_cast<ToolHandle<T>&>( *this );
265 }
266 #endif
267
268 protected:
269 const IAlgTool* getAsIAlgTool() const override
270 {
271 // const cast to support T being const
272 return GaudiHandle<T>::get();
273 }
274
275 IAlgTool* getAsIAlgTool() override
276 {
277 // const cast to support T being const
278 return details::nonConst( GaudiHandle<T>::get() );
279 }
280
281 StatusCode i_retrieve( IAlgTool*& algTool ) const override
282 {
283 return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(), algTool, ToolHandleInfo::parent(),
284 ToolHandleInfo::createIf() );
285 }
286
287 private:
288 //
289 // Private data members
290 //
291 mutable ServiceHandle<IToolSvc> m_pToolSvc;
292 };
293
297 template <class T>
298 class PublicToolHandle : public ToolHandle<T>
299 {
300 public:
301 PublicToolHandle( bool createIf = true ) : ToolHandle<T>( nullptr, createIf ) {}
302 PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
303 : PublicToolHandle{std::string{toolTypeAndName}, createIf}
304 {
305 }
306 PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
307 : ToolHandle<T>( toolTypeAndName, nullptr, createIf )
308 {
309 }
310
312 template <typename CT = T, typename NCT = typename std::remove_const<T>::type>
313 PublicToolHandle( const PublicToolHandle<NCT>& other,
314 std::enable_if_t<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>* = nullptr )
315 : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) )
316 {
317 }
318
321 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
322 inline PublicToolHandle( OWNER* owner, std::string name, std::string toolType, std::string doc = "" )
323 : PublicToolHandle()
324 {
325 // convert name and type to a valid type/name string
326 // - if type does not contain '/' use type/name
327 // - otherwise type is already a type/name string
328 if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
329 toolType += '/';
330 toolType += name;
331 }
332 owner->declareTool( *this, std::move( toolType ) ).ignore();
333 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
334 p->template setOwnerType<OWNER>();
335 }
336 };
337
338 //-------------------------------------------------------------------------//
339
350 template <class T>
351 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>>
352 {
353 public:
354 //
355 // Constructors
356 //
363 ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
364 bool createIf = true )
365 : ToolHandleInfo( parent, createIf )
366 , GaudiHandleArray<ToolHandle<T>>( myTypesAndNames, ToolHandleInfo::toolComponentType( parent ),
367 ToolHandleInfo::toolParentName( parent ) )
368 {
369 }
370
375 ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
376 : ToolHandleInfo( parent, createIf )
377 , GaudiHandleArray<ToolHandle<T>>( ToolHandleInfo::toolComponentType( parent ),
378 ToolHandleInfo::toolParentName( parent ) )
379 {
380 }
381
386 bool push_back( const std::string& toolTypeAndName ) override
387 {
388 ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
389 GaudiHandleArray<ToolHandle<T>>::push_back( handle );
390 return true;
391 }
392
394 bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
395
398 template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
399 inline ToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
400 std::string doc = "" )
401 : ToolHandleArray( typesAndNames, owner )
402 {
403 owner->addToolsArray( *this );
404 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
405 p->template setOwnerType<OWNER>();
406 }
407 };
408
412 template <class T>
413 class PublicToolHandleArray : public ToolHandleArray<T>
414 {
415 public:
416 PublicToolHandleArray( bool createIf = true ) : ToolHandleArray<T>( nullptr, createIf ) {}
417 PublicToolHandleArray( const std::vector<std::string>& typesAndNames, bool createIf = true )
418 : ToolHandleArray<T>( typesAndNames, nullptr, createIf )
419 {
420 }
421
424 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
425 inline PublicToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
426 std::string doc = "" )
427 : PublicToolHandleArray( typesAndNames )
428 {
429 owner->addToolsArray( *this );
430 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
431 p->template setOwnerType<OWNER>();
432 }
433 };
434
435 template <class T>
436 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle )
437 {
438 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
439 }
440
441 template <class T>
442 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle )
443 {
444 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
445 }
446
447 #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:135
PublicToolHandleArray
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:413
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
StatusCode::SUCCESS
Definition: StatusCode.h:29
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:167
__attribute__
#define __attribute__(x)
Definition: System.cpp:89
std::string::empty
T empty(T...args)
ToolHandleInfo::parent
const IInterface * parent() const noexcept
Definition: ToolHandle.h:41
ToolHandleArray::ToolHandleArray
ToolHandleArray(OWNER *owner, std::string name, const std::vector< std::string > &typesAndNames={}, std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:399
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
ToolHandle::m_pToolSvc
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:291
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:244
ToolHandle::retrieve
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition: ToolHandle.h:223
BaseToolHandle::setEnabled
bool setEnabled(const bool flag)
Definition: ToolHandle.h:98
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:37
BaseToolHandle::disable
void disable()
Definition: ToolHandle.h:96
GaudiHandleInfo::setComponentType
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:72
BaseToolHandle::isEnabled
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:87
ToolHandleInfo::toolParentName
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:49
std::is_same
PublicToolHandle::PublicToolHandle
PublicToolHandle(const PublicToolHandle< NCT > &other, std::enable_if_t< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value > *=nullptr)
Copy constructor from a non const T to const T tool handle.
Definition: ToolHandle.h:313
BaseToolHandle
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:67
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:57
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:417
BaseToolHandle::BaseToolHandle
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:71
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:75
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:386
ToolHandleInfo::toolComponentType
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:47
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:237
Gaudi::Functional::details::details2::push_back
void push_back(Container &c, const Value &v, std::true_type)
Definition: FunctionalDetails.h:215
PublicToolHandle::PublicToolHandle
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:306
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:144
ToolHandle::i_retrieve
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:281
PublicToolHandle::PublicToolHandle
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:302
std::enable_if
std::string
STL class.
INamedInterface.h
ToolHandleArray::ToolHandleArray
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:375
details::nonConst
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
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:246
IInterface
Definition of the basic interface.
Definition: IInterface.h:277
gaudirun.type
type
Definition: gaudirun.py:133
GaudiHandle::get
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:261
IAlgTool.h
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:223
BaseToolHandle::retrieve
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:76
BaseToolHandle::enable
void enable()
Definition: ToolHandle.h:94
ServiceHandle.h
std::move
T move(T...args)
PublicToolHandle::PublicToolHandle
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:301
ToolHandle::getAsIAlgTool
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:275
std::add_const
GaudiHandle::operator->
T * operator->()
Definition: GaudiHandle.h:275
std::is_const
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:208
Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
INamedInterface
IInterface compliant class extending IInterface with the name() method.
Definition: INamedInterface.h:15
std::string::find
T find(T...args)
PublicToolHandle::PublicToolHandle
PublicToolHandle(OWNER *owner, std::string name, std::string toolType, std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:322
ToolHandleInfo::m_parent
const IInterface * m_parent
Definition: ToolHandle.h:56
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:269
PublicToolHandle
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:298
ToolHandle::ToolHandle
ToolHandle(OWNER *owner, std::string name, std::string toolType, std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:193
ToolHandleInfo::~ToolHandleInfo
virtual ~ToolHandleInfo()=default
ToolHandle::retrieve
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:236
ToolHandleArray::ToolHandleArray
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:363
ToolHandleInfo::m_createIf
bool m_createIf
Definition: ToolHandle.h:57
StatusCode::FAILURE
Definition: StatusCode.h:29
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:121
Service
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:416
std::ostream
STL class.
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
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:183
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(OWNER *owner, std::string name, const std::vector< std::string > &typesAndNames={}, std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:425
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:394
ToolHandle::release
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:230
ToolHandleInfo::ToolHandleInfo
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:29
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:416
ToolHandleInfo::createIf
bool createIf() const noexcept
Definition: ToolHandle.h:39