Commit 65df636b authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

Add serialization macros.

parent 8ec6b6b9
......@@ -29,7 +29,7 @@ COMPUTE_PROJECT_ARGS(PROJECT_ARGS LANGUAGES CXX C)
PROJECT(${PROJECT_NAME} ${PROJECT_ARGS})
# Search for Boost.
SET(BOOST_COMPONENTS filesystem system)
SET(BOOST_COMPONENTS filesystem system serialization)
SEARCH_FOR_BOOST()
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/find-external/TinyXML)
......@@ -49,6 +49,8 @@ SET(${PROJECT_NAME}_HEADERS
include/hpp/util/parser.hh
include/hpp/util/factories/ignoretag.hh
include/hpp/util/factories/sequence.hh
include/hpp/util/serialization.hh
include/hpp/util/serialization-fwd.hh
)
SET(${PROJECT_NAME}_SOURCES
......
//
// Copyright (c) 2020 CNRS
// Authors: Joseph Mirabel
//
// 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/>.
#ifndef HPP_UTIL_SERIALIZATION_FWD_HH
#define HPP_UTIL_SERIALIZATION_FWD_HH
#include <boost/serialization/split_member.hpp>
namespace boost {
namespace serialization {
class access;
} // namespace serialization
} // namespace boost
/// Macro that should be put in a serializable class
/// It declares function \c serialize.
/// \note an empty constructor (that can leave the object unitialized) must exist.
#define HPP_SERIALIZABLE() \
friend class boost::serialization::access; \
/** Allow serialization of the class using \c boost::serialization. */ \
template<class Archive> \
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.
#define HPP_SERIALIZABLE_SPLIT() \
BOOST_SERIALIZATION_SPLIT_MEMBER() \
friend class boost::serialization::access; \
/** Allow to save class using \c boost::serialization. */ \
template<class Archive> \
void save(Archive & ar, const unsigned int version) const; \
/** Allow to load class using \c boost::serialization. */ \
template<class Archive> \
void load(Archive & ar, const unsigned int version)
#endif // HPP_UTIL_SERIALIZATION_FWD_HH
//
// Copyright (c) 2020 CNRS
// Authors: Joseph Mirabel
//
// 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/>.
#ifndef HPP_UTIL_SERIALIZATION_HH
#define HPP_UTIL_SERIALIZATION_HH
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/polymorphic_oarchive.hpp>
#include <boost/archive/polymorphic_iarchive.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_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_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)
#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)
namespace hpp {
namespace serialization {
using boost::serialization::make_nvp;
} // namespace util
} // namespace hpp
#endif // HPP_UTIL_SERIALIZATION_HH
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