HighFive 2.9.0
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
28namespace deprecated {
29// If ever used, recognize dimensions of FixedLenStringArray
30template <std::size_t N>
31class FixedLenStringArray;
32} // namespace deprecated
33
34namespace details {
35// converter function for hsize_t -> size_t when hsize_t != size_t
36template <typename Size>
37inline std::vector<std::size_t> to_vector_size_t(const std::vector<Size>& vec) {
38 static_assert(std::is_same<Size, std::size_t>::value == false,
39 " hsize_t != size_t mandatory here");
40 std::vector<size_t> res(vec.size());
41 std::transform(vec.cbegin(), vec.cend(), res.begin(), [](Size e) {
42 return static_cast<size_t>(e);
43 });
44 return res;
45}
46
47// converter function for hsize_t -> size_t when size_t == hsize_t
48inline std::vector<std::size_t> to_vector_size_t(const std::vector<std::size_t>& vec) {
49 return vec;
50}
51
52// read name from a H5 object using the specified function
53template <typename T>
54inline std::string get_name(T fct) {
55 const size_t maxLength = 255;
56 char buffer[maxLength + 1];
57 ssize_t retcode = fct(buffer, static_cast<hsize_t>(maxLength) + 1);
58 if (retcode < 0) {
59 HDF5ErrMapper::ToException<GroupException>("Error accessing object name");
60 }
61 const size_t length = static_cast<std::size_t>(retcode);
62 if (length <= maxLength) {
63 return std::string(buffer, length);
64 }
65 std::vector<char> bigBuffer(length + 1, 0);
66 fct(bigBuffer.data(), length + 1);
67 return std::string(bigBuffer.data(), length);
68}
69
70template <class Container>
71inline std::string format_vector(const Container& container) {
72 auto sout = std::stringstream{};
73
74 sout << "[ ";
75 for (size_t i = 0; i < container.size(); ++i) {
76 sout << container[i] << (i == container.size() - 1 ? "" : ", ");
77 }
78 sout << "]";
79
80 return sout.str();
81}
82
83} // namespace details
84} // namespace HighFive
Definition H5_definitions.hpp:22
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:43