ATLAS Offline Software
Loading...
Searching...
No Matches
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
15template< class T >
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:
19 if( m_ptool ) {
20 m_name = m_ptool->name();
21 }
22}
23
24template< class T >
25ToolHandle< T >::ToolHandle( const std::string& typeAndName, INamedInterface* parent )
26: ToolHandleBase( typeAndName , parent ), m_ptool( 0 ) {
27
28}
29
30template<typename T> template<typename T2>
31ToolHandle<T> ::
32ToolHandle (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
40template< class T >
41PublicToolHandle< T >::PublicToolHandle( const std::string& typeAndName )
42: ToolHandle< T >( typeAndName, nullptr ) {
43
44}
45
46template<typename T> template<typename T2>
47PublicToolHandle<T> ::
48PublicToolHandle (T2 *parent, const std::string& propertyName,
49 const std::string& toolName,
50 const std::string& propertyTitle)
51 : PublicToolHandle (toolName)
52{
53 parent->declareProperty (propertyName, *this, propertyTitle);
54}
55
56template< class T >
57T& ToolHandle< T >::operator*() {
58
59 // Retrieve the tool pointer if necessary:
60 if( ! m_ptool ) {
61 m_ptool = asg::ToolStore::get< T >( name() );
62 }
63 // Check if we succeeded:
64 if( ! m_ptool ) {
65 throw std::runtime_error( "Couldn't find tool with name \"" +
66 name() + "\"" );
67 }
68
69 // Return a reference to the tool:
70 return *m_ptool;
71}
72
73template< class T >
74const T& ToolHandle< T >::operator*() const {
75
76 // Retrieve the tool pointer if necessary:
77 if( ! m_ptool ) {
78 m_ptool = asg::ToolStore::get< T >( name() );
79 }
80 // Check if we succeeded:
81 if( ! m_ptool ) {
82 throw std::runtime_error( "Couldn't find tool with name \"" +
83 name() + "\"" );
84 }
85
86 // Return a reference to the tool:
87 return *m_ptool;
88}
89
90template< class T >
91T* ToolHandle<T>::operator->() {
92
93 // Retrieve the tool pointer if necessary:
94 if( ! m_ptool ) {
95 m_ptool = asg::ToolStore::get< T >( name() );
96 }
97 // Check if we succeeded:
98 if( ! m_ptool ) {
99 throw std::runtime_error( "Couldn't find tool with name \"" +
100 name() + "\"" );
101 }
102
103 // Return the (possibly null-)pointer to the tool:
104 return m_ptool;
105}
106
107template< class T >
108const T* ToolHandle<T>::operator->() const {
109
110 // Retrieve the tool pointer if necessary:
111 if( ! m_ptool ) {
112 m_ptool = asg::ToolStore::get< T >( name() );
113 }
114 // Check if we succeeded:
115 if( ! m_ptool ) {
116 throw std::runtime_error( "Couldn't find tool with name \"" +
117 name() + "\"" );
118 }
119
120 // Return the (possibly null-)pointer to the tool:
121 return m_ptool;
122}
123
124template< class T >
125StatusCode ToolHandle< T >::retrieve() const {
126
127 // If we have the tool already, there's nothing to do:
128 if( m_ptool ) {
129 return StatusCode::SUCCESS;
130 }
131
132 // Try to retrieve the tool:
133 m_ptool = asg::ToolStore::get< T >( name() );
134
135 // Check if it succeeded:
136 if( m_ptool ) {
137 return StatusCode::SUCCESS;
138 } else {
139 return StatusCode::FAILURE;
140 }
141}
142
143template< class T >
144void ToolHandle< T >::disable () noexcept {
145 m_ptool = nullptr;
146}
147
148template< class T >
149bool ToolHandle< T >::empty() const {
150
151 return ( ( m_ptool == 0 ) && ( name().size() == 0 ) );
152}
153
154template< class T >
155bool ToolHandle< T >::isSet() const {
156
157 return ( !( m_ptool == 0 ) );
158}
159
160template< class T >
161std::ostream& operator<<( std::ostream& out,
162 const ToolHandle< T >& handle ) {
163
164 out << "ASG ToolHandle with name = \"" << handle.name() << "\", pointer = ";
165 const T* ptool = nullptr;
166 if (handle.retrieve())
167 ptool = handle.operator->();
168 out << ptool;
169
170 // Return the same stream object:
171 return out;
172}
173
174namespace asg
175{
176 namespace detail
177 {
178 StatusCode packStringSingle (const std::string& value,
179 std::string& result);
180
181 template<typename T> struct GetStringHelper;
182
183 template<typename T> struct GetStringHelper<ToolHandle<T> >
184 {
185 static StatusCode get (const ToolHandle<T>& value,
186 std::string& result) {
187 return packStringSingle (value.typeAndName(), result);
188 }
189 };
190
191 template<typename T> struct GetStringHelper<PublicToolHandle<T> >
192 : public GetStringHelper<ToolHandle<T> >
193 {
194 };
195 }
196}
197
198#endif // ASGTOOLS_TOOLHANDLE_ICC