#include <h5cpp/all>
#include <array>
#include <cstring>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
template <class Fn>
static void try_block(const char* label, Fn&& fn) {
try { fn(); }
} catch (...) {
std::cout <<
"✘ failed " << label <<
" (unknown exception)\n";
}
}
int main() {
auto check = [](const char* label, bool ok) {
std::cout << (ok ?
"✔ ok " :
"✘ failed") <<
" " << label <<
"\n";
};
try_block("std::string scalar round-trip", [&]() {
std::cout <<
" string = \"" << back <<
"\"\n";
check("std::string scalar round-trip", back == s);
});
try_block("std::string_view write + read-as-string", [&]() {
std::string_view sv = owner;
h5::read(fd,
"/strings/from_view", back);
std::cout <<
" string_view -> string = \"" << back <<
"\"\n";
check("std::string_view write + read-as-string", back == owner);
});
#ifndef _MSC_VER
try_block("char[20] fixed-length round-trip", [&]() {
char src[20] = "fixed-length";
char back[20] = {};
std::cout <<
" char[20] = \"" << back <<
"\"\n";
bool ok = true;
for (int i = 0; ok && i < 20; ++i) ok = (back[i] == src[i]);
check("char[20] fixed-length round-trip", ok);
});
#else
std::cout <<
"◇ skipped char[20] round-trip on MSVC (overload ambiguity)\n";
#endif
try_block("std::array<char, 20> fixed-length round-trip", [&]() {
h5::read(fd,
"/strings/fixed_arr", back);
std::cout <<
" array<char,20> = \"" << back.data() <<
"\"\n";
check("std::array<char, 20> fixed-length round-trip", back == src);
});
try_block("std::vector<std::array<char,8>>(3) fls_dataset round-trip", [&]() {
std::cout <<
" vector<array<char,8>>(3) = [\""
<< back[0].data() << "\",\""
<< back[1].data() << "\",\""
<< back[2].data() << "\"]\n";
bool ok = back.size() == src.size();
for (size_t i = 0; ok && i < src.size(); ++i) ok = (back[i] == src[i]);
check("std::vector<std::array<char,8>>(3) fls_dataset round-trip", ok);
});
try_block("std::vector<std::string>(5) round-trip", [&]() {
std::cout <<
" vector<string>(5) = " << back <<
"\n";
check("std::vector<std::string>(5) round-trip", back == v);
});
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