Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ServiceHandle.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
13 #include <Gaudi/Concepts.h>
14 #include <GaudiKernel/Bootstrap.h>
18 #include <GaudiKernel/IProperty.h>
20 #include <GaudiKernel/MsgStream.h>
22 #include <stdexcept>
23 #include <string>
24 #include <type_traits>
25 
26 class IAlgTool;
27 class IToolSvc;
28 class ServiceHandleProperty;
29 
37 template <class T>
38 class ServiceHandle : public GaudiHandle<T> {
39 public:
47  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
48  : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
49 
51  template <typename CT = T, typename NCT = std::remove_const_t<T>>
52  requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
53  ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
54 
57  template <std::derived_from<IProperty> OWNER>
58  inline ServiceHandle( OWNER* owner, std::string PropName, const std::string& svcName, std::string doc = "" )
59  : ServiceHandle( svcName, owner->name() ) {
60  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
61  p->template setOwnerType<OWNER>();
62  }
63 
64  StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
65 
66  GaudiHandleBase::setTypeAndName( serviceName );
67  GaudiHandleBase::setParentName( theParentName );
68 
69  return StatusCode::SUCCESS;
70  }
71 
75 
78 
82 
83 protected:
85  StatusCode retrieve( T*& service ) const override {
87  if ( auto svc = helper.service<T>( GaudiHandleBase::typeAndName(), /* quiet = */ true, /* createIf = */ true ) ) {
88  svc->addRef();
89  service = svc.get();
90  return StatusCode::SUCCESS;
91  }
92  return StatusCode::FAILURE;
93  }
94 
95 private:
96  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
97  if ( !m_pSvcLocator ) {
99  if ( !m_pSvcLocator ) {
100  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
101  }
102  }
103  return m_pSvcLocator;
104  }
105 
106  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
107  if ( !m_pMessageSvc ) {
108  m_pMessageSvc = serviceLocator(); // default message service
109  if ( !m_pMessageSvc ) {
110  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
111  }
112  }
113  return m_pMessageSvc;
114  }
115 
118 };
119 
130 template <class T>
131 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
132 public:
135  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
136  const std::string& myParentName )
137  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
138 
139  virtual ~ServiceHandleArray() {}
140 
141  ServiceHandleArray( const std::string& myParentName )
142  : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
143 
144  virtual bool push_back( const std::string& serviceTypeAndName ) {
145  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
147  return true;
148  }
149 
150  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
151 };
152 
153 template <class T>
154 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
155  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
156 }
157 
158 template <class T>
159 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
160  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
161 }
ServiceHandleArray::push_back
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
Definition: ServiceHandle.h:144
ServiceLocatorHelper::service
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:50
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:62
GaudiHandle.h
IAlgTool
Definition: IAlgTool.h:29
GaudiHandleBase::messageName
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:52
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Definition: ServiceHandle.h:96
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:141
GaudiException.h
GaudiHandleArray< ServiceHandle< T > >::push_back
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle with given type and name.
ServiceHandle
Definition: ServiceHandle.h:38
GaudiException
Definition: GaudiException.h:29
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:64
GaudiHandleBase::name
std::string name() const
The instance name: the part after the '/'.
Definition: GaudiHandle.cpp:32
ServiceHandleArray::push_back
virtual bool push_back(const ServiceHandle< T > &myHandle)
Definition: ServiceHandle.h:150
IMessageSvc.h
GaudiHandleInfo::setParentName
void setParentName(std::string parent)
The name of the parent.
Definition: GaudiHandle.h:84
GaudiHandle
Definition: GaudiHandle.h:176
ServiceHandleArray::~ServiceHandleArray
virtual ~ServiceHandleArray()
Definition: ServiceHandle.h:139
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: ServiceHandle.h:135
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
details::nonConst
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:26
ServiceHandle::get
T * get() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:77
StatusCode
Definition: StatusCode.h:64
ServiceHandle::ServiceHandle
ServiceHandle(OWNER *owner, std::string PropName, const std::string &svcName, std::string doc="")
Autodeclaring constructor with property name, service type/name and documentation.
Definition: ServiceHandle.h:58
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:26
ServiceHandle::messageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:106
SmartIF< ISvcLocator >
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:85
ServiceHandle::operator*
T & operator*() const
Definition: ServiceHandle.h:81
ServiceHandle::operator->
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:80
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:47
operator<<
std::ostream & operator<<(std::ostream &os, const ServiceHandle< T > &handle)
Definition: ServiceHandle.h:154
ServiceHandleArray
Definition: ServiceHandle.h:131
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
ServiceLocatorHelper.h
Concepts.h
Bootstrap.h
ServiceHandle::requires
requires(std::is_const_v< CT > &&!std::is_same_v< CT, NCT >) ServiceHandle(const ServiceHandle< NCT > &other)
Copy constructor from a non const T to const T service handle.
Definition: ServiceHandle.h:52
GaudiHandleBase::typeAndName
const std::string & typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:128
IProperty.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
ServiceHandle::m_pSvcLocator
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:116
ISvcLocator.h
IToolSvc
Definition: IToolSvc.h:28
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:397
ServiceHandle::m_pMessageSvc
SmartIF< IMessageSvc > m_pMessageSvc
Definition: ServiceHandle.h:117
MsgStream.h
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19