Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (f31105fd)
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 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
12 #define GAUDIKERNEL_SERVICEHANDLE_H
13 
14 // Includes
15 #include <Gaudi/Concepts.h>
16 #include <GaudiKernel/Bootstrap.h>
20 #include <GaudiKernel/IProperty.h>
22 #include <GaudiKernel/MsgStream.h>
24 
25 #include <stdexcept>
26 #include <string>
27 #include <type_traits>
28 
29 // class predeclarations
30 class IAlgTool;
31 class IToolSvc;
32 class ServiceHandleProperty;
33 
41 template <class T>
42 class ServiceHandle : public GaudiHandle<T> {
43 public:
44  //
45  // Constructors etc.
46  //
54  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
55  : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
56 
58  template <typename CT = T, typename NCT = std::remove_const_t<T>>
59  requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
60  ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
61 
64  template <std::derived_from<IProperty> OWNER>
65  inline ServiceHandle( OWNER* owner, std::string PropName, const std::string& svcName, std::string doc = "" )
66  : ServiceHandle( svcName, owner->name() ) {
67  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
68  p->template setOwnerType<OWNER>();
69  }
70 
71  StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
72 
73  GaudiHandleBase::setTypeAndName( serviceName );
74  GaudiHandleBase::setParentName( theParentName );
75 
76  return StatusCode::SUCCESS;
77  }
78 
82 
83  // /** Release the Service.
84  // Function must be repeated here to avoid hiding the function release( T*& ) */
85  // StatusCode release() const { // not really const, because it updates m_pObject
86  // return GaudiHandle<T>::release();
87  // }
88 
91 
95 
96 protected:
98  StatusCode retrieve( T*& service ) const override { return i_retrieve( service ); }
99 
101  template <Gaudi::IsInterface I = T>
102  StatusCode i_retrieve( I*& service ) const {
104  return helper.getService( GaudiHandleBase::typeAndName(), true, I::interfaceID(), (void**)&service );
105  }
106 
108  template <typename I = T>
109  requires( !Gaudi::IsInterface<I> )
110  StatusCode i_retrieve( I*& service ) const {
111  IService* svc = nullptr;
112  return i_retrieve( svc ).andThen( [&] {
113  service = dynamic_cast<I*>( svc );
114  if ( !service )
115  throw GaudiException( "unable to dcast Service " + this->typeAndName() + " to " +
116  System::typeinfoName( typeid( T ) ),
117  this->typeAndName() + " retrieve", StatusCode::FAILURE );
118  } );
119  }
120 
121  // /** Do the real release of the Service */
122  // virtual StatusCode release( T* service ) const {
123  // return service->release();
124  // }
125 
126 private:
127  //
128  // Private helper functions
129  //
130  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
131  if ( !m_pSvcLocator ) {
133  if ( !m_pSvcLocator ) {
134  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
135  }
136  }
137  return m_pSvcLocator;
138  }
139 
140  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
141  if ( !m_pMessageSvc ) {
142  m_pMessageSvc = serviceLocator(); // default message service
143  if ( !m_pMessageSvc ) {
144  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
145  }
146  }
147  return m_pMessageSvc;
148  }
149  //
150  // private data members
151  //
154 };
155 
166 template <class T>
167 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
168 public:
169  //
170  // Constructors
171  //
174  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
175  const std::string& myParentName )
176  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
177 
178  virtual ~ServiceHandleArray() {}
179 
180  ServiceHandleArray( const std::string& myParentName )
181  : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
182 
183  virtual bool push_back( const std::string& serviceTypeAndName ) {
184  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
186  return true;
187  }
188 
189  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
190 };
191 
192 template <class T>
193 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
194  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
195 }
196 
197 template <class T>
199  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
200 }
201 
202 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
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:183
IService
Definition: IService.h:28
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:65
GaudiHandle.h
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
GaudiHandleBase::messageName
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:52
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:164
std::move
T move(T... args)
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Definition: ServiceHandle.h:130
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:180
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:42
std::vector< std::string >
GaudiException
Definition: GaudiException.h:32
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:71
GaudiHandleBase::name
std::string name() const
The instance name: the part after the '/'.
Definition: GaudiHandle.cpp:32
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
ServiceHandleArray::push_back
virtual bool push_back(const ServiceHandle< T > &myHandle)
Definition: ServiceHandle.h:189
IMessageSvc.h
GaudiHandleInfo::setParentName
void setParentName(std::string parent)
The name of the parent.
Definition: GaudiHandle.h:87
GaudiHandle
Definition: GaudiHandle.h:179
ServiceHandleArray::~ServiceHandleArray
virtual ~ServiceHandleArray()
Definition: ServiceHandle.h:178
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: ServiceHandle.h:174
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:29
ServiceHandle::get
T * get() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:90
StatusCode
Definition: StatusCode.h:65
std::ostream
STL class.
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:65
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:27
ServiceHandle::messageSvc
SmartIF< IMessageSvc > & messageSvc() const
Definition: ServiceHandle.h:140
ServiceLocatorHelper::getService
StatusCode getService(std::string_view name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:52
SmartIF< ISvcLocator >
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:98
ServiceHandle::requires
requires(!Gaudi::IsInterface< I >) StatusCode i_retrieve(I *&service) const
retrieve the service for ServiceHandles<ActualService>
Definition: ServiceHandle.h:109
ServiceHandle::operator*
T & operator*() const
Definition: ServiceHandle.h:94
ServiceHandle::operator->
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:93
ServiceHandle::i_retrieve
StatusCode i_retrieve(I *&service) const
retrieve the service for ServiceHandles<ISomeInterfaces>
Definition: ServiceHandle.h:102
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:54
operator<<
std::ostream & operator<<(std::ostream &os, const ServiceHandle< T > &handle)
Definition: ServiceHandle.h:193
ServiceHandleArray
Definition: ServiceHandle.h:167
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
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:59
GaudiHandleBase::typeAndName
const std::string & typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:131
IProperty.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ServiceHandle::m_pSvcLocator
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:152
ISvcLocator.h
IToolSvc
Definition: IToolSvc.h:29
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:411
ServiceHandle::m_pMessageSvc
SmartIF< IMessageSvc > m_pMessageSvc
Definition: ServiceHandle.h:153
MsgStream.h
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19