The Gaudi Framework  v30r3 (a5ef0a68)
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 {
33 public:
34  //
35  // Constructors etc.
36  //
44  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
45  : GaudiHandle<T>( serviceName, "Service", theParentName )
46  {
47  }
48 
53  : GaudiHandle<CT>( other )
54  {
55  }
56 
59  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
60  inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
61  : ServiceHandle( svcName, owner->name() )
62  {
63  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
64  p->template setOwnerType<OWNER>();
65  }
66 
67  StatusCode initialize( const std::string& serviceName, const std::string& theParentName )
68  {
69 
70  GaudiHandleBase::setTypeAndName( serviceName );
71  GaudiHandleBase::setParentName( theParentName );
72 
73  return StatusCode::SUCCESS;
74  }
75 
79  { // not really const, because it updates m_pObject
80  return GaudiHandle<T>::retrieve();
81  }
82 
83  // /** Release the Service.
84  // Function must be repeated here to avoid hiding the function release( T*& ) */
85  // StatusCode release() const { // not really const, because it updates m_pObject
86  // return GaudiHandle<T>::release();
87  // }
88 
90  T* get() const { return details::nonConst( GaudiHandle<T>::get() ); }
91 
94  T& operator*() const { return *details::nonConst( GaudiHandle<T>::operator->() ); }
95 
96 protected:
98  StatusCode retrieve( T*& service ) const override
99  {
101  return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
102  }
103 
104  // /** Do the real release of the Service */
105  // virtual StatusCode release( T* service ) const {
106  // return service->release();
107  // }
108 
109 private:
110  //
111  // Private helper functions
112  //
114  { // not really const, because it may change m_pSvcLocator
115  if ( !m_pSvcLocator ) {
117  if ( !m_pSvcLocator ) {
118  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
119  }
120  }
121  return m_pSvcLocator;
122  }
123 
125  { // not really const, because it may change m_pMessageSvc
126  if ( !m_pMessageSvc ) {
127  m_pMessageSvc = serviceLocator(); // default message service
128  if ( !m_pMessageSvc ) {
129  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
130  }
131  }
132  return m_pMessageSvc;
133  }
134  //
135  // private data members
136  //
139 };
140 
151 template <class T>
152 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>>
153 {
154 public:
155  //
156  // Constructors
157  //
160  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
161  const std::string& myParentName )
162  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName )
163  {
164  }
165 
166  virtual ~ServiceHandleArray() {}
167 
168  ServiceHandleArray( const std::string& myParentName ) : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName )
169  {
170  }
171 
172  virtual bool push_back( const std::string& serviceTypeAndName )
173  {
174  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
176  return true;
177  }
178 
179  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
180 };
181 
182 template <class T>
183 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle )
184 {
185  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
186 }
187 
188 template <class T>
189 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle )
190 {
191  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
192 }
193 
194 #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:60
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:170
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:44
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:94
SmartIF< IMessageSvc > & messageSvc() const
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:58
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:93
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:76
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:67
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:21
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:226
Array of Handles to be used in lieu of vector of naked pointers to tools.
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:78
T move(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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
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:23
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:51
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:122
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle (&#39;smart pointer&#39;) to a service.
Definition: ServiceHandle.h:44
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:98
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:420