BlueBrain / HighFive

HighFive - Header-only C++ HDF5 interface
https://bluebrain.github.io/HighFive/
Boost Software License 1.0
696 stars 162 forks source link

Example for adding auto serialisation support for user defined types #617

Open aborzunov opened 2 years ago

aborzunov commented 2 years ago

Is your feature request related to a problem? Please describe.

On master branch we can:

auto v = std::vector<std::array<double, 2048ul>>();
/* fill v */
file.createDataSet("/path", v);

But we will fail on

struct alignas(avx512) buffer_t: public std::array<double, 2048ul>
{ };
auto v = std::vector<buffer_t>();
/* fill v */
file.createDataSet("/path", v);

Describe the solution you'd like Example with implementing inspector<> specialisation in user/or highfive namesapce.

Describe alternatives you've considered

struct alignas(avx512) buffer_t: public std::array<double, 2048ul>
{ 
  using super_t = std::array<double, 2048ul>;
};
auto v = std::vector<typename buffer_t::super_t>();
// fill v in this way:
// v.push_back(buffer_t{});
file.createDataSet("/path", v);

Additional context A better solution would be implement auto serialisation for range-types (.begin(), .end() methods present)!

alkino commented 2 years ago

Your issue about having documentation is a copy of #566, the second idea about supporting types with .begin(), .end() is nice too.

alkino commented 2 years ago

I wonder if you can do:

namespace HighFive {
namespace details {
    template <>
    struct inspector<buffer_t>: inspector<std::array<double, 2048ul>> {};
}
}
aborzunov commented 2 years ago

Yes, this works fine. Adding something like this (and one more complex example) in docs would help users a lot.

But, as you agreed, supporting generic range types with begin(), end() would be wonderful feature. Feel free to close this duplication issue or rewording this one to more generic case.

alkino commented 1 year ago

The only problem I can see is when choosing to using the auto-serialisation and when choosing the normal serialisation? The normal serialisation - more specific - will always be faster so we should keep it.