Skip to content

Commit

Permalink
Fix : Add support for std::byte as Dataset type for c++17 and above (B…
Browse files Browse the repository at this point in the history
…lueBrain#698)

Add support for `std::byte` as Dataset type for C++17 and above.
  • Loading branch information
WeinaJi committed Mar 6, 2023
1 parent 20be06f commit c6ffef4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
os: ubuntu-latest
pkgs: 'libboost-all-dev libeigen3-dev libopencv-dev'
flags: '-DHIGHFIVE_USE_EIGEN:Bool=ON -DHIGHFIVE_USE_OPENCV:Bool=ON -GNinja'
- config:
os: ubuntu-20.04
pkgs: 'libboost-all-dev'
flags: '-DCMAKE_CXX_STANDARD=17'

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ if(HIGHFIVE_USE_XTENSOR)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

add_compile_definitions(HIGHFIVE_CXX_STD=${CMAKE_CXX_STANDARD})

# Search dependencies (hdf5, boost, eigen, xtensor, mpi) and build target highfive_deps
include(${PROJECT_SOURCE_DIR}/CMake/HighFiveTargetDeps.cmake)
Expand Down
11 changes: 11 additions & 0 deletions include/highfive/bits/H5DataType_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <string>
#include <complex>
#include <cstring>
#if HIGHFIVE_CXX_STD >= 17
#include <cstddef>
#endif

#include <H5Ppublic.h>
#include <H5Tpublic.h>
Expand Down Expand Up @@ -166,6 +169,14 @@ inline AtomicType<std::string>::AtomicType() {
_hid = create_string(H5T_VARIABLE);
}

#if HIGHFIVE_CXX_STD >= 17
// std byte
template <>
inline AtomicType<std::byte>::AtomicType() {
_hid = H5Tcopy(H5T_NATIVE_B8);
}
#endif

// Fixed-Length strings
// require class specialization templated for the char length
template <size_t StrLen>
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_all_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,33 @@ TEMPLATE_TEST_CASE("Scalar in std::vector<std::array>", "[Types]", bool, std::st
CHECK(value.size() == 5);
}
}

#if HIGHFIVE_CXX_STD >= 17
TEMPLATE_PRODUCT_TEST_CASE("Scalar in std::vector<std::byte>", "[Types]", std::vector, std::byte) {
const std::string FILE_NAME("Test_vector_byte.h5");
const std::string DATASET_NAME("dset");
TestType t1(5, std::byte(0xCD));

{
// Create a new file using the default property lists.
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);

// Create the dataset
DataSet dataset = file.createDataSet(DATASET_NAME, {5}, create_datatype<std::byte>());

// Write into the initial part of the dataset
dataset.write(t1);
}

// read it back
{
File file(FILE_NAME, File::ReadOnly);

TestType value(5, std::byte(0xCD));
DataSet dataset = file.getDataSet("/" + DATASET_NAME);
dataset.read(value);
CHECK(t1 == value);
CHECK(value.size() == 5);
}
}
#endif

0 comments on commit c6ffef4

Please sign in to comment.