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 "GaudiKernel/Bootstrap.h"
0016 #include "GaudiKernel/GaudiException.h"
0017 #include "GaudiKernel/GaudiHandle.h"
0018 #include "GaudiKernel/IMessageSvc.h"
0019 #include "GaudiKernel/IProperty.h"
0020 #include "GaudiKernel/ISvcLocator.h"
0021 #include "GaudiKernel/MsgStream.h"
0022 #include "GaudiKernel/ServiceLocatorHelper.h"
0023
0024 #include <stdexcept>
0025 #include <string>
0026 #include <type_traits>
0027
0028
0029 class IAlgTool;
0030 class IToolSvc;
0031 class ServiceHandleProperty;
0032
0033
0034
0035
0036
0037
0038
0039
0040 template <class T>
0041 class ServiceHandle : public GaudiHandle<T> {
0042 public:
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 ServiceHandle( const std::string& serviceName, const std::string& theParentName )
0054 : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
0055
0056
0057 template <typename CT = T, typename NCT = std::remove_const_t<T>,
0058 typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
0059 ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
0060
0061
0062
0063 template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
0064 inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
0065 : ServiceHandle( svcName, owner->name() ) {
0066 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
0067 p->template setOwnerType<OWNER>();
0068 }
0069
0070 StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
0071
0072 GaudiHandleBase::setTypeAndName( serviceName );
0073 GaudiHandleBase::setParentName( theParentName );
0074
0075 return StatusCode::SUCCESS;
0076 }
0077
0078
0079
0080 using GaudiHandle<T>::retrieve;
0081
0082
0083
0084
0085
0086
0087
0088
0089 T* get() const { return ::details::nonConst( GaudiHandle<T>::get() ); }
0090
0091
0092 T* operator->() const { return ::details::nonConst( GaudiHandle<T>::operator->() ); }
0093 T& operator*() const { return *::details::nonConst( GaudiHandle<T>::operator->() ); }
0094
0095 protected:
0096
0097 StatusCode retrieve( T*& service ) const override {
0098 const ServiceLocatorHelper helper( *serviceLocator(), GaudiHandleBase::messageName(), this->parentName() );
0099 return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
0100 }
0101
0102
0103
0104
0105
0106
0107 private:
0108
0109
0110
0111 SmartIF<ISvcLocator>& serviceLocator() const {
0112 if ( !m_pSvcLocator ) {
0113 m_pSvcLocator = Gaudi::svcLocator();
0114 if ( !m_pSvcLocator ) {
0115 throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
0116 }
0117 }
0118 return m_pSvcLocator;
0119 }
0120
0121 SmartIF<IMessageSvc>& messageSvc() const {
0122 if ( !m_pMessageSvc ) {
0123 m_pMessageSvc = serviceLocator();
0124 if ( !m_pMessageSvc ) {
0125 throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
0126 }
0127 }
0128 return m_pMessageSvc;
0129 }
0130
0131
0132
0133 mutable SmartIF<ISvcLocator> m_pSvcLocator;
0134 mutable SmartIF<IMessageSvc> m_pMessageSvc;
0135 };
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 template <class T>
0148 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
0149 public:
0150
0151
0152
0153
0154
0155 ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
0156 const std::string& myParentName )
0157 : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
0158
0159 virtual ~ServiceHandleArray() {}
0160
0161 ServiceHandleArray( const std::string& myParentName )
0162 : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
0163
0164 virtual bool push_back( const std::string& serviceTypeAndName ) {
0165 ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
0166 GaudiHandleArray<ServiceHandle<T>>::push_back( handle );
0167 return true;
0168 }
0169
0170 virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
0171 };
0172
0173 template <class T>
0174 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
0175 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
0176 }
0177
0178 template <class T>
0179 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
0180 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
0181 }
0182
0183 #endif