9#ifndef H5NODE_TRAITS_MISC_HPP
10#define H5NODE_TRAITS_MISC_HPP
22#include "../H5DataSet.hpp"
23#include "../H5File.hpp"
24#include "../H5Group.hpp"
25#include "../H5Selection.hpp"
26#include "../H5Utility.hpp"
27#include "../H5PropertyList.hpp"
28#include "H5DataSet_misc.hpp"
29#include "H5Iterables_misc.hpp"
30#include "H5Selection_misc.hpp"
31#include "H5Slice_traits_misc.hpp"
32#include "H5PropertyList_misc.hpp"
37template <
typename Derivate>
45 DataSet ds{H5Dcreate2(
static_cast<Derivate*
>(
this)->getId(
false),
46 dataset_name.c_str(), dtype._hid, space._hid,
47 linkCreateProps.getId(
false), dsetCreateProps.getId(
false), dsetAccessProps.getId(
false))};
49 HDF5ErrMapper::ToException<DataSetException>(
50 std::string(
"Unable to create the dataset \"") + dataset_name +
56template <
typename Derivate>
57template<
typename Type>
65 return createDataSet(dataset_name, space,
66 create_and_check_datatype<Type>(),
67 linkCreateProps, dsetCreateProps, dsetAccessProps);
70template <
typename Derivate>
80 create_and_check_datatype<
typename details::inspector<T>::base_type>(),
81 linkCreateProps, dsetCreateProps, dsetAccessProps);
86template <
typename Derivate>
87template <std::
size_t N>
94 DataSet ds = createDataSet<char[N]>(
95 dataset_name,
DataSpace(data.size()), linkCreateProps, dsetCreateProps, dsetAccessProps);
100template <
typename Derivate>
104 DataSet ds{H5Dopen2(
static_cast<const Derivate*
>(
this)->getId(
false),
105 dataset_name.c_str(), accessProps.getId(
false))};
107 HDF5ErrMapper::ToException<DataSetException>(
108 std::string(
"Unable to open the dataset \"") + dataset_name +
"\":");
113template <
typename Derivate>
115 const std::string& dtype_name,
117 DataType dtype{H5Topen2(
static_cast<const Derivate*
>(
this)->getId(
false),
118 dtype_name.c_str(), dtypeAccessProps.getId(
false))};
119 if (dtype._hid < 0) {
120 HDF5ErrMapper::ToException<DataSetException>(
121 std::string(
"Unable to open the datatype \"") + dtype_name +
"\":");
126template <
typename Derivate>
131 Group group{H5Gcreate2(
static_cast<Derivate*
>(
this)->getId(
false),
132 group_name.c_str(), linkCreateProps.getId(
false), groupCreateProps.getId(
false), groupAccessProps.getId(
false))};
133 if (group._hid < 0) {
134 HDF5ErrMapper::ToException<GroupException>(
135 std::string(
"Unable to create the group \"") + group_name +
"\":");
140template <
typename Derivate>
144 Group group{H5Gopen2(
static_cast<const Derivate*
>(
this)->getId(
false),
145 group_name.c_str(), groupAccessProps.getId(
false))};
146 if (group._hid < 0) {
147 HDF5ErrMapper::ToException<GroupException>(
148 std::string(
"Unable to open the group \"") + group_name +
"\":");
153template <
typename Derivate>
156 hid_t fileId = H5Iget_file_id(
static_cast<const Derivate*
>(
this)->getId(
false));
157 if (!H5Iis_valid(fileId)){
158 HDF5ErrMapper::ToException<GroupException>(
159 std::string(
"File ID is invalid. Probably the object doesn't belong to any file"));
161 return File::FromId(fileId,
false);
164template <
typename Derivate>
167 if (H5Gget_num_objs(
static_cast<const Derivate*
>(
this)->getId(
false), &res) < 0) {
168 HDF5ErrMapper::ToException<GroupException>(
169 std::string(
"Unable to count objects in existing group or file"));
171 return static_cast<size_t>(res);
174template <
typename Derivate>
176 return details::get_name([&](
char* buffer, hsize_t length) {
177 return H5Lget_name_by_idx(
178 static_cast<const Derivate*
>(
this)->getId(
false),
".", H5_INDEX_NAME, H5_ITER_INC,
179 index, buffer, length, linkAccessProps.getId(
false));
183template <
typename Derivate>
185 return static_cast<const Derivate*
>(
this)->_getLinkInfo(objName);
188template <
typename Derivate>
190 return static_cast<const Derivate*
>(
this)->_unpackSoftLink(objName);
193template <
typename Derivate>
195 const std::string& objName, std::string& fileName_out){
196 return static_cast<const Derivate*
>(
this)->_unpackExternalLink(objName, fileName_out);
199template <
typename Derivate>
201 const std::string& dst_path,
204 herr_t status = H5Lmove(
static_cast<const Derivate*
>(
this)->getId(
false), src_path.c_str(),
205 static_cast<const Derivate*
>(
this)->getId(
false), dst_path.c_str(),
206 linkCreateProps.getId(
false), linkAccessProps.getId(
false));
208 HDF5ErrMapper::ToException<GroupException>(
209 std::string(
"Unable to move link to \"") + dst_path +
"\":");
215template <
typename Derivate>
218 std::vector<std::string> names;
221 size_t num_objs = getNumberObjects();
222 names.reserve(num_objs);
224 if (H5Literate(
static_cast<const Derivate*
>(
this)->getId(
false), H5_INDEX_NAME,
226 &details::internal_h5gt_iterate<H5L_info_t>,
227 static_cast<void*
>(&iterateData)) < 0) {
228 HDF5ErrMapper::ToException<GroupException>(
229 std::string(
"Unable to list objects in group"));
235template <
typename Derivate>
238 bool raise_errors)
const {
239 if (node_name.empty())
243 const auto val = H5Lexists(
static_cast<const Derivate*
>(
this)->getId(
false),
244 node_name.c_str(), accessProps.getId(
false));
247 HDF5ErrMapper::ToException<GroupException>(
"Invalid link for exist()");
256 return (node_name ==
"/") ? true : (val > 0);
259template <
typename Derivate>
262 bool raise_errors)
const {
263 if (node_name.empty())
267 const auto val = H5Oexists_by_name(
static_cast<const Derivate*
>(
this)->getId(
false),
268 node_name.c_str(), accessProps.getId(
false));
271 HDF5ErrMapper::ToException<GroupException>(
"Invalid link for resolved()");
280 return (node_name ==
"/") ? true : (val > 0);
283template <
typename Derivate>
286 bool raise_errors)
const {
289 if (objName.find(
'/') != std::string::npos) {
290 _exist(
"/", linkAccessProps);
292 return (objName ==
"/") ? true : _exist(objName, linkAccessProps,
false);
294 return _exist(objName, linkAccessProps, raise_errors);
297template <
typename Derivate>
300 bool raise_errors)
const {
303 if (objName.find(
'/') != std::string::npos) {
304 _resolved(
"/", linkAccessProps);
306 return (objName ==
"/") ? true : _resolved(objName, linkAccessProps,
false);
308 return _resolved(objName, linkAccessProps, raise_errors);
311template <
typename Derivate>
314 bool raise_errors)
const {
315 if (!exist(objName, linkAccessProps, raise_errors))
318 if (!resolved(objName, linkAccessProps, raise_errors))
324template <
typename Derivate>
326 const std::string& objName,
const ObjectType& objType,
329 if (!existAndResolved(objName, linkAccessProps, raise_errors))
332 if (getObjectType(objName, linkAccessProps) != objType)
338template <
typename Derivate>
340 const herr_t val = H5Ldelete(
static_cast<const Derivate*
>(
this)->getId(
false),
341 obj_name.c_str(), linkAccessProps.getId(
false));
343 HDF5ErrMapper::ToException<GroupException>(
344 std::string(
"Invalid name for unlink() "));
348template <
typename Derivate>
352 if (H5Lget_info(
static_cast<const Derivate*
>(
this)->getId(
false),
353 obj_name.c_str(), &linkinfo, linkAccessProps.getId(
false)) < 0
354 || linkinfo.type == H5L_TYPE_ERROR) {
355 HDF5ErrMapper::ToException<GroupException>(
356 std::string(
"Unable to obtain info for link ") + obj_name);
358 return _convert_link_type(linkinfo.type);
361template <
typename Derivate>
364 return _open(obj_name, accessProps).getObjectType();
367template <
typename Derivate>
369 const Group& obj,
const std::string& objNewName,
374 _copy(obj, objNewName, copyProps, linkCreateProps);
375 return static_cast<const Derivate*
>(
this)->getGroup(
376 objNewName, groupAccessProps);
379template <
typename Derivate>
381 const DataSet& obj,
const std::string& objNewName,
386 _copy(obj, objNewName, copyProps, linkCreateProps);
387 return static_cast<const Derivate*
>(
this)->getDataSet(
388 objNewName, dsetAccessProps);
391template <
typename Derivate>
393 const DataType& obj,
const std::string& objNewName,
398 _copy(obj, objNewName, copyProps, linkCreateProps);
399 return static_cast<const Derivate*
>(
this)->getDataType(
400 objNewName, dtypeAccessProps);
403template <
typename Derivate>
404template<
typename Node,
405 typename std::enable_if<
406 std::is_same<Node, File>::value |
407 std::is_same<Node, Group>::value>::type*>
410 const std::string& linkName,
411 const LinkType& linkType,
412 const std::string& targetPath,
417 _createLink(target, linkName, linkType, targetPath, linkCreateProps, linkAccessProps);
418 return static_cast<const Derivate*
>(
this)->getGroup(
419 linkName, groupAccessProps);
422template <
typename Derivate>
423inline DataSet NodeTraits<Derivate>::createLink(
424 const DataSet& target,
425 const std::string& linkName,
426 const LinkType& linkType,
427 const std::string& targetPath,
428 const LinkCreateProps& linkCreateProps,
429 const LinkAccessProps& linkAccessProps,
430 const DataSetAccessProps& dsetAccessProps)
432 _createLink(target, linkName, linkType, targetPath, linkCreateProps, linkAccessProps);
433 return static_cast<const Derivate*
>(
this)->getDataSet(linkName, dsetAccessProps);
436template <
typename Derivate>
437inline DataType NodeTraits<Derivate>::createLink(
438 const DataType& target,
439 const std::string& linkName,
440 const LinkType& linkType,
441 const std::string& targetPath,
442 const LinkCreateProps& linkCreateProps,
443 const LinkAccessProps& linkAccessProps,
444 const DataTypeAccessProps& dtypeAccessProps)
446 _createLink(target, linkName, linkType, targetPath, linkCreateProps, linkAccessProps);
447 return static_cast<const Derivate*
>(
this)->getDataType(linkName, dtypeAccessProps);
451template <
typename Derivate>
452inline Object NodeTraits<Derivate>::_open(
const std::string& node_name,
453 const LinkAccessProps& accessProps)
const {
454 hid_t
id = H5Oopen(
static_cast<const Derivate*
>(
this)->getId(
false),
456 accessProps.getId(
false));
458 HDF5ErrMapper::ToException<GroupException>(
459 std::string(
"Unable to open \"") + node_name +
"\":");
464template <
typename Derivate>
466inline void NodeTraits<Derivate>::_copy(
467 const T& obj,
const std::string& newName,
468 const ObjectCopyProps& copyProps,
469 const LinkCreateProps& linkCreateProps)
471 hid_t fileId = H5Iget_file_id(obj.getId(
false));
472 bool closeFileId =
true;
473 if (!H5Iis_valid(fileId)){
475 HDF5ErrMapper::ToException<GroupException>(
476 std::string(
"File ID is invalid. Probably the object doesn't belong to any file"));
479 herr_t status = H5Ocopy(
480 fileId, obj.getPath().c_str(),
481 static_cast<const Derivate*
>(
this)->getId(
false), newName.c_str(),
482 copyProps.getId(
false), linkCreateProps.getId(
false));
485 HDF5ErrMapper::ToException<GroupException>(
486 std::string(
"Unable to copy object to \"") + newName +
"\":");
495template <
typename Derivate>
497inline void NodeTraits<Derivate>::_createLink(
499 const std::string& linkName,
500 const LinkType& linkType,
501 std::string targetPath,
502 const LinkCreateProps& linkCreateProps,
503 const LinkAccessProps& linkAccessProps)
506 if (targetPath.empty())
507 targetPath = target.getPath();
509 if (linkType == LinkType::Soft){
510 status = H5Lcreate_soft(
512 static_cast<const Derivate*
>(
this)->getId(
false),
513 linkName.c_str(), linkCreateProps.getId(
false), linkAccessProps.getId(
false));
514 }
else if (linkType == LinkType::Hard){
515 status = H5Lcreate_hard(
518 static_cast<const Derivate*
>(
this)->getId(
false),
519 linkName.c_str(), linkCreateProps.getId(
false), linkAccessProps.getId(
false));
520 }
else if (linkType == LinkType::External){
521 status = H5Lcreate_external(
522 target.getFileName().c_str(),
524 static_cast<const Derivate*
>(
this)->getId(
false),
525 linkName.c_str(), linkCreateProps.getId(
false), linkAccessProps.getId(
false));
529 HDF5ErrMapper::ToException<GroupException>(
530 std::string(
"Unable to create link"));
Definition H5PropertyList.hpp:255
Definition H5PropertyList.hpp:171
Class representing a dataset.
Definition H5DataSet.hpp:28
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
Definition H5PropertyList.hpp:295
HDF5 Data Type.
Definition H5DataType.hpp:48
File class.
Definition H5File.hpp:25
A structure representing a set of fixed-length strings.
Definition H5DataType.hpp:305
Definition H5PropertyList.hpp:162
Definition H5PropertyList.hpp:157
Represents an hdf5 group.
Definition H5Group.hpp:23
Definition H5PropertyList.hpp:138
Definition H5PropertyList.hpp:129
Definition H5Object.hpp:200
NodeTraits: Base class for Group and File.
Definition H5Node_traits.hpp:23
std::string unpackExternalLink(const std::string &objName, std::string &fileName_out)
unpackExternalLink retrieve target path object path and file path
Definition H5Node_traits_misc.hpp:194
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const LinkCreateProps &linkCreateProps=LinkCreateProps(), const DataSetCreateProps &dsetCreateProps=DataSetCreateProps(), const DataSetAccessProps &dsetAccessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space
Definition H5Node_traits_misc.hpp:39
void unlink(const std::string &obj_name, const LinkAccessProps &linkAccessProps=LinkAccessProps()) const
unlink the given dataset or group
Definition H5Node_traits_misc.hpp:339
std::string getObjectName(size_t index, const LinkAccessProps &linkAccessProps=LinkAccessProps()) const
return the name of the object with the given index
Definition H5Node_traits_misc.hpp:175
size_t getNumberObjects() const
return the number of leaf objects of the node / group
Definition H5Node_traits_misc.hpp:165
ObjectType getObjectType(const std::string &obj_name, const LinkAccessProps &accessProps=LinkAccessProps()) const
A shorthand to get the kind of object pointed to (group, dataset, type...)
Definition H5Node_traits_misc.hpp:362
std::string unpackSoftLink(const std::string &objName)
unpackSoftLink retrieve target path object path
Definition H5Node_traits_misc.hpp:189
bool existAndResolved(const std::string &obj_name, const LinkAccessProps &linkAccessProps=LinkAccessProps(), bool raise_errors=false) const
existAndResolved invokes exist() and then reoslved()
Definition H5Node_traits_misc.hpp:312
bool resolved(const std::string &obj_name, const LinkAccessProps &linkAccessProps=LinkAccessProps(), bool raise_errors=false) const
resolved Hard links always resolved but Soft/External links may point to unexistant objects
Definition H5Node_traits_misc.hpp:298
Group copy(const Group &obj, const std::string &objNewName, const ObjectCopyProps ©Props=ObjectCopyProps(), const LinkCreateProps &linkCreateProps=LinkCreateProps(), const GroupAccessProps &groupAccessProps=GroupAccessProps())
Copies specified object to the new destination (this File/Group) under new name. The destination must...
Definition H5Node_traits_misc.hpp:368
bool rename(const std::string &src_path, const std::string &dest_path, const LinkCreateProps &linkCreateProps=LinkCreateProps(), const LinkAccessProps &linkAccessProps=LinkAccessProps()) const
moves an object and its content within an HDF5 file.
Definition H5Node_traits_misc.hpp:200
Group getGroup(const std::string &group_name, const GroupAccessProps &groupAccessProps=GroupAccessProps()) const
open an existing group with the name group_name
Definition H5Node_traits_misc.hpp:142
LinkType getLinkType(const std::string &obj_name, const LinkAccessProps &linkAccessProps=LinkAccessProps()) const
Returns the kind of link of the given name (soft, hard...)
Definition H5Node_traits_misc.hpp:349
std::vector< std::string > listObjectNames() const
list all leaf objects name of the node / group
Definition H5Node_traits_misc.hpp:216
DataSet getDataSet(const std::string &dataset_name, const DataSetAccessProps &accessProps=DataSetAccessProps()) const
get an existing dataset in the current file
Definition H5Node_traits_misc.hpp:102
bool exist(const std::string &obj_name, const LinkAccessProps &linkAccessProps=LinkAccessProps(), bool raise_errors=false) const
check a dataset or group exists in the current node / group
Definition H5Node_traits_misc.hpp:284
Group createGroup(const std::string &group_name, const LinkCreateProps &linkCreateProps=LinkCreateProps(), const GroupCreateProps &groupCreateProps=GroupCreateProps(), const GroupAccessProps &groupAccessProps=GroupAccessProps())
create a new group, and eventually intermediate groups
Definition H5Node_traits_misc.hpp:127
Definition H5PropertyList.hpp:305
Utility class to disable HDF5 stack printing inside a scope.
Definition H5Utility.hpp:20
void write(const T &buffer)
Definition H5Slice_traits_misc.hpp:275
Definition H5Iterables_misc.hpp:24