ATLAS Offline Software
ToolHandle.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ASGTOOLS_TOOLHANDLE_ICC
6 #define ASGTOOLS_TOOLHANDLE_ICC
7 
8 // System include(s):
9 #include <stdexcept>
10 #include <iostream>
11 
12 // Local include(s):
13 #include "AsgTools/ToolStore.h"
14 
15 template< class T >
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:
19  if( m_ptool ) {
20  m_name = m_ptool->name();
21  }
22 }
23 
24 template< class T >
25 ToolHandle< T >::ToolHandle( const std::string& typeAndName, INamedInterface* parent )
26 : ToolHandleBase( typeAndName , parent ), m_ptool( 0 ) {
27 
28 }
29 
30 template<typename T> template<typename T2>
31 ToolHandle<T> ::
32 ToolHandle (T2 *parent, const std::string& propertyName,
33  const std::string& toolName,
34  const std::string& propertyTitle)
35  : ToolHandle (toolName, parent)
36 {
37  parent->declareProperty (propertyName, *this, propertyTitle);
38 }
39 
40 template< class T >
41 T& ToolHandle< T >::operator*() {
42 
43  // Retrieve the tool pointer if necessary:
44  if( ! m_ptool ) {
45  m_ptool = asg::ToolStore::get< T >( name() );
46  }
47  // Check if we succeeded:
48  if( ! m_ptool ) {
49  throw std::runtime_error( "Couldn't find tool with name \"" +
50  name() + "\"" );
51  }
52 
53  // Return a reference to the tool:
54  return *m_ptool;
55 }
56 
57 template< class T >
58 const T& ToolHandle< T >::operator*() const {
59 
60  // Retrieve the tool pointer if necessary:
61  if( ! m_ptool ) {
62  m_ptool = asg::ToolStore::get< T >( name() );
63  }
64  // Check if we succeeded:
65  if( ! m_ptool ) {
66  throw std::runtime_error( "Couldn't find tool with name \"" +
67  name() + "\"" );
68  }
69 
70  // Return a reference to the tool:
71  return *m_ptool;
72 }
73 
74 template< class T >
75 T* ToolHandle<T>::operator->() {
76 
77  // Retrieve the tool pointer if necessary:
78  if( ! m_ptool ) {
79  m_ptool = asg::ToolStore::get< T >( name() );
80  }
81  // Check if we succeeded:
82  if( ! m_ptool ) {
83  throw std::runtime_error( "Couldn't find tool with name \"" +
84  name() + "\"" );
85  }
86 
87  // Return the (possibly null-)pointer to the tool:
88  return m_ptool;
89 }
90 
91 template< class T >
92 const T* ToolHandle<T>::operator->() const {
93 
94  // Retrieve the tool pointer if necessary:
95  if( ! m_ptool ) {
96  m_ptool = asg::ToolStore::get< T >( name() );
97  }
98  // Check if we succeeded:
99  if( ! m_ptool ) {
100  throw std::runtime_error( "Couldn't find tool with name \"" +
101  name() + "\"" );
102  }
103 
104  // Return the (possibly null-)pointer to the tool:
105  return m_ptool;
106 }
107 
108 template< class T >
109 StatusCode ToolHandle< T >::retrieve() const {
110 
111  // If we have the tool already, there's nothing to do:
112  if( m_ptool ) {
113  return StatusCode::SUCCESS;
114  }
115 
116  // Try to retrieve the tool:
117  m_ptool = asg::ToolStore::get< T >( name() );
118 
119  // Check if it succeeded:
120  if( m_ptool ) {
121  return StatusCode::SUCCESS;
122  } else {
123  return StatusCode::FAILURE;
124  }
125 }
126 
127 template< class T >
128 void ToolHandle< T >::disable () noexcept {
129  m_ptool = nullptr;
130 }
131 
132 template< class T >
133 bool ToolHandle< T >::empty() const {
134 
135  return ( ( m_ptool == 0 ) && ( name().size() == 0 ) );
136 }
137 
138 template< class T >
139 bool ToolHandle< T >::isSet() const {
140 
141  return ( !( m_ptool == 0 ) );
142 }
143 
144 template< class T >
145 std::ostream& operator<<( std::ostream& out,
146  const ToolHandle< T >& handle ) {
147 
148  out << "ASG ToolHandle with name = \"" << handle.name() << "\", pointer = ";
149  const T* ptool = nullptr;
150  if (handle.retrieve())
151  ptool = handle.operator->();
152  out << ptool;
153 
154  // Return the same stream object:
155  return out;
156 }
157 
158 namespace asg
159 {
160  namespace detail
161  {
162  StatusCode packStringSingle (const std::string& value,
163  std::string& result);
164 
165  template<typename T> struct GetStringHelper;
166 
167  template<typename T> struct GetStringHelper<ToolHandle<T> >
168  {
169  static StatusCode get (const ToolHandle<T>& value,
170  std::string& result) {
171  return packStringSingle (value.typeAndName(), result);
172  }
173  };
174  }
175 }
176 
177 #endif // ASGTOOLS_TOOLHANDLE_ICC