joboccara / NamedType

Implementation of strong types in C++
MIT License
766 stars 85 forks source link

cereal serialization functions #49

Open pwm1234 opened 3 years ago

pwm1234 commented 3 years ago

It took me a little while to figure out how to best serialize a NamedType using cereal (which is the BEST C++ serialization library I have found - if someone knows of a better one, please let me know). I am not sure of a good way to add this to the fluent codebase, so I thought I would post this issue in case this is helpful to others.

Cereal Serialization using external minimal serialization functions as described in Serialization Functions documentation.

namespace fluent {
template <typename Archive,
          typename ValueType,
          typename TagType,
          template <typename>
          class... Skills>
void load_minimal(
    Archive&,
    NamedType<ValueType, TagType, Skills...>& nobj,
    typename NamedType<ValueType, TagType, Skills...>::UnderlyingType const& val)
{
    using Ntype = NamedType<ValueType, TagType, Skills...>;
    nobj = Ntype{val};
}

template <typename Archive,
          typename ValueType,
          typename TagType,
          template <typename> class... Skills>
auto save_minimal(Archive&,
                  const NamedType<ValueType, TagType, Skills...>& nobj)
{
    return nobj.get();
}
}  // namespace fluent
joboccara commented 3 years ago

Thanks for the reference!