H5Node_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 H5NODE_TRAITS_MISC_HPP
10 #define H5NODE_TRAITS_MISC_HPP
11 
12 #include "H5Iterables_misc.hpp"
13 #include "H5Node_traits.hpp"
14 
15 #include <string>
16 #include <vector>
17 
18 #include "../H5Attribute.hpp"
19 #include "../H5DataSet.hpp"
20 #include "../H5DataSpace.hpp"
21 #include "../H5DataType.hpp"
22 #include "../H5Exception.hpp"
23 #include "../H5Group.hpp"
24 
25 #include <H5Apublic.h>
26 #include <H5Dpublic.h>
27 #include <H5Fpublic.h>
28 #include <H5Gpublic.h>
29 #include <H5Ppublic.h>
30 #include <H5Tpublic.h>
31 
32 namespace HighFive {
33 
34 template <typename Derivate>
35 inline DataSet
36 NodeTraits<Derivate>::createDataSet(const std::string& dataset_name,
37  const DataSpace& space,
38  const DataType& dtype,
39  const DataSetCreateProps& createProps,
40  const DataSetAccessProps& accessProps)
41 {
42  DataSet set;
43  if ((set._hid = H5Dcreate2(static_cast<Derivate*>(this)->getId(),
44  dataset_name.c_str(), dtype._hid, space._hid,
45  H5P_DEFAULT, createProps.getId(),
46  accessProps.getId())) < 0) {
47  HDF5ErrMapper::ToException<DataSetException>(
48  std::string("Unable to create the dataset \"") + dataset_name +
49  "\":");
50  }
51  return set;
52 }
53 
54 template <typename Derivate>
55 template <typename Type>
56 inline DataSet
57 NodeTraits<Derivate>::createDataSet(const std::string& dataset_name,
58  const DataSpace& space,
59  const DataSetCreateProps& createProps,
60  const DataSetAccessProps& accessProps)
61 {
62  return createDataSet(dataset_name, space, AtomicType<Type>(),
63  createProps, accessProps);
64 }
65 
66 template <typename Derivate>
67 template <typename T>
68 inline DataSet
69 NodeTraits<Derivate>::createDataSet(const std::string& dataset_name,
70  const T& data,
71  const DataSetCreateProps& createProps,
72  const DataSetAccessProps& accessProps)
73 {
74  DataSet ds = createDataSet(
75  dataset_name, DataSpace::From(data),
77  createProps, accessProps);
78  ds.write(data);
79  return ds;
80 }
81 
82 template <typename Derivate>
83 inline DataSet
84 NodeTraits<Derivate>::getDataSet(const std::string& dataset_name,
85  const DataSetAccessProps& accessProps) const {
86  DataSet set;
87  if ((set._hid = H5Dopen2(static_cast<const Derivate*>(this)->getId(),
88  dataset_name.c_str(), accessProps.getId())) < 0) {
89  HDF5ErrMapper::ToException<DataSetException>(
90  std::string("Unable to open the dataset \"") + dataset_name +
91  "\":");
92  }
93  return set;
94 }
95 
96 template <typename Derivate>
97 inline Group NodeTraits<Derivate>::createGroup(const std::string& group_name) {
98  Group group;
99  if ((group._hid = H5Gcreate2(static_cast<Derivate*>(this)->getId(),
100  group_name.c_str(), H5P_DEFAULT, H5P_DEFAULT,
101  H5P_DEFAULT)) < 0) {
102  HDF5ErrMapper::ToException<GroupException>(
103  std::string("Unable to create the group \"") + group_name + "\":");
104  }
105  return group;
106 }
107 
108 template <typename Derivate>
109 inline Group
110 NodeTraits<Derivate>::getGroup(const std::string& group_name) const {
111  Group group;
112  if ((group._hid = H5Gopen2(static_cast<const Derivate*>(this)->getId(),
113  group_name.c_str(), H5P_DEFAULT)) < 0) {
114  HDF5ErrMapper::ToException<GroupException>(
115  std::string("Unable to open the group \"") + group_name + "\":");
116  }
117  return group;
118 }
119 
120 template <typename Derivate>
122  hsize_t res;
123  if (H5Gget_num_objs(static_cast<const Derivate*>(this)->getId(), &res) <
124  0) {
125  HDF5ErrMapper::ToException<GroupException>(
126  std::string("Unable to count objects in existing group or file"));
127  }
128  return res;
129 }
130 
131 template <typename Derivate>
132 inline std::string NodeTraits<Derivate>::getObjectName(size_t index) const {
133  const ssize_t maxLength = 1023;
134  char buffer[maxLength + 1];
135  ssize_t length =
136  H5Lget_name_by_idx(static_cast<const Derivate*>(this)->getId(), ".",
137  H5_INDEX_NAME, H5_ITER_INC, index,
138  buffer, maxLength, H5P_DEFAULT);
139  if (length < 0)
140  HDF5ErrMapper::ToException<GroupException>(
141  "Error accessing object name");
142  if (length <= maxLength)
143  return std::string(buffer, length);
144  std::vector<char> bigBuffer(length + 1, 0);
145  H5Lget_name_by_idx(static_cast<const Derivate*>(this)->getId(), ".",
146  H5_INDEX_NAME, H5_ITER_INC, index,
147  bigBuffer.data(), length, H5P_DEFAULT);
148  return std::string(bigBuffer.data(), length);
149 }
150 
151 template <typename Derivate>
152 inline std::vector<std::string> NodeTraits<Derivate>::listObjectNames() const {
153 
154  std::vector<std::string> names;
155  details::HighFiveIterateData iterateData(names);
156 
157  size_t num_objs = getNumberObjects();
158  names.reserve(num_objs);
159 
160  if (H5Literate(static_cast<const Derivate*>(this)->getId(), H5_INDEX_NAME,
161  H5_ITER_INC, NULL,
162  &details::internal_high_five_iterate<H5L_info_t>,
163  static_cast<void*>(&iterateData)) < 0) {
164  HDF5ErrMapper::ToException<GroupException>(
165  std::string("Unable to list objects in group"));
166  }
167 
168  return names;
169 }
170 
171 template <typename Derivate>
172 inline bool NodeTraits<Derivate>::exist(const std::string& node_name) const {
173  htri_t val = H5Lexists(static_cast<const Derivate*>(this)->getId(),
174  node_name.c_str(), H5P_DEFAULT);
175  if( val < 0){
176  HDF5ErrMapper::ToException<GroupException>(
177  std::string("Invalid link for exist() "));
178  }
179 
180  return (val > 0 );
181 }
182 
183 }
184 
185 #endif // H5NODE_TRAITS_MISC_HPP
static DataSpace From(const ScalarValue &scalar_value)
void write(const T &buffer)
size_t getNumberObjects() const
return the number of leaf objects of the node / group
std::string getObjectName(size_t index) const
return the name of the object with the given index
bool exist(const std::string &node_name) const
check a dataset or group exists in the current node / group
DataSet getDataSet(const std::string &dataset_name, const DataSetAccessProps &accessProps=DataSetAccessProps()) const
get an existing dataset in the current file
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
Group getGroup(const std::string &group_name) const
open an existing group with the name group_name
HDF5 Data Type.
Definition: H5DataType.hpp:21
std::vector< std::string > listObjectNames() const
list all leaf objects name of the node / group
Group createGroup(const std::string &group_name)
create a new group with the name group_name
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space ...
#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