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"
16 ToolHandle< 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();
25 ToolHandle< T >::ToolHandle( const std::string& typeAndName, INamedInterface* parent )
26 : ToolHandleBase( typeAndName , parent ), m_ptool( 0 ) {
30 template<typename T> template<typename T2>
32 ToolHandle (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);
41 T& ToolHandle< T >::operator*() {
43 // Retrieve the tool pointer if necessary:
45 m_ptool = asg::ToolStore::get< T >( name() );
47 // Check if we succeeded:
49 throw std::runtime_error( "Couldn't find tool with name \"" +
53 // Return a reference to the tool:
58 const T& ToolHandle< T >::operator*() const {
60 // Retrieve the tool pointer if necessary:
62 m_ptool = asg::ToolStore::get< T >( name() );
64 // Check if we succeeded:
66 throw std::runtime_error( "Couldn't find tool with name \"" +
70 // Return a reference to the tool:
75 T* ToolHandle<T>::operator->() {
77 // Retrieve the tool pointer if necessary:
79 m_ptool = asg::ToolStore::get< T >( name() );
81 // Check if we succeeded:
83 throw std::runtime_error( "Couldn't find tool with name \"" +
87 // Return the (possibly null-)pointer to the tool:
92 const T* ToolHandle<T>::operator->() const {
94 // Retrieve the tool pointer if necessary:
96 m_ptool = asg::ToolStore::get< T >( name() );
98 // Check if we succeeded:
100 throw std::runtime_error( "Couldn't find tool with name \"" +
104 // Return the (possibly null-)pointer to the tool:
109 StatusCode ToolHandle< T >::retrieve() const {
111 // If we have the tool already, there's nothing to do:
113 return StatusCode::SUCCESS;
116 // Try to retrieve the tool:
117 m_ptool = asg::ToolStore::get< T >( name() );
119 // Check if it succeeded:
121 return StatusCode::SUCCESS;
123 return StatusCode::FAILURE;
128 void ToolHandle< T >::disable () noexcept {
133 bool ToolHandle< T >::empty() const {
135 return ( ( m_ptool == 0 ) && ( name().size() == 0 ) );
139 bool ToolHandle< T >::isSet() const {
141 return ( !( m_ptool == 0 ) );
145 std::ostream& operator<<( std::ostream& out,
146 const ToolHandle< T >& handle ) {
148 out << "ASG ToolHandle with name = \"" << handle.name() << "\", pointer = ";
149 const T* ptool = nullptr;
150 if (handle.retrieve())
151 ptool = handle.operator->();
154 // Return the same stream object:
162 StatusCode packStringSingle (const std::string& value,
163 std::string& result);
165 template<typename T> struct GetStringHelper;
167 template<typename T> struct GetStringHelper<ToolHandle<T> >
169 static StatusCode get (const ToolHandle<T>& value,
170 std::string& result) {
171 return packStringSingle (value.typeAndName(), result);
177 #endif // ASGTOOLS_TOOLHANDLE_ICC