Unverified Commit deb7f602 authored by Florent Lamiraux's avatar Florent Lamiraux Committed by GitHub
Browse files

Merge pull request #22 from jmirabel/serialization

Serialization
parents 5e73cf2b 96eecf4b
......@@ -20,6 +20,7 @@
#define HPP_UTIL_SERIALIZATION_FWD_HH
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/split_free.hpp>
namespace boost {
namespace serialization {
......@@ -37,9 +38,9 @@ class access;
void serialize(Archive & ar, const unsigned int version)
/// Macro that should be put in a serializable class
/// It declare a \c load and \c save function.
/// \note for the load function, an empty constructor (that can leave the object
/// unitialized) must exist.
/// It declares functions \c serialize, \c load and \c save. The two last
/// must be implemented by the caller.
/// \note an empty constructor (that can leave the object unitialized) must exist.
#define HPP_SERIALIZABLE_SPLIT() \
BOOST_SERIALIZATION_SPLIT_MEMBER() \
friend class boost::serialization::access; \
......@@ -50,4 +51,23 @@ class access;
template<class Archive> \
void load(Archive & ar, const unsigned int version)
/// Macro that should be put in a serializable class, using a free function
/// It declares free function \c serialize.
/// This macro must be called outside of any namespace.
/// \note an empty constructor (that can leave the object unitialized) must exist.
#define HPP_SERIALIZABLE_FREE(type) \
namespace boost { namespace serialization { \
template<class Archive> \
void serialize(Archive & ar, type& t, const unsigned int version); \
}}
/// Macro that should be put in a serializable class, using a free function
/// It declares free functions \c serialize, \c load and \c save. The two last
/// must be implemented by the caller.
/// This macro must be called outside of any namespace.
/// \note an empty constructor (that can leave the object unitialized) must exist.
#define HPP_SERIALIZABLE_SPLIT_FREE(type) \
HPP_SERIALIZABLE_FREE(type)
#endif // HPP_UTIL_SERIALIZATION_FWD_HH
......@@ -28,29 +28,59 @@
#include <boost/archive/polymorphic_oarchive.hpp>
#include <boost/archive/polymorphic_iarchive.hpp>
#include <boost/preprocessor/comma_if.hpp>
#include <boost/preprocessor/facilities/is_empty.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/nvp.hpp>
#define _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,archive) \
template void type::load<archive##_iarchive>(archive##_iarchive& ar, const unsigned int ver); \
template void type::save<archive##_oarchive>(archive##_oarchive& ar, const unsigned int ver) const
#define _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,archive,arg) \
template void type load<archive##_iarchive>(archive##_iarchive& ar, \
arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
const unsigned int ver); \
template void type save<archive##_oarchive>(archive##_oarchive& ar, \
BOOST_PP_IF(BOOST_PP_IS_EMPTY(arg),,const) arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
const unsigned int ver) BOOST_PP_IF(BOOST_PP_IS_EMPTY(arg),const,)
#define _HPP_SERIALIZATION_IMPLEMENT(type,archive) \
template void type::serialize<archive##_iarchive>(archive##_iarchive& ar, const unsigned int ver);\
template void type::serialize<archive##_oarchive>(archive##_oarchive& ar, const unsigned int ver)\
#define _HPP_SERIALIZATION_IMPLEMENT(type,archive,arg) \
template void type serialize<archive##_iarchive>(archive##_iarchive& ar, \
arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
const unsigned int ver); \
template void type serialize<archive##_oarchive>(archive##_oarchive& ar, \
arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
const unsigned int ver)
#define HPP_SERIALIZATION_SPLIT_IMPLEMENT(type) \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,boost::archive::polymorphic); \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,boost::archive::xml); \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,boost::archive::text); \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,boost::archive::binary)
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::polymorphic,); \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::xml,); \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::text,); \
_HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::binary,)
#define HPP_SERIALIZATION_IMPLEMENT(type) \
_HPP_SERIALIZATION_IMPLEMENT(type,boost::archive::polymorphic); \
_HPP_SERIALIZATION_IMPLEMENT(type,boost::archive::xml); \
_HPP_SERIALIZATION_IMPLEMENT(type,boost::archive::text); \
_HPP_SERIALIZATION_IMPLEMENT(type,boost::archive::binary)
_HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::polymorphic,); \
_HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::xml,); \
_HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::text,); \
_HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::binary,)
#define HPP_SERIALIZATION_FREE_IMPLEMENT(type) \
namespace boost { namespace serialization { \
_HPP_SERIALIZATION_IMPLEMENT(,archive::polymorphic,type& t); \
_HPP_SERIALIZATION_IMPLEMENT(,archive::xml,type& t); \
_HPP_SERIALIZATION_IMPLEMENT(,archive::text,type& t); \
_HPP_SERIALIZATION_IMPLEMENT(,archive::binary,type& t); \
}}
#define HPP_SERIALIZATION_SPLIT_FREE_IMPLEMENT(type) \
namespace boost { namespace serialization { \
template<class Archive> \
void serialize(Archive & ar, type& t, const unsigned int version) { \
split_free(ar, t, version); \
} \
_HPP_SERIALIZATION_IMPLEMENT(,archive::polymorphic,type& t); \
_HPP_SERIALIZATION_IMPLEMENT(,archive::xml,type& t); \
_HPP_SERIALIZATION_IMPLEMENT(,archive::text,type& t); \
_HPP_SERIALIZATION_IMPLEMENT(,archive::binary,type& t); \
}}
namespace hpp {
namespace serialization {
......
......@@ -31,6 +31,10 @@ DEFINE_TEST(exception)
DEFINE_TEST(exception-factory)
DEFINE_TEST(timer)
ADD_UNIT_TEST(serialization serialization.cc serialization-test.cc)
TARGET_INCLUDE_DIRECTORIES(serialization PRIVATE ${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(serialization ${PROJECT_NAME})
SET(HPP_LOGGINGDIR "${CMAKE_CURRENT_BINARY_DIR}")
CONFIG_FILES(run_debug.sh)
ADD_EXECUTABLE(debug debug.cc)
......
// Copyright (c) 2020, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
//
// This file is part of hpp-util.
// hpp-util is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-util is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-util. If not, see <http://www.gnu.org/licenses/>.
#include <sstream>
#include "config.h"
#include "common.hh"
#include "serialization.hh"
#include <boost/archive/polymorphic_xml_iarchive.hpp>
#include <boost/archive/polymorphic_xml_oarchive.hpp>
template<typename T>
int run_test_tpl ()
{
std::stringstream ss;
T t(10);
{
boost::archive::polymorphic_xml_oarchive oa(ss);
oa << boost::serialization::make_nvp("t", t);
}
Foo* t_r = NULL;
{
boost::archive::polymorphic_xml_iarchive ia(ss);
ia >> boost::serialization::make_nvp("t", t_r);
}
if (t_r == NULL || t_r->i_ != t.i_) return TEST_FAILED;
return TEST_SUCCEED;
}
int run_test ()
{
if (run_test_tpl<Foo >() == TEST_FAILED) return TEST_FAILED;
if (run_test_tpl<Bar >() == TEST_FAILED) return TEST_FAILED;
if (run_test_tpl<FooFree>() == TEST_FAILED) return TEST_FAILED;
if (run_test_tpl<BarFree>() == TEST_FAILED) return TEST_FAILED;
return TEST_SUCCEED;
}
GENERATE_TEST ()
// Copyright (c) 2020, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
//
// This file is part of hpp-util.
// hpp-util is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-util is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-util. If not, see <http://www.gnu.org/licenses/>.
#include <hpp/util/serialization.hh>
#include "serialization.hh"
BOOST_CLASS_EXPORT(Foo)
template<class Archive>
void Foo::serialize(Archive & ar, const unsigned int version)
{
(void) version;
ar & hpp::serialization::make_nvp("i", i_);
}
HPP_SERIALIZATION_IMPLEMENT(Foo);
BOOST_CLASS_EXPORT(Bar)
template<class Archive>
void Bar::load(Archive & ar, const unsigned int version)
{
(void) version;
ar & hpp::serialization::make_nvp("i", i_);
}
template<class Archive>
void Bar::save(Archive & ar, const unsigned int version) const
{
(void) version;
ar & hpp::serialization::make_nvp("i", i_);
}
HPP_SERIALIZATION_SPLIT_IMPLEMENT(Bar);
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, FooFree& foo, const unsigned int version)
{
(void) version;
ar & hpp::serialization::make_nvp("i", foo.i_);
}
template<class Archive>
void load(Archive & ar, BarFree& bar, const unsigned int version)
{
(void) version;
ar & hpp::serialization::make_nvp("i", bar.i_);
}
template<class Archive>
void save(Archive & ar, const BarFree& bar, const unsigned int version)
{
(void) version;
ar & hpp::serialization::make_nvp("i", bar.i_);
}
}
}
HPP_SERIALIZATION_FREE_IMPLEMENT(FooFree)
HPP_SERIALIZATION_SPLIT_FREE_IMPLEMENT(BarFree)
// Copyright (c) 2020, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
//
// This file is part of hpp-util.
// hpp-util is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-util is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-util. If not, see <http://www.gnu.org/licenses/>.
#include <hpp/util/serialization-fwd.hh>
class Foo {
public:
Foo(int i) : i_ (i) {};
int i_;
private:
Foo() {}
HPP_SERIALIZABLE();
};
class Bar {
public:
Bar(int i) : i_ (i) {};
int i_;
private:
Bar() {}
HPP_SERIALIZABLE_SPLIT();
};
class FooFree {
public:
FooFree(int i) : i_ (i) {};
int i_;
private:
FooFree() {}
};
HPP_SERIALIZABLE_FREE(FooFree)
class BarFree {
public:
BarFree(int i) : i_ (i) {};
int i_;
private:
BarFree() {}
};
HPP_SERIALIZABLE_SPLIT_FREE(BarFree)
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