Loading [MathJax]/jax/output/HTML-CSS/config.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
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
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 
76  T * get() const { return GaudiHandle<T>::nonConst( GaudiHandle<T>::get() ); }
77 
81 
82 protected:
84  StatusCode retrieve( T*& service ) const override {
86  return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
87  }
88 
89 // /** Do the real release of the Service */
90 // virtual StatusCode release( T* service ) const {
91 // return service->release();
92 // }
93 
94 private:
95  //
96  // Private helper functions
97  //
98  SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
99  if ( !m_pSvcLocator ) {
101  if ( !m_pSvcLocator ) {
102  throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
103  }
104  }
105  return m_pSvcLocator;
106  }
107 
108  SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
109  if ( !m_pMessageSvc ) {
110  m_pMessageSvc = serviceLocator(); // default message service
111  if( !m_pMessageSvc ) {
112  throw GaudiException("Service [MessageSvc] not found",
114  }
115  }
116  return m_pMessageSvc;
117  }
118  //
119  // private data members
120  //
123 };
124 
135 template < class T >
136 class ServiceHandleArray : public GaudiHandleArray< ServiceHandle<T> > {
137 public:
138  //
139  // Constructors
140  //
143  ServiceHandleArray( const std::vector< std::string >& myTypesAndNamesList,
144  const std::string& myComponentType, const std::string& myParentName ):
145  GaudiHandleArray< ServiceHandle<T> >( myTypesAndNamesList,
146  myComponentType,
147  myParentName)
148  { }
149 
150  virtual ~ServiceHandleArray() {}
151 
152  ServiceHandleArray( const std::string& myParentName )
153  : GaudiHandleArray< ServiceHandle<T> >( "Service", myParentName)
154  { }
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 ) {
163  return push_back( myHandle.typeAndName() );
164  }
165 
166 };
167 
168 template <class T>
169 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
170  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
171 }
172 
173 template <class T>
174 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
175  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
176 }
177 
178 #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
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.
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:321
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:80
SmartIF< IMessageSvc > & messageSvc() const
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:50
T * operator->() const
Allow non const access to the service, even from a const handle...
Definition: ServiceHandle.h:79
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:73
void push_back(Container &c, const Value &v, std::true_type)
SmartIF< ISvcLocator > & serviceLocator() const
Do the real release of the Service.
Definition: ServiceHandle.h:98
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
Do the real retrieval of the Service.
Definition: ServiceHandle.h:84
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