Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-04 08:41:06

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include <string>
0012 
0013 #include "../H5PropertyList.hpp"
0014 #include "H5_definitions.hpp"
0015 #include "H5Converter_misc.hpp"
0016 
0017 namespace HighFive {
0018 
0019 enum class IndexType : std::underlying_type<H5_index_t>::type {
0020     NAME = H5_INDEX_NAME,
0021     CRT_ORDER = H5_INDEX_CRT_ORDER,
0022 };
0023 
0024 ///
0025 /// \brief NodeTraits: Base class for Group and File
0026 ///
0027 template <typename Derivate>
0028 class NodeTraits {
0029   public:
0030     ///
0031     /// \brief createDataSet Create a new dataset in the current file of
0032     /// datatype type and of size space
0033     /// \param dataset_name identifier of the dataset
0034     /// \param space Associated DataSpace, see \ref DataSpace for more information
0035     /// \param type Type of Data
0036     /// \param createProps A property list with data set creation properties
0037     /// \param accessProps A property list with data set access properties
0038     /// \param parents Create intermediate groups if needed. Default: true.
0039     /// \return DataSet Object
0040     DataSet createDataSet(const std::string& dataset_name,
0041                           const DataSpace& space,
0042                           const DataType& type,
0043                           const DataSetCreateProps& createProps = DataSetCreateProps::Default(),
0044                           const DataSetAccessProps& accessProps = DataSetAccessProps::Default(),
0045                           bool parents = true);
0046 
0047     ///
0048     /// \brief createDataSet create a new dataset in the current file with a
0049     /// size specified by space
0050     /// \param dataset_name identifier of the dataset
0051     /// \param space Associated DataSpace, see \ref DataSpace for more information
0052     /// \param createProps A property list with data set creation properties
0053     /// \param accessProps A property list with data set access properties
0054     /// \param parents Create intermediate groups if needed. Default: true.
0055     /// \return DataSet Object
0056     template <typename T>
0057     DataSet createDataSet(const std::string& dataset_name,
0058                           const DataSpace& space,
0059                           const DataSetCreateProps& createProps = DataSetCreateProps::Default(),
0060                           const DataSetAccessProps& accessProps = DataSetAccessProps::Default(),
0061                           bool parents = true);
0062 
0063     ///
0064     /// \brief createDataSet create a new dataset in the current file and
0065     /// write to it, inferring the DataSpace from the data.
0066     /// \param dataset_name identifier of the dataset
0067     /// \param data Associated data, must support DataSpace::From, see
0068     /// \ref DataSpace for more information
0069     /// \param createProps A property list with data set creation properties
0070     /// \param accessProps A property list with data set access properties
0071     /// \param parents Create intermediate groups if needed. Default: true.
0072     /// \return DataSet Object
0073     template <typename T>
0074     DataSet createDataSet(const std::string& dataset_name,
0075                           const T& data,
0076                           const DataSetCreateProps& createProps = DataSetCreateProps::Default(),
0077                           const DataSetAccessProps& accessProps = DataSetAccessProps::Default(),
0078                           bool parents = true);
0079 
0080 
0081     ///
0082     /// \brief get an existing dataset in the current file
0083     /// \param dataset_name
0084     /// \param accessProps property list to configure dataset chunk cache
0085     /// \return return the named dataset, or throw exception if not found
0086     DataSet getDataSet(const std::string& dataset_name,
0087                        const DataSetAccessProps& accessProps = DataSetAccessProps::Default()) const;
0088 
0089     ///
0090     /// \brief create a new group, and eventually intermediate groups
0091     /// \param group_name
0092     /// \param parents Create intermediate groups if needed. Default: true.
0093     /// \return the group object
0094     Group createGroup(const std::string& group_name, bool parents = true);
0095 
0096     ///
0097     /// \brief create a new group, and eventually intermediate groups
0098     /// \param group_name
0099     /// \param createProps A property list with group creation properties
0100     /// \param parents Create intermediate groups if needed. Default: true.
0101     /// \return the group object
0102     Group createGroup(const std::string& group_name,
0103                       const GroupCreateProps& createProps,
0104                       bool parents = true);
0105 
0106     ///
0107     /// \brief open an existing group with the name group_name
0108     /// \param group_name
0109     /// \return the group object
0110     Group getGroup(const std::string& group_name) const;
0111 
0112     ///
0113     /// \brief open a commited datatype with the name type_name
0114     /// \param type_name
0115     /// \return the datatype object
0116     DataType getDataType(
0117         const std::string& type_name,
0118         const DataTypeAccessProps& accessProps = DataTypeAccessProps::Default()) const;
0119 
0120     ///
0121     /// \brief return the number of leaf objects of the node / group
0122     /// \return number of leaf objects
0123     size_t getNumberObjects() const;
0124 
0125     ///
0126     /// \brief return the name of the object with the given index
0127     /// \return the name of the object
0128     std::string getObjectName(size_t index) const;
0129 
0130     ///
0131     /// \brief moves an object and its content within an HDF5 file.
0132     /// \param src_path relative path of the object to current File/Group
0133     /// \param dst_path new relative path of the object to current File/Group
0134     /// \param parents Create intermediate groups if needed. Default: true.
0135     /// \return boolean that is true if the move was successful
0136     bool rename(const std::string& src_path,
0137                 const std::string& dst_path,
0138                 bool parents = true) const;
0139 
0140     ///
0141     /// \brief list all leaf objects name of the node / group
0142     /// \param idx_type tell if the list should be ordered by Name or CreationOrderTime.
0143     /// CreationOrderTime can be use only if the file/group has been created with
0144     /// the HighFive::LinkCreationTime property.
0145     /// \return number of leaf objects
0146     std::vector<std::string> listObjectNames(IndexType idx_type = IndexType::NAME) const;
0147 
0148     ///
0149     /// \brief check a dataset or group exists in the current node / group
0150     /// \param node_path dataset/group name to check
0151     /// \return true if a dataset/group with the associated name exists, or false
0152     bool exist(const std::string& node_path) const;
0153 
0154     ///
0155     /// \brief unlink the given dataset or group
0156     /// \param node_path dataset/group name to unlink
0157     void unlink(const std::string& node_path) const;
0158 
0159     ///
0160     /// \brief Returns the kind of link of the given name (soft, hard...)
0161     /// \param node_path The entry to check, path relative to the current group
0162     LinkType getLinkType(const std::string& node_path) const;
0163 
0164     ///
0165     /// \brief A shorthand to get the kind of object pointed to (group, dataset, type...)
0166     /// \param node_path The entry to check, path relative to the current group
0167     ObjectType getObjectType(const std::string& node_path) const;
0168 
0169     ///
0170     /// \brief A shorthand to create softlink to any object which provides `getPath`
0171     /// The link will be created with default properties along with required parent groups
0172     template <typename T, typename = decltype(&T::getPath)>
0173     void createSoftLink(const std::string& linkName, const T& obj) {
0174         static_assert(!std::is_same<T, Attribute>::value,
0175                       "hdf5 doesn't support soft links to Attributes");
0176         createSoftLink(linkName, obj.getPath());
0177     }
0178 
0179     ///
0180     /// \brief Creates softlinks
0181     /// \param link_name The name of the link
0182     /// \param obj_path The target object path
0183     /// \param linkCreateProps A Link_Create property list. Notice "parents=true" overrides
0184     /// \param linkAccessProps The Link_Access property list
0185     /// \param parents Whether parent groups should be created: Default: true
0186     void createSoftLink(const std::string& link_name,
0187                         const std::string& obj_path,
0188                         LinkCreateProps linkCreateProps = LinkCreateProps(),
0189                         const LinkAccessProps& linkAccessProps = LinkAccessProps(),
0190                         bool parents = true);
0191 
0192     void createExternalLink(const std::string& link_name,
0193                             const std::string& h5_file,
0194                             const std::string& obj_path,
0195                             LinkCreateProps linkCreateProps = LinkCreateProps(),
0196                             const LinkAccessProps& linkAccessProps = LinkAccessProps(),
0197                             bool parents = true);
0198 
0199     ///
0200     /// \brief Creates hardlinks
0201     /// \param link_name The name of the link
0202     /// \param target_obj The target object
0203     /// \param linkCreateProps A Link_Create property list. Notice "parents=true" overrides
0204     /// \param linkAccessProps The Link_Access property list
0205     /// \param parents Whether parent groups should be created: Default: true
0206     template <typename T, typename = decltype(&T::getPath)>
0207     void createHardLink(const std::string& link_name,
0208                         const T& target_obj,
0209                         LinkCreateProps linkCreateProps = LinkCreateProps(),
0210                         const LinkAccessProps& linkAccessProps = LinkAccessProps(),
0211                         bool parents = true);
0212 
0213   private:
0214     using derivate_type = Derivate;
0215 
0216     // A wrapper over the low-level H5Lexist
0217     // It makes behavior consistent among versions and by default transforms
0218     // errors to exceptions
0219     bool _exist(const std::string& node_path, bool raise_errors = true) const;
0220 };
0221 
0222 
0223 ///
0224 /// \brief The possible types of group entries (link concept)
0225 ///
0226 enum class LinkType {
0227     Hard,
0228     Soft,
0229     External,
0230     Other  // Reserved or User-defined
0231 };
0232 
0233 
0234 }  // namespace HighFive