| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

DataObjectHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_DATAOBJECTHANDLE_H
2 #define GAUDIHIVE_DATAOBJECTHANDLE_H
3
4 #include "GaudiKernel/DataObjectHandleBase.h"
5 #include "GaudiKernel/ServiceLocatorHelper.h"
6 #include "GaudiKernel/GaudiException.h"
7 #include "GaudiKernel/Property.h"
8 #include "GaudiKernel/Algorithm.h"
9 #include "GaudiKernel/AlgTool.h"
10
11 #include <type_traits>
12
13 //---------------------------------------------------------------------------
14
25 //---------------------------------------------------------------------------
26
27
28 template<typename T>
29 class DataObjectHandle : public DataObjectHandleBase {
30 public:
31 using DataObjectHandleBase::DataObjectHandleBase;
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",
85 StatusCode::FAILURE);
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",
123 StatusCode::FAILURE);
124 }
125 return objectp;
126 }
127
128 //---------------------------------------------------------------------------
129 template<typename T>
130 T* DataObjectHandle<T>::getOrCreate (){
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
Algorithm.h
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
StatusCode::FAILURE
Definition: StatusCode.h:29
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
Gaudi::DataHandle::objKey
virtual const std::string & objKey() const
Definition: DataHandle.h:56
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
AlgTool.h
DataObjectHandle::m_goodType
bool m_goodType
Definition: DataObjectHandle.h:62
DataObjectHandleBase::m_EDS
SmartIF< IDataProviderSvc > m_EDS
Definition: DataObjectHandleBase.h:65
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
DataObjectHandleBase::m_init
bool m_init
Definition: DataObjectHandleBase.h:68
DataObjectHandle::exist
bool exist() const
Check the existence of the object in the transient store.
Definition: DataObjectHandle.h:47
std::string
STL class.
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GaudiException.h
IOTest.rc
rc
Definition: IOTest.py:92
DataObjectHandle::put
T * put(T *object)
Register object in transient store.
Definition: DataObjectHandle.h:117
Gaudi::DataHandle::m_owner
IDataHandleHolder * m_owner
Definition: DataHandle.h:77
DataObjectHandleBase.h
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:24
Property.h
ServiceLocatorHelper.h
DataObjectHandleBase::fetch
DataObject * fetch() const
Definition: DataObjectHandleBase.cpp:66
DataObjectHandle::getIfExists
T * getIfExists() const
Bypass check of existence of object in transient store Only uses main location of the...
Definition: DataObjectHandle.h:42
IDataProviderSvc::registerObject
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
DataObjectHandle::getOrCreate
T * getOrCreate()
Get object from store or create a new one if it doesn&#39;t exist.
Definition: DataObjectHandle.h:130
Gaudi::DataHandle::owner
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:49
DataObjectHandle::get
T * get() const
Retrieve object from transient data store.
Definition: DataObjectHandle.h:36
DataObjectHandleBase::DataObjectHandleBase
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
Definition: DataObjectHandleBase.cpp:45