Warning, file /include/GaudiKernel/ServiceHandle.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
0012 #define GAUDIKERNEL_SERVICEHANDLE_H
0013
0014
0015 #include <Gaudi/Concepts.h>
0016 #include <GaudiKernel/Bootstrap.h>
0017 #include <GaudiKernel/GaudiException.h>
0018 #include <GaudiKernel/GaudiHandle.h>
0019 #include <GaudiKernel/IMessageSvc.h>
0020 #include <GaudiKernel/IProperty.h>
0021 #include <GaudiKernel/ISvcLocator.h>
0022 #include <GaudiKernel/MsgStream.h>
0023 #include <GaudiKernel/ServiceLocatorHelper.h>
0024
0025 #include <stdexcept>
0026 #include <string>
0027 #include <type_traits>
0028
0029
0030 class IAlgTool;
0031 class IToolSvc;
0032 class ServiceHandleProperty;
0033
0034
0035
0036
0037
0038
0039
0040
0041 template <class T>
0042 class ServiceHandle : public GaudiHandle<T> {
0043 public:
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 ServiceHandle( const std::string& serviceName, const std::string& theParentName )
0055 : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
0056
0057
0058 template <typename CT = T, typename NCT = std::remove_const_t<T>>
0059 requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
0060 ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
0061
0062
0063
0064 template <std::derived_from<IProperty> OWNER>
0065 inline ServiceHandle( OWNER* owner, std::string PropName, const std::string& svcName, std::string doc = "" )
0066 : ServiceHandle( svcName, owner->name() ) {
0067 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
0068 p->template setOwnerType<OWNER>();
0069 }
0070
0071 StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
0072
0073 GaudiHandleBase::setTypeAndName( serviceName );
0074 GaudiHandleBase::setParentName( theParentName );
0075
0076 return StatusCode::SUCCESS;
0077 }
0078
0079
0080
0081 using GaudiHandle<T>::retrieve;
0082
0083
0084
0085
0086
0087
0088
0089
0090 T* get() const { return ::details::nonConst( GaudiHandle<T>::get() ); }
0091
0092
0093 T* operator->() const { return ::details::nonConst( GaudiHandle<T>::operator->() ); }
0094 T& operator*() const { return *::details::nonConst( GaudiHandle<T>::operator->() ); }
0095
0096 protected:
0097
0098 StatusCode retrieve( T*& service ) const override { return i_retrieve( service ); }
0099
0100
0101 template <Gaudi::IsInterface I = T>
0102 StatusCode i_retrieve( I*& service ) const {
0103 const ServiceLocatorHelper helper( *serviceLocator(), GaudiHandleBase::messageName(), this->parentName() );
0104 return helper.getService( GaudiHandleBase::typeAndName(), true, I::interfaceID(), (void**)&service );
0105 }
0106
0107
0108 template <typename I = T>
0109 requires( !Gaudi::IsInterface<I> )
0110 StatusCode i_retrieve( I*& service ) const {
0111 IService* svc = nullptr;
0112 return i_retrieve( svc ).andThen( [&] {
0113 service = dynamic_cast<I*>( svc );
0114 if ( !service )
0115 throw GaudiException( "unable to dcast Service " + this->typeAndName() + " to " +
0116 System::typeinfoName( typeid( T ) ),
0117 this->typeAndName() + " retrieve", StatusCode::FAILURE );
0118 } );
0119 }
0120
0121
0122
0123
0124
0125
0126 private:
0127
0128
0129
0130 SmartIF<ISvcLocator>& serviceLocator() const {
0131 if ( !m_pSvcLocator ) {
0132 m_pSvcLocator = Gaudi::svcLocator();
0133 if ( !m_pSvcLocator ) {
0134 throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
0135 }
0136 }
0137 return m_pSvcLocator;
0138 }
0139
0140 SmartIF<IMessageSvc>& messageSvc() const {
0141 if ( !m_pMessageSvc ) {
0142 m_pMessageSvc = serviceLocator();
0143 if ( !m_pMessageSvc ) {
0144 throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
0145 }
0146 }
0147 return m_pMessageSvc;
0148 }
0149
0150
0151
0152 mutable SmartIF<ISvcLocator> m_pSvcLocator;
0153 mutable SmartIF<IMessageSvc> m_pMessageSvc;
0154 };
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166 template <class T>
0167 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
0168 public:
0169
0170
0171
0172
0173
0174 ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
0175 const std::string& myParentName )
0176 : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
0177
0178 virtual ~ServiceHandleArray() {}
0179
0180 ServiceHandleArray( const std::string& myParentName )
0181 : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
0182
0183 virtual bool push_back( const std::string& serviceTypeAndName ) {
0184 ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
0185 GaudiHandleArray<ServiceHandle<T>>::push_back( handle );
0186 return true;
0187 }
0188
0189 virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
0190 };
0191
0192 template <class T>
0193 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
0194 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
0195 }
0196
0197 template <class T>
0198 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
0199 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
0200 }
0201
0202 #endif