The Gaudi Framework  v29r0 (ff2e7097)
ServiceHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
2 #define GAUDIKERNEL_SERVICEHANDLE_H
3 
4 // Includes
10 #include "GaudiKernel/MsgStream.h"
12 
13 #include <stdexcept>
14 #include <string>
15 #include <type_traits>
16 
17 // class predeclarations
18 class IAlgTool;
19 class IToolSvc;
20 class ServiceHandleProperty;
21 
29 template <class T>
30 class ServiceHandle : public GaudiHandle<T>
31 {
32 public:
33  //
34  // Constructors etc.
35  //
43  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
44  : GaudiHandle<T>( serviceName, "Service", theParentName )
45  {
46  }
47 
52  : GaudiHandle<CT>( other )
53  {
54  }
55 
58  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
59  inline ServiceHandle( OWNER* owner, std::string PropName, std::string svcName, std::string doc = "" )
60  : ServiceHandle( svcName, owner->name() )
61  {
62  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( PropName ), *this, std::move( doc ) );
63  p->template setOwnerType<OWNER>();
64  }
65 
66  StatusCode initialize( const std::string& serviceName, const std::string& theParentName )
67  {
68 
69  GaudiHandleBase::setTypeAndName( serviceName );
70  GaudiHandleBase::setParentName( theParentName );
71 
72  return StatusCode::SUCCESS;
73  }
74 
78  { // not really const, because it updates m_pObject
79  return GaudiHandle<T>::retrieve();
80  }
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 
89  T* get() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::get() ); }
90 
94 
95 protected:
97  StatusCode retrieve( T*& service ) const override
98  {
100  return helper.getService( GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service );
101  }
102 
103  // /** Do the real release of the Service */
104  // virtual StatusCode release( T* service ) const {
105  // return service->release();
106  // }
107 
108 private:
109  //
110  // Private helper functions
111  //
113  { // not really const, because it may change m_pSvcLocator
114  if ( !m_pSvcLocator ) {
116  if ( !m_pSvcLocator ) {
117  throw GaudiException( "SvcLocator not found", "Core component not found", StatusCode::FAILURE );
118  }
119  }
120  return m_pSvcLocator;
121  }
122 
124  { // not really const, because it may change m_pMessageSvc
125  if ( !m_pMessageSvc ) {
126  m_pMessageSvc = serviceLocator(); // default message service
127  if ( !m_pMessageSvc ) {
128  throw GaudiException( "Service [MessageSvc] not found", this->parentName(), StatusCode::FAILURE );
129  }
130  }
131  return m_pMessageSvc;
132  }
133  //
134  // private data members
135  //
138 };
139 
150 template <class T>
151 class ServiceHandleArray : public GaudiHandleArray<ServiceHandle<T>>
152 {
153 public:
154  //
155  // Constructors
156  //
159  ServiceHandleArray( const std::vector<std::string>& myTypesAndNamesList, const std::string& myComponentType,
160  const std::string& myParentName )
161  : GaudiHandleArray<ServiceHandle<T>>( myTypesAndNamesList, myComponentType, myParentName )
162  {
163  }
164 
165  virtual ~ServiceHandleArray() {}
166 
167  ServiceHandleArray( const std::string& myParentName ) : GaudiHandleArray<ServiceHandle<T>>( "Service", myParentName )
168  {
169  }
170 
171  virtual bool push_back( const std::string& serviceTypeAndName )
172  {
173  ServiceHandle<T> handle( serviceTypeAndName, GaudiHandleInfo::parentName() );
175  return true;
176  }
177 
178  virtual bool push_back( const ServiceHandle<T>& myHandle ) { return push_back( myHandle.typeAndName() ); }
179 };
180 
181 template <class T>
182 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle )
183 {
184  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
185 }
186 
187 template <class T>
188 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle )
189 {
190  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
191 }
192 
193 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H
Handle to be used in lieu of naked pointers to services.
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:59
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:157
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:44
Define general base for Gaudi exception.
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:309
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:93
SmartIF< IMessageSvc > & messageSvc() const
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:47
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:92
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:65
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:66
STL class.
GAUDI_API ISvcLocator * svcLocator()
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:213
Array of Handles to be used in lieu of vector of naked pointers to tools.
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:77
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
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:50
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:111
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:97
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:413