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