mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Add RangeSet serialization
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/icl/closed_interval.hpp>
|
||||
#include <boost/icl/interval_set.hpp>
|
||||
#include <boost/serialization/split_free.hpp>
|
||||
|
||||
namespace ripple
|
||||
{
|
||||
@@ -128,5 +129,72 @@ prevMissing(RangeSet<T> const & rs, T t, T minVal = 0)
|
||||
return boost::none;
|
||||
return boost::icl::last(tgt);
|
||||
}
|
||||
} // namespace ripple
|
||||
} // namespace ripple
|
||||
|
||||
|
||||
// The boost serialization documents recommended putting free-function helpers
|
||||
// in the boost serialization namespace
|
||||
|
||||
namespace boost {
|
||||
namespace serialization {
|
||||
template <class Archive, class T>
|
||||
void
|
||||
save(Archive& ar,
|
||||
ripple::ClosedInterval<T> const& ci,
|
||||
const unsigned int version)
|
||||
{
|
||||
ar << ci.lower() << ci.upper();
|
||||
}
|
||||
|
||||
template <class Archive, class T>
|
||||
void
|
||||
load(Archive& ar, ripple::ClosedInterval<T>& ci, const unsigned int version)
|
||||
{
|
||||
T low, up;
|
||||
ar >> low >> up;
|
||||
ci = ripple::ClosedInterval<T>{low, up};
|
||||
}
|
||||
|
||||
template <class Archive, class T>
|
||||
void
|
||||
serialize(Archive& ar,
|
||||
ripple::ClosedInterval<T>& ci,
|
||||
const unsigned int version)
|
||||
{
|
||||
split_free(ar, ci, version);
|
||||
}
|
||||
|
||||
template <class Archive, class T>
|
||||
void
|
||||
save(Archive& ar, ripple::RangeSet<T> const& rs, const unsigned int version)
|
||||
{
|
||||
ar << rs.iterative_size();
|
||||
for (auto const& r : rs)
|
||||
ar << r;
|
||||
}
|
||||
|
||||
template <class Archive, class T>
|
||||
void
|
||||
load(Archive& ar, ripple::RangeSet<T>& rs, const unsigned int version)
|
||||
{
|
||||
rs.clear();
|
||||
std::size_t intervals;
|
||||
ar >> intervals;
|
||||
for (std::size_t i = 0; i < intervals; ++i)
|
||||
{
|
||||
ripple::ClosedInterval<T> ci;
|
||||
ar >> ci;
|
||||
rs.insert(ci);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Archive, class T>
|
||||
void
|
||||
serialize(Archive& ar, ripple::RangeSet<T>& rs, const unsigned int version)
|
||||
{
|
||||
split_free(ar, rs, version);
|
||||
}
|
||||
|
||||
} // serialization
|
||||
} // boost
|
||||
#endif
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/basics/RangeSet.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <boost/archive/binary_iarchive.hpp>
|
||||
#include <boost/archive/binary_oarchive.hpp>
|
||||
|
||||
namespace ripple
|
||||
{
|
||||
@@ -75,11 +77,41 @@ public:
|
||||
set.erase(range(4u, 5u));
|
||||
BEAST_EXPECT(to_string(set) == "1-2,6");
|
||||
}
|
||||
|
||||
void
|
||||
testSerialization()
|
||||
{
|
||||
|
||||
auto works = [](RangeSet<std::uint32_t> const & orig)
|
||||
{
|
||||
std::stringstream ss;
|
||||
boost::archive::binary_oarchive oa(ss);
|
||||
oa << orig;
|
||||
|
||||
boost::archive::binary_iarchive ia(ss);
|
||||
RangeSet<std::uint32_t> deser;
|
||||
ia >> deser;
|
||||
|
||||
return orig == deser;
|
||||
};
|
||||
|
||||
RangeSet<std::uint32_t> rs;
|
||||
|
||||
BEAST_EXPECT(works(rs));
|
||||
|
||||
rs.insert(3);
|
||||
BEAST_EXPECT(works(rs));
|
||||
|
||||
rs.insert(range(7u, 10u));
|
||||
BEAST_EXPECT(works(rs));
|
||||
|
||||
}
|
||||
void
|
||||
run()
|
||||
{
|
||||
testPrevMissing();
|
||||
testToString();
|
||||
testSerialization();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user