The Gaudi Framework  v32r2 (46d42edc)
ServiceHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
2 #define GAUDIKERNEL_SERVICEHANDLE_H
3 
4 // Includes
11 #include "GaudiKernel/MsgStream.h"
13 
14 #include <stdexcept>
15 #include <string>
16 #include <type_traits>
17 
18 // class predeclarations
19 class IAlgTool;
20 class IToolSvc;
21 class ServiceHandleProperty;
22 
30 template <class T>
31 class ServiceHandle : public GaudiHandle<T> {
32 public:
33  //
34  // Constructors etc.
35  //
43  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
44  : GaudiHandle<T>( serviceName, "Service", theParentName ) {}
45 
47  template <typename CT = T, typename NCT = std::remove_const_t<T>,
48  typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
49  ServiceHandle( const ServiceHandle<NCT>& other ) : GaudiHandle<CT>( other ) {}
50 
53  template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
54  inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
55  : ServiceHandle( svcName, owner->name() ) {
56  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
57  p->template setOwnerType<OWNER>();
58  }
59 
60  StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
61 
62  GaudiHandleBase::setTypeAndName( serviceName );
63  GaudiHandleBase::setParentName( theParentName );
64 
65  return StatusCode::SUCCESS;
66  }
67 
70  StatusCode retrieve() const { // not really const, because it updates m_pObject
71  return GaudiHandle<T>::retrieve();
72  }
73 
74  // /** Release the Service.
75  // Function must be repeated here to avoid hiding the function release( T*& ) */
76  // StatusCode release() const { // not really const, because it updates m_pObject
77  // return GaudiHandle<T>::release();
78  // }
79 
82 
86 
87 protected:
89  StatusCode retrieve( T*& service ) const override {
91  return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
92  }
93 
94  // /** Do the real release of the Service */
95  // virtual StatusCode release( T* service ) const {
96  // return service->release();
97  // }
98 
99 private:
100  //
101  // Private helper functions
102  //
103  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
104  if ( !m_pSvcLocator ) {
106  if ( !m_pSvcLocator ) {
107  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
108  }
109  }
110  return m_pSvcLocator;
111  }
112 
113  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
114  if ( !m_pMessageSvc ) {
115  m_pMessageSvc = serviceLocator(); // default message service
116  if ( !m_pMessageSvc ) {
117  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
118  }
119  }
120  return m_pMessageSvc;
121  }
122  //
123  // private data members
124  //
127 };
128 
139 template <class T>
140 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
141 public:
142  //
143  // Constructors
144  //
147  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
148  const std::string& myParentName )
149  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
150 
151  virtual ~ServiceHandleArray() {}
152 
153  ServiceHandleArray( const std::string& myParentName )
154  : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
155 
156  virtual bool push_back( const std::string& serviceTypeAndName ) {
157  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
159  return true;
160  }
161 
162  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
163 };
164 
165 template <class T>
166 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
167  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
168 }
169 
170 template <class T>
172  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
173 }
174 
175 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
Handle to be used in lieu of naked pointers to services.
Definition: ServiceHandle.h:31
SmartIF< ISvcLocator > m_pSvcLocator
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
ServiceHandle(OWNER *owner, std::string PropName, std::string svcName, std::string doc="")
Autodeclaring constructor with property name, service type/name and documentation.
Definition: ServiceHandle.h:54
an helper to share the implementation of service() among the various kernel base classes
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
Define general base for Gaudi exception.
virtual ~ServiceHandleArray()
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:115
StatusCode getService(const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
SmartIF< IMessageSvc > m_pMessageSvc
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:71
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:84
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:60
STL class.
std::string name() const
The instance name: the part after the '/'.
Definition: GaudiHandle.cpp:22
GAUDI_API ISvcLocator * svcLocator()
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:70
Array of Handles to be used in lieu of vector of naked pointers to tools.
T move(T... args)
virtual bool push_back(const ServiceHandle< T > &myHandle)
T * get() const
Release the Service.
Definition: ServiceHandle.h:81
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:42
T & operator *() const
Definition: ServiceHandle.h:85
std::ostream & operator<<(std::ostream &os, const ServiceHandle< T > &handle)
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
constexpr static const auto FAILURE
Definition: StatusCode.h:86
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:89
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
ServiceHandle(const ServiceHandle< NCT > &other)
Copy constructor from a non const T to const T service handle.
Definition: ServiceHandle.h:49
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle ('smart pointer') to a service.
Definition: ServiceHandle.h:43
STL class.
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
ServiceHandleArray(const std::string &myParentName)
SmartIF< IMessageSvc > & messageSvc() const
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:393