The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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>
22#include <stdexcept>
23#include <string>
24#include <type_traits>
25
26class IAlgTool;
27class IToolSvc;
28class ServiceHandleProperty;
29
37template <class T>
38class ServiceHandle : public GaudiHandle<T> {
39public:
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
67 GaudiHandleBase::setParentName( theParentName );
68
70 }
71
74 using GaudiHandle<T>::retrieve;
75
77 T* get() const { return ::details::nonConst( GaudiHandle<T>::get() ); }
78
80 T* operator->() const { return ::details::nonConst( GaudiHandle<T>::operator->() ); }
82
83protected:
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();
91 }
93 }
94
95private:
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
129
130template <class T>
131class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
132public:
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
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
153template <class T>
154inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
155 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
156}
157
158template <class T>
159inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
160 return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
161}
std::ostream & operator<<(std::ostream &os, const ServiceHandle< T > &handle)
Define general base for Gaudi exception.
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
std::string name() const
The instance name: the part after the '/'.
std::string messageName() const
name used for printing messages
const std::string & typeAndName() const
The full type and name: "type/name".
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Handle to be used in lieu of naked pointers to gaudis.
T * get()
Return the wrapped pointer, not calling retrieve() if null.
StatusCode retrieve() const
Retrieve the component.
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
void setParentName(std::string parent)
The name of the parent.
Definition GaudiHandle.h:84
const std::string & parentName() const
The name of the parent.
Definition GaudiHandle.h:62
The interface implemented by the AlgTool base class.
Definition IAlgTool.h:29
The interface implemented by the IToolSvc base class.
Definition IToolSvc.h:28
Array of Handles to be used in lieu of vector of naked pointers to tools.
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle with given type and name.
virtual ~ServiceHandleArray()
ServiceHandleArray(const std::string &myParentName)
virtual bool push_back(const ServiceHandle< T > &myHandle)
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Handle to be used in lieu of naked pointers to services.
SmartIF< IMessageSvc > m_pMessageSvc
ServiceHandle(const ServiceHandle< NCT > &other)
Copy constructor from a non const T to const T service handle.
SmartIF< ISvcLocator > & serviceLocator() const
ServiceHandle(OWNER *owner, std::string PropName, const std::string &svcName, std::string doc="")
Autodeclaring constructor with property name, service type/name and documentation.
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
SmartIF< IMessageSvc > & messageSvc() const
T * operator->() const
Allow non const access to the service, even from a const handle...
T & operator*() const
SmartIF< ISvcLocator > m_pSvcLocator
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
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...
an helper to share the implementation of service() among the various kernel base classes
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API ISvcLocator * svcLocator()
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition GaudiHandle.h:26