Commit 71b80e6b authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

[Serialization] Fix removal of duplicated vectors.

parent 9b665b86
......@@ -125,7 +125,7 @@ struct archive {
typedef archive<::hpp::pinocchio::vector_t, eigen_compare<::hpp::pinocchio::vector_t> > vector_archive;
template<class Archive, typename Key, typename Compare = std::less<Key>>
template<class Archive, typename Key>
inline void load_or_save_no_remove_duplicate_check (Archive& ar,
const char* name,
Key& key,
......@@ -134,17 +134,30 @@ inline void load_or_save_no_remove_duplicate_check (Archive& ar,
(void) version;
Key* value = &key;
ar & boost::serialization::make_nvp(name, value);
if (!Archive::is_saving::value) key = *value;
}
template<class Archive, typename Key>
inline void save_impl (Archive& ar,
const char* name,
const Key& key,
const unsigned int version)
{
(void) version;
Key const* value = &key;
ar << boost::serialization::make_nvp(name, value);
}
template<class Archive, typename Key, typename Compare = std::less<Key>>
inline void save (Archive& ar,
inline void save_impl (Archive& ar,
std::set<Key const*, ptr_less<Key,Compare> >& set,
const char* name,
const Key& key,
const unsigned int version)
{
(void) version;
static_assert(Archive::is_saving::value, "Archive is saving");
if(!Archive::is_saving::value)
throw std::logic_error("HPP serialization: cannot load into a const element. This should never happen.");
auto result = set.insert(&key);
bool inserted = result.second;
Key const* k = (inserted ? &key : *result.first);
......@@ -159,7 +172,7 @@ inline void serialize (Archive& ar,
const unsigned int version)
{
if (Archive::is_saving::value) {
save(ar, set, name, key, version);
save_impl(ar, set, name, key, version);
} else {
load_or_save_no_remove_duplicate_check(ar, name, key, version);
}
......@@ -173,7 +186,15 @@ static inline void run (Archive& ar,
Key& key,
const unsigned int version)
{
serialize(ar, static_cast<archive<Key, Compare>&>(ar).datas, name, key, version);
serialize(ar, dynamic_cast<archive<Key, Compare>&>(ar).datas, name, key, version);
}
template<typename Archive, typename Key, typename Compare = std::less<Key> >
static inline void save (Archive& ar,
const char* name,
const Key& key,
const unsigned int version)
{
save_impl(ar, dynamic_cast<archive<Key, Compare>&>(ar).datas, name, key, version);
}
};
......@@ -185,7 +206,21 @@ static inline void run (Archive& ar,
Key& key,
const unsigned int version)
{
load_or_save_no_remove_duplicate_check(ar, name, key, version);
if (dynamic_cast<archive<Key, Compare>*>(&ar) != NULL)
serialiaze_impl<true>::run<Archive, Key, Compare>(ar, name, key, version);
else
load_or_save_no_remove_duplicate_check(ar, name, key, version);
}
template<typename Archive, typename Key, typename Compare = std::less<Key> >
static inline void save (Archive& ar,
const char* name,
const Key& key,
const unsigned int version)
{
if (dynamic_cast<archive<Key, Compare>*>(&ar) != NULL)
serialiaze_impl<true>::save<Archive, Key, Compare>(ar, name, key, version);
else
save_impl(ar, name, key, version);
}
};
......@@ -199,6 +234,16 @@ inline void serialize (Archive& ar,
serialiaze_impl<is_base>::template run<Archive, Key, Compare>(ar, name, key, version);
}
template<typename Archive, typename Key, typename Compare = std::less<Key>,
bool is_base = std::is_base_of<archive<Key, Compare>, Archive>::value >
inline void save (Archive& ar,
const char* name,
const Key& key,
const unsigned int version)
{
serialiaze_impl<is_base>::template save<Archive, Key, Compare>(ar, name, key, version);
}
template<typename Archive>
inline void serialize_vector (Archive& ar,
const char* name,
......@@ -209,6 +254,16 @@ inline void serialize_vector (Archive& ar,
(ar, name, key, version);
}
template<typename Archive>
inline void save_vector (Archive& ar,
const char* name,
const ::hpp::pinocchio::vector_t& key,
const unsigned int version)
{
save<Archive, ::hpp::pinocchio::vector_t, vector_archive::compare_type>
(ar, name, key, version);
}
} // namespace remove_duplicate
} // namespace pinocchio
} // namespace hpp
......
......@@ -21,7 +21,7 @@
#include <hpp/util/serialization.hh>
#include <pinocchio/serialization/eigen.hpp>
#include <hpp/pinocchio/serialization.hh>
#include "../src/size-visitor.hh"
#include "../src/addition-visitor.hh"
......@@ -123,7 +123,7 @@ void load (Archive & ar, hpp::pinocchio::LiegroupElement& c, const unsigned int
hpp::pinocchio::LiegroupSpacePtr_t space;
hpp::pinocchio::vector_t vector;
ar & make_nvp("space", space);
ar & make_nvp("vector", vector);
hpp::serialization::remove_duplicate::serialize_vector(ar, "vector", vector, version);
c = hpp::pinocchio::LiegroupElement(vector, space);
}
template<class Archive>
......@@ -131,7 +131,7 @@ void save (Archive & ar, const hpp::pinocchio::LiegroupElement& c, const unsigne
{
(void) version;
ar & make_nvp("space", c.space());
ar & make_nvp("vector", c.vector());
hpp::serialization::remove_duplicate::save_vector(ar, "vector", c.vector(), version);
}
} // namespace serialization
} // namespace boost
......
......@@ -27,6 +27,7 @@
#include <hpp/pinocchio/liegroup-element.hh>
#include <hpp/pinocchio/liegroup/serialization.hh>
#include <hpp/pinocchio/serialization.hh>
#include "../src/comparison.hh"
#include "../src/size-visitor.hh"
......@@ -477,7 +478,7 @@ namespace hpp {
ar & BOOST_SERIALIZATION_NVP(liegroupTypes_);
ar & BOOST_SERIALIZATION_NVP(nq_);
ar & BOOST_SERIALIZATION_NVP(nv_);
ar & BOOST_SERIALIZATION_NVP(neutral_);
serialization::remove_duplicate::serialize_vector(ar, "neutral_", neutral_, version);
ar & BOOST_SERIALIZATION_NVP(weak_);
}
......
......@@ -28,6 +28,7 @@
#include <boost/test/unit_test.hpp>
#include <boost/assign/list_of.hpp>
#include <hpp/pinocchio/liegroup-element.hh>
#include <hpp/pinocchio/serialization.hh>
using boost::assign::list_of;
using hpp::pinocchio::size_type;
......@@ -291,16 +292,25 @@ BOOST_AUTO_TEST_CASE (log_)
BOOST_CHECK ((hpp::pinocchio::log(e).isZero(1e-10)));
}
template<typename base_archive = boost::archive::xml_oarchive>
struct oarchive :
base_archive, hpp::serialization::remove_duplicate::vector_archive
{
oarchive(std::ostream& is) : base_archive (is) {}
};
void test_serialization(LiegroupSpacePtr_t space)
{
LiegroupElement e (space); e.setNeutral ();
std::stringstream ss;
{
boost::archive::xml_oarchive oa(ss);
oarchive<boost::archive::xml_oarchive> oa(ss);
oa << boost::serialization::make_nvp("element", e);
}
BOOST_TEST_MESSAGE(ss.str());
LiegroupElement e2;
{
boost::archive::xml_iarchive ia(ss);
......
......@@ -73,8 +73,8 @@ void check_remove_duplicate_impl ()
vector_t a_r, b_r;
{
boost::archive::xml_iarchive ia(ss);
hpp::serialization::remove_duplicate::serialize_vector(ia, "a", a, 0);
hpp::serialization::remove_duplicate::serialize_vector(ia, "b", b, 0);
hpp::serialization::remove_duplicate::serialize_vector(ia, "a", a_r, 0);
hpp::serialization::remove_duplicate::serialize_vector(ia, "b", b_r, 0);
}
BOOST_CHECK_EQUAL(a, a_r);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment