The Gaudi Framework  master (e199b415)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ServiceHandle< T > Class Template Reference

#include <GaudiKernel/ServiceHandle.h>

Inheritance diagram for ServiceHandle< T >:
Collaboration diagram for ServiceHandle< T >:

Public Member Functions

 ServiceHandle (const std::string &serviceName, const std::string &theParentName)
 Create a handle ('smart pointer') to a service. More...
 
template<typename CT = T, typename NCT = std::remove_const_t<T>>
 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. More...
 
template<std::derived_from< IProperty > OWNER>
 ServiceHandle (OWNER *owner, std::string PropName, const std::string &svcName, std::string doc="")
 Autodeclaring constructor with property name, service type/name and documentation. More...
 
StatusCode initialize (const std::string &serviceName, const std::string &theParentName)
 
T * get () const
 Allow non const access to the service, even from a const handle... More...
 
T * operator-> () const
 Allow non const access to the service, even from a const handle... More...
 
T & operator* () const
 
- Public Member Functions inherited from GaudiHandle< T >
template<typename CT = T, typename NCT = std::remove_const_t<T>>
 GaudiHandle (const GaudiHandle< NCT > &other) requires(std
 Copy constructor needed for correct ref-counting. More...
 
 GaudiHandle (const GaudiHandle &other)
 Copy constructor needed for correct ref-counting. More...
 
template<typename CT = T, typename NCT = std::remove_const_t<T>>
 requires (std::is_const_v< CT > &&!std::is_same_v< CT, NCT >) GaudiHandle &operator
 Assignment operator for correct ref-counting. More...
 
virtual StatusCode release (T *comp) const
 Release the component. More...
 
- Public Member Functions inherited from GaudiHandleBase
const std::stringtypeAndName () const
 The full type and name: "type/name". More...
 
std::string type () const
 The concrete component class name: the part before the '/'. More...
 
std::string name () const
 The instance name: the part after the '/'. More...
 
bool empty () const
 Check if the handle has been set to empty string (i.e. More...
 
void setTypeAndName (std::string myTypeAndName)
 The component "type/name" string. More...
 
void setName (std::string_view myName)
 Set the instance name (part after the '/') without changing the class type. More...
 
std::string pythonPropertyClassName () const override
 Name of the componentType with "Handle" appended. More...
 
std::string messageName () const
 name used for printing messages More...
 
std::string pythonRepr () const override
 Python representation of handle, i.e. More...
 
- Public Member Functions inherited from GaudiHandleInfo
virtual ~GaudiHandleInfo ()
 virtual destructor so that derived class destructor is called. More...
 
const std::stringcomponentType () const
 
const std::stringpropertyName () const
 name as used in declareProperty(name,gaudiHandle) More...
 
void setPropertyName (std::string propName)
 set name as used in declareProperty(name,gaudiHandle). More...
 
const std::stringparentName () const
 The name of the parent. More...
 

Protected Member Functions

StatusCode retrieve (T *&service) const override
 Do the real retrieval of the Service. More...
 
template<Gaudi::IsInterface I = T>
StatusCode i_retrieve (I *&service) const
 retrieve the service for ServiceHandles<ISomeInterfaces> More...
 
template<typename I = T>
 requires (!Gaudi::IsInterface< I >) StatusCode i_retrieve(I *&service) const
 retrieve the service for ServiceHandles<ActualService> More...
 
- Protected Member Functions inherited from GaudiHandle< T >
 GaudiHandle (std::string myTypeAndName, std::string myComponentType, std::string myParentName)
 
- Protected Member Functions inherited from GaudiHandleBase
 GaudiHandleBase (std::string myTypeAndName, std::string myComponentType, std::string myParentName)
 Create a handle ('smart pointer') to a gaudi component. More...
 
- Protected Member Functions inherited from GaudiHandleInfo
 GaudiHandleInfo (std::string myComponentType, std::string myParentName)
 Some basic information and helper functions shared between various handles/arrays. More...
 
void setComponentType (std::string componentType)
 The component type. More...
 
void setParentName (std::string parent)
 The name of the parent. More...
 

Private Member Functions

SmartIF< ISvcLocator > & serviceLocator () const
 
SmartIF< IMessageSvc > & messageSvc () const
 

Private Attributes

SmartIF< ISvcLocatorm_pSvcLocator
 
SmartIF< IMessageSvcm_pMessageSvc
 

Additional Inherited Members

- Public Types inherited from GaudiHandleBase
using PropertyType = GaudiHandleProperty
 

Detailed Description

template<class T>
class ServiceHandle< T >

Handle to be used in lieu of naked pointers to services. This allows better control through the framework of service loading and usage.

Author
Marti.nosp@m.n.Wo.nosp@m.udstr.nosp@m.a@ce.nosp@m.rn.ch

Definition at line 42 of file ServiceHandle.h.

Constructor & Destructor Documentation

◆ ServiceHandle() [1/2]

template<class T >
ServiceHandle< T >::ServiceHandle ( const std::string serviceName,
const std::string theParentName 
)
inline

Create a handle ('smart pointer') to a service.

The arguments are passed on to ServiceSvc, and have the same meaning:

Parameters
serviceNamename of the service
parentNamename of the parent Algorithm, AlgTool or Service. It is used for log printout at retrieve(), and for retrieving a thread-dependent service (if applicable)

Definition at line 54 of file ServiceHandle.h.

55  : GaudiHandle<T>( serviceName, "Service", theParentName ) {}

◆ ServiceHandle() [2/2]

template<class T >
template<std::derived_from< IProperty > OWNER>
ServiceHandle< T >::ServiceHandle ( OWNER *  owner,
std::string  PropName,
const std::string svcName,
std::string  doc = "" 
)
inline

Autodeclaring constructor with property name, service type/name and documentation.

Note
the use of requires is required to avoid ambiguities

Definition at line 65 of file ServiceHandle.h.

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  }

Member Function Documentation

◆ get()

template<class T >
T* ServiceHandle< T >::get ( ) const
inline

Allow non const access to the service, even from a const handle...

Definition at line 90 of file ServiceHandle.h.

◆ i_retrieve()

template<class T >
template<Gaudi::IsInterface I = T>
StatusCode ServiceHandle< T >::i_retrieve ( I *&  service) const
inlineprotected

retrieve the service for ServiceHandles<ISomeInterfaces>

Definition at line 102 of file ServiceHandle.h.

102  {
104  return helper.getService( GaudiHandleBase::typeAndName(), true, I::interfaceID(), (void**)&service );
105  }

◆ initialize()

template<class T >
StatusCode ServiceHandle< T >::initialize ( const std::string serviceName,
const std::string theParentName 
)
inline

Definition at line 71 of file ServiceHandle.h.

71  {
72 
73  GaudiHandleBase::setTypeAndName( serviceName );
74  GaudiHandleBase::setParentName( theParentName );
75 
76  return StatusCode::SUCCESS;
77  }

◆ messageSvc()

template<class T >
SmartIF<IMessageSvc>& ServiceHandle< T >::messageSvc ( ) const
inlineprivate

Definition at line 140 of file ServiceHandle.h.

140  { // 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  }

◆ operator*()

template<class T >
T& ServiceHandle< T >::operator* ( ) const
inline

Definition at line 94 of file ServiceHandle.h.

◆ operator->()

template<class T >
T* ServiceHandle< T >::operator-> ( ) const
inline

Allow non const access to the service, even from a const handle...

Definition at line 93 of file ServiceHandle.h.

◆ requires() [1/2]

template<class T >
template<typename I = T>
ServiceHandle< T >::requires ( !Gaudi::IsInterface< I >  ) const &
inlineprotected

retrieve the service for ServiceHandles<ActualService>

Definition at line 109 of file ServiceHandle.h.

110  {
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  }

◆ requires() [2/2]

template<class T >
template<typename CT = T, typename NCT = std::remove_const_t<T>>
ServiceHandle< T >::requires ( std::is_const_v< CT > &&!std::is_same_v< CT, NCT >  ) const &
inline

Copy constructor from a non const T to const T service handle.

Definition at line 59 of file ServiceHandle.h.

60  : GaudiHandle<CT>( other ) {}

◆ retrieve()

template<class T >
StatusCode ServiceHandle< T >::retrieve ( T *&  service) const
inlineoverrideprotected

Do the real retrieval of the Service.

Definition at line 98 of file ServiceHandle.h.

98 { return i_retrieve( service ); }

◆ serviceLocator()

template<class T >
SmartIF<ISvcLocator>& ServiceHandle< T >::serviceLocator ( ) const
inlineprivate

Definition at line 130 of file ServiceHandle.h.

130  { // 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  }

Member Data Documentation

◆ m_pMessageSvc

template<class T >
SmartIF<IMessageSvc> ServiceHandle< T >::m_pMessageSvc
mutableprivate

Definition at line 153 of file ServiceHandle.h.

◆ m_pSvcLocator

template<class T >
SmartIF<ISvcLocator> ServiceHandle< T >::m_pSvcLocator
mutableprivate

Definition at line 152 of file ServiceHandle.h.


The documentation for this class was generated from the following file:
IService
Definition: IService.h:28
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:65
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
GaudiException
Definition: GaudiException.h: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
GaudiHandleInfo::setParentName
void setParentName(std::string parent)
The name of the parent.
Definition: GaudiHandle.h:87
GaudiHandle
Definition: GaudiHandle.h:179
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
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:27
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
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
GaudiHandleBase::typeAndName
const std::string & typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:131
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ServiceHandle::m_pSvcLocator
SmartIF< ISvcLocator > m_pSvcLocator
Definition: ServiceHandle.h:152
ServiceHandle::m_pMessageSvc
SmartIF< IMessageSvc > m_pMessageSvc
Definition: ServiceHandle.h:153
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19