Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archive::{is_loading,is_saving}::value #360

Closed
headupinclouds opened this issue Nov 20, 2016 · 3 comments
Closed

Archive::{is_loading,is_saving}::value #360

headupinclouds opened this issue Nov 20, 2016 · 3 comments
Milestone

Comments

@headupinclouds
Copy link
Contributor

headupinclouds commented Nov 20, 2016

Is the following {is_loading,is_saving} type addition compatible with the cereal library? This would greatly improve compatibility with existing boost serialization code and is seemingly unobtrusive.

template<class ArchiveType, std::uint32_t Flags = 0>
  class OutputArchive : public detail::OutputArchiveBase
{
  public:
    typedef bool_<false> is_loading;  // <- proposal
    typedef bool_<true> is_saving;     // <- proposal
   // ...
};

This supports the following serialization pattern:

struct Bar
{
    template <typename Archive> void serialize(Archive &ar, const std::uint32_t version)
    {
        std::int16_t tmp = foo;
        if(Archive::is_loading::value)
        {
            ar & tmp;
            foo = tmp; // For illustration only
        }
        else
        {
            tmp = foo; // For illustration only
            ar & tmp;
        }
        ar & foo;
    }
    std::int32_t foo;
};
@AzothAmmo
Copy link
Contributor

You can actually already get this information by simply using std::is_base_of with the archive and either cereal::detail::InputArchiveBase or cereal::detail::OutputArchiveBase, though I suspect what you are after is identical syntax to boost.

The best place to add this to cereal would be in cereal.hpp to the InputArchive and OutputArchive classes, something along the lines of

using is_loading = std::true_type;
using is_saving  = std::false_type;

as appropriate, defined within the doxygen block for the Boost Transition Layer

If you choose to do a pull request adding this to cereal, please do so against the current develop branch and also include appropriate doxygen comments for the new values.

@headupinclouds
Copy link
Contributor Author

Thanks for the quick feedback (and a great library). I will put together a PR over the holiday. Having this change upstream will greatly simplify a boost -> cereal transition.

headupinclouds added a commit to headupinclouds/cereal that referenced this issue Nov 25, 2016
This provides compatibility for asymmetric serialize routines when transitioning from boost serialization and associated archive types.

See: USCiLab#360

Example:
```
    template <typename Archive>
    void serialize(Archive &ar, const std::uint32_t version)
    {
        std::int16_t tmp = foo;
        if(Archive::is_loading::value)
        {
            ar & tmp;
            foo = tmp; // For illustration only
        }
        else
        {
            tmp = foo; // For illustration only
            ar & tmp;
        }
        ar & foo;
    }
```
@AzothAmmo AzothAmmo added this to the v1.2.2 milestone Nov 28, 2016
@AzothAmmo
Copy link
Contributor

PR merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants