The Gaudi Framework  v28r3 (cc1cf868)
DataObjectHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_DATAOBJECTHANDLE_H
2 #define GAUDIHIVE_DATAOBJECTHANDLE_H
3 
7 #include "GaudiKernel/Property.h"
9 #include "GaudiKernel/AlgTool.h"
10 
11 #include <type_traits>
12 
13 //---------------------------------------------------------------------------
14 
25 //---------------------------------------------------------------------------
26 
27 
28 template<typename T>
30 public:
32 
36  T* get() const { return get(true); }
37 
42  T* getIfExists() const { return get(false); }
43 
47  bool exist() const { return get(false) != nullptr; }
48 
52  T* getOrCreate();
53 
57  T* put (T* object);
58 
59 private:
60 
61  T* get(bool mustExist) const;
62  mutable bool m_goodType = false;
63 
64 };
65 
66 template<typename T>
71  DataObjectReadHandle{DataObjID{k}, owner} {}
72 
75 
78  template <class OWNER, class K,
80  inline DataObjectReadHandle( OWNER* owner,
81  std::string name, const K& key = {}, std::string doc = "" )
82  : DataObjectHandle<T>( owner, Gaudi::DataHandle::Reader, std::move(name), key, std::move(doc) ) {}
83 };
84 template<typename T>
89  DataObjectWriteHandle{DataObjID{k}, owner} {}
90 
93 
96  template <class OWNER, class K,
98  inline DataObjectWriteHandle( OWNER* owner,
99  std::string name, const K& key = {}, std::string doc = "" )
100  : DataObjectHandle<T>( owner, Gaudi::DataHandle::Writer, std::move(name), key, std::move(doc) ) {}
101 };
102 
103 //---------------------------------------------------------------------------
104 //
112 template<typename T>
113 T* DataObjectHandle<T>::get(bool mustExist) const {
114 
115  auto dataObj = fetch();
116 
117  if (UNLIKELY(!dataObj) ) {
118  if (mustExist) { // Problems in getting from the store
119  throw GaudiException("Cannot retrieve " + objKey() +
120  " from transient store.",
121  m_owner ? owner()->name() : "no owner",
123  }
124  return nullptr;
125  }
126 
127  if (UNLIKELY(!m_goodType)) { // Check type compatibility once
128 
129  T* obj = dynamic_cast<T*>(dataObj);
130  m_goodType = ( obj!=nullptr );
131 
132  if (UNLIKELY(!m_goodType)) {
133 
134  std::string errorMsg("The type provided for "+ objKey()
135  + " is " + System::typeinfoName(typeid(T))
136  + " and is different from the one of the object in the store.");
137  //log << MSG::ERROR << errorMsg << endmsg;
138  throw GaudiException (errorMsg,"Wrong DataObjectType",StatusCode::FAILURE);
139  }
140  //log << MSG::DEBUG << "The data type (" << typeid(T).name()
141  // << ") specified for the handle of " << dataProductName()
142  // << " is the same of the object in the store. "
143  // << "From now on the result of a static_cast will be returned." << endmsg;
144  return obj;
145  }
146 
147  // setRead();
148  // From the second read on, this is safe
149  return static_cast<T*> (dataObj);
150 }
151 
152 //---------------------------------------------------------------------------
153 template<typename T>
154 T* DataObjectHandle<T>::put (T *objectp){
155  assert(m_init);
156  StatusCode rc = m_EDS->registerObject(objKey(), objectp);
157  if (!rc.isSuccess()) {
158  throw GaudiException("Error in put of " + objKey(),
159  "DataObjectHandle<T>::put",
161  }
162  return objectp;
163 }
164 
165 //---------------------------------------------------------------------------
166 template<typename T>
168 
169  //this process needs to be locking for multi-threaded applications
170  //lock(); --> done in caller
171 
172  T* obj = get(false);
173 
174  //object exists, we are done
175  if(obj){
176  //unlock();
177  return obj;
178  }
179 
180  //create it
181  return put(new T{});
182 
183  //unlock();
184 }
185 
186 
187 #endif
Define general base for Gaudi exception.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
virtual const std::string & objKey() const
Definition: DataHandle.h:56
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
SmartIF< IDataProviderSvc > m_EDS
#define UNLIKELY(x)
Definition: Kernel.h:126
bool exist() const
Check the existence of the object in the transient store.
DataObjectReadHandle(const DataObjID &k, IDataHandleHolder *owner)
STL class.
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
rc
Definition: IOTest.py:92
T * put(T *object)
Register object in transient store.
IDataHandleHolder * m_owner
Definition: DataHandle.h:77
T move(T...args)
DataObjectWriteHandle(const DataObjID &k, IDataHandleHolder *owner)
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
DataObjectWriteHandle(OWNER *owner, std::string name, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.
DataObject * fetch() const
T * getIfExists() const
Bypass check of existence of object in transient store Only uses main location of the...
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
T * getOrCreate()
Get object from store or create a new one if it doesn&#39;t exist.
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:49
T * get() const
Retrieve object from transient data store.
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
DataObjectReadHandle(OWNER *owner, std::string name, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.