2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5#ifndef ASGTOOLS_TOOLHANDLE_ICC
6#define ASGTOOLS_TOOLHANDLE_ICC
13#include "AsgTools/ToolStore.h"
16ToolHandle< T >::ToolHandle( T* ptool )
17: ToolHandleBase(), m_ptool( ptool ) {
18 // Set the name in the base class in case we received a valid pointer:
20 m_name = m_ptool->name();
25ToolHandle< T >::ToolHandle( const std::string& typeAndName, INamedInterface* parent )
26: ToolHandleBase( typeAndName , parent ), m_ptool( 0 ) {
30template<typename T> template<typename T2>
32ToolHandle (T2 *parent, const std::string& propertyName,
33 const std::string& toolName,
34 const std::string& propertyTitle)
35 : ToolHandle (toolName, parent)
37 parent->declareProperty (propertyName, *this, propertyTitle);
41PublicToolHandle< T >::PublicToolHandle( const std::string& typeAndName )
42: ToolHandle< T >( typeAndName, nullptr ) {
46template<typename T> template<typename T2>
48PublicToolHandle (T2 *parent, const std::string& propertyName,
49 const std::string& toolName,
50 const std::string& propertyTitle)
51 : PublicToolHandle (toolName)
53 parent->declareProperty (propertyName, *this, propertyTitle);
57T& ToolHandle< T >::operator*() {
59 // Retrieve the tool pointer if necessary:
61 m_ptool = asg::ToolStore::get< T >( name() );
63 // Check if we succeeded:
65 throw std::runtime_error( "Couldn't find tool with name \"" +
69 // Return a reference to the tool:
74const T& ToolHandle< T >::operator*() const {
76 // Retrieve the tool pointer if necessary:
78 m_ptool = asg::ToolStore::get< T >( name() );
80 // Check if we succeeded:
82 throw std::runtime_error( "Couldn't find tool with name \"" +
86 // Return a reference to the tool:
91T* ToolHandle<T>::operator->() {
93 // Retrieve the tool pointer if necessary:
95 m_ptool = asg::ToolStore::get< T >( name() );
97 // Check if we succeeded:
99 throw std::runtime_error( "Couldn't find tool with name \"" +
103 // Return the (possibly null-)pointer to the tool:
108const T* ToolHandle<T>::operator->() const {
110 // Retrieve the tool pointer if necessary:
112 m_ptool = asg::ToolStore::get< T >( name() );
114 // Check if we succeeded:
116 throw std::runtime_error( "Couldn't find tool with name \"" +
120 // Return the (possibly null-)pointer to the tool:
125StatusCode ToolHandle< T >::retrieve() const {
127 // If we have the tool already, there's nothing to do:
129 return StatusCode::SUCCESS;
132 // Try to retrieve the tool:
133 m_ptool = asg::ToolStore::get< T >( name() );
135 // Check if it succeeded:
137 return StatusCode::SUCCESS;
139 return StatusCode::FAILURE;
144void ToolHandle< T >::disable () noexcept {
149bool ToolHandle< T >::empty() const {
151 return ( ( m_ptool == 0 ) && ( name().size() == 0 ) );
155bool ToolHandle< T >::isSet() const {
157 return ( !( m_ptool == 0 ) );
161std::ostream& operator<<( std::ostream& out,
162 const ToolHandle< T >& handle ) {
164 out << "ASG ToolHandle with name = \"" << handle.name() << "\", pointer = ";
165 const T* ptool = nullptr;
166 if (handle.retrieve())
167 ptool = handle.operator->();
170 // Return the same stream object:
178 StatusCode packStringSingle (const std::string& value,
179 std::string& result);
181 template<typename T> struct GetStringHelper;
183 template<typename T> struct GetStringHelper<ToolHandle<T> >
185 static StatusCode get (const ToolHandle<T>& value,
186 std::string& result) {
187 return packStringSingle (value.typeAndName(), result);
191 template<typename T> struct GetStringHelper<PublicToolHandle<T> >
192 : public GetStringHelper<ToolHandle<T> >
198#endif // ASGTOOLS_TOOLHANDLE_ICC