| The Gaudi Framework v29r0 (ff2e7097)

- 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 const IAlgTool* get() const { return getAsIAlgTool(); }
79
80 IAlgTool* get() { return getAsIAlgTool(); }
81
82 virtual std::string typeAndName() const = 0;
83
84 protected:
85 virtual const IAlgTool* getAsIAlgTool() const = 0;
86
87 virtual IAlgTool* getAsIAlgTool() = 0;
88 };
89
100 template <class T>
101 class ToolHandle : public BaseToolHandle, public GaudiHandle<T>
102 {
103
104 friend class Algorithm;
105 friend class AlgTool;
106 friend class Service;
107
108 public:
112 ToolHandle( const IInterface* parent = nullptr, bool createIf = true )
113 : BaseToolHandle( parent, createIf )
114 , GaudiHandle<T>( GaudiHandle<T>::getDefaultType(), toolComponentType( parent ), toolParentName( parent ) )
115 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
116 {
117 }
118
120 template <typename CT = T, typename NCT = typename std::remove_const<T>::type>
121 ToolHandle( const ToolHandle<NCT>& other,
122 typename std::enable_if<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>::type* = nullptr )
123 : BaseToolHandle( other.parent(), other.createIf() )
124 , GaudiHandle<CT>( other )
125 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
126 {
127 }
128
129 public:
130 //
131 // Constructors etc.
132 //
133
153 #if defined( TOOLHANDLE_DEPR_WARN )
154 // warn about using deprecated explicit ToolHandle construction
155 #pragma message( "Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property" )
156
157 __attribute__( ( deprecated ) )
158
159 #endif
160 ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
161 : BaseToolHandle( parent, createIf )
162 , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
163 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
164 {
165 }
166
169 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
170 inline ToolHandle( OWNER* owner, std::string name, std::string toolType, std::string doc = "" ) : ToolHandle( owner )
171 {
172 // convert name and type to a valid type/name string
173 // - if type does not contain '/' use type/name
174 // - otherwise type is already a type/name string
175 if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
176 toolType += '/';
177 toolType += name;
178 }
179 owner->declareTool( *this, std::move( toolType ) ).ignore();
180 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
181 p->template setOwnerType<OWNER>();
182 }
183
184 public:
185 StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
186 {
187
188 GaudiHandleBase::setTypeAndName( toolTypeAndName );
189 GaudiHandleBase::setComponentType( toolComponentType( parent ) );
190 GaudiHandleBase::setParentName( toolParentName( parent ) );
191
192 m_parent = parent;
193 m_createIf = createIf;
194
195 return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
196 }
197
200 StatusCode retrieve() const
201 { // not really const, because it updates m_pObject
202 return GaudiHandle<T>::retrieve();
203 }
204
207 StatusCode release() const
208 { // not really const, because it updates m_pObject
209 return GaudiHandle<T>::release();
210 }
211
213 StatusCode retrieve( T*& algTool ) const override
214 {
215 IAlgTool* iface = nullptr;
216 algTool = i_retrieve( iface ) ? dynamic_cast<T*>( iface ) : nullptr;
217 return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
218 }
219
221 StatusCode release( T* algTool ) const override { return m_pToolSvc->releaseTool( this->nonConst( algTool ) ); }
222
223 std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
224
225 typename std::add_const<T>::type* get() const { return GaudiHandle<T>::get(); }
226
227 T* get() { return GaudiHandle<T>::get(); }
228
229 // Allow access to non-const Tool methods of const ToolHandle
230 #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
231 T* operator->() { return GaudiHandle<T>::operator->(); }
232 T& operator*() { return *( GaudiHandle<T>::operator->() ); }
233
234 T* operator->() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() ); }
235 T& operator*() const { return *( GaudiHandle<T>::nonConst( GaudiHandle<T>::operator->() ) ); }
236 #endif
237
238 #ifdef ATLAS
239 [[deprecated( "FIXME!! should not call non-const method from a const ToolHandle" )]] ToolHandle<T>& unConst() const
240 {
241 return const_cast<ToolHandle<T>&>( *this );
242 }
243 #endif
244
245 protected:
246 const IAlgTool* getAsIAlgTool() const override
247 {
248 // const cast to support T being const
249 return GaudiHandle<T>::get();
250 }
251
252 IAlgTool* getAsIAlgTool() override
253 {
254 // const cast to support T being const
255 return this->nonConst( GaudiHandle<T>::get() );
256 }
257
258 StatusCode i_retrieve( IAlgTool*& algTool ) const override
259 {
260 return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(), algTool, ToolHandleInfo::parent(),
261 ToolHandleInfo::createIf() );
262 }
263
264 private:
265 //
266 // Private data members
267 //
268 mutable ServiceHandle<IToolSvc> m_pToolSvc;
269 };
270
274 template <class T>
275 class PublicToolHandle : public ToolHandle<T>
276 {
277 public:
278 PublicToolHandle( bool createIf = true ) : ToolHandle<T>( nullptr, createIf ) {}
279 PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
280 : PublicToolHandle{std::string{toolTypeAndName}, createIf}
281 {
282 }
283 PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
284 : ToolHandle<T>( toolTypeAndName, nullptr, createIf )
285 {
286 }
287
289 template <typename CT = T, typename NCT = typename std::remove_const<T>::type>
290 PublicToolHandle(
291 const PublicToolHandle<NCT>& other,
292 typename std::enable_if<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>::type* = nullptr )
293 : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) )
294 {
295 }
296
299 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
300 inline PublicToolHandle( OWNER* owner, std::string name, std::string toolType, std::string doc = "" )
301 : PublicToolHandle()
302 {
303 // convert name and type to a valid type/name string
304 // - if type does not contain '/' use type/name
305 // - otherwise type is already a type/name string
306 if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
307 toolType += '/';
308 toolType += name;
309 }
310 owner->declareTool( *this, std::move( toolType ) ).ignore();
311 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
312 p->template setOwnerType<OWNER>();
313 }
314 };
315
316 //-------------------------------------------------------------------------//
317
328 template <class T>
329 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>>
330 {
331 public:
332 //
333 // Constructors
334 //
341 ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
342 bool createIf = true )
343 : ToolHandleInfo( parent, createIf )
344 , GaudiHandleArray<ToolHandle<T>>( myTypesAndNames, ToolHandleInfo::toolComponentType( parent ),
345 ToolHandleInfo::toolParentName( parent ) )
346 {
347 }
348
353 ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
354 : ToolHandleInfo( parent, createIf )
355 , GaudiHandleArray<ToolHandle<T>>( ToolHandleInfo::toolComponentType( parent ),
356 ToolHandleInfo::toolParentName( parent ) )
357 {
358 }
359
364 bool push_back( const std::string& toolTypeAndName ) override
365 {
366 ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
367 GaudiHandleArray<ToolHandle<T>>::push_back( handle );
368 return true;
369 }
370
372 bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
373
376 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
377 inline ToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
378 std::string doc = "" )
379 : ToolHandleArray( typesAndNames, owner )
380 {
381 owner->addToolsArray( *this );
382 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
383 p->template setOwnerType<OWNER>();
384 }
385 };
386
390 template <class T>
391 class PublicToolHandleArray : public ToolHandleArray<T>
392 {
393 public:
394 PublicToolHandleArray( bool createIf = true ) : ToolHandleArray<T>( nullptr, createIf ) {}
395 PublicToolHandleArray( const std::vector<std::string>& typesAndNames, bool createIf = true )
396 : ToolHandleArray<T>( typesAndNames, nullptr, createIf )
397 {
398 }
399
402 template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
403 inline PublicToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
404 std::string doc = "" )
405 : PublicToolHandleArray( typesAndNames )
406 {
407 owner->addToolsArray( *this );
408 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
409 p->template setOwnerType<OWNER>();
410 }
411 };
412
413 template <class T>
414 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle )
415 {
416 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
417 }
418
419 template <class T>
420 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle )
421 {
422 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
423 }
424
425 #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:112
PublicToolHandleArray
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:391
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
PublicToolHandle::PublicToolHandle
PublicToolHandle(const PublicToolHandle< 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:290
StatusCode::SUCCESS
Definition: StatusCode.h:31
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:157
__attribute__
#define __attribute__(x)
Definition: System.cpp:79
std::string::empty
T empty(T...args)
ToolHandleInfo::parent
const IInterface * parent() const noexcept
Definition: ToolHandle.h:41
GaudiHandle::nonConst
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:309
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:377
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
ToolHandle::m_pToolSvc
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:268
StatusCode::FAILURE
Definition: StatusCode.h:31
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:221
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:37
GaudiHandleInfo::setComponentType
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:62
ToolHandleInfo::toolParentName
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:49
std::is_same
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:47
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:395
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:65
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:364
ToolHandleInfo::toolComponentType
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:47
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:227
Gaudi::Functional::details::details2::push_back
void push_back(Container &c, const Value &v, std::true_type)
Definition: FunctionalDetails.h:217
PublicToolHandle::PublicToolHandle
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:283
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:121
ToolHandle::i_retrieve
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:258
PublicToolHandle::PublicToolHandle
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:279
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:353
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:223
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:251
IAlgTool.h
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:213
BaseToolHandle::retrieve
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:76
ServiceHandle.h
std::move
T move(T...args)
PublicToolHandle::PublicToolHandle
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:278
ToolHandle::getAsIAlgTool
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:252
std::add_const
GaudiHandle::operator->
T * operator->()
Definition: GaudiHandle.h:265
std::is_const
ToolHandle::retrieve
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:200
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:185
Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
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:300
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:246
PublicToolHandle
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:275
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:170
ToolHandleInfo::~ToolHandleInfo
virtual ~ToolHandleInfo()=default
ToolHandle::retrieve
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:213
ToolHandleArray::ToolHandleArray
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:341
ToolHandleInfo::m_createIf
bool m_createIf
Definition: ToolHandle.h:57
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:83
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:111
Service
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:394
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:160
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:403
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:372
ToolHandle::release
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:207
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:413
ToolHandleInfo::createIf
bool createIf() const noexcept
Definition: ToolHandle.h:39