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 "H5Annotate_traits.hpp"
13 #include "H5Iterables_misc.hpp"
14 
15 #include <string>
16 #include <vector>
17 
18 #include "../H5Attribute.hpp"
19 #include "../H5DataSpace.hpp"
20 #include "../H5DataType.hpp"
21 #include "../H5Exception.hpp"
22 
23 #include <H5Apublic.h>
24 
25 namespace HighFive {
26 
27 template <typename Derivate>
28 inline Attribute
29 AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
30  const DataSpace& space,
31  const DataType& dtype) {
32  Attribute attribute;
33  if ((attribute._hid = H5Acreate2(
34  static_cast<Derivate*>(this)->getId(), attribute_name.c_str(),
35  dtype._hid, space._hid, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
36  HDF5ErrMapper::ToException<AttributeException>(
37  std::string("Unable to create the attribute \"") + attribute_name +
38  "\":");
39  }
40  return attribute;
41 }
42 
43 template <typename Derivate>
44 template <typename Type>
45 inline Attribute
46 AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
47  const DataSpace& space) {
48  return createAttribute(attribute_name, space, AtomicType<Type>());
49 }
50 
51 template <typename Derivate>
52 template <typename T>
53 inline Attribute
54 AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
55  const T& data) {
57  attribute_name, DataSpace::From(data),
59  att.write(data);
60  return att;
61 }
62 
63 template <typename Derivate>
65  const std::string& attribute_name) const {
66  Attribute attribute;
67  if ((attribute._hid = H5Aopen(static_cast<const Derivate*>(this)->getId(),
68  attribute_name.c_str(), H5P_DEFAULT)) < 0) {
69  HDF5ErrMapper::ToException<AttributeException>(
70  std::string("Unable to open the attribute \"") + attribute_name +
71  "\":");
72  }
73  return attribute;
74 }
75 
76 template <typename Derivate>
78  int res = H5Aget_num_attrs(static_cast<const Derivate*>(this)->getId());
79  if (res < 0) {
80  HDF5ErrMapper::ToException<AttributeException>(std::string(
81  "Unable to count attributes in existing group or file"));
82  }
83  return res;
84 }
85 
86 template <typename Derivate>
87 inline std::vector<std::string>
89 
90  std::vector<std::string> names;
91  details::HighFiveIterateData iterateData(names);
92 
93  size_t num_objs = getNumberAttributes();
94  names.reserve(num_objs);
95 
96  if (H5Aiterate2(static_cast<const Derivate*>(this)->getId(), H5_INDEX_NAME,
97  H5_ITER_INC, NULL,
98  &details::internal_high_five_iterate<H5A_info_t>,
99  static_cast<void*>(&iterateData)) < 0) {
100  HDF5ErrMapper::ToException<AttributeException>(
101  std::string("Unable to list attributes in group"));
102  }
103 
104  return names;
105 }
106 
107 template <typename Derivate>
108 inline bool
109 AnnotateTraits<Derivate>::hasAttribute(const std::string& attr_name) const {
110  int res = H5Aexists(static_cast<const Derivate*>(this)->getId(),
111  attr_name.c_str());
112  if (res < 0) {
113  HDF5ErrMapper::ToException<AttributeException>(
114  std::string("Unable to check for attribute in group"));
115  }
116  return res;
117 }
118 }
119 
120 #endif // H5ANNOTATE_TRAITS_MISC_HPP
static DataSpace From(const ScalarValue &scalar_value)
size_t getNumberAttributes() const
return the number of attributes of the node / group
Attribute getAttribute(const std::string &attribute_name) const
open an existing attribute with the name attribute_name
Attribute createAttribute(const std::string &attribute_name, const T &data)
createAttribute create a new attribute on the current dataset and write to it, inferring the DataSpac...
bool hasAttribute(const std::string &attr_name) const
checks an attribute exists
std::vector< std::string > listAttributeNames() const
list all attribute name of the node / group
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
Attribute createAttribute(const std::string &attribute_name, const DataSpace &space, const DataType &type)
create a new attribute with the name attribute_name
HDF5 Data Type.
Definition: H5DataType.hpp:21
void write(const T &buffer)
#define NULL
Definition: mydefs.hpp:141


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06