Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ToolHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TOOLHANDLE_H
2 #define GAUDIKERNEL_TOOLHANDLE_H
3 
4 // Includes
6 #include "GaudiKernel/IAlgTool.h"
8 #include "GaudiKernel/IToolSvc.h"
10 #include "GaudiKernel/TaggedBool.h"
11 
12 #include <stdexcept>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
16 
17 // forward declarations
18 class IInterface;
19 class IToolSvc;
20 
21 namespace Gaudi {
22  class Algorithm;
23 }
24 class AlgTool;
25 class Service;
26 
27 using DisableTool = Gaudi::tagged_bool<class DisableTool_tag>;
28 using EnableTool = Gaudi::tagged_bool<class EnableTool_tag>;
29 
32 
33 protected:
34  ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
35  : m_parent( parent ), m_createIf( createIf ) {}
36 
37 public:
38  virtual ~ToolHandleInfo() = default;
39 
40  bool isPublic() const noexcept { return !m_parent; }
41 
42  bool createIf() const noexcept { return m_createIf; }
43 
44  const IInterface* parent() const noexcept { return m_parent; }
45 
46  //
47  // Some helper functions
48  //
49 
50  static std::string toolComponentType( const IInterface* parent ) { return parent ? "PrivateTool" : "PublicTool"; }
51 
52  static std::string toolParentName( const IInterface* parent ) {
53  auto* pNamed = ( parent ? dynamic_cast<const INamedInterface*>( parent ) : nullptr );
54  return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
55  }
56 
57 protected:
58  const IInterface* m_parent = nullptr;
59  bool m_createIf{true};
60 };
61 
70 
71 protected:
72  BaseToolHandle( const IInterface* parent = nullptr, bool createIf = true ) : ToolHandleInfo( parent, createIf ) {}
73 
74  virtual StatusCode i_retrieve( IAlgTool*& ) const = 0;
75 
76 public:
77  StatusCode retrieve( IAlgTool*& tool ) const { return i_retrieve( tool ); }
78 
79  virtual StatusCode retrieve() const = 0;
80  virtual StatusCode retrieve( DisableTool sd ) = 0;
81  virtual StatusCode retrieve( EnableTool sd ) = 0;
82 
83  const IAlgTool* get() const { return getAsIAlgTool(); }
84 
85  IAlgTool* get() { return getAsIAlgTool(); }
86 
87  virtual std::string typeAndName() const = 0;
88 
90  inline bool isEnabled() const {
91  // the handle is considered enabled if the type/name is valid and the
92  // enabled flag was set to true, or it was already retrieved
93  return ( m_enabled && !typeAndName().empty() ) || get();
94  }
95 
96  inline void enable() { m_enabled = true; }
97 
98  inline void disable() { m_enabled = false; }
99 
100  inline bool setEnabled( const bool flag ) {
101  auto old = m_enabled;
102  m_enabled = flag;
103  return old;
104  }
105 
106 protected:
107  virtual const IAlgTool* getAsIAlgTool() const = 0;
108 
109  virtual IAlgTool* getAsIAlgTool() = 0;
110 
111  bool m_enabled = true;
112 };
113 
124 template <class T>
125 class ToolHandle : public BaseToolHandle, public GaudiHandle<T> {
126 
127  friend class Gaudi::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 
144  : BaseToolHandle( other.parent(), other.createIf() )
145  , GaudiHandle<CT>( other )
146  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
147 
148 public:
149  //
150  // Constructors etc.
151  //
152 
172 #if defined( TOOLHANDLE_DEPR_WARN )
173 // warn about using deprecated explicit ToolHandle construction
174 # pragma message( "Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property" )
175 
177 
178 #endif
179  ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
180  : BaseToolHandle( parent, createIf )
181  , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
182  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {
183  }
184 
187  template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
188  inline ToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
189  : ToolHandle( owner ) {
190  // convert name and type to a valid type/name string
191  // - if type does not contain '/' use type/type
192  // - otherwise type is already a type/name string
193  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
194  owner->declareTool( *this, std::move( toolType ) ).ignore();
195  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
196  p->template setOwnerType<OWNER>();
197  }
198 
199 public:
200  StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr,
201  bool createIf = true ) {
202 
203  GaudiHandleBase::setTypeAndName( toolTypeAndName );
204  GaudiHandleBase::setComponentType( toolComponentType( parent ) );
205  GaudiHandleBase::setParentName( toolParentName( parent ) );
206 
207  m_parent = parent;
208  m_createIf = createIf;
209 
210  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
211  }
212 
215  StatusCode retrieve() const override { // not really const, because it updates m_pObject
216  return GaudiHandle<T>::retrieve();
217  }
218 
219  StatusCode retrieve( DisableTool sd ) override {
220  if ( isEnabled() && sd == DisableTool{false} ) {
221  return GaudiHandle<T>::retrieve();
222  } else {
223  disable();
224  return StatusCode::SUCCESS;
225  }
226  }
227 
228  StatusCode retrieve( EnableTool sd ) override {
229  if ( isEnabled() && sd == EnableTool{true} ) {
230  return GaudiHandle<T>::retrieve();
231  } else {
232  disable();
233  return StatusCode::SUCCESS;
234  }
235  }
236 
239  StatusCode release() const { // not really const, because it updates m_pObject
240  return GaudiHandle<T>::release();
241  }
242 
244  StatusCode retrieve( T*& algTool ) const override {
245  IAlgTool* iface = nullptr;
246  if ( i_retrieve( iface ).isFailure() ) { return StatusCode::FAILURE; }
247 
248  algTool = dynamic_cast<T*>( iface );
249  if ( algTool == nullptr ) {
250  throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
251  System::typeinfoName( typeid( T ) ),
252  typeAndName() + " retrieve", StatusCode::FAILURE );
253  }
254  return StatusCode::SUCCESS;
255  }
256 
258  StatusCode release( T* algTool ) const override { return m_pToolSvc->releaseTool( ::details::nonConst( algTool ) ); }
259 
260  std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
261 
262  typename std::add_const<T>::type* get() const { return GaudiHandle<T>::get(); }
263 
264  T* get() { return GaudiHandle<T>::get(); }
265 
266 // Allow access to non-const Tool methods of const ToolHandle
267 #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
268  T* operator->() { return GaudiHandle<T>::operator->(); }
269  T& operator*() { return *( GaudiHandle<T>::operator->() ); }
270 
271  T* operator->() const { return ::details::nonConst( GaudiHandle<T>::operator->() ); }
272  T& operator*() const { return *( ::details::nonConst( GaudiHandle<T>::operator->() ) ); }
273 #endif
274 
275 #ifdef ATLAS
276  [[deprecated( "FIXME!! should not call non-const method from a const ToolHandle" )]] ToolHandle<T>& unConst() const {
277  return const_cast<ToolHandle<T>&>( *this );
278  }
279 #endif
280 
281 protected:
282  const IAlgTool* getAsIAlgTool() const override {
283  // const cast to support T being const
284  return GaudiHandle<T>::get();
285  }
286 
287  IAlgTool* getAsIAlgTool() override {
288  // const cast to support T being const
290  }
291 
292  StatusCode i_retrieve( IAlgTool*& algTool ) const override {
293  return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(), algTool, ToolHandleInfo::parent(),
295  }
296 
297 private:
298  //
299  // Private data members
300  //
302 };
303 
307 template <class T>
308 class PublicToolHandle : public ToolHandle<T> {
309 public:
310  PublicToolHandle( bool createIf = true ) : ToolHandle<T>( nullptr, createIf ) {}
311  PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
312  : PublicToolHandle{std::string{toolTypeAndName}, createIf} {}
313  PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
314  : ToolHandle<T>( toolTypeAndName, nullptr, createIf ) {}
315 
319  std::enable_if_t<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>* = nullptr )
320  : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) ) {}
321 
324  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
325  inline PublicToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
326  : PublicToolHandle() {
327  // convert name and type to a valid type/name string
328  // - if type does not contain '/' use type/type
329  // - otherwise type is already a type/name string
330  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
331  owner->declareTool( *this, std::move( toolType ) ).ignore();
332  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
333  p->template setOwnerType<OWNER>();
334  }
335 };
336 
337 //-------------------------------------------------------------------------//
338 
349 template <class T>
350 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>> {
351 public:
352  //
353  // Constructors
354  //
361  ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
362  bool createIf = true )
363  : ToolHandleInfo( parent, createIf )
364  , GaudiHandleArray<ToolHandle<T>>( myTypesAndNames, ToolHandleInfo::toolComponentType( parent ),
365  ToolHandleInfo::toolParentName( parent ) ) {}
366 
371  ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
372  : ToolHandleInfo( parent, createIf )
373  , GaudiHandleArray<ToolHandle<T>>( ToolHandleInfo::toolComponentType( parent ),
374  ToolHandleInfo::toolParentName( parent ) ) {}
375 
380  bool push_back( const std::string& toolTypeAndName ) override {
381  ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
383  return true;
384  }
385 
387  bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
388 
391  template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
392  inline ToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
393  std::string doc = "" )
394  : ToolHandleArray( typesAndNames, owner ) {
395  owner->addToolsArray( *this );
396  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
397  p->template setOwnerType<OWNER>();
398  }
399 };
400 
404 template <class T>
406 public:
407  PublicToolHandleArray( bool createIf = true ) : ToolHandleArray<T>( nullptr, createIf ) {}
408  PublicToolHandleArray( const std::vector<std::string>& typesAndNames, bool createIf = true )
409  : ToolHandleArray<T>( typesAndNames, nullptr, createIf ) {}
410 
413  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
414  inline PublicToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
415  std::string doc = "" )
416  : PublicToolHandleArray( typesAndNames ) {
417  owner->addToolsArray( *this );
418  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
419  p->template setOwnerType<OWNER>();
420  }
421 };
422 
423 template <class T>
424 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
425  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
426 }
427 
428 template <class T>
429 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
430  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
431 }
432 
433 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:135
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:405
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
#define __attribute__(x)
Definition: System.cpp:89
T empty(T...args)
const IInterface * parent() const noexcept
Definition: ToolHandle.h:44
Define general base for Gaudi exception.
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:392
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition: ToolHandle.h:258
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition: ToolHandle.h:215
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:309
bool setEnabled(const bool flag)
Definition: ToolHandle.h:100
bool isPublic() const noexcept
Definition: ToolHandle.h:40
void disable()
Definition: ToolHandle.h:98
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:68
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:90
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:52
struct deprecated("use MergingTransformer instead")]] ListTransformer
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
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:318
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:69
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:350
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:408
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:72
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:71
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:380
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:50
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:224
void push_back(Container &c, const Value &v, std::true_type)
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:313
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:142
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:292
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:311
STL class.
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:371
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:325
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
std::string typeAndName() const override
Definition: ToolHandle.h:260
Definition of the basic interface.
Definition: IInterface.h:244
StatusCode retrieve(DisableTool sd) override
Definition: ToolHandle.h:219
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:248
Gaudi::tagged_bool< class EnableTool_tag > EnableTool
Definition: ToolHandle.h:28
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:77
void enable()
Definition: ToolHandle.h:96
T move(T...args)
Gaudi::tagged_bool< class DisableTool_tag > DisableTool
Definition: ToolHandle.h:27
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:310
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:287
T * operator->()
Definition: GaudiHandle.h:261
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:125
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:200
Alias for backward compatibility.
Definition: Algorithm.h:56
IInterface compliant class extending IInterface with the name() method.
T find(T...args)
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
const IAlgTool * getAsIAlgTool() const override
Definition: ToolHandle.h:282
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:308
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:244
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:361
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:89
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:115
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:407
STL class.
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:253
Helper functions to set/get the application return code.
Definition: __init__.py:1
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:179
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:414
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:188
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:31
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:387
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:239
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:34
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:393
StatusCode retrieve(EnableTool sd) override
Definition: ToolHandle.h:228
bool createIf() const noexcept
Definition: ToolHandle.h:42