HighFive 2.7.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Annotate_traits_misc.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#include <string>
12#include <vector>
13
14#include <H5Apublic.h>
15#include <H5Ppublic.h>
16
17#include "H5Attribute_misc.hpp"
18#include "H5Iterables_misc.hpp"
19
20namespace HighFive {
21
22template <typename Derivate>
23inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
24 const DataSpace& space,
25 const DataType& dtype) {
26 auto attr_id = H5Acreate2(static_cast<Derivate*>(this)->getId(),
27 attribute_name.c_str(),
28 dtype.getId(),
29 space.getId(),
30 H5P_DEFAULT,
31 H5P_DEFAULT);
32 if (attr_id < 0) {
34 std::string("Unable to create the attribute \"") + attribute_name + "\":");
35 }
36 return detail::make_attribute(attr_id);
37}
38
39template <typename Derivate>
40template <typename Type>
41inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
42 const DataSpace& space) {
43 return createAttribute(attribute_name, space, create_and_check_datatype<Type>());
44}
45
46template <typename Derivate>
47template <typename T>
48inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
49 const T& data) {
51 createAttribute(attribute_name,
52 DataSpace::From(data),
53 create_and_check_datatype<typename details::inspector<T>::base_type>());
54 att.write(data);
55 return att;
56}
57
58template <typename Derivate>
59inline void AnnotateTraits<Derivate>::deleteAttribute(const std::string& attribute_name) {
60 if (H5Adelete(static_cast<const Derivate*>(this)->getId(), attribute_name.c_str()) < 0) {
62 std::string("Unable to delete attribute \"") + attribute_name + "\":");
63 }
64}
65
66template <typename Derivate>
67inline Attribute AnnotateTraits<Derivate>::getAttribute(const std::string& attribute_name) const {
68 const auto attr_id =
69 H5Aopen(static_cast<const Derivate*>(this)->getId(), attribute_name.c_str(), H5P_DEFAULT);
70 if (attr_id < 0) {
72 std::string("Unable to open the attribute \"") + attribute_name + "\":");
73 }
74 return detail::make_attribute(attr_id);
75}
77template <typename Derivate>
79 int res = H5Aget_num_attrs(static_cast<const Derivate*>(this)->getId());
80 if (res < 0) {
82 std::string("Unable to count attributes in existing group or file"));
83 }
84 return static_cast<size_t>(res);
85}
86
87template <typename Derivate>
88inline std::vector<std::string> AnnotateTraits<Derivate>::listAttributeNames() const {
89 std::vector<std::string> names;
90 details::HighFiveIterateData iterateData(names);
91
92 size_t num_objs = getNumberAttributes();
93 names.reserve(num_objs);
94
95 if (H5Aiterate2(static_cast<const Derivate*>(this)->getId(),
96 H5_INDEX_NAME,
97 H5_ITER_INC,
98 NULL,
99 &details::internal_high_five_iterate<H5A_info_t>,
100 static_cast<void*>(&iterateData)) < 0) {
102 std::string("Unable to list attributes in group"));
103 }
104
105 return names;
106}
107
108template <typename Derivate>
109inline bool AnnotateTraits<Derivate>::hasAttribute(const std::string& attr_name) const {
110 int res = H5Aexists(static_cast<const Derivate*>(this)->getId(), attr_name.c_str());
111 if (res < 0) {
113 std::string("Unable to check for attribute in group"));
114 }
115 return res;
116}
117
118} // namespace HighFive
Attribute createAttribute(const std::string &attribute_name, const DataSpace &space, const DataType &type)
create a new attribute with the name attribute_name
Definition H5Annotate_traits_misc.hpp:23
std::vector< std::string > listAttributeNames() const
list all attribute name of the node / group
Definition H5Annotate_traits_misc.hpp:88
void deleteAttribute(const std::string &attribute_name)
deleteAttribute let you delete an attribute by its name.
Definition H5Annotate_traits_misc.hpp:59
Attribute getAttribute(const std::string &attribute_name) const
open an existing attribute with the name attribute_name
Definition H5Annotate_traits_misc.hpp:67
bool hasAttribute(const std::string &attr_name) const
checks an attribute exists
Definition H5Annotate_traits_misc.hpp:109
size_t getNumberAttributes() const
return the number of attributes of the node / group
Definition H5Annotate_traits_misc.hpp:78
Class representing an attribute of a dataset or group.
Definition H5Attribute.hpp:46
void write(const T &buffer)
Definition H5Attribute_misc.hpp:117
Class representing the space (dimensions) of a dataset.
Definition H5DataSpace.hpp:25
static DataSpace From(const T &value)
Create a dataspace matching a type accepted by details::inspector.
Definition H5Dataspace_misc.hpp:131
HDF5 Data Type.
Definition H5DataType.hpp:54
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:65
Definition H5_definitions.hpp:15
DataType create_and_check_datatype()
Create a DataType instance representing type T and perform a sanity check on its size.
Definition H5DataType_misc.hpp:479
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:42