The Gaudi Framework  v29r0 (ff2e7097)
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 
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 
26 {
27 
28 protected:
29  ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
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 
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 
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 )
114  , GaudiHandle<T>( GaudiHandle<T>::getDefaultType(), toolComponentType( parent ), toolParentName( parent ) )
115  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
116  {
117  }
118 
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 )
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 );
191 
192  m_parent = parent;
194 
195  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
196  }
197 
201  { // not really const, because it updates m_pObject
202  return GaudiHandle<T>::retrieve();
203  }
204 
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 
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(),
262  }
263 
264 private:
265  //
266  // Private data members
267  //
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 
291  const PublicToolHandle<NCT>& other,
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 )
346  {
347  }
348 
353  ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
357  {
358  }
359 
364  bool push_back( const std::string& toolTypeAndName ) override
365  {
366  ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
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>
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
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:112
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:391
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
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
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:157
#define __attribute__(x)
Definition: System.cpp:79
T empty(T...args)
const IInterface * parent() const noexcept
Definition: ToolHandle.h:41
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:309
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
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:268
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:221
bool isPublic() const noexcept
Definition: ToolHandle.h:37
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:62
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:49
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:67
Array of Handles to be used in lieu of vector of naked pointers to tools.
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:47
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:395
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:71
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:65
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:364
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:47
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:227
void push_back(Container &c, const Value &v, std::true_type)
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:283
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
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:258
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:279
STL class.
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:353
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
std::string typeAndName() const override
Definition: ToolHandle.h:223
Definition of the basic interface.
Definition: IInterface.h:277
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:251
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:213
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:76
T move(T...args)
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:278
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:252
T * operator->()
Definition: GaudiHandle.h:265
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:200
Handle to be used in lieu of naked pointers to tools.
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:185
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
IInterface compliant class extending IInterface with the name() method.
T find(T...args)
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
const IInterface * m_parent
Definition: ToolHandle.h:56
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
const IAlgTool * getAsIAlgTool() const override
Definition: ToolHandle.h:246
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:275
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
virtual ~ToolHandleInfo()=default
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:213
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:341
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:83
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:111
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:394
STL class.
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
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(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
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
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
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:207
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:29
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:413
bool createIf() const noexcept
Definition: ToolHandle.h:39