Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
50  : GaudiHandle<CT>( other ) {}
51 
54  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
55  inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
56  : ServiceHandle( svcName, owner->name() ) {
57  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
58  p->template setOwnerType<OWNER>();
59  }
60 
61  StatusCode initialize( const std::string& serviceName, const std::string& theParentName ) {
62 
63  GaudiHandleBase::setTypeAndName( serviceName );
64  GaudiHandleBase::setParentName( theParentName );
65 
66  return StatusCode::SUCCESS;
67  }
68 
71  StatusCode retrieve() const { // not really const, because it updates m_pObject
72  return GaudiHandle<T>::retrieve();
73  }
74 
75  // /** Release the Service.
76  // Function must be repeated here to avoid hiding the function release( T*& ) */
77  // StatusCode release() const { // not really const, because it updates m_pObject
78  // return GaudiHandle<T>::release();
79  // }
80 
83 
87 
88 protected:
90  StatusCode retrieve( T*& service ) const override {
92  return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
93  }
94 
95  // /** Do the real release of the Service */
96  // virtual StatusCode release( T* service ) const {
97  // return service->release();
98  // }
99 
100 private:
101  //
102  // Private helper functions
103  //
104  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
105  if ( !m_pSvcLocator ) {
107  if ( !m_pSvcLocator ) {
108  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
109  }
110  }
111  return m_pSvcLocator;
112  }
113 
114  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
115  if ( !m_pMessageSvc ) {
116  m_pMessageSvc = serviceLocator(); // default message service
117  if ( !m_pMessageSvc ) {
118  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
119  }
120  }
121  return m_pMessageSvc;
122  }
123  //
124  // private data members
125  //
128 };
129 
140 template <class T>
141 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>> {
142 public:
143  //
144  // Constructors
145  //
148  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
149  const std::string& myParentName )
150  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName ) {}
151 
152  virtual ~ServiceHandleArray() {}
153 
154  ServiceHandleArray( const std::string& myParentName )
155  : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName ) {}
156 
157  virtual bool push_back( const std::string& serviceTypeAndName ) {
158  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
160  return true;
161  }
162 
163  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
164 };
165 
166 template <class T>
167 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
168  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
169 }
170 
171 template <class T>
172 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
173  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
174 }
175 
176 #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:55
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
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:42
Define general base for Gaudi exception.
virtual ~ServiceHandleArray()
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
SmartIF< IMessageSvc > m_pMessageSvc
T & operator*() const
Definition: ServiceHandle.h:86
SmartIF< IMessageSvc > & messageSvc() const
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:85
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:71
void push_back(Container &c, const Value &v, std::true_type)
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:61
STL class.
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
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
Array of Handles to be used in lieu of vector of naked pointers to tools.
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:71
T move(T...args)
virtual bool push_back(const ServiceHandle< T > &myHandle)
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
std::string type() const
The concrete component class name: the part before the &#39;/&#39;.
Definition: GaudiHandle.cpp:11
constexpr static const auto FAILURE
Definition: StatusCode.h:86
virtual bool push_back(const std::string &serviceTypeAndName)
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
std::string name() const
The instance name: the part after the &#39;/&#39;.
Definition: GaudiHandle.cpp:22
ServiceHandle(const ServiceHandle< NCT > &other, typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type *=nullptr)
Copy constructor from a non const T to const T service handle.
Definition: ServiceHandle.h:48
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:115
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle (&#39;smart pointer&#39;) to a service.
Definition: ServiceHandle.h:43
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:90
STL class.
StatusCode getService(const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
ServiceHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
ServiceHandleArray(const std::string &myParentName)
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:393