The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
ServiceHandle< T > Class Template Reference

Handle to be used in lieu of naked pointers to services. More...

#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.
 
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.
 
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.
 
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...
 
T * operator-> () const
 Allow non const access to the service, even from a const handle...
 
T & operator* () const
 
- Public Member Functions inherited from GaudiHandle< T >
template<typename CT = T, typename NCT = std::remove_const_t<T>>
requires ( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
 GaudiHandle (const GaudiHandle< NCT > &other)
 Copy constructor needed for correct ref-counting.
 
 GaudiHandle (const GaudiHandle &other)
 Copy constructor needed for correct ref-counting.
 
template<typename CT = T, typename NCT = std::remove_const_t<T>>
requires ( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
GaudiHandleoperator= (const GaudiHandle< NCT > &other)
 Assignment operator for correct ref-counting.
 
GaudiHandleoperator= (const GaudiHandle &other)
 Assignment operator for correct ref-counting.
 
StatusCode retrieve () const
 Retrieve the component.
 
StatusCode release () const
 Release the component.
 
bool isValid () const
 Check if the handle is valid (try to retrive the object is not done yet).
 
 operator bool () const
 For testing if handle has component.
 
T * get ()
 Return the wrapped pointer, not calling retrieve() if null.
 
std::add_const_t< T > * get () const
 Return the wrapped pointer, not calling retrieve() if null.
 
bool isSet () const
 True if the wrapped pointer is not null.
 
T & operator* ()
 
T * operator-> ()
 
std::add_const_t< T > & operator* () const
 
std::add_const_t< T > * operator-> () const
 
std::string getDefaultType ()
 Helper function to get default type string from the class type.
 
std::string getDefaultName ()
 
- Public Member Functions inherited from GaudiHandleBase
const std::string & typeAndName () const
 The full type and name: "type/name".
 
std::string type () const
 The concrete component class name: the part before the '/'.
 
std::string name () const
 The instance name: the part after the '/'.
 
bool empty () const
 Check if the handle has been set to empty string (i.e.
 
void setTypeAndName (std::string myTypeAndName)
 The component "type/name" string.
 
void setName (std::string_view myName)
 Set the instance name (part after the '/') without changing the class type.
 
std::string pythonPropertyClassName () const override
 Name of the componentType with "Handle" appended.
 
std::string messageName () const
 name used for printing messages
 
std::string pythonRepr () const override
 Python representation of handle, i.e.
 
- Public Member Functions inherited from GaudiHandleInfo
virtual ~GaudiHandleInfo ()
 virtual destructor so that derived class destructor is called.
 
const std::string & componentType () const
 
const std::string & propertyName () const
 name as used in declareProperty(name,gaudiHandle)
 
void setPropertyName (std::string propName)
 set name as used in declareProperty(name,gaudiHandle).
 
const std::string & parentName () const
 The name of the parent.
 

Protected Member Functions

StatusCode retrieve (T *&service) const override
 Do the real retrieval of the Service.
 
- Protected Member Functions inherited from GaudiHandle< T >
 GaudiHandle (std::string myTypeAndName, std::string myComponentType, std::string myParentName)
 
virtual StatusCode release (T *comp) const
 Release the component.
 
- 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.
 
- Protected Member Functions inherited from GaudiHandleInfo
 GaudiHandleInfo (std::string myComponentType, std::string myParentName)
 Some basic information and helper functions shared between various handles/arrays.
 
void setComponentType (std::string componentType)
 The component type.
 
void setParentName (std::string parent)
 The name of the parent.
 

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 38 of file ServiceHandle.h.

Constructor & Destructor Documentation

◆ ServiceHandle() [1/3]

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 47 of file ServiceHandle.h.

48 : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Handle to be used in lieu of naked pointers to services.

◆ ServiceHandle() [2/3]

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

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

Definition at line 53 of file ServiceHandle.h.

◆ ServiceHandle() [3/3]

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 58 of file ServiceHandle.h.

60 auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
61 p->template setOwnerType<OWNER>();
62 }
std::string name() const
The instance name: the part after the '/'.
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.

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 77 of file ServiceHandle.h.

◆ initialize()

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

Definition at line 64 of file ServiceHandle.h.

64 {
65
68
70 }
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
void setParentName(std::string parent)
The name of the parent.
Definition GaudiHandle.h:84

◆ messageSvc()

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

Definition at line 106 of file ServiceHandle.h.

106 { // 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 }
const std::string & parentName() const
The name of the parent.
Definition GaudiHandle.h:62
SmartIF< IMessageSvc > m_pMessageSvc
SmartIF< ISvcLocator > & serviceLocator() const

◆ operator*()

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

Definition at line 81 of file ServiceHandle.h.

std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition GaudiHandle.h:26

◆ 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 80 of file ServiceHandle.h.

◆ retrieve()

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

Do the real retrieval of the Service.

Implements GaudiHandle< T >.

Definition at line 85 of file ServiceHandle.h.

85 {
87 if ( auto svc = helper.service<T>( GaudiHandleBase::typeAndName(), /* quiet = */ true, /* createIf = */ true ) ) {
88 svc->addRef();
89 service = svc.get();
91 }
93 }
T * get() const
Allow non const access to the service, even from a const handle...

◆ serviceLocator()

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

Definition at line 96 of file ServiceHandle.h.

96 { // 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 }
SmartIF< ISvcLocator > m_pSvcLocator
GAUDI_API ISvcLocator * svcLocator()

Member Data Documentation

◆ m_pMessageSvc

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

Definition at line 117 of file ServiceHandle.h.

◆ m_pSvcLocator

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

Definition at line 116 of file ServiceHandle.h.


The documentation for this class was generated from the following file: