2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
5 /// @author Nils Krumnack
6 /// @author David Adams <dladams@bnl.gov> (for original implementation for tools)
10 #ifndef ASGSERVICES_SERVICEHANDLE_ICC
11 #define ASGSERVICES_SERVICEHANDLE_ICC
18 #include "AsgServices/ServiceStore.h"
21 ServiceHandle< T >::ServiceHandle( const std::shared_ptr<T>& pservice,
22 const std::string& parentName)
23 : ServiceHandleBase("",parentName), m_pservice( pservice )
25 // Set the name in the base class in case we received a valid pointer:
27 m_name = m_pservice->name();
32 ServiceHandle< T >::ServiceHandle( const std::string& typeAndName,
33 const std::string& parentName )
34 : ServiceHandleBase( typeAndName , parentName )
37 template<typename T> template<typename T2>
39 ServiceHandle (T2 *parent, const std::string& propertyName,
40 const std::string& serviceName,
41 const std::string& propertyTitle)
42 : ServiceHandle (serviceName, parent->name())
44 parent->declareProperty (propertyName, *this, propertyTitle);
48 T& ServiceHandle< T >::operator*()
50 // Retrieve the service pointer if necessary:
52 m_pservice = asg::ServiceStore::get< T >( name() );
54 // Check if we succeeded:
56 throw std::runtime_error( "Couldn't find service with name \"" +
59 // Return a reference to the service:
64 const T& ServiceHandle< T >::operator*() const
66 // Retrieve the service pointer if necessary:
68 m_pservice = asg::ServiceStore::get< T >( name() );
70 // Check if we succeeded:
72 throw std::runtime_error( "Couldn't find service with name \"" +
75 // Return a reference to the service:
80 T* ServiceHandle<T>::operator->()
82 // Retrieve the service pointer if necessary:
84 m_pservice = asg::ServiceStore::get< T >( name() );
86 // Check if we succeeded:
88 throw std::runtime_error( "Couldn't find service with name \"" +
92 // Return the (possibly null-)pointer to the service:
93 return m_pservice.get();
97 const T* ServiceHandle<T>::operator->() const
100 // Retrieve the service pointer if necessary:
102 m_pservice = asg::ServiceStore::get< T >( name() );
104 // Check if we succeeded:
106 throw std::runtime_error( "Couldn't find service with name \"" +
109 // Return the (possibly null-)pointer to the service:
110 return m_pservice.get();
114 StatusCode ServiceHandle< T >::retrieve() const
116 // If we have the service already, there's nothing to do:
118 return StatusCode::SUCCESS;
120 // Try to retrieve the service:
121 m_pservice = asg::ServiceStore::get< T >( name() );
123 // Check if it succeeded:
125 return StatusCode::SUCCESS;
127 return StatusCode::FAILURE;
132 void ServiceHandle< T >::disable () noexcept
138 bool ServiceHandle< T >::empty() const
140 return ( ( m_pservice == nullptr ) && ( name().size() == 0 ) );
144 bool ServiceHandle< T >::isSet() const
146 return ( !( m_pservice == nullptr ) );
150 std::ostream& operator<<( std::ostream& out,
151 const ServiceHandle< T >& handle )
153 out << "ASG ServiceHandle with name = \"" << handle.name() << "\", pointer = ";
154 const T* pservice = nullptr;
155 if (handle.retrieve())
159 // Return the same stream object:
167 StatusCode packStringSingle (const std::string& value,
168 std::string& result);
170 template<typename T> struct GetStringHelper;
172 template<typename T> struct GetStringHelper<ServiceHandle<T> >
174 static StatusCode get (const ServiceHandle<T>& value,
175 std::string& result) {
176 return packStringSingle (value.typeAndName(), result);
182 #endif // ASGSERVICES_SERVICEHANDLE_ICC