The Gaudi Framework  master (37c0b60a)
ServiceHandle.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 <GaudiKernel/Bootstrap.h>
19 #include <GaudiKernel/IProperty.h>
21 #include <GaudiKernel/MsgStream.h>
23 
24 #include <stdexcept>
25 #include <string>
26 #include <type_traits>
27 
28 // class predeclarations
29 class IAlgTool;
30 class IToolSvc;
31 class ServiceHandleProperty;
32 
40 template <class T>
41 class ServiceHandle : public GaudiHandle<T> {
42 public:
43  //
44  // Constructors etc.
45  //
53  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
54  : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
55 
57  template <typename CT = T, typename NCT = std::remove_const_t<T>,
58  typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
59  ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
60 
63  template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
64  inline ServiceHandle( OWNER* owner, std::string PropName, const std::string& svcName, std::string doc = "" )
65  : ServiceHandle( svcName, owner->name() ) {
66  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
67  p->template setOwnerType<OWNER>();
68  }
69 
70  StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
71 
72  GaudiHandleBase::setTypeAndName( serviceName );
73  GaudiHandleBase::setParentName( theParentName );
74 
75  return StatusCode::SUCCESS;
76  }
77 
81 
82  // /** Release the Service.
83  // Function must be repeated here to avoid hiding the function release( T*& ) */
84  // StatusCode release() const { // not really const, because it updates m_pObject
85  // return GaudiHandle<T>::release();
86  // }
87 
90 
94 
95 protected:
97  StatusCode retrieve( T*& service ) const override {
99  return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
100  }
101 
102  // /** Do the real release of the Service */
103  // virtual StatusCode release( T* service ) const {
104  // return service->release();
105  // }
106 
107 private:
108  //
109  // Private helper functions
110  //
111  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
112  if ( !m_pSvcLocator ) {
114  if ( !m_pSvcLocator ) {
115  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
116  }
117  }
118  return m_pSvcLocator;
119  }
120 
121  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
122  if ( !m_pMessageSvc ) {
123  m_pMessageSvc = serviceLocator(); // default message service
124  if ( !m_pMessageSvc ) {
125  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
126  }
127  }
128  return m_pMessageSvc;
129  }
130  //
131  // private data members
132  //
135 };
136 
147 template <class T>
148 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
149 public:
150  //
151  // Constructors
152  //
155  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
156  const std::string& myParentName )
157  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
158 
159  virtual ~ServiceHandleArray() {}
160 
161  ServiceHandleArray( const std::string& myParentName )
162  : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
163 
164  virtual bool push_back( const std::string& serviceTypeAndName ) {
165  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
167  return true;
168  }
169 
170  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
171 };
172 
173 template <class T>
174 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
175  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
176 }
177 
178 template <class T>
180  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
181 }
182 
183 #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:164
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
std::move
T move(T... args)
ServiceHandle::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const
Definition: ServiceHandle.h:111
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::string &myParentName)
Definition: ServiceHandle.h:161
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:41
std::vector< std::string >
GaudiException
Definition: GaudiException.h:31
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:70
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:170
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:159
ServiceHandleArray::ServiceHandleArray
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: ServiceHandle.h:155
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:89
StatusCode
Definition: StatusCode.h:65
std::ostream
STL class.
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:121
ServiceLocatorHelper::getService
StatusCode getService(std::string_view name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:52
SmartIF< ISvcLocator >
ServiceHandle::ServiceHandle
ServiceHandle(const ServiceHandle< NCT > &other)
Copy constructor from a non const T to const T service handle.
Definition: ServiceHandle.h:59
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:97
ServiceHandle::operator*
T & operator*() const
Definition: ServiceHandle.h:93
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:64
ServiceHandle::operator->
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:92
ServiceHandle::ServiceHandle
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:53
operator<<
std::ostream & operator<<(std::ostream &os, const ServiceHandle< T > &handle)
Definition: ServiceHandle.h:174
ServiceHandleArray
Definition: ServiceHandle.h:148
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ServiceLocatorHelper.h
Bootstrap.h
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:133
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:134
MsgStream.h
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19