QDaq  0.2.6
Qt-based Data Aqcuisition
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
h5helper_v1_0.cpp
1 #include "h5helper_v1_0.h"
2 
3 #include "QDaqVector.h"
4 #include "QDaqObject.h"
5 
6 #include <QMetaProperty>
7 
8 #define CLASS_ATTR_NAME "Class"
9 
10 void h5helper_v1_0::write(CommonFG* h5obj, const char* name, const QString& S)
11 {
12  int len = S.length();
13  if (len==0) return;
14  StrType type(PredType::C_S1, len);
15  hsize_t dims = 1;
16  DataSpace space(1,&dims);
17  DataSet ds = h5obj->createDataSet(name, type, space);
18  ds.write(S.toLatin1().constData(),type);
19 }
20 void h5helper_v1_0::write(CommonFG* h5obj, const char* name, const QStringList& S)
21 {
22  int ncols = 0, nrows = S.size();
23  foreach(const QString& s, S) if (s.length() > ncols) ncols = s.length();
24 
25  QVector<char> wbuff(ncols*nrows);
26  for(int i=0; i<nrows; i++)
27  {
28  QByteArray ba = S.at(i).toLatin1();
29  memcpy(wbuff.data() + (i*ncols),ba.constData(),ba.length());
30  }
31 
32  StrType type(PredType::C_S1, ncols);
33 
34  /* Create dataspace for datasets */
35  hsize_t dims = nrows;
36  DataSpace space(1, &dims);
37 
38  /* Create a dataset */
39  DataSet dataset = h5obj->createDataSet(name, type, space);
40 
41  /* Write dataset to disk */
42  dataset.write(wbuff.constData(), type, space);
43 
44  /* Close Dataset */
45  dataset.close();
46 }
47 
48 
49 bool h5helper_v1_0::read(CommonFG* h5obj, const char* name, QString& str)
50 {
51  if (!h5exist_ds(h5obj,name)) {
52  pushWarning(QString("DataSet '%1' not found in group '%2'")
53  .arg(name).arg(groupName(h5obj)));
54  return false;
55  }
56  DataSet ds = h5obj->openDataSet( name );
57  H5T_class_t type_class = ds.getTypeClass();
58  if (type_class==H5T_STRING)
59  {
60  StrType st = ds.getStrType();
61  int sz = st.getSize();
62  QByteArray buff(sz,'0');
63  ds.read(buff.data(),st);
64  str = QString(buff);
65  return true;
66  }
67  return false;
68 }
69 bool h5helper_v1_0::read(CommonFG* h5obj, const char* name, QStringList& S)
70 {
71  if (!h5exist_ds(h5obj,name)) {
72  pushWarning(QString("DataSet '%1' not found in group '%2'")
73  .arg(name).arg(groupName(h5obj)));
74  return false;
75  }
76  DataSet ds = h5obj->openDataSet( name );
77  H5T_class_t type_class = ds.getTypeClass();
78  if (type_class==H5T_STRING)
79  {
80  // get string type & size
81  StrType st = ds.getStrType();
82  int ncols = st.getSize();
83  /*
84  * Get dataspace and allocate memory for read buffer.
85  */
86  DataSpace space= ds.getSpace();
87  hsize_t sz;
88  space.getSimpleExtentDims(&sz);
89  int nrows = sz;
90  QVector<char> rbuff(nrows*ncols,0);
91 
92  ds.read(rbuff.data(),st,space);
93  for(int i=0; i<nrows; i++)
94  {
95  QByteArray ba(rbuff.constData() + i*ncols, ncols);
96  S << QString(ba);
97  }
98  return true;
99  }
100  return false;
101 }
102 
103 void h5helper_v1_0::write(CommonFG* h5obj, const char* name, const QDaqVector& v)
104 {
105  hsize_t dims = v.size();
106  if (dims<1) dims=1;
107  DataSpace space(1,&dims);
108  DataSet ds = h5obj->createDataSet(name, PredType::NATIVE_DOUBLE, space);
109  if (v.size()) ds.write(v.constData(),PredType::NATIVE_DOUBLE);
110 }
111 
112 bool h5helper_v1_0::read(CommonFG* h5obj, const char* name, QDaqVector& value)
113 {
114  if (!h5exist_ds(h5obj,name)) {
115  pushWarning(QString("DataSet '%1' not found in group '%2'")
116  .arg(name).arg(groupName(h5obj)));
117  return false;
118  }
119  DataSet ds = h5obj->openDataSet(name);
120  H5T_class_t ds_type = ds.getTypeClass();
121  if (ds_type==H5T_FLOAT)
122  {
123  DataSpace dspace = ds.getSpace();
124  int sz = dspace.getSimpleExtentNpoints();
125  value.setCapacity(sz);
126  ds.read(value.data(), ds.getDataType());
127  return true;
128  }
129  return false;
130 }
131 
132 bool h5helper_v1_0::read(CommonFG* h5obj, const char* name, int& value)
133 {
134  if (!h5exist_ds(h5obj,name)) {
135  pushWarning(QString("DataSet '%1' not found in group '%2'")
136  .arg(name).arg(groupName(h5obj)));
137  return false;
138  }
139  DataSet ds = h5obj->openDataSet(name);
140  H5T_class_t ds_type = ds.getTypeClass();
141  if (ds_type==H5T_INTEGER)
142  {
143  ds.read(&value, ds.getDataType());
144  return true;
145  }
146  return false;
147 }
148 
149 bool h5helper_v1_0::read(CommonFG* h5obj, const char* name, double& value)
150 {
151  if (!h5exist_ds(h5obj,name)) {
152  pushWarning(QString("DataSet '%1' not found in group '%2'")
153  .arg(name).arg(groupName(h5obj)));
154  return false;
155  }
156  DataSet ds = h5obj->openDataSet(name);
157  H5T_class_t ds_type = ds.getTypeClass();
158  if (ds_type==H5T_FLOAT)
159  {
160  ds.read(&value, ds.getDataType());
161  return true;
162  }
163  return false;
164 }
165 
166 void h5helper_v1_0::write(CommonFG* h5obj, const char* name, const double& value)
167 {
168  DataSpace space(H5S_SCALAR);
169  DataSet ds = h5obj->createDataSet(name,PredType::NATIVE_DOUBLE, space);
170  ds.write(&value,PredType::NATIVE_DOUBLE);
171 }
172 
173 void h5helper_v1_0::write(CommonFG* h5obj, const char* name, const int& value)
174 {
175  DataSpace space(H5S_SCALAR);
176  DataSet ds = h5obj->createDataSet(name,PredType::NATIVE_INT, space);
177  ds.write(&value,PredType::NATIVE_INT);
178 }
179 
180 void h5helper_v1_0::writeProperties(CommonFG* h5obj, const QDaqObject* m_object, const QMetaObject* metaObject)
181 {
182  // get the super-class meta-object
183  const QMetaObject* super = metaObject->superClass();
184 
185  // if it is null -> we reached the top, i.e. the QObject level -> return
186  if (!super) return;
187 
188  // if this is the first , write the class name
189  if (metaObject==m_object->metaObject())
190  {
191  write(h5obj,CLASS_ATTR_NAME, QString(metaObject->className()));
192  }
193 
194  // first write the properties of the super-class (this produces all properties up to the current object)
195  writeProperties(h5obj, m_object, super);
196 
197  // write this class properties
198  for (int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++)
199  {
200  QMetaProperty metaProperty = metaObject->property(idx);
201  if (metaProperty.isReadable() && metaProperty.isStored())
202  {
203  QVariant value = metaProperty.read(m_object);
204  if (metaProperty.isEnumType())
205  {
206  QMetaEnum metaEnum = metaProperty.enumerator();
207  int i = *reinterpret_cast<const int *>(value.constData());
208  write(h5obj,metaProperty.name(),metaEnum.valueToKey(i));
209  }
210  else {
211  int objtype = value.userType();
212  if (objtype==QVariant::Bool || objtype==QVariant::Char ||
213  objtype==QVariant::Int || objtype==QVariant::UInt)
214  {
215  write(h5obj,metaProperty.name(),value.toInt());
216  }
217  else if (objtype==QVariant::String)
218  {
219  QString S = value.toString();
220  if (S.isEmpty()) S=" ";
221  write(h5obj,metaProperty.name(),S);
222  }
223  else if (objtype==QVariant::StringList)
224  {
225  QStringList S = value.toStringList();
226  if (!S.isEmpty())
227  write(h5obj,metaProperty.name(),S);
228  }
229  else if (objtype==QVariant::Double) write(h5obj,metaProperty.name(),value.toDouble());
230  else if (objtype==qMetaTypeId<QDaqVector>())
231  write(h5obj,metaProperty.name(),value.value<QDaqVector>());
232  else if (objtype==qMetaTypeId<QDaqObject*>())
233  write(h5obj,metaProperty.name(),value.value<QDaqObject*>());
234  else if (objtype==qMetaTypeId<QDaqObjectList>())
235  write(h5obj,metaProperty.name(),value.value<QDaqObjectList>());
236  }
237  }
238  }
239 
240  // write the object's dynamic properties
241  if (metaObject==m_object->metaObject()) writeDynamicProperties(h5obj,m_object);
242 
243 }
244 
245 void h5helper_v1_0::writeDynamicProperties(CommonFG* h5obj, const QDaqObject* m_object)
246 {
247  if (!m_object->dynamicPropertyNames().isEmpty()) {
248  foreach(const QByteArray& ba, m_object->dynamicPropertyNames())
249  {
250  QVariant v = m_object->property(ba.constData());
251  write(h5obj,ba.constData(),v.toString());
252  }
253  }
254 }
255 
256 void h5helper_v1_0::readProperties(CommonFG *h5obj, QDaqObject* obj)
257 {
258  // get the meta-object
259  const QMetaObject* metaObject = obj->metaObject();
260 
261  // the "class" and "objectName" properties have been read when the obj was constructed
262  // start at index = 2
263  for (int idx = 2; idx < metaObject->propertyCount(); idx++)
264  {
265  QMetaProperty metaProperty = metaObject->property(idx);
266  if (metaProperty.isWritable() && metaProperty.isStored())
267  {
268  if (metaProperty.isEnumType())
269  {
270  QMetaEnum metaEnum = metaProperty.enumerator();
271  QString key;
272  if (read(h5obj,metaProperty.name(),key))
273  {
274  int v = metaEnum.keyToValue(key.toLatin1().constData());
275  if (v>=0)
276  metaProperty.write(obj,QVariant(v));
277  }
278  }
279  else {
280  int objtype = metaProperty.userType();
281  if (objtype==QVariant::Bool || objtype==QVariant::Char ||
282  objtype==QVariant::Int || objtype==QVariant::UInt)
283  {
284  int v;
285  if (read(h5obj,metaProperty.name(),v))
286  metaProperty.write(obj,QVariant(objtype,&v));
287  }
288  else if (objtype==QVariant::String)
289  {
290  QString v;
291  if (read(h5obj,metaProperty.name(),v))
292  metaProperty.write(obj,QVariant(v));
293  }
294  else if (objtype==QVariant::StringList)
295  {
296  QStringList v;
297  if (read(h5obj,metaProperty.name(),v))
298  metaProperty.write(obj,QVariant(v));
299  }
300  else if (objtype==QVariant::Double)
301  {
302  double v;
303  if (read(h5obj,metaProperty.name(),v))
304  metaProperty.write(obj,QVariant(v));
305  }
306  else if (objtype==qMetaTypeId<QDaqVector>())
307  {
308  QDaqVector v;
309  if (read(h5obj,metaProperty.name(),v))
310  metaProperty.write(obj,QVariant::fromValue(v));
311  }
312  else if (objtype==qMetaTypeId<QDaqObject*>())
313  {
314  QString path;
315  if (read(h5obj,metaProperty.name(),path))
316  deferObjPtrRead(obj,metaProperty.name(),path);
317  }
318  else if (objtype==qMetaTypeId<QDaqObjectList>())
319  {
320  QStringList pathList;
321  if (read(h5obj,metaProperty.name(),pathList))
322  deferObjPtrRead(obj,metaProperty.name(),pathList);
323  }
324  }
325  }
326  }
327 
328  readDynamicProperties(h5obj,obj);
329 }
330 
331 void h5helper_v1_0::readDynamicProperties(CommonFG* h5obj, QDaqObject* m_object)
332 {
333  Q_UNUSED(h5obj);
334  Q_UNUSED(m_object);
335 }
336 
337 Group h5helper_v1_0::createGroup(CommonFG* loc, const char* name)
338 {
339  return loc->createGroup(name);
340 }
341 
342 QByteArrayList h5helper_v1_0::getGroupNames(CommonFG* h5g, bool isRoot)
343 {
344  Q_UNUSED(isRoot);
345 
346  QByteArrayList names;
347 
348  int n = h5g->getNumObjs();
349  for(int i=0; i<n; ++i)
350  {
351  H5G_obj_t typ = h5g->getObjTypeByIdx(i);
352  if (typ==H5G_GROUP)
353  {
354  QByteArray groupName;
355  groupName.fill('\0',256);
356  h5g->getObjnameByIdx(i,groupName.data(),256);
357  names.push_back(groupName);
358  }
359  }
360 
361  return names;
362 }
363 
364 
365 
Base class of all QDaq objects.
Definition: QDaqObject.h:108
void setCapacity(int c)
Set the capacity.
Definition: QDaqVector.h:75
int size() const
Return the number of elememts stored in the buffer.
Definition: QDaqVector.h:64
A buffer for storing double numbers.
Definition: QDaqVector.h:40
QList< QDaqObject * > QDaqObjectList
A QList of QDaqObject pointers.
Definition: QDaqObject.h:23
double * data()
Return a pointer to the data.
Definition: QDaqVector.h:100
const double * constData() const
Return a const pointer to the data.
Definition: QDaqVector.h:98