All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 //---------------------------------------------------------------------------
67 //
75 template<typename T>
76 T* DataObjectHandle<T>::get(bool mustExist) const {
77 
78  auto dataObj = fetch();
79 
80  if (UNLIKELY(!dataObj) ) {
81  if (mustExist) { // Problems in getting from the store
82  throw GaudiException("Cannot retrieve " + objKey() +
83  " from transient store.",
84  m_owner ? owner()->name() : "no owner",
86  }
87  return nullptr;
88  }
89 
90  if (UNLIKELY(!m_goodType)) { // Check type compatibility once
91 
92  T* obj = dynamic_cast<T*>(dataObj);
93  m_goodType = ( obj!=nullptr );
94 
95  if (UNLIKELY(!m_goodType)) {
96 
97  std::string errorMsg("The type provided for "+ objKey()
98  + " is " + System::typeinfoName(typeid(T))
99  + " and is different from the one of the object in the store.");
100  //log << MSG::ERROR << errorMsg << endmsg;
101  throw GaudiException (errorMsg,"Wrong DataObjectType",StatusCode::FAILURE);
102  }
103  //log << MSG::DEBUG << "The data type (" << typeid(T).name()
104  // << ") specified for the handle of " << dataProductName()
105  // << " is the same of the object in the store. "
106  // << "From now on the result of a static_cast will be returned." << endmsg;
107  return obj;
108  }
109 
110  // setRead();
111  // From the second read on, this is safe
112  return static_cast<T*> (dataObj);
113 }
114 
115 //---------------------------------------------------------------------------
116 template<typename T>
117 T* DataObjectHandle<T>::put (T *objectp){
118  assert(m_init);
119  StatusCode rc = m_EDS->registerObject(objKey(), objectp);
120  if (!rc.isSuccess()) {
121  throw GaudiException("Error in put of " + objKey(),
122  "DataObjectHandle<T>::put",
124  }
125  return objectp;
126 }
127 
128 //---------------------------------------------------------------------------
129 template<typename T>
131 
132  //this process needs to be locking for multi-threaded applications
133  //lock(); --> done in caller
134 
135  T* obj = get(false);
136 
137  //object exists, we are done
138  if(obj){
139  //unlock();
140  return obj;
141  }
142 
143  //create it
144  return put(new T{});
145 
146  //unlock();
147 }
148 
149 
150 #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.
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
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
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)