add h5_types + class STabel : public Table ( field types known by compile time)

This commit is contained in:
Christian Zimmermann 2023-01-22 23:34:30 +01:00
parent 69a9344790
commit 59932895e9
5 changed files with 156 additions and 6 deletions

View file

@ -3,6 +3,7 @@
#define __cxz_h5_table_cc_h__ #define __cxz_h5_table_cc_h__
#include "h5_table.h" #include "h5_table.h"
#include "h5_types.h"
namespace CNORXZ namespace CNORXZ
{ {
@ -15,9 +16,52 @@ namespace CNORXZ
} }
template <typename... Ts> template <typename... Ts>
Table& appendRecord(const Tuple<Ts...>& t) const STabel<Ts...>::STabel(const String& name, const ContentBase* _parent) :
Table(name, _parent)
{ {
// checks..!!!!
}
template <typename... Ts>
STabel& STabel<Ts...>::appendRecord(const Tuple<Ts...>& t)
{
if(not mCheckedFile){
this->open();
}
this->close(); // H5TB does not require an open dataset
CXZ_ASSERT(mFields != nullptr,
"field names have to be initialized before creating table");
CXZ_ASSERT(mFields->size() == sizeof...(Ts),
"expected tuple of size = " << mFields->size()
<< ", got: " << sizeof...(Ts));
RangePtr appr = CRangeFactory(1).create();
auto offsets = iter<0,sizeof...(Ts)>
( [&](auto i) { return &std::get<i>(t) - &t; },
[](const auto&... e) { return Vector<SizeT>({e...}); });
auto sizes = iter<0,sizeof...(Ts)>
( [&](auto i) { return sizeof(std::get<i>(t)); },
[](const auto&... e) { return Vector<SizeT>({e...}); });
auto types = iter<0,sizeof...(Ts)>
( [&](auto i) { return getTypeId(std::get<i>(t)); },
[](const auto&... e) { return Vector<hid_t>({e...}); });
if(mRecords == nullptr){
mRecords = appr;
mSizes = MArray<SizeT>(std::move(sizes));
mOffsets = MArray<SizeT>(std::move(offsets));
mTypes = MArray<hid_t>(std::move(types));
// init
}
else {
// check consistency
mRecords = mRecords->extend(appr);
}
// append
H5TBappend_records(mParent->id(), mName, 1, sizeof(t),
mOffsets.data(), mSizes.data(), &t);
return *this;
} }
} }
} }

View file

@ -23,15 +23,11 @@ namespace CNORXZ
virtual String filename() const override final; virtual String filename() const override final;
Table& openDSet(); Table& openDSet();
Table& initFieldNames(const Vector<String>& fnames);
template <class F> template <class F>
decltype(auto) iterRecords(F&& f) const; decltype(auto) iterRecords(F&& f) const;
template <typename... Ts>
Table& appendRecord(const Tuple<Ts...>& t) const;
template <typename... Ts>
Table& appendRecord(const MArray<Tuple<Ts...>>& t) const;
private: private:
RangePtr mRecords; RangePtr mRecords;
@ -40,6 +36,19 @@ namespace CNORXZ
MArray<SizeT> mOffsets; MArray<SizeT> mOffsets;
MArray<hid_t> mTypes; MArray<hid_t> mTypes;
hid_t mType; hid_t mType;
bool mCheckedFile = false;
};
template <typename... Ts>
class STabel : public Table
{
public:
DEFAULT_MEMBERS(STabel);
STabel(const String& name, const ContentBase* _parent);
Table& appendRecord(const Tuple<Ts...>& t);
Table& appendRecord(const MArray<Tuple<Ts...>>& t);
}; };
} }
} }

View file

@ -0,0 +1,30 @@
#ifndef __cxz_h5_types_cc_h__
#define __cxz_h5_types_cc_h__
namespace CNORXZ
{
namespace hdf5
{
template <typename T, SizeT N>
inline hid_t TypeId<Arr<T,N>>::get(Arr<T,N> x)
{
static hid_t arrtype = H5Tarray_create2(getTypeId(x[0]), 1, N);
return arrtype;
}
template <typename T>
hid_t getTypeId(T x)
{
if constexpr (TypeIsNative<T>::value){
return NativeTypeId<T>::value;
}
else {
return TypeId<T>::get(x);
}
}
}
}
#endif

View file

@ -0,0 +1,60 @@
#ifndef __cxz_h5_types_h__
#define __cxz_h5_types_h__
namespace CNORXZ
{
namespace hdf5
{
/********************
* TypeIsNative *
********************/
template <typename T>
struct TypeIsNative { static constexpr bool value = false; };
template <>
struct TypeIsNative<SizeT> { static constexpr bool value = true; };
template <typename T>
struct TypeIsNative<Int> { static constexpr bool value = true; };
template <typename T>
struct TypeIsNative<Double> { static constexpr bool value = true; };
/********************
* NativeTypeId *
********************/
template <typename T>
struct NativeTypeId { static constexpr hid_t value = 0; };
template <>
struct NativeTypeId<SizeT> { static constexpr hid_t value = H5T_NATIVE_ULONG; };
template <>
struct NativeTypeId<Int> { static constexpr hid_t value = H5T_NATIVE_INT; };
template <>
struct NativeTypeId<Double> { static constexpr hid_t value = H5T_NATIVE_DOUBLE; };
/**************
* TypeId *
**************/
template <typename T>
struct TypeId<T> { static inline hid_t get() { return 0; } };
template <typename T, SizeT N>
struct TypeId<Arr<T,N>> { static inline hid_t get(); };
/*****************
* getTypeId *
*****************/
template <typename T>
hid_t getTypeId(const T& x);
}
}
#endif

View file

@ -56,6 +56,7 @@ namespace CNORXZ
} }
mTypes = MArray<hid_t>(mFields, std::move(types)); mTypes = MArray<hid_t>(mFields, std::move(types));
} }
mCheckedFile = true; // now an empty mRecords etc indicates that there is currently no table at the requested location
return *this; return *this;
} }
@ -87,5 +88,11 @@ namespace CNORXZ
return *this; return *this;
} }
Table& Table::initFieldNames(const Vector<String>& fnames)
{
CXZ_ASSERT(mFields == nullptr, "fields already initialized");
mFields = URangeFactory<String>(fnames).create();
return *this;
}
} }
} }