#include "custom_types.hpp"
#include <h5cpp/all>
#include <iomanip>
#include <iostream>
#include <vector>
#if defined(__STDCPP_FLOAT16_T__)
#include <stdfloat>
#endif
namespace {
void section(const char* title) {
}
}
int main() {
auto fd =
h5::create(
"datatypes.h5", H5F_ACC_TRUNC);
section("1. strong typedef — Celsius");
{
Celsius{-40.0f}, Celsius{0.0f}, Celsius{20.5f}, Celsius{100.0f}};
h5::write(fd,
"/strong_typedef/temps", temps);
for (
auto& c : temps)
std::cout << c <<
" ";
for (
auto& c : back)
std::cout << c <<
" ";
}
section("2. N-bit packed integer — bitstring::n_bit");
{
namespace bs = bitstring;
bs::n_bit{2}, bs::n_bit{3},
bs::n_bit{1}, bs::n_bit{0}};
for (
auto& v : values)
std::cout << v <<
" ";
for (
auto& v : back)
std::cout << v <<
" ";
std::cout <<
"\n storage: H5T_NATIVE_UCHAR with H5Tset_precision(2)\n";
}
section("3. opaque bytes — bitstring::two_bit");
{
namespace bs = bitstring;
bs::two_bit{0b11'11'00'00},
bs::two_bit{0b10'10'01'01}};
for (
auto& b : blobs)
std::cout <<
"[" << b <<
"] ";
for (
auto& b : back)
std::cout <<
"[" << b <<
"] ";
std::cout <<
"\n storage: H5T_OPAQUE (1 byte) tagged \"bitstring::two_bit\"\n";
}
section("4. naming + introspection");
{
std::cout <<
" " << h5::name<float>::value <<
"\n";
std::cout <<
" " << h5::name<Celsius>::value <<
"\n";
std::cout <<
" " << h5::name<bitstring::n_bit>::value <<
"\n";
std::cout <<
" " << h5::name<bitstring::two_bit>::value <<
"\n";
std::cout <<
" " << h5::name<Status>::value <<
"\n";
std::cout <<
"\n runtime dt_t<T> introspection:\n";
}
#if defined(__STDCPP_FLOAT16_T__)
section("5. half-precision float — std::float16_t");
{
std::float16_t{0.0f},
std::float16_t{1.0f},
std::float16_t{2.71828f}};
for (
auto v : vec)
std::cout << static_cast<float>(v) <<
" ";
for (
auto v : back)
std::cout << static_cast<float>(v) <<
" ";
std::cout <<
"\n storage: 2-byte IEEE binary16\n";
}
#else
section("5. half-precision float — std::float16_t");
std::cout <<
" skipped: this TU was not built with __STDCPP_FLOAT16_T__\n";
std::cout <<
" see examples/half-float/ for third-party variants\n";
#endif
section("6. strong enum — H5T_ENUM");
{
Status::Failed, Status::Active,
Status::Inactive};
for (
auto s : states)
std::cout << s <<
" ";
for (
auto s : back)
std::cout << s <<
" ";
std::cout <<
"\n storage: H5T_ENUM over H5T_NATIVE_UINT8\n";
std::cout <<
" (h5dump prints names; round-trip preserves values)\n";
}
std::cout <<
"\nWrote everything to ./datatypes.h5\n";
std::cout <<
"Inspect with: h5dump -pH datatypes.h5\n";
return 0;
}
h5::at_t create(const hid_t &parent, const std::string &path, args_t &&... args)
Create a new attribute of element type T on a parent HDF5 object.
Definition H5Acreate.hpp:100
T aread(const hid_t &ds, const std::string &name, const h5::acpl_t &acpl=h5::default_acpl)
Read an attribute by name and return its value as type T.
Definition H5Aread.hpp:76
h5::gr_t write(const LOC &parent, const std::string &path, const T &src)
Write a sparse matrix or vector as a CSC group.
Definition H5Dsparse.hpp:185