Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ option(HIGHFIVE_VERBOSE "Set logging level to verbose." OFF)
option(HIGHFIVE_HAS_FRIEND_DECLARATIONS "Enable additional friend declarations. Certain compiler require this On, others Off." OFF)
mark_as_advanced(HIGHFIVE_HAS_FRIEND_DECLARATIONS)

option(HIGHFIVE_USE_STRING_VIEW "Use std::string_view instead of std::string has H5 objects keys." OFF)
mark_as_advanced(HIGHFIVE_HAS_STRING_VIEW_NAMES)

option(HIGHFIVE_FIND_HDF5 "Find and link with HDF5." On)

set(HIGHFIVE_CMAKE_INSTALL_DIR "lib/cmake/HighFive" CACHE STRING
Expand Down Expand Up @@ -100,6 +103,12 @@ if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS ${HIGHFIVE_CXX_STANDAR
message(FATAL_ERROR "HighFive needs to be compiled with at least C++${HIGHFIVE_CXX_STANDARD_DEFAULT}")
endif()

if(HIGHFIVE_USE_STRING_VIEW)
if(CMAKE_CXX_STANDARD LESS 17)
message(FATAL_ERROR "HighFive needs to be compiled with at least C++17 to use std::string_view as keys")
endif()
endif()

if(HIGHFIVE_TEST_MDSPAN AND CMAKE_CXX_STANDARD LESS 23)
message(FATAL_ERROR "HIGHFIVE_TEST_MDSPAN requires C++23 or newer, but CMAKE_CXX_STANDARD is ${CMAKE_CXX_STANDARD}")
endif()
Expand Down
4 changes: 4 additions & 0 deletions cmake/HighFiveFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ if(HIGHFIVE_HAS_FRIEND_DECLARATIONS)
target_compile_definitions(HighFiveFlags INTERFACE -DHIGHFIVE_HAS_FRIEND_DECLARATIONS=1)
endif()

if(HIGHFIVE_USE_STRING_VIEW)
target_compile_definitions(HighFiveFlags INTERFACE -DHIGHFIVE_USE_STRING_VIEW=1)
endif()

if(HIGHFIVE_SANITIZER)
target_compile_options(HighFiveFlags INTERFACE -fsanitize=${HIGHFIVE_SANITIZER})
target_link_options(HighFiveFlags INTERFACE -fsanitize=${HIGHFIVE_SANITIZER})
Expand Down
6 changes: 3 additions & 3 deletions include/highfive/H5File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

#include "H5Object.hpp"
#include "H5PropertyList.hpp"
#include "H5Label.hpp"
#include "bits/H5Annotate_traits.hpp"
#include "bits/H5Node_traits.hpp"

namespace HighFive {


///
/// \brief File class
///
Expand Down Expand Up @@ -68,7 +68,7 @@ class File: public Object, public NodeTraits<File>, public AnnotateTraits<File>
/// \param fileAccessProps: the file access properties
///
/// Open or create a new HDF5 file
explicit File(const std::string& filename,
explicit File(const H5Label& filename,
AccessMode openFlags = ReadOnly,
const FileAccessProps& fileAccessProps = FileAccessProps::Default());

Expand All @@ -80,7 +80,7 @@ class File: public Object, public NodeTraits<File>, public AnnotateTraits<File>
/// \param fileAccessProps: the file access properties
///
/// Open or create a new HDF5 file
File(const std::string& filename,
File(const H5Label& filename,
AccessMode access_mode,
const FileCreateProps& fileCreateProps,
const FileAccessProps& fileAccessProps = FileAccessProps::Default());
Expand Down
66 changes: 66 additions & 0 deletions include/highfive/H5Label.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c), 2026, Alain Miniussi <alain.miniussi@oca.eu>
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
*/
#pragma once

#include <string>

#if HIGHFIVE_USE_STRING_VIEW
# ifndef __cpp_lib_string_view
# error "std::string_view not available in this standard librarie
# include <string_view>
# endif
#endif

namespace HighFive {
/// \brief Build a path composed of all parameter separated with a "/".
/// \param s can be a std::string, a std::string_view (if supposrted) or a const char*.
/// \return The build path
inline std::string path(std::string s) { return s; }
/// \overload
template<typename... S> inline std::string path(std::string first, S... s);

#if HIGHFIVE_USE_STRING_VIEW
using H5Label = std::string_view;

/// \overload
inline std::string path(std::string_view s) { return std::string{s}; }
/// \overload
inline std::string path(char const* s) { return std::string{s}; }
/// \overload
template<typename... S> std::string path(std::string_view first, S... s);
template<typename... S> std::string path(char const* first, S... s);

#else // !HIGHFIVE_USE_STRING_VIEW

using H5Label = std::string;

#endif // HIGHFIVE_USE_STRING_VIEW

template<typename... S>
inline
std::string path(std::string first, S... s) {
return first + "/" + path(s...);
}

#if HIGHFIVE_USE_STRING_VIEW

template<typename... S>
inline
std::string path(std::string_view first, S... s) {
return std::string{first} + "/" + path(s...);
}

template<typename... S>
inline
std::string path(char const* first, S... s) {
return std::string{first} + "/" + path(s...);
}

#endif
}
13 changes: 7 additions & 6 deletions include/highfive/bits/H5Annotate_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <string>

#include "../H5Label.hpp"
#include "../H5Attribute.hpp"

namespace HighFive {
Expand All @@ -24,7 +25,7 @@ class AnnotateTraits {
/// \param type
/// \return the attribute object
///
Attribute createAttribute(const std::string& attribute_name,
Attribute createAttribute(const H5Label& attribute_name,
const DataSpace& space,
const DataType& type);

Expand All @@ -36,7 +37,7 @@ class AnnotateTraits {
/// informations
/// \return Attribute Object
template <typename Type>
Attribute createAttribute(const std::string& attribute_name, const DataSpace& space);
Attribute createAttribute(const H5Label& attribute_name, const DataSpace& space);

///
/// \brief createAttribute create a new attribute on the current dataset and
Expand All @@ -47,18 +48,18 @@ class AnnotateTraits {
/// \return Attribute Object
///
template <typename T>
Attribute createAttribute(const std::string& attribute_name, const T& data);
Attribute createAttribute(const H5Label& attribute_name, const T& data);

///
/// \brief deleteAttribute let you delete an attribute by its name.
/// \param attribute_name identifier of the attribute
void deleteAttribute(const std::string& attribute_name);
void deleteAttribute(const H5Label& attribute_name);

///
/// \brief open an existing attribute with the name attribute_name
/// \param attribute_name identifier of the attribute
/// \return the attribute object
Attribute getAttribute(const std::string& attribute_name) const;
Attribute getAttribute(const H5Label& attribute_name) const;

///
/// \brief return the number of attributes of the node / group
Expand All @@ -73,7 +74,7 @@ class AnnotateTraits {
///
/// \brief checks an attribute exists
/// \return number of attributes
bool hasAttribute(const std::string& attr_name) const;
bool hasAttribute(const H5Label& attr_name) const;

private:
using derivate_type = Derivate;
Expand Down
20 changes: 10 additions & 10 deletions include/highfive/bits/H5Annotate_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
namespace HighFive {

template <typename Derivate>
inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
inline Attribute AnnotateTraits<Derivate>::createAttribute(const H5Label& attribute_name,
const DataSpace& space,
const DataType& dtype) {
auto attr_id = detail::h5a_create2(static_cast<Derivate*>(this)->getId(),
attribute_name.c_str(),
std::string{attribute_name}.c_str(),
dtype.getId(),
space.getId(),
H5P_DEFAULT,
Expand All @@ -35,14 +35,14 @@ inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& at

template <typename Derivate>
template <typename Type>
inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
inline Attribute AnnotateTraits<Derivate>::createAttribute(const H5Label& attribute_name,
const DataSpace& space) {
return createAttribute(attribute_name, space, create_and_check_datatype<Type>());
}

template <typename Derivate>
template <typename T>
inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& attribute_name,
inline Attribute AnnotateTraits<Derivate>::createAttribute(const H5Label& attribute_name,
const T& data) {
Attribute att =
createAttribute(attribute_name,
Expand All @@ -53,14 +53,14 @@ inline Attribute AnnotateTraits<Derivate>::createAttribute(const std::string& at
}

template <typename Derivate>
inline void AnnotateTraits<Derivate>::deleteAttribute(const std::string& attribute_name) {
detail::h5a_delete(static_cast<const Derivate*>(this)->getId(), attribute_name.c_str());
inline void AnnotateTraits<Derivate>::deleteAttribute(const H5Label& attribute_name) {
detail::h5a_delete(static_cast<const Derivate*>(this)->getId(), std::string{attribute_name}.c_str());
}

template <typename Derivate>
inline Attribute AnnotateTraits<Derivate>::getAttribute(const std::string& attribute_name) const {
inline Attribute AnnotateTraits<Derivate>::getAttribute(const H5Label& attribute_name) const {
const auto attr_id = detail::h5a_open(static_cast<const Derivate*>(this)->getId(),
attribute_name.c_str(),
std::string{attribute_name}.c_str(),
H5P_DEFAULT);
return detail::make_attribute(attr_id);
}
Expand Down Expand Up @@ -90,8 +90,8 @@ inline std::vector<std::string> AnnotateTraits<Derivate>::listAttributeNames() c
}

template <typename Derivate>
inline bool AnnotateTraits<Derivate>::hasAttribute(const std::string& attr_name) const {
return detail::h5a_exists(static_cast<const Derivate*>(this)->getId(), attr_name.c_str()) > 0;
inline bool AnnotateTraits<Derivate>::hasAttribute(const H5Label& attr_name) const {
return detail::h5a_exists(static_cast<const Derivate*>(this)->getId(), std::string{attr_name}.c_str()) > 0;
}

} // namespace HighFive
10 changes: 5 additions & 5 deletions include/highfive/bits/H5File_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ inline unsigned convert_open_flag(File::AccessMode openFlags) {
}
} // namespace

inline File::File(const std::string& filename,
inline File::File(const H5Label& filename,
AccessMode openFlags,
const FileAccessProps& fileAccessProps)
: File(filename, openFlags, FileCreateProps::Default(), fileAccessProps) {}


inline File::File(const std::string& filename,
inline File::File(const H5Label& filename,
AccessMode access_mode,
const FileCreateProps& fileCreateProps,
const FileAccessProps& fileAccessProps) {
Expand All @@ -76,7 +76,7 @@ inline File::File(const std::string& filename,
silencer = std::make_unique<SilenceHDF5>();
}

_hid = detail::nothrow::h5f_open(filename.c_str(), openMode, fileAccessProps.getId());
_hid = detail::nothrow::h5f_open(std::string{filename}.c_str(), openMode, fileAccessProps.getId());

if (isValid()) {
return; // Done
Expand All @@ -87,13 +87,13 @@ inline File::File(const std::string& filename,
createMode = H5F_ACC_EXCL;
} else {
HDF5ErrMapper::ToException<FileException>(
std::string("Unable to open file " + filename));
std::string("Unable to open file " + std::string{filename}));
}
}

auto fcpl = fileCreateProps.getId();
auto fapl = fileAccessProps.getId();
_hid = detail::h5f_create(filename.c_str(), createMode, fcpl, fapl);
_hid = detail::h5f_create(std::string{filename}.c_str(), createMode, fcpl, fapl);
}

inline const std::string& File::getName() const {
Expand Down
Loading