25#define RUN_NEXUSIOHELPER_FUNCTION(narrow, type, func_name, ...) \
27 case NXnumtype::FLOAT32: \
28 return func_name<T, float, narrow>(__VA_ARGS__); \
29 case NXnumtype::FLOAT64: \
30 return func_name<T, double, narrow>(__VA_ARGS__); \
31 case NXnumtype::INT8: \
32 return func_name<T, int8_t, narrow>(__VA_ARGS__); \
33 case NXnumtype::UINT8: \
34 return func_name<T, uint8_t, narrow>(__VA_ARGS__); \
35 case NXnumtype::INT16: \
36 return func_name<T, int16_t, narrow>(__VA_ARGS__); \
37 case NXnumtype::UINT16: \
38 return func_name<T, uint16_t, narrow>(__VA_ARGS__); \
39 case NXnumtype::INT32: \
40 return func_name<T, int32_t, narrow>(__VA_ARGS__); \
41 case NXnumtype::UINT32: \
42 return func_name<T, uint32_t, narrow>(__VA_ARGS__); \
43 case NXnumtype::INT64: \
44 return func_name<T, int64_t, narrow>(__VA_ARGS__); \
45 case NXnumtype::UINT64: \
46 return func_name<T, uint64_t, narrow>(__VA_ARGS__); \
48 std::string msg = "NexusIOHelper: Unknown type " + std::to_string((int)type) + " in Nexus file"; \
49 throw std::runtime_error(msg); \
53 return std::accumulate(size.cbegin(), size.cend(), int64_t{1}, std::multiplies<>());
56std::pair<Nexus::Info, bool> checkIfOpenAndGetInfo(Nexus::File &file,
const std::string &&entry) {
57 std::pair<Nexus::Info, bool> info_and_close;
58 info_and_close.second =
false;
59 if (!file.isDataSetOpen()) {
61 info_and_close.second =
true;
63 info_and_close.first = file.getInfo();
64 return info_and_close;
70template <
typename T,
typename U, Narrowing narrow>
71void doReadNexusAnyVector(std::vector<T> &out, Nexus::File &file,
const size_t size,
const bool close_data) {
75 throw std::runtime_error(
"Narrowing is forbidden in NexusIOHelper::readNexusAnyVector");
76 }
else if constexpr (std::is_same_v<T, U>) {
82 std::vector<U> buf(size);
84 std::transform(buf.cbegin(), buf.cend(), out.begin(), [](U a) -> T { return static_cast<T>(a); });
93template <
typename T,
typename U, Narrowing narrow>
94std::vector<T> readNexusAnyVector(Nexus::File &file,
const size_t size,
const bool close_data) {
95 std::vector<T>
vec(size);
96 doReadNexusAnyVector<T, U, narrow>(
vec, file, size, close_data);
101template <
typename T,
typename U, Narrowing narrow>
102void readNexusAnyVector(std::vector<T> &out, Nexus::File &file,
const size_t size,
const bool close_data) {
103 if (out.size() < size)
104 throw std::runtime_error(
"The output buffer is too small in NexusIOHelper::readNexusAnyVector");
105 doReadNexusAnyVector<T, U, narrow>(out, file, size, close_data);
111template <
typename T,
typename U, Narrowing narrow>
112void doReadNexusAnySlab(std::vector<T> &out, Nexus::File &file,
const Nexus::DimVector &start,
113 const Nexus::DimVector &size,
const int64_t volume,
const bool close_data) {
117 throw std::runtime_error(
"Narrowing is forbidden in NexusIOHelper::readNexusAnySlab");
118 }
else if constexpr (std::is_same_v<T, U>) {
120 file.getSlab(out.data(), start, size);
124 std::vector<U> buf(volume);
125 file.getSlab(buf.data(), start, size);
126 std::transform(buf.cbegin(), buf.cend(), out.begin(), [](U a) -> T { return static_cast<T>(a); });
129 if (volume > 0 && close_data) {
135template <
typename T,
typename U, Narrowing narrow>
137 const bool close_data) {
138 const auto volume = vectorVolume(size);
139 std::vector<T>
vec(volume);
140 doReadNexusAnySlab<T, U, narrow>(
vec, file, start, size, volume, close_data);
145template <
typename T,
typename U, Narrowing narrow>
146void readNexusAnySlab(std::vector<T> &out, Nexus::File &file,
const Nexus::DimVector &start,
148 const auto volume = vectorVolume(size);
149 if (out.size() <
static_cast<size_t>(volume))
150 throw std::runtime_error(
"The output buffer is too small in NexusIOHelper::readNexusAnySlab");
151 doReadNexusAnySlab<T, U, narrow>(out, file, start, size, volume, close_data);
157template <
typename T,
typename U, Narrowing narrow> T readNexusAnyVariable(Nexus::File &file,
const bool close_data) {
162 throw std::runtime_error(
"Narrowing is forbidden in NexusIOHelper::readAnyVariable");
163 }
else if constexpr (std::is_same_v<T, U>) {
167 file.getData<U>(&bufu);
168 buf = std::move(
static_cast<T
>(bufu));
182template <
typename T, Narrowing narrow = Narrowing::Prevent>
184 const auto info_and_close = checkIfOpenAndGetInfo(file, std::move(std::move(entry)));
186 vectorVolume((info_and_close.first).dims), info_and_close.second);
193template <
typename T, Narrowing narrow = Narrowing::Prevent>
194void readNexusVector(std::vector<T> &out, Nexus::File &file,
const std::string &entry =
"") {
195 const auto info_and_close = checkIfOpenAndGetInfo(file, std::move(std::move(entry)));
197 vectorVolume((info_and_close.first).dims), info_and_close.second);
203template <
typename T, Narrowing narrow = Narrowing::Prevent>
206 const auto info_and_close = checkIfOpenAndGetInfo(file, std::move(std::move(entry)));
208 info_and_close.second);
215template <
typename T, Narrowing narrow = Narrowing::Prevent>
218 const auto info_and_close = checkIfOpenAndGetInfo(file, std::move(std::move(entry)));
220 info_and_close.second);
223template <
typename T, Narrowing narrow = Narrowing::Prevent>
225 const auto info_and_close = checkIfOpenAndGetInfo(file, std::move(std::move(entry)));