HighFive 2.7.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Utils.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#pragma once
10
11// internal utilities functions
12#include <algorithm>
13#include <array>
14#include <cstddef> // __GLIBCXX__
15#include <exception>
16#include <string>
17#include <type_traits>
18#include <vector>
19#include <sstream>
20
21#include <H5public.h>
22
23#include "../H5Exception.hpp"
24#include "H5Friends.hpp"
25
26namespace HighFive {
27
28// If ever used, recognize dimensions of FixedLenStringArray
29template <std::size_t N>
30class FixedLenStringArray;
31
32namespace details {
33// converter function for hsize_t -> size_t when hsize_t != size_t
34template <typename Size>
35inline std::vector<std::size_t> to_vector_size_t(const std::vector<Size>& vec) {
36 static_assert(std::is_same<Size, std::size_t>::value == false,
37 " hsize_t != size_t mandatory here");
38 std::vector<size_t> res(vec.size());
39 std::transform(vec.cbegin(), vec.cend(), res.begin(), [](Size e) {
40 return static_cast<size_t>(e);
41 });
42 return res;
43}
44
45// converter function for hsize_t -> size_t when size_t == hsize_t
46inline std::vector<std::size_t> to_vector_size_t(const std::vector<std::size_t>& vec) {
47 return vec;
48}
49
50// read name from a H5 object using the specified function
51template <typename T>
52inline std::string get_name(T fct) {
53 const size_t maxLength = 255;
54 char buffer[maxLength + 1];
55 ssize_t retcode = fct(buffer, static_cast<hsize_t>(maxLength) + 1);
56 if (retcode < 0) {
57 HDF5ErrMapper::ToException<GroupException>("Error accessing object name");
58 }
59 const size_t length = static_cast<std::size_t>(retcode);
60 if (length <= maxLength) {
61 return std::string(buffer, length);
62 }
63 std::vector<char> bigBuffer(length + 1, 0);
64 fct(bigBuffer.data(), length + 1);
65 return std::string(bigBuffer.data(), length);
66}
67
68template <class Container>
69inline std::string format_vector(const Container& container) {
70 auto sout = std::stringstream{};
71
72 sout << "[ ";
73 for (size_t i = 0; i < container.size(); ++i) {
74 sout << container[i] << (i == container.size() - 1 ? "" : ", ");
75 }
76 sout << "]";
77
78 return sout.str();
79}
80
81} // namespace details
82} // namespace HighFive
Definition H5_definitions.hpp:15
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:42