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/ServiceHandle.h"
7 #include "GaudiKernel/IToolSvc.h"
8 #include "GaudiKernel/INamedInterface.h"
9 //#include "GaudiKernel/SmartIF.h"
10 
11 #include <string>
12 #include <vector>
13 #include <stdexcept>
14 
15 // forward declarations
16 class IInterface;
17 class IAlgTool;
18 class IToolSvc;
19 
21 class ToolHandleInfo {
22 protected:
23  ToolHandleInfo( const IInterface* parent = 0, bool createIf = true )
25  {}
26 
27 public:
28  virtual ~ToolHandleInfo() = default;
29 
30  bool isPublic() const { return !m_parent; }
31 
32  bool createIf() const { return m_createIf; }
33 
34  const IInterface* parent() const { return m_parent; }
35 
36  //
37  // Some helper functions
38  //
39  const std::string toolComponentType( const IInterface* parent ) const {
40  return parent ? "PrivateTool" : "PublicTool";
41  }
42 
43  const std::string toolParentName( const IInterface* parent ) const {
44  if (!parent) return "ToolSvc";
45  const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
46  return pNamed ? pNamed->name() : "" ;
47  }
48 
49 private:
50  const IInterface* m_parent;
51  bool m_createIf;
52 };
53 
64 template< class T >
65 class ToolHandle : public ToolHandleInfo, public GaudiHandle<T> {
66 public:
67  //
68  // Constructors etc.
69  //
73  ToolHandle( int ) = delete;
74  ToolHandle( std::nullptr_t ) = delete;
75 
76  explicit ToolHandle( const IInterface* parent = 0, bool createIf = true )
77  : ToolHandleInfo(parent,createIf),
80  ToolHandleInfo::toolParentName(parent) ),
81  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
82  {}
83 
101  ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = 0, bool createIf = true )
102  : ToolHandleInfo(parent,createIf),
103  GaudiHandle<T>( toolTypeAndName,
105  ToolHandleInfo::toolParentName(parent) ),
106  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
107  {}
108 
111  StatusCode retrieve() const { // not really const, because it updates m_pObject
112  return GaudiHandle<T>::retrieve();
113  }
114 
117  StatusCode release() const { // not really const, because it updates m_pObject
118  return GaudiHandle<T>::release();
119  }
120 
122  virtual StatusCode retrieve( T*& algTool ) const {
123  return m_pToolSvc->retrieve( GaudiHandleBase::typeAndName(), T::interfaceID(),
124  (IAlgTool*&)(algTool),
126  }
127 
129  virtual StatusCode release( T* algTool ) const {
130  return m_pToolSvc->releaseTool( algTool );
131  }
132 
133 private:
134  //
135  // Private data members
136  //
138 };
139 
150 template < class T >
151 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
152 public:
153  //
154  // Constructors
155  //
162  ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
163  const IInterface* parent = 0, bool createIf = true )
164  : ToolHandleInfo( parent, createIf ),
165  GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
167  ToolHandleInfo::toolParentName(parent) )
168  {}
169 
174  ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
175  : ToolHandleInfo( parent, createIf ),
177  ToolHandleInfo::toolParentName(parent) )
178  { }
179 
184  virtual bool push_back( const std::string& toolTypeAndName ) {
185  ToolHandle<T> handle( toolTypeAndName,
189  return true;
190  }
191 
193  virtual bool push_back( const ToolHandle<T>& myHandle ) {
194  return push_back( myHandle.typeAndName() );
195  }
196 
197 };
198 
199 
200 template <class T>
201 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
202  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
203 }
204 
205 
206 template <class T>
207 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
208  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
209 }
210 
211 
212 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:17
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:159
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:250
const std::string toolParentName(const IInterface *parent) const
Definition: ToolHandle.h:43
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:22
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:49
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:199
ToolHandle(int)=delete
Constructor for a tool with default tool type and name.
virtual const std::string & name() const =0
Retrieve the name of the instance.
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:137
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
const IInterface * parent() const
Definition: ToolHandle.h:34
Definition of the basic interface.
Definition: IInterface.h:234
const std::string toolComponentType(const IInterface *parent) const
Definition: ToolHandle.h:39
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:189
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:48
const IInterface * m_parent
Definition: ToolHandle.h:50
bool createIf() const
Definition: ToolHandle.h:32
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:111
bool isPublic() const
Definition: ToolHandle.h:30
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:20
IInterface compliant class extending IInterface with the name() method.
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:22
virtual ~ToolHandleInfo()=default
virtual bool push_back(const std::string &toolTypeAndName)
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:184
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:81
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:108
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=0, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:162
ToolHandleInfo(const IInterface *parent=0, bool createIf=true)
Definition: ToolHandle.h:23
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:21
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:117
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:364