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 
12 //---------------------------------------------------------------------------
13 
24 //---------------------------------------------------------------------------
25 
26 
27 template<typename T>
29 
30 public:
31 
37 
38  virtual ~DataObjectHandle() {}
39 
43  T* get() { return get(true); }
44 
49  T* getIfExists() { return get(false); }
50 
54  bool exist() { return get(false) != NULL; }
55 
59  T* getOrCreate();
60 
64  void put (T* object);
65 
66 private:
67 
68  T* get(bool mustExist);
69 
70 };
71 
72 //---------------------------------------------------------------------------
73 
74 template<typename T>
77 
78 //---------------------------------------------------------------------------
79 template<typename T>
83  DataObjectHandleBase(k,a,owner) {}
84 
85 template<typename T>
89  DataObjectHandleBase(k,a,owner) {}
90 
91 //---------------------------------------------------------------------------
92 
100 template<typename T>
101 T* DataObjectHandle<T>::get(bool mustExist) {
102 
103  if (!m_init) {
104  init();
105  }
106 
107  //MsgStream log(m_MS,"DataObjectHandle");
108 
109  DataObject* dataObjectp = NULL;
110 
111  StatusCode sc = m_EDS->retrieveObject(objKey(), dataObjectp);
112 
113  T* returnObject = NULL;
114  if ( LIKELY( sc.isSuccess() ) ){
115 
116  if (UNLIKELY(!m_goodType)){ // Check type compatibility once
117 
118  // DP: can use a gaudi feature?
119  m_goodType = (NULL != dynamic_cast<T*> (dataObjectp));
120  //( typeid(tmp) == typeid(*dataObjectp) ) ;
121 
122  T tmp;
123 
124  const std::string dataType(typeid(tmp).name());
125 
126  if (!m_goodType){
127  std::string errorMsg("The type provided for "+ objKey()
128  + " is " + dataType
129  + " and is different form the one of the object in the store.");
130  //log << MSG::ERROR << errorMsg << endmsg;
131  throw GaudiException (errorMsg,"Wrong DataObjectType",StatusCode::FAILURE);
132  }
133  else{
134  //log << MSG::DEBUG << "The data type (" << dataType
135  // << ") specified for the handle of " << dataProductName()
136  // << " is the same of the object in the store. "
137  // << "From now on the result of a static_cast will be returned." << endmsg;
138  }
139  }
140 
141  if (LIKELY(m_goodType)) { // From the second read on, this is safe
142  returnObject = static_cast<T*> (dataObjectp);
143  }
144 
145  }
146  else if(mustExist){ // Problems in getting from the store
147  throw GaudiException("Cannot retrieve " + objKey() +
148  " from transient store.",
149  m_owner != 0 ? owner()->name() : "no owner",
151  }
152 
153  // setRead();
154  return returnObject;
155 }
156 
157 //---------------------------------------------------------------------------
158 template<typename T>
159 void DataObjectHandle<T>::put (T *objectp){
160 
161  if (!m_init) {
162  init();
163  }
164 
165  StatusCode sc = m_EDS->registerObject(objKey(), objectp);
166  sc.ignore();
167  // if ( LIKELY( sc.isSuccess() ) )
168  // setWritten();
169 }
170 
171 //---------------------------------------------------------------------------
172 template<typename T>
174 
175  //this process needs to be locking for multi-threaded applications
176  //lock(); --> done in caller
177 
178  T* obj = get(false);
179 
180  //object exists, we are done
181  if(obj != NULL){
182 
183  //unlock();
184  return obj;
185  }
186 
187  //MsgStream log(m_MS,"DataObjectHandle");
188  //log << MSG::DEBUG << "Object " << objKey() << " does not exist, creating it" << endmsg;
189 
190  //create it
191  obj = new T();
192  put(obj);
193 
194  //unlock();
195  return obj;
196 }
197 
198 
199 #endif
virtual ~DataObjectHandle()
Define general base for Gaudi exception.
bool exist()
Check the existence of the object in the transient store.
virtual const std::string & objKey() const
Definition: DataHandle.h:52
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
void put(T *object)
Register object in transient store.
T * get()
Retrieve object from transient data store.
STL class.
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:25
T * getIfExists()
Bypass check of existence of object in transient store Only uses main location of the...
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
IDataHandleHolder * m_owner
Definition: DataHandle.h:64
#define LIKELY(x)
Definition: Kernel.h:125
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
void ignore() const
Definition: StatusCode.h:106
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
T * getOrCreate()
Get object from store or create a new one if it doesn&#39;t exist.
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:45
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.