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 <string>
14 #include <stdexcept>
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 public:
32  //
33  // Constructors etc.
34  //
42  ServiceHandle( const std::string& serviceName, const std::string& theParentName )
43  : GaudiHandle<T>(serviceName, "Service", theParentName)
44  { }
45 
47  template< typename CT = T,
48  typename NCT = typename std::remove_const<T>::type >
51  !std::is_same<CT,NCT>::value >::type * = nullptr )
52  : GaudiHandle<CT>( other )
53  { }
54 
55  StatusCode initialize(const std::string& serviceName, const std::string& theParentName){
56 
58  GaudiHandleBase::setParentName(theParentName);
59 
60  return StatusCode::SUCCESS;
61  }
62 
65  StatusCode retrieve() const { // not really const, because it updates m_pObject
66  return GaudiHandle<T>::retrieve();
67  }
68 
69 // /** Release the Service.
70 // Function must be repeated here to avoid hiding the function release( T*& ) */
71 // StatusCode release() const { // not really const, because it updates m_pObject
72 // return GaudiHandle<T>::release();
73 // }
74 
75 protected:
77  StatusCode retrieve( T*& service ) const override {
79  return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
80  }
81 
82 // /** Do the real release of the Service */
83 // virtual StatusCode release( T* service ) const {
84 // return service->release();
85 // }
86 
87 private:
88  //
89  // Private helper functions
90  //
91  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
92  if ( !m_pSvcLocator ) {
94  if ( !m_pSvcLocator ) {
95  throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
96  }
97  }
98  return m_pSvcLocator;
99  }
100 
101  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
102  if ( !m_pMessageSvc ) {
103  m_pMessageSvc = serviceLocator(); // default message service
104  if( !m_pMessageSvc ) {
105  throw GaudiException("Service [MessageSvc] not found",
107  }
108  }
109  return m_pMessageSvc;
110  }
111  //
112  // private data members
113  //
116 };
117 
128 template < class T >
129 class ServiceHandleArray : public GaudiHandleArray< ServiceHandle<T> > {
130 public:
131  //
132  // Constructors
133  //
136  ServiceHandleArray( const std::vector< std::string >& myTypesAndNamesList,
137  const std::string& myComponentType, const std::string& myParentName ):
138  GaudiHandleArray< ServiceHandle<T> >( myTypesAndNamesList,
139  myComponentType,
140  myParentName)
141  { }
142 
143  virtual ~ServiceHandleArray() {}
144 
145  ServiceHandleArray( const std::string& myParentName )
146  : GaudiHandleArray< ServiceHandle<T> >( "Service", myParentName)
147  { }
148 
149  virtual bool push_back( const std::string& serviceTypeAndName ) {
150  ServiceHandle<T> handle( serviceTypeAndName,GaudiHandleInfo::parentName());
151  GaudiHandleArray< ServiceHandle<T> >::push_back( handle );
152  return true;
153  }
154 
155  virtual bool push_back( const ServiceHandle<T>& myHandle ) {
156  return push_back( myHandle.typeAndName() );
157  }
158 
159 };
160 
161 template <class T>
162 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
163  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
164 }
165 
166 template <class T>
167 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
168  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
169 }
170 
171 #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:18
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:172
std::string messageName() const
name used for printing messages
Definition: GaudiHandle.cpp:48
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
SmartIF< IMessageSvc > & messageSvc() const
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:50
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:73
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:91
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:55
STL class.
GAUDI_API ISvcLocator * svcLocator()
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:228
Array of Handles to be used in lieu of vector of naked pointers to tools.
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:65
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:13
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, 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:49
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:121
ServiceHandle(const std::string &serviceName, const std::string &theParentName)
Create a handle (&#39;smart pointer&#39;) to a service.
Definition: ServiceHandle.h:42
StatusCode retrieve(T *&service) const override
Release the Service.
Definition: ServiceHandle.h:77
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:428