HighFive 2.3.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#ifndef H5ANNOTATE_TRAITS_MISC_HPP
10#define H5ANNOTATE_TRAITS_MISC_HPP
11
12#include <string>
13#include <vector>
14
15#include <H5Apublic.h>
16#include <H5Ppublic.h>
17
18#include "H5Attribute_misc.hpp"
19#include "H5Iterables_misc.hpp"
20
21namespace HighFive {
22
23template <typename Derivate>
24inline Attribute
25AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
26 const DataSpace& space,
27 const DataType& dtype) {
28 auto attr_id = H5Acreate2(static_cast<Derivate*>(this)->getId(),
29 attribute_name.c_str(),
30 dtype._hid, space._hid, H5P_DEFAULT, H5P_DEFAULT);
31 if (attr_id < 0) {
32 HDF5ErrMapper::ToException<AttributeException>(
33 std::string("Unable to create the attribute \"") + attribute_name + "\":");
34 }
35 return Attribute(attr_id);
36}
37
38template <typename Derivate>
39template <typename Type>
41AnnotateTraits<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
49AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
50 const T& data) {
51 Attribute att = createAttribute(
52 attribute_name,
53 DataSpace::From(data),
54 create_and_check_datatype<typename details::inspector<T>::base_type>());
55 att.write(data);
56 return att;
57}
59template<typename Derivate>
60inline void
61AnnotateTraits<Derivate>::deleteAttribute(const std::string& attribute_name) {
62 if (H5Adelete(static_cast<const Derivate*>(this)->getId(), attribute_name.c_str()) < 0) {
63 HDF5ErrMapper::ToException<AttributeException>(
64 std::string("Unable to delete attribute \"") + attribute_name + "\":");
65 }
66}
67
68template <typename Derivate>
70 const std::string& attribute_name) const {
71 const auto attr_id = H5Aopen(static_cast<const Derivate*>(this)->getId(),
72 attribute_name.c_str(), H5P_DEFAULT);
73 if (attr_id < 0) {
74 HDF5ErrMapper::ToException<AttributeException>(
75 std::string("Unable to open the attribute \"") + attribute_name + "\":");
76 }
77 return Attribute(attr_id);
78}
80template <typename Derivate>
82 int res = H5Aget_num_attrs(static_cast<const Derivate*>(this)->getId());
83 if (res < 0) {
84 HDF5ErrMapper::ToException<AttributeException>(std::string(
85 "Unable to count attributes in existing group or file"));
86 }
87 return static_cast<size_t>(res);
88}
89
90template <typename Derivate>
91inline std::vector<std::string>
93
94 std::vector<std::string> names;
95 details::HighFiveIterateData iterateData(names);
96
97 size_t num_objs = getNumberAttributes();
98 names.reserve(num_objs);
99
100 if (H5Aiterate2(static_cast<const Derivate*>(this)->getId(), H5_INDEX_NAME,
101 H5_ITER_INC, NULL,
102 &details::internal_high_five_iterate<H5A_info_t>,
103 static_cast<void*>(&iterateData)) < 0) {
104 HDF5ErrMapper::ToException<AttributeException>(
105 std::string("Unable to list attributes in group"));
106 }
107
108 return names;
109}
110
111template <typename Derivate>
112inline bool
113AnnotateTraits<Derivate>::hasAttribute(const std::string& attr_name) const {
114 int res = H5Aexists(static_cast<const Derivate*>(this)->getId(),
115 attr_name.c_str());
116 if (res < 0) {
117 HDF5ErrMapper::ToException<AttributeException>(
118 std::string("Unable to check for attribute in group"));
119 }
120 return res;
121}
122
123} // namespace HighFive
124
125#endif // H5ANNOTATE_TRAITS_MISC_HPP
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:25
std::vector< std::string > listAttributeNames() const
list all attribute name of the node / group
Definition: H5Annotate_traits_misc.hpp:92
void deleteAttribute(const std::string &attribute_name)
deleteAttribute let you delete an attribute by its name.
Definition: H5Annotate_traits_misc.hpp:61
Attribute getAttribute(const std::string &attribute_name) const
open an existing attribute with the name attribute_name
Definition: H5Annotate_traits_misc.hpp:69
bool hasAttribute(const std::string &attr_name) const
checks an attribute exists
Definition: H5Annotate_traits_misc.hpp:113
size_t getNumberAttributes() const
return the number of attributes of the node / group
Definition: H5Annotate_traits_misc.hpp:81
Class representing an attribute of a dataset or group.
Definition: H5Attribute.hpp:25
void write(const T &buffer)
Definition: H5Attribute_misc.hpp:94
Class representing the space (dimensions) of a dataset.
Definition: H5DataSpace.hpp:37
static DataSpace From(const T &value)
Create a dataspace matching a type accepted by details::inspector.
Definition: H5Dataspace_misc.hpp:133
HDF5 Data Type.
Definition: H5DataType.hpp:42
hid_t _hid
Definition: H5Object.hpp:87
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:434