h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5Annotate_traits_misc.hpp
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 h5gt {
22
23template <typename Derivate>
24inline Attribute
25AnnotateTraits<Derivate>::createAttribute(const std::string& attr_name,
26 const DataSpace& space,
27 const DataType& dtype) {
28 Attribute attribute;
29 if ((attribute._hid = H5Acreate2(
30 static_cast<Derivate*>(this)->getId(false), attr_name.c_str(),
31 dtype._hid, space._hid, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
32 HDF5ErrMapper::ToException<AttributeException>(
33 std::string("Unable to create the attribute \"") + attr_name + "\":");
34 }
35 return attribute;
36}
37
38template <typename Derivate>
39template <typename Type>
41AnnotateTraits<Derivate>::createAttribute(const std::string& attr_name,
42 const DataSpace& space) {
43 return createAttribute(attr_name, space, create_and_check_datatype<Type>());
44}
45
46template <typename Derivate>
47template <typename T>
48inline Attribute
49AnnotateTraits<Derivate>::createAttribute(const std::string& attr_name,
50 const T& data) {
51 Attribute att = createAttribute(
52 attr_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& attr_name) {
62 if (H5Adelete(static_cast<const Derivate*>(this)->getId(false), attr_name.c_str()) < 0) {
63 HDF5ErrMapper::ToException<AttributeException>(
64 std::string("Unable to delete attribute \"") + attr_name + "\":");
65 }
66}
67
68template <typename Derivate>
70 const std::string& attr_name) const {
71 Attribute attribute;
72 if ((attribute._hid = H5Aopen(static_cast<const Derivate*>(this)->getId(false),
73 attr_name.c_str(), H5P_DEFAULT)) < 0) {
74 HDF5ErrMapper::ToException<AttributeException>(
75 std::string("Unable to open the attribute \"") + attr_name +
76 "\":");
77 }
78 return attribute;
80
81template <typename Derivate>
83 int res = H5Aget_num_attrs(static_cast<const Derivate*>(this)->getId(false));
84 if (res < 0) {
85 HDF5ErrMapper::ToException<AttributeException>(
86 std::string(
87 "Unable to count attributes in existing group or file"));
88 }
89 return static_cast<size_t>(res);
90}
91
92template <typename Derivate>
93inline std::vector<std::string>
95
96 std::vector<std::string> names;
97 details::H5GTIterateData iterateData(names);
98
99 size_t num_objs = getNumberAttributes();
100 names.reserve(num_objs);
101
102 if (H5Aiterate2(static_cast<const Derivate*>(this)->getId(false), H5_INDEX_NAME,
103 H5_ITER_INC, NULL,
104 &details::internal_h5gt_iterate<H5A_info_t>,
105 static_cast<void*>(&iterateData)) < 0) {
106 HDF5ErrMapper::ToException<AttributeException>(
107 std::string("Unable to list attributes in group"));
108 }
109
110 return names;
111}
112
113template <typename Derivate>
114inline bool
115AnnotateTraits<Derivate>::hasAttribute(const std::string& attr_name) const {
116 if (attr_name.empty())
117 return false;
118
119 int res = H5Aexists(static_cast<const Derivate*>(this)->getId(false),
120 attr_name.c_str());
121 if (res < 0) {
122 HDF5ErrMapper::ToException<AttributeException>(
123 std::string("Unable to check for attribute in group"));
124 }
125 return res;
126}
127
128} // namespace h5gt
129
130#endif // H5ANNOTATE_TRAITS_MISC_HPP
std::vector< std::string > listAttributeNames() const
list all attribute name of the object
Definition H5Annotate_traits_misc.hpp:94
void deleteAttribute(const std::string &attr_name)
deleteAttribute let you delete an attribute by its name.
Definition H5Annotate_traits_misc.hpp:61
bool hasAttribute(const std::string &attr_name) const
checks an attribute exists
Definition H5Annotate_traits_misc.hpp:115
Attribute getAttribute(const std::string &attr_name) const
open an existing attribute with the name attr_name
Definition H5Annotate_traits_misc.hpp:69
size_t getNumberAttributes() const
return the number of attributes of the object
Definition H5Annotate_traits_misc.hpp:82
Attribute createAttribute(const std::string &attr_name, const DataSpace &space, const DataType &type)
create a new attribute with the name attr_name
Definition H5Annotate_traits_misc.hpp:25
Class representing an attribute of a dataset or group.
Definition H5Attribute.hpp:22
void write(const T &buffer)
Definition H5Attribute_misc.hpp:113
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:137
HDF5 Data Type.
Definition H5DataType.hpp:48
Definition H5Iterables_misc.hpp:24