1 #include "h5helper_v1_0.h"
3 #include "QDaqVector.h"
4 #include "QDaqObject.h"
6 #include <QMetaProperty>
8 #define CLASS_ATTR_NAME "Class"
10 void h5helper_v1_0::write(CommonFG* h5obj,
const char* name,
const QString& S)
14 StrType type(PredType::C_S1, len);
16 DataSpace space(1,&dims);
17 DataSet ds = h5obj->createDataSet(name, type, space);
18 ds.write(S.toLatin1().constData(),type);
20 void h5helper_v1_0::write(CommonFG* h5obj,
const char* name,
const QStringList& S)
22 int ncols = 0, nrows = S.size();
23 foreach(
const QString& s, S)
if (s.length() > ncols) ncols = s.length();
25 QVector<char> wbuff(ncols*nrows);
26 for(
int i=0; i<nrows; i++)
28 QByteArray ba = S.at(i).toLatin1();
29 memcpy(wbuff.data() + (i*ncols),ba.constData(),ba.length());
32 StrType type(PredType::C_S1, ncols);
36 DataSpace space(1, &dims);
39 DataSet dataset = h5obj->createDataSet(name, type, space);
42 dataset.write(wbuff.constData(), type, space);
49 bool h5helper_v1_0::read(CommonFG* h5obj,
const char* name, QString& str)
51 if (!h5exist_ds(h5obj,name)) {
52 pushWarning(QString(
"DataSet '%1' not found in group '%2'")
53 .arg(name).arg(groupName(h5obj)));
56 DataSet ds = h5obj->openDataSet( name );
57 H5T_class_t type_class = ds.getTypeClass();
58 if (type_class==H5T_STRING)
60 StrType st = ds.getStrType();
61 int sz = st.getSize();
62 QByteArray buff(sz,
'0');
63 ds.read(buff.data(),st);
69 bool h5helper_v1_0::read(CommonFG* h5obj,
const char* name, QStringList& S)
71 if (!h5exist_ds(h5obj,name)) {
72 pushWarning(QString(
"DataSet '%1' not found in group '%2'")
73 .arg(name).arg(groupName(h5obj)));
76 DataSet ds = h5obj->openDataSet( name );
77 H5T_class_t type_class = ds.getTypeClass();
78 if (type_class==H5T_STRING)
81 StrType st = ds.getStrType();
82 int ncols = st.getSize();
86 DataSpace space= ds.getSpace();
88 space.getSimpleExtentDims(&sz);
90 QVector<char> rbuff(nrows*ncols,0);
92 ds.read(rbuff.data(),st,space);
93 for(
int i=0; i<nrows; i++)
95 QByteArray ba(rbuff.constData() + i*ncols, ncols);
103 void h5helper_v1_0::write(CommonFG* h5obj,
const char* name,
const QDaqVector& v)
105 hsize_t dims = v.
size();
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);
112 bool h5helper_v1_0::read(CommonFG* h5obj,
const char* name,
QDaqVector& value)
114 if (!h5exist_ds(h5obj,name)) {
115 pushWarning(QString(
"DataSet '%1' not found in group '%2'")
116 .arg(name).arg(groupName(h5obj)));
119 DataSet ds = h5obj->openDataSet(name);
120 H5T_class_t ds_type = ds.getTypeClass();
121 if (ds_type==H5T_FLOAT)
123 DataSpace dspace = ds.getSpace();
124 int sz = dspace.getSimpleExtentNpoints();
126 ds.read(value.
data(), ds.getDataType());
132 bool h5helper_v1_0::read(CommonFG* h5obj,
const char* name,
int& value)
134 if (!h5exist_ds(h5obj,name)) {
135 pushWarning(QString(
"DataSet '%1' not found in group '%2'")
136 .arg(name).arg(groupName(h5obj)));
139 DataSet ds = h5obj->openDataSet(name);
140 H5T_class_t ds_type = ds.getTypeClass();
141 if (ds_type==H5T_INTEGER)
143 ds.read(&value, ds.getDataType());
149 bool h5helper_v1_0::read(CommonFG* h5obj,
const char* name,
double& value)
151 if (!h5exist_ds(h5obj,name)) {
152 pushWarning(QString(
"DataSet '%1' not found in group '%2'")
153 .arg(name).arg(groupName(h5obj)));
156 DataSet ds = h5obj->openDataSet(name);
157 H5T_class_t ds_type = ds.getTypeClass();
158 if (ds_type==H5T_FLOAT)
160 ds.read(&value, ds.getDataType());
166 void h5helper_v1_0::write(CommonFG* h5obj,
const char* name,
const double& value)
168 DataSpace space(H5S_SCALAR);
169 DataSet ds = h5obj->createDataSet(name,PredType::NATIVE_DOUBLE, space);
170 ds.write(&value,PredType::NATIVE_DOUBLE);
173 void h5helper_v1_0::write(CommonFG* h5obj,
const char* name,
const int& value)
175 DataSpace space(H5S_SCALAR);
176 DataSet ds = h5obj->createDataSet(name,PredType::NATIVE_INT, space);
177 ds.write(&value,PredType::NATIVE_INT);
180 void h5helper_v1_0::writeProperties(CommonFG* h5obj,
const QDaqObject* m_object,
const QMetaObject* metaObject)
183 const QMetaObject* super = metaObject->superClass();
189 if (metaObject==m_object->metaObject())
191 write(h5obj,CLASS_ATTR_NAME, QString(metaObject->className()));
195 writeProperties(h5obj, m_object, super);
198 for (
int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++)
200 QMetaProperty metaProperty = metaObject->property(idx);
201 if (metaProperty.isReadable() && metaProperty.isStored())
203 QVariant value = metaProperty.read(m_object);
204 if (metaProperty.isEnumType())
206 QMetaEnum metaEnum = metaProperty.enumerator();
207 int i = *
reinterpret_cast<const int *
>(value.constData());
208 write(h5obj,metaProperty.name(),metaEnum.valueToKey(i));
211 int objtype = value.userType();
212 if (objtype==QVariant::Bool || objtype==QVariant::Char ||
213 objtype==QVariant::Int || objtype==QVariant::UInt)
215 write(h5obj,metaProperty.name(),value.toInt());
217 else if (objtype==QVariant::String)
219 QString S = value.toString();
220 if (S.isEmpty()) S=
" ";
221 write(h5obj,metaProperty.name(),S);
223 else if (objtype==QVariant::StringList)
225 QStringList S = value.toStringList();
227 write(h5obj,metaProperty.name(),S);
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>())
241 if (metaObject==m_object->metaObject()) writeDynamicProperties(h5obj,m_object);
245 void h5helper_v1_0::writeDynamicProperties(CommonFG* h5obj,
const QDaqObject* m_object)
247 if (!m_object->dynamicPropertyNames().isEmpty()) {
248 foreach(
const QByteArray& ba, m_object->dynamicPropertyNames())
250 QVariant v = m_object->property(ba.constData());
251 write(h5obj,ba.constData(),v.toString());
256 void h5helper_v1_0::readProperties(CommonFG *h5obj,
QDaqObject* obj)
259 const QMetaObject* metaObject = obj->metaObject();
263 for (
int idx = 2; idx < metaObject->propertyCount(); idx++)
265 QMetaProperty metaProperty = metaObject->property(idx);
266 if (metaProperty.isWritable() && metaProperty.isStored())
268 if (metaProperty.isEnumType())
270 QMetaEnum metaEnum = metaProperty.enumerator();
272 if (read(h5obj,metaProperty.name(),key))
274 int v = metaEnum.keyToValue(key.toLatin1().constData());
276 metaProperty.write(obj,QVariant(v));
280 int objtype = metaProperty.userType();
281 if (objtype==QVariant::Bool || objtype==QVariant::Char ||
282 objtype==QVariant::Int || objtype==QVariant::UInt)
285 if (read(h5obj,metaProperty.name(),v))
286 metaProperty.write(obj,QVariant(objtype,&v));
288 else if (objtype==QVariant::String)
291 if (read(h5obj,metaProperty.name(),v))
292 metaProperty.write(obj,QVariant(v));
294 else if (objtype==QVariant::StringList)
297 if (read(h5obj,metaProperty.name(),v))
298 metaProperty.write(obj,QVariant(v));
300 else if (objtype==QVariant::Double)
303 if (read(h5obj,metaProperty.name(),v))
304 metaProperty.write(obj,QVariant(v));
306 else if (objtype==qMetaTypeId<QDaqVector>())
309 if (read(h5obj,metaProperty.name(),v))
310 metaProperty.write(obj,QVariant::fromValue(v));
312 else if (objtype==qMetaTypeId<QDaqObject*>())
315 if (read(h5obj,metaProperty.name(),path))
316 deferObjPtrRead(obj,metaProperty.name(),path);
318 else if (objtype==qMetaTypeId<QDaqObjectList>())
320 QStringList pathList;
321 if (read(h5obj,metaProperty.name(),pathList))
322 deferObjPtrRead(obj,metaProperty.name(),pathList);
328 readDynamicProperties(h5obj,obj);
331 void h5helper_v1_0::readDynamicProperties(CommonFG* h5obj,
QDaqObject* m_object)
337 Group h5helper_v1_0::createGroup(CommonFG* loc,
const char* name)
339 return loc->createGroup(name);
342 QByteArrayList h5helper_v1_0::getGroupNames(CommonFG* h5g,
bool isRoot)
346 QByteArrayList names;
348 int n = h5g->getNumObjs();
349 for(
int i=0; i<n; ++i)
351 H5G_obj_t typ = h5g->getObjTypeByIdx(i);
354 QByteArray groupName;
355 groupName.fill(
'\0',256);
356 h5g->getObjnameByIdx(i,groupName.data(),256);
357 names.push_back(groupName);
Base class of all QDaq objects.
void setCapacity(int c)
Set the capacity.
int size() const
Return the number of elememts stored in the buffer.
A buffer for storing double numbers.
QList< QDaqObject * > QDaqObjectList
A QList of QDaqObject pointers.
double * data()
Return a pointer to the data.
const double * constData() const
Return a const pointer to the data.