hops
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 
21 namespace HighFive {
22 
23 template <typename Derivate>
24 inline Attribute
26  const DataSpace& space,
27  const DataType& dtype) {
28  Attribute attribute;
29  if ((attribute._hid = H5Acreate2(
30  static_cast<Derivate*>(this)->getId(), attribute_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 \"") + attribute_name + "\":");
34  }
35  return attribute;
36 }
37 
38 template <typename Derivate>
39 template <typename Type>
40 inline Attribute
42  const DataSpace& space) {
43  return createAttribute(attribute_name, space, create_and_check_datatype<Type>());
44 }
45 
46 template <typename Derivate>
47 template <typename T>
48 inline Attribute
50  const T& data) {
51  Attribute att = createAttribute(
52  attribute_name,
53  DataSpace::From(data),
55  att.write(data);
56  return att;
57 }
58 
59 template<typename Derivate>
60 inline void
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 
68 template <typename Derivate>
70  const std::string& attribute_name) const {
71  Attribute attribute;
72  if ((attribute._hid = H5Aopen(static_cast<const Derivate*>(this)->getId(),
73  attribute_name.c_str(), H5P_DEFAULT)) < 0) {
74  HDF5ErrMapper::ToException<AttributeException>(
75  std::string("Unable to open the attribute \"") + attribute_name +
76  "\":");
77  }
78  return attribute;
79 }
80 
81 template <typename Derivate>
83  int res = H5Aget_num_attrs(static_cast<const Derivate*>(this)->getId());
84  if (res < 0) {
85  HDF5ErrMapper::ToException<AttributeException>(std::string(
86  "Unable to count attributes in existing group or file"));
87  }
88  return static_cast<size_t>(res);
89 }
90 
91 template <typename Derivate>
92 inline std::vector<std::string>
94 
95  std::vector<std::string> names;
96  details::HighFiveIterateData iterateData(names);
97 
98  size_t num_objs = getNumberAttributes();
99  names.reserve(num_objs);
100 
101  if (H5Aiterate2(static_cast<const Derivate*>(this)->getId(), H5_INDEX_NAME,
102  H5_ITER_INC, NULL,
103  &details::internal_high_five_iterate<H5A_info_t>,
104  static_cast<void*>(&iterateData)) < 0) {
105  HDF5ErrMapper::ToException<AttributeException>(
106  std::string("Unable to list attributes in group"));
107  }
108 
109  return names;
110 }
111 
112 template <typename Derivate>
113 inline bool
115  int res = H5Aexists(static_cast<const Derivate*>(this)->getId(),
116  attr_name.c_str());
117  if (res < 0) {
118  HDF5ErrMapper::ToException<AttributeException>(
119  std::string("Unable to check for attribute in group"));
120  }
121  return res;
122 }
123 
124 } // namespace HighFive
125 
126 #endif // H5ANNOTATE_TRAITS_MISC_HPP
HighFive::DataType
HDF5 Data Type.
Definition: H5DataType.hpp:42
HighFive::AnnotateTraits::getNumberAttributes
size_t getNumberAttributes() const
return the number of attributes of the node / group
Definition: H5Annotate_traits_misc.hpp:82
HighFive::AnnotateTraits::createAttribute
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
HighFive::AnnotateTraits::hasAttribute
bool hasAttribute(const std::string &attr_name) const
checks an attribute exists
Definition: H5Annotate_traits_misc.hpp:114
HighFive::DataSpace::From
static DataSpace From(const ScalarValue &scalar_value)
Definition: H5Dataspace_misc.hpp:129
HighFive::AnnotateTraits::deleteAttribute
void deleteAttribute(const std::string &attribute_name)
deleteAttribute let you delete an attribute by its name.
Definition: H5Annotate_traits_misc.hpp:61
HighFive::ObjectType::Attribute
@ Attribute
HighFive::details::HighFiveIterateData
Definition: H5Iterables_misc.hpp:24
H5Iterables_misc.hpp
HighFive::details::type_of_array::type
unqualified_t< T > type
Definition: H5Utils.hpp:137
HighFive::Attribute
Class representing an attribute of a dataset or group.
Definition: H5Attribute.hpp:23
HighFive::AnnotateTraits::listAttributeNames
std::vector< std::string > listAttributeNames() const
list all attribute name of the node / group
Definition: H5Annotate_traits_misc.hpp:93
HighFive::AnnotateTraits::getAttribute
Attribute getAttribute(const std::string &attribute_name) const
open an existing attribute with the name attribute_name
Definition: H5Annotate_traits_misc.hpp:69
HighFive::Object::_hid
hid_t _hid
Definition: H5Object.hpp:81
HighFive::create_and_check_datatype
DataType create_and_check_datatype()
Create a DataType instance representing type T and perform a sanity check on its size.
Definition: H5DataType_misc.hpp:406
HighFive::DataSpace
Class representing the space (dimensions) of a dataset.
Definition: H5DataSpace.hpp:37
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
H5Attribute_misc.hpp
HighFive
Definition: H5_definitions.hpp:15
HighFive::Attribute::write
void write(const T &buffer)
Definition: H5Attribute_misc.hpp:106