9#ifndef H5NODE_TRAITS_MISC_HPP
10#define H5NODE_TRAITS_MISC_HPP
22#include "../H5DataSet.hpp"
23#include "../H5Group.hpp"
24#include "../H5Selection.hpp"
25#include "../H5Utility.hpp"
34template <
typename Derivate>
44 const auto hid = H5Dcreate2(
static_cast<Derivate*
>(
this)->getId(),
45 dataset_name.c_str(), dtype.
_hid, space.
_hid,
48 HDF5ErrMapper::ToException<DataSetException>(
49 std::string(
"Unable to create the dataset \"") + dataset_name +
"\":");
54template <
typename Derivate>
55template <
typename Type>
62 return createDataSet(dataset_name, space,
63 create_and_check_datatype<Type>(),
64 createProps, accessProps, parents);
67template <
typename Derivate>
78 createProps, accessProps, parents);
83template <
typename Derivate>
84template <std::
size_t N>
91 DataSet ds = createDataSet<char[N]>(
92 dataset_name,
DataSpace(data.
size()), createProps, accessProps, parents);
97template <
typename Derivate>
101 const auto hid = H5Dopen2(
static_cast<const Derivate*
>(
this)->getId(),
102 dataset_name.c_str(), accessProps.
getId());
104 HDF5ErrMapper::ToException<DataSetException>(
105 std::string(
"Unable to open the dataset \"") + dataset_name +
"\":");
110template <
typename Derivate>
116 const auto hid = H5Gcreate2(
static_cast<Derivate*
>(
this)->getId(),
117 group_name.c_str(), lcpl.
getId(), H5P_DEFAULT, H5P_DEFAULT);
119 HDF5ErrMapper::ToException<GroupException>(
120 std::string(
"Unable to create the group \"") + group_name +
"\":");
125template <
typename Derivate>
128 const auto hid = H5Gopen2(
static_cast<const Derivate*
>(
this)->getId(),
129 group_name.c_str(), H5P_DEFAULT);
131 HDF5ErrMapper::ToException<GroupException>(
132 std::string(
"Unable to open the group \"") + group_name +
"\":");
137template <
typename Derivate>
140 if (H5Gget_num_objs(
static_cast<const Derivate*
>(
this)->getId(), &res) < 0) {
141 HDF5ErrMapper::ToException<GroupException>(
142 std::string(
"Unable to count objects in existing group or file"));
144 return static_cast<size_t>(res);
147template <
typename Derivate>
149 return details::get_name([&](
char* buffer, hsize_t length) {
150 return H5Lget_name_by_idx(
151 static_cast<const Derivate*
>(
this)->getId(),
".", H5_INDEX_NAME, H5_ITER_INC,
152 index, buffer,
static_cast<size_t>(length), H5P_DEFAULT);
156template <
typename Derivate>
158 const std::string& dst_path,
159 bool parents)
const {
162 herr_t status = H5Lmove(
static_cast<const Derivate*
>(
this)->getId(), src_path.c_str(),
163 static_cast<const Derivate*
>(
this)->getId(), dst_path.c_str(), lcpl.
getId(), H5P_DEFAULT);
165 HDF5ErrMapper::ToException<GroupException>(
166 std::string(
"Unable to move link to \"") + dst_path +
"\":");
172template <
typename Derivate>
175 std::vector<std::string> names;
176 details::HighFiveIterateData iterateData(names);
178 size_t num_objs = getNumberObjects();
179 names.reserve(num_objs);
181 if (H5Literate(
static_cast<const Derivate*
>(
this)->getId(), H5_INDEX_NAME,
183 &details::internal_high_five_iterate<H5L_info_t>,
184 static_cast<void*
>(&iterateData)) < 0) {
185 HDF5ErrMapper::ToException<GroupException>(
186 std::string(
"Unable to list objects in group"));
192template <
typename Derivate>
193inline bool NodeTraits<Derivate>::_exist(
const std::string& node_name,
194 bool raise_errors)
const {
195 SilenceHDF5 silencer{};
196 const auto val = H5Lexists(
static_cast<const Derivate*
>(
this)->getId(),
197 node_name.c_str(), H5P_DEFAULT);
200 HDF5ErrMapper::ToException<GroupException>(
"Invalid link for exist()");
209 return (node_name ==
"/") ? true : (val > 0);
212template <
typename Derivate>
216 if (group_path.find(
'/') != std::string::npos) {
219 return (group_path ==
"/") ? true : _exist(group_path,
false);
221 return _exist(group_path);
225template <
typename Derivate>
227 const herr_t val = H5Ldelete(
static_cast<const Derivate*
>(
this)->getId(),
228 node_name.c_str(), H5P_DEFAULT);
230 HDF5ErrMapper::ToException<GroupException>(
231 std::string(
"Invalid name for unlink() "));
240static inline LinkType _convert_link_type(
const H5L_type_t& ltype)
noexcept {
246 case H5L_TYPE_EXTERNAL:
255template <
typename Derivate>
258 if (H5Lget_info(
static_cast<const Derivate*
>(
this)->getId(),
259 node_name.c_str(), &linkinfo, H5P_DEFAULT) < 0
260 || linkinfo.type == H5L_TYPE_ERROR) {
261 HDF5ErrMapper::ToException<GroupException>(
262 std::string(
"Unable to obtain info for link ") + node_name);
264 return _convert_link_type(linkinfo.type);
267template <
typename Derivate>
269 return _open(node_name).getType();
273template <
typename Derivate>
275 const std::string& obj_path,
278 const bool parents) {
282 auto status = H5Lcreate_soft(obj_path.c_str(),
283 static_cast<const Derivate*
>(
this)->getId(),
285 linkCreateProps.
getId(), linkAccessProps.
getId());
287 HDF5ErrMapper::ToException<GroupException>(
288 std::string(
"Unable to create soft link: "));
293template <
typename Derivate>
295 const std::string& h5_file,
296 const std::string& obj_path,
299 const bool parents) {
303 auto status = H5Lcreate_external(h5_file.c_str(),
305 static_cast<const Derivate*
>(
this)->getId(),
307 linkCreateProps.
getId(), linkAccessProps.
getId());
309 HDF5ErrMapper::ToException<GroupException>(
310 std::string(
"Unable to create external link: "));
315template <
typename Derivate>
318 const auto id = H5Oopen(
static_cast<const Derivate*
>(
this)->getId(),
320 accessProps.
getId());
322 HDF5ErrMapper::ToException<GroupException>(
323 std::string(
"Unable to open \"") + node_name +
"\":");
Class representing a dataset.
Definition H5DataSet.hpp:31
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
A structure representing a set of fixed-length strings.
Definition H5DataType.hpp:239
std::size_t size() const noexcept
Definition H5DataType.hpp:281
Represents an hdf5 group.
Definition H5Group.hpp:25
NodeTraits: Base class for Group and File.
Definition H5Node_traits.hpp:23
Group getGroup(const std::string &group_name) const
open an existing group with the name group_name
Definition H5Node_traits_misc.hpp:127
void unlink(const std::string &node_name) const
unlink the given dataset or group
Definition H5Node_traits_misc.hpp:226
void createExternalLink(const std::string &link_name, const std::string &h5_file, const std::string &obj_path, LinkCreateProps linkCreateProps=LinkCreateProps(), const LinkAccessProps &linkAccessProps=LinkAccessProps(), const bool parents=true)
Definition H5Node_traits_misc.hpp:294
DataSet getDataSet(const std::string &dataset_name, const DataSetAccessProps &accessProps=DataSetAccessProps::Default()) const
get an existing dataset in the current file
Definition H5Node_traits_misc.hpp:99
void createSoftLink(const std::string &linkName, const T &obj)
A shorthand to create softlink to any object which provides getPath The link will be created with def...
Definition H5Node_traits.hpp:159
Group createGroup(const std::string &group_name, bool parents=true)
create a new group, and eventually intermediate groups
Definition H5Node_traits_misc.hpp:111
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps::Default(), const DataSetAccessProps &accessProps=DataSetAccessProps::Default(), bool parents=true)
createDataSet Create a new dataset in the current file of datatype type and of size space
Definition H5Node_traits_misc.hpp:36
bool rename(const std::string &src_path, const std::string &dest_path, bool parents=true) const
moves an object and its content within an HDF5 file.
Definition H5Node_traits_misc.hpp:157
bool exist(const std::string &node_name) const
check a dataset or group exists in the current node / group
Definition H5Node_traits_misc.hpp:213
std::vector< std::string > listObjectNames() const
list all leaf objects name of the node / group
Definition H5Node_traits_misc.hpp:173
ObjectType getObjectType(const std::string &node_name) const
A shorthand to get the kind of object pointed to (group, dataset, type...)
Definition H5Node_traits_misc.hpp:268
size_t getNumberObjects() const
return the number of leaf objects of the node / group
Definition H5Node_traits_misc.hpp:138
std::string getObjectName(size_t index) const
return the name of the object with the given index
Definition H5Node_traits_misc.hpp:148
LinkType getLinkType(const std::string &node_name) const
Returns the kind of link of the given name (soft, hard...)
Definition H5Node_traits_misc.hpp:256
Definition H5Object.hpp:36
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:55
hid_t _hid
Definition H5Object.hpp:87
HDF5 property Lists.
Definition H5PropertyList.hpp:62
void add(const P &property)
Definition H5PropertyList_misc.hpp:77
void write(const T &buffer)
Definition H5Slice_traits_misc.hpp:214
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
LinkType
The possible types of group entries (link concept)
Definition H5Node_traits.hpp:202
ObjectType
Enum of the types of objects (H5O api)
Definition H5Object.hpp:25