Skip to content

Commit

Permalink
Add RangeSet serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bachase authored and scottschurr committed Jan 17, 2018
1 parent 2d5ddbf commit 819ea46
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ use_boost(
filesystem
program_options
regex
serialization
system
thread)

Expand Down
3 changes: 2 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ def config_env(toolchain, variant, env):
'boost_program_options',
'boost_regex',
'boost_system',
'boost_thread'
'boost_thread',
'boost_serialization'
]
env.Append(LIBS=['dl'])

Expand Down
70 changes: 69 additions & 1 deletion src/ripple/basics/RangeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
32 changes: 32 additions & 0 deletions src/test/basics/RangeSet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -75,11 +77,41 @@ class RangeSet_test : public beast::unit_test::suite
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();
}
};

Expand Down

0 comments on commit 819ea46

Please sign in to comment.