The Gaudi Framework  master (08f81203)
Loading...
Searching...
No Matches
ToolHandle.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
15#include <GaudiKernel/IBinder.h>
20#include <cassert>
21#include <concepts>
22#include <ostream>
23#include <string>
24#include <tuple>
25#include <type_traits>
26#include <utility>
27#include <vector>
28
29class IInterface;
30class IToolSvc;
31namespace Gaudi {
32 class Algorithm;
33}
34class AlgTool;
35class Service;
38
41
42protected:
43 ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
45
46public:
47 virtual ~ToolHandleInfo() = default;
48
49 bool isPublic() const noexcept { return !m_parent; }
50
51 bool createIf() const noexcept { return m_createIf; }
52
53 const IInterface* parent() const noexcept { return m_parent; }
54
55 //
56 // Some helper functions
57 //
58
59 static std::string toolComponentType( const IInterface* parent ) { return parent ? "PrivateTool" : "PublicTool"; }
60
61 static std::string toolParentName( const IInterface* parent ) {
62 auto* pNamed = ( parent ? dynamic_cast<const INamedInterface*>( parent ) : nullptr );
63 return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
64 }
65
66protected:
67 const IInterface* m_parent = nullptr;
68 bool m_createIf{ true };
69};
70
79
80protected:
81 BaseToolHandle( const IInterface* parent = nullptr, bool createIf = true ) : ToolHandleInfo( parent, createIf ) {}
82
83 virtual StatusCode i_retrieve( IAlgTool*& ) const = 0;
84
85public:
86 StatusCode retrieve( IAlgTool*& tool ) const { return i_retrieve( tool ); }
87
88 virtual StatusCode retrieve() const = 0;
89 virtual StatusCode retrieve( DisableTool sd ) = 0;
90 virtual StatusCode retrieve( EnableTool sd ) = 0;
91
92 virtual StatusCode release() const = 0;
93
94 const IAlgTool* get() const { return getAsIAlgTool(); }
95
96 IAlgTool* get() { return getAsIAlgTool(); }
97
98 virtual std::string typeAndName() const = 0;
99
101 bool isEnabled() const {
102 // the handle is considered enabled if the type/name is valid and the
103 // enabled flag was set to true, or it was already retrieved
104 return ( m_enabled && !typeAndName().empty() ) || get();
105 }
106
107 void enable() { m_enabled = true; }
108
109 void disable() { m_enabled = false; }
110
111 bool setEnabled( bool flag ) { return std::exchange( m_enabled, flag ); }
112
113protected:
114 virtual const IAlgTool* getAsIAlgTool() const = 0;
115
116 virtual IAlgTool* getAsIAlgTool() = 0;
117
118 bool m_enabled = true;
119};
120
131template <class T>
132class ToolHandle : public BaseToolHandle, public GaudiHandle<T> {
133
134 friend class Gaudi::Algorithm;
135 friend class AlgTool;
136 friend class Service;
137
138 template <typename... Args, std::size_t... Is>
139 ToolHandle( std::tuple<Args...>&& args, std::index_sequence<Is...> )
140 : ToolHandle( std::get<Is>( std::move( args ) )... ) {}
141
142public:
146 ToolHandle( const IInterface* parent = nullptr, bool createIf = true )
149 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
150
152 template <typename CT = T, typename NCT = std::remove_const_t<T>>
153 requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
155 : BaseToolHandle( other.parent(), other.createIf() )
156 , GaudiHandle<CT>( other )
157 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
158
159public:
160 //
161 // Constructors etc.
162 //
163
182
183#if defined( TOOLHANDLE_DEPR_WARN )
184// warn about using deprecated explicit ToolHandle construction
185# pragma message( "Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property" )
186
187 __attribute__( ( deprecated ) )
188
189#endif
190 explicit ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
192 , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
193 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {
194 }
195
198 template <std::derived_from<IProperty> OWNER>
199 ToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" ) : ToolHandle( owner ) {
200 // convert name and type to a valid type/name string
201 // - if type does not contain '/' use type/type
202 // - otherwise type is already a type/name string
203 if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
204 owner->declareTool( *this, std::move( toolType ) ).ignore();
205 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
206 p->template setOwnerType<OWNER>();
207 }
208
209 template <typename... Args>
210 ToolHandle( std::tuple<Args...>&& args ) : ToolHandle( std::move( args ), std::index_sequence_for<Args...>{} ) {}
211
213 using GaudiHandleBase::operator=;
214
215public:
216 StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr,
217 bool createIf = true ) {
218
219 GaudiHandleBase::setTypeAndName( toolTypeAndName );
222
225
226 return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
227 }
228
231 StatusCode retrieve() const override { // not really const, because it updates m_pObject
233 }
234
236 if ( isEnabled() && sd == DisableTool{ false } ) {
238 } else {
239 disable();
240 return StatusCode::SUCCESS;
241 }
242 }
243
245 if ( isEnabled() && sd == EnableTool{ true } ) {
247 } else {
248 disable();
249 return StatusCode::SUCCESS;
250 }
251 }
252
255 StatusCode release() const override { // not really const, because it updates m_pObject
257 }
258
260 StatusCode retrieve( T*& algTool ) const override {
261 IAlgTool* iface = nullptr;
262 if ( i_retrieve( iface ).isFailure() ) { return StatusCode::FAILURE; }
263
264 algTool = dynamic_cast<T*>( iface );
265 if ( !algTool ) {
266 throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
267 System::typeinfoName( typeid( T ) ),
268 typeAndName() + " retrieve", StatusCode::FAILURE );
269 }
270 return StatusCode::SUCCESS;
271 }
272
274 StatusCode release( T* algTool ) const override { return m_pToolSvc->releaseTool( ::details::nonConst( algTool ) ); }
275
276 std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
277
278 std::add_const_t<T>* get() const { return GaudiHandle<T>::get(); }
279
280 T* get() { return GaudiHandle<T>::get(); }
281
282 friend std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
283 return os << static_cast<const GaudiHandleInfo&>( handle );
284 }
285
286protected:
287 const IAlgTool* getAsIAlgTool() const override {
288 // const cast to support T being const
289 return GaudiHandle<T>::get();
290 }
291
293 // const cast to support T being const
294 return ::details::nonConst( GaudiHandle<T>::get() );
295 }
296
297 StatusCode i_retrieve( IAlgTool*& algTool ) const override {
298 return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(), algTool, ToolHandleInfo::parent(),
300 }
301
302private:
303 //
304 // Private data members
305 //
307};
308
309// explicit specialization for IBinder<IFace>
310template <typename IFace>
311class ToolHandle<Gaudi::Interface::Bind::IBinder<IFace>> : public ToolHandle<IAlgTool> {
312
313 void* m_ptr = nullptr;
314 Gaudi::Interface::Bind::Box<IFace> ( *m_bind )( void const*, const EventContext& ) = nullptr;
315
316public:
319 StatusCode retrieve() const override {
320 // FIXME: why is `retrieve` const????
321 auto self = const_cast<ToolHandle<Gaudi::Interface::Bind::IBinder<IFace>>*>( this );
322
324 IAlgTool* tool = self->get();
325 assert( tool != nullptr ); // retrieve was succesfull, so get() better return something valid!
326 if ( ( self->m_ptr = tool->template cast<IFace>() ) ) {
327 self->m_bind = []( const void* ptr, const EventContext& ) {
328 return Gaudi::Interface::Bind::Box<IFace>( static_cast<IFace const*>( ptr ) );
329 };
330 } else if ( ( self->m_ptr = tool->template cast<Gaudi::Interface::Bind::IBinder<IFace>>() ) ) {
331 self->m_bind = []( const void* ptr, const EventContext& ctx ) {
332 return static_cast<Gaudi::Interface::Bind::IBinder<IFace> const*>( ptr )->bind( ctx );
333 };
334 } else {
335 return StatusCode::FAILURE;
336 }
337 return StatusCode::SUCCESS;
338 } );
339 }
340
341 auto bind( const EventContext& ctx ) const {
342 if ( !m_bind || !m_ptr ) {
343 throw GaudiException{ "request bind on toolhandle which was not (successfully) 'retrieved'", __PRETTY_FUNCTION__,
345 }
346 return ( *m_bind )( m_ptr, ctx );
347 }
348};
349
353template <class T>
354class PublicToolHandle : public ToolHandle<T> {
355public:
356 PublicToolHandle( bool createIf = true ) : ToolHandle<T>( nullptr, createIf ) {}
357 explicit PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
358 : PublicToolHandle{ std::string{ toolTypeAndName }, createIf } {}
359 explicit PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
360 : ToolHandle<T>( toolTypeAndName, nullptr, createIf ) {}
361
363 template <typename CT = T, typename NCT = std::remove_const_t<T>>
364 requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
366 : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) ) {}
367
370 template <std::derived_from<IProperty> OWNER>
371 inline PublicToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
372 : PublicToolHandle() {
373 // convert name and type to a valid type/name string
374 // - if type does not contain '/' use type/type
375 // - otherwise type is already a type/name string
376 if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
377 owner->declareTool( *this, std::move( toolType ) ).ignore();
378 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
379 p->template setOwnerType<OWNER>();
380 }
381};
382
383//-------------------------------------------------------------------------//
384
394
395template <class T>
396class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>> {
397public:
398 //
399 // Constructors
400 //
407 ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
408 bool createIf = true )
412
421
426 bool push_back( const std::string& toolTypeAndName ) override {
427 ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
429 return true;
430 }
431
433 bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
434
437 template <std::derived_from<IProperty> OWNER>
438 inline ToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
439 std::string doc = "" )
440 : ToolHandleArray( owner ) {
441 owner->addToolsArray( *this );
443 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
444 p->template setOwnerType<OWNER>();
445 }
446
447 friend std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
448 return os << static_cast<const GaudiHandleInfo&>( handle );
449 }
450};
451
455template <class T>
457public:
458 PublicToolHandleArray( bool createIf = true ) : ToolHandleArray<T>( nullptr, createIf ) {}
459 PublicToolHandleArray( const std::vector<std::string>& typesAndNames, bool createIf = true )
460 : ToolHandleArray<T>( typesAndNames, nullptr, createIf ) {}
461
464 template <std::derived_from<IProperty> OWNER>
465 PublicToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
466 std::string doc = "" )
468 owner->addToolsArray( *this );
470 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
471 p->template setOwnerType<OWNER>();
472 }
473};
Gaudi::tagged_bool< class DisableTool_tag > DisableTool
Definition ToolHandle.h:36
Gaudi::tagged_bool< class EnableTool_tag > EnableTool
Definition ToolHandle.h:37
Base class from which all the concrete tool classes should be derived.
Definition AlgTool.h:55
const IAlgTool * get() const
Definition ToolHandle.h:94
virtual std::string typeAndName() const =0
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition ToolHandle.h:101
virtual StatusCode retrieve(EnableTool sd)=0
virtual const IAlgTool * getAsIAlgTool() const =0
bool setEnabled(bool flag)
Definition ToolHandle.h:111
virtual StatusCode retrieve(DisableTool sd)=0
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:81
virtual StatusCode i_retrieve(IAlgTool *&) const =0
IAlgTool * get()
Definition ToolHandle.h:96
StatusCode retrieve(IAlgTool *&tool) const
Definition ToolHandle.h:86
virtual StatusCode release() const =0
virtual StatusCode retrieve() const =0
virtual IAlgTool * getAsIAlgTool()=0
This class represents an entry point to all the event specific data.
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:87
Define general base for Gaudi exception.
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in <myTypesAndNamesList>.
const std::vector< std::string > typesAndNames() const
Return a vector with "type/name" strings of all handles in the array.
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
const std::string & typeAndName() const
The full type and name: "type/name".
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
StatusCode release() const
Release the component.
T * get()
Return the wrapped pointer, not calling retrieve() if null.
StatusCode retrieve() const
Retrieve the component.
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
void setParentName(std::string parent)
The name of the parent.
Definition GaudiHandle.h:84
const std::string & parentName() const
The name of the parent.
Definition GaudiHandle.h:62
void setComponentType(std::string componentType)
The component type.
Definition GaudiHandle.h:81
The interface implemented by the AlgTool base class.
Definition IAlgTool.h:29
Definition of the basic interface.
Definition IInterface.h:225
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition IInterface.h:234
IInterface compliant class extending IInterface with the name() method.
The interface implemented by the IToolSvc base class.
Definition IToolSvc.h:28
Helper class to construct ToolHandle instances for public tools via the auto registering constructor.
Definition ToolHandle.h:456
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition ToolHandle.h:459
PublicToolHandleArray(bool createIf=true)
Definition ToolHandle.h:458
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:465
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition ToolHandle.h:359
PublicToolHandle(OWNER *owner, std::string propName, std::string toolType, std::string doc="")
Autodeclaring constructor with property propName, tool type/name and documentation.
Definition ToolHandle.h:371
PublicToolHandle(bool createIf=true)
Definition ToolHandle.h:356
PublicToolHandle(const PublicToolHandle< NCT > &other)
Copy constructor from a non const T to const T tool handle.
Definition ToolHandle.h:365
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition ToolHandle.h:357
Handle to be used in lieu of naked pointers to services.
Base class for all services.
Definition Service.h:39
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition StatusCode.h:151
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
auto bind(const EventContext &ctx) const
Definition ToolHandle.h:341
Gaudi::Interface::Bind::Box< IFace >(* m_bind)(void const *, const EventContext &)
Definition ToolHandle.h:314
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition ToolHandle.h:396
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:438
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition ToolHandle.h:417
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition ToolHandle.h:407
friend std::ostream & operator<<(std::ostream &os, const ToolHandleArray< T > &handle)
Definition ToolHandle.h:447
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition ToolHandle.h:426
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:433
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Create a handle ('smart pointer') to a tool.
Definition ToolHandle.h:190
ToolHandle(std::tuple< Args... > &&args)
Definition ToolHandle.h:210
ToolHandle(std::tuple< Args... > &&args, std::index_sequence< Is... >)
Definition ToolHandle.h:139
IAlgTool * getAsIAlgTool() override
Definition ToolHandle.h:292
std::string typeAndName() const override
Definition ToolHandle.h:276
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition ToolHandle.h:274
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition ToolHandle.h:260
ToolHandle(OWNER *owner, std::string propName, std::string toolType, std::string doc="")
Autodeclaring constructor with property propName, tool type/name and documentation.
Definition ToolHandle.h:199
friend std::ostream & operator<<(std::ostream &os, const ToolHandle< T > &handle)
Definition ToolHandle.h:282
const IAlgTool * getAsIAlgTool() const override
Definition ToolHandle.h:287
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:216
StatusCode retrieve(EnableTool sd) override
Definition ToolHandle.h:244
StatusCode retrieve(DisableTool sd) override
Definition ToolHandle.h:235
StatusCode release() const override
Release the AlgTool.
Definition ToolHandle.h:255
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition ToolHandle.h:297
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition ToolHandle.h:146
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition ToolHandle.h:231
ToolHandle(const ToolHandle< NCT > &other)
Copy constructor from a non const T to const T tool handle.
Definition ToolHandle.h:154
bool createIf() const noexcept
Definition ToolHandle.h:51
const IInterface * parent() const noexcept
Definition ToolHandle.h:53
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:43
bool isPublic() const noexcept
Definition ToolHandle.h:49
const IInterface * m_parent
Definition ToolHandle.h:67
static std::string toolParentName(const IInterface *parent)
Definition ToolHandle.h:61
virtual ~ToolHandleInfo()=default
static std::string toolComponentType(const IInterface *parent)
Definition ToolHandle.h:59
STL class.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:254
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition GaudiHandle.h:26
STL namespace.