#include <h5cpp/all>
#include <cstdint>
#include <iostream>
#include <memory>
namespace sn {
struct point_t { double x, y, z; };
}
}
}
H5CPP_REGISTER_STRUCT(sn::point_t);
int main() {
auto check = [](const char* label, bool ok) {
std::cout << (ok ?
"✔ ok " :
"✘ failed") <<
" " << label <<
"\n";
};
{
h5::write(fd,
"/uniq_arr", src, h5::count{N});
h5::read(fd,
"/uniq_arr", dst, h5::count{N});
bool ok = true;
for (
std::size_t i = 0; ok && i < N; ++i) ok = (dst[i] == src[i]);
check("std::unique_ptr<double[]>(16) write + read", ok);
}
{
for (
std::size_t i = 0; i < N; ++i) src[i] = static_cast<int>(i * i);
h5::write(fd,
"/shared_arr", src, h5::count{N});
h5::read(fd,
"/shared_arr", dst, h5::count{N});
bool ok = true;
for (
std::size_t i = 0; ok && i < N; ++i) ok = (dst[i] == src[i]);
check("std::shared_ptr<int[]>(12) write + read", ok);
}
{
auto src = std::make_unique<double>(3.14159);
auto dst = std::make_unique<double>(0.0);
check("std::unique_ptr<double> single scalar", *dst == *src);
}
{
auto src = std::make_shared<std::int64_t>(0xCAFEF00DBABE);
auto dst = std::make_shared<std::int64_t>(0);
check("std::shared_ptr<int64_t> single scalar", *dst == *src);
}
{
for (
std::size_t i = 0; i < N; ++i) src[i] =
float(i) * 0.5f;
h5::write(fd,
"/auto_alloc", src, h5::count{N});
bool ok = true;
for (
std::size_t i = 0; ok && i < N; ++i) ok = (buf[i] == src[i]);
check("auto h5::read<unique_ptr<float[]>>(fd, path)", ok);
}
{
src[i] = sn::point_t{double(i), 2.0 * i, 3.0 * i};
h5::write(fd,
"/points_uniq", src, h5::count{N});
h5::read(fd,
"/points_uniq", dst, h5::count{N});
bool pre_ok = true;
pre_ok = (dst[i].x == src[i].x && dst[i].y == src[i].y && dst[i].z == src[i].z);
check("unique_ptr<sn::point_t[]>(5) pre-alloc + read", pre_ok);
bool auto_ok = true;
auto_ok = (auto_buf[i].x == src[i].x
&& auto_buf[i].y == src[i].y
&& auto_buf[i].z == src[i].z);
check("auto h5::read<unique_ptr<sn::point_t[]>>(...) compound POD", auto_ok);
}
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
std::enable_if_t<!std::is_same_v< T, char ** >, void > read(const h5::ds_t &ds, T *ptr, args_t &&... args)
Read elements from an open HDF5 dataset into caller-allocated memory.
Definition H5Dread.hpp:56
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
public namespace
Definition compat.hpp:11