Skip to content

Commit

Permalink
Allow Buffers to depend on the file datatype.
Browse files Browse the repository at this point in the history
The upcoming `StringBuffer` will need to know the file datatype, because
in HDF5 the expect datalayout, and therefore the way the buffer is
populated, is different for fixed and variable length strings.
  • Loading branch information
1uc committed Jul 12, 2023
1 parent 8423365 commit 51ea857
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
11 changes: 7 additions & 4 deletions include/highfive/bits/H5Attribute_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ inline T Attribute::read() const {
template <typename T>
inline void Attribute::read(T& array) const {
const DataSpace& mem_space = getMemSpace();
auto file_datatype = getDataType();
const details::BufferInfo<T> buffer_info(
getDataType(),
file_datatype,
[this]() -> std::string { return this->getName(); },
details::BufferInfo<T>::read);

Expand All @@ -82,7 +83,7 @@ inline void Attribute::read(T& array) const {
return;
}

auto r = details::data_converter::get_reader<T>(dims, array);
auto r = details::data_converter::get_reader<T>(dims, array, file_datatype);
read(r.get_pointer(), buffer_info.data_type);
// re-arrange results
r.unserialize(array);
Expand Down Expand Up @@ -125,8 +126,10 @@ inline void Attribute::write(const T& buffer) {
return;
}

auto file_datatype = getDataType();

const details::BufferInfo<T> buffer_info(
getDataType(),
file_datatype,
[this]() -> std::string { return this->getName(); },
details::BufferInfo<T>::write);

Expand All @@ -136,7 +139,7 @@ inline void Attribute::write(const T& buffer) {
<< " into dataset of dimensions " << mem_space.getNumberDimensions();
throw DataSpaceException(ss.str());
}
auto w = details::data_converter::serialize<T>(buffer);
auto w = details::data_converter::serialize<T>(buffer, file_datatype);
write_raw(w.get_pointer(), buffer_info.data_type);
}

Expand Down
21 changes: 12 additions & 9 deletions include/highfive/bits/H5Converter_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct enable_shallow_copy: public std::enable_if<inspector<T>::is_trivially_cop
template <class T, class V = void>
struct enable_deep_copy: public std::enable_if<!inspector<T>::is_trivially_copyable, V> {};


template <typename T, bool IsReadOnly>
struct ShallowCopyBuffer {
using type = unqualified_t<T>;
Expand Down Expand Up @@ -83,19 +84,18 @@ struct Writer<T, typename enable_shallow_copy<T>::type>: public ShallowCopyBuffe
using super = ShallowCopyBuffer<T, true>;

public:
explicit Writer(const T& val)
explicit Writer(const T& val, const DataType& file_datatype)
: super(val){};
};

template <typename T>
struct Writer<T, typename enable_deep_copy<T>::type>: public DeepCopyBuffer<T> {
explicit Writer(const T& val)
explicit Writer(const T& val, const DataType& file_datatype)
: DeepCopyBuffer<T>(inspector<T>::getDimensions(val)) {
inspector<T>::serialize(val, this->get_pointer());
}
};


template <typename T, typename Enable = void>
struct Reader;

Expand All @@ -106,7 +106,7 @@ struct Reader<T, typename enable_shallow_copy<T>::type>: public ShallowCopyBuffe
using type = typename super::type;

public:
Reader(const std::vector<size_t>&, type& val)
Reader(const std::vector<size_t>&, type& val, const DataType&)
: super(val) {}
};

Expand All @@ -117,23 +117,26 @@ struct Reader<T, typename enable_deep_copy<T>::type>: public DeepCopyBuffer<T> {
using type = typename super::type;

public:
Reader(const std::vector<size_t>& _dims, type&)
Reader(const std::vector<size_t>& _dims, type&, const DataType&)
: super(_dims) {}
};


struct data_converter {
template <typename T>
static Writer<T> serialize(const typename inspector<T>::type& val) {
return Writer<T>(val);
static Writer<T> serialize(const typename inspector<T>::type& val,
const DataType& file_datatype) {
return Writer<T>(val, file_datatype);
}

template <typename T>
static Reader<T> get_reader(const std::vector<size_t>& dims, T& val) {
static Reader<T> get_reader(const std::vector<size_t>& dims,
T& val,
const DataType& file_datatype) {
// TODO Use bufferinfo for recursive_ndim
auto effective_dims = details::squeezeDimensions(dims, inspector<T>::recursive_ndim);
inspector<T>::prepare(val, effective_dims);
return Reader<T>(effective_dims, val);
return Reader<T>(effective_dims, val, file_datatype);
}
};

Expand Down
12 changes: 8 additions & 4 deletions include/highfive/bits/H5Slice_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ inline void SliceTraits<Derivate>::read(T& array, const DataTransferProps& xfer_
const auto& slice = static_cast<const Derivate&>(*this);
const DataSpace& mem_space = slice.getMemSpace();

auto file_datatype = slice.getDataType();

const details::BufferInfo<T> buffer_info(
slice.getDataType(),
file_datatype,
[&slice]() -> std::string { return details::get_dataset(slice).getPath(); },
details::BufferInfo<T>::Operation::read);

Expand All @@ -193,7 +195,7 @@ inline void SliceTraits<Derivate>::read(T& array, const DataTransferProps& xfer_
return;
}

auto r = details::data_converter::get_reader<T>(dims, array);
auto r = details::data_converter::get_reader<T>(dims, array, file_datatype);
read(r.get_pointer(), buffer_info.data_type, xfer_props);
// re-arrange results
r.unserialize(array);
Expand Down Expand Up @@ -251,8 +253,10 @@ inline void SliceTraits<Derivate>::write(const T& buffer, const DataTransferProp
return;
}

auto file_datatype = slice.getDataType();

const details::BufferInfo<T> buffer_info(
slice.getDataType(),
file_datatype,
[&slice]() -> std::string { return details::get_dataset(slice).getPath(); },
details::BufferInfo<T>::Operation::write);

Expand All @@ -263,7 +267,7 @@ inline void SliceTraits<Derivate>::write(const T& buffer, const DataTransferProp
<< " into dataset with n = " << buffer_info.n_dimensions << " dimensions.";
throw DataSpaceException(ss.str());
}
auto w = details::data_converter::serialize<T>(buffer);
auto w = details::data_converter::serialize<T>(buffer, file_datatype);
write_raw(w.get_pointer(), buffer_info.data_type, xfer_props);
}

Expand Down

0 comments on commit 51ea857

Please sign in to comment.