hpp-util  4.10.1
Debugging tools for the HPP project.
serialization.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 CNRS
3 // Authors: Joseph Mirabel
4 //
5 // This file is part of hpp-util
6 // hpp-util is free software: you can redistribute it
7 // and/or modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation, either version
9 // 3 of the License, or (at your option) any later version.
10 //
11 // hpp-util is distributed in the hope that it will be
12 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Lesser Public License for more details. You should have
15 // received a copy of the GNU Lesser General Public License along with
16 // hpp-util If not, see
17 // <http://www.gnu.org/licenses/>.
18 
19 #ifndef HPP_UTIL_SERIALIZATION_HH
20 #define HPP_UTIL_SERIALIZATION_HH
21 
22 #include <boost/version.hpp>
23 #include <boost/archive/binary_oarchive.hpp>
24 #include <boost/archive/text_oarchive.hpp>
25 #include <boost/archive/xml_oarchive.hpp>
26 #include <boost/archive/binary_iarchive.hpp>
27 #include <boost/archive/text_iarchive.hpp>
28 #include <boost/archive/xml_iarchive.hpp>
29 
30 // Old version of boost do not support well serialization.
31 #if BOOST_VERSION >= 106500
32 #include <boost/archive/polymorphic_oarchive.hpp>
33 #include <boost/archive/polymorphic_iarchive.hpp>
34 #endif
35 
36 #include <boost/preprocessor/comma_if.hpp>
37 #include <boost/preprocessor/facilities/is_empty.hpp>
38 #include <boost/serialization/export.hpp>
39 #include <boost/serialization/shared_ptr.hpp>
40 #include <boost/serialization/nvp.hpp>
41 
42 #define _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type,archive,arg) \
43  template void type load<archive##_iarchive>(archive##_iarchive& ar, \
44  arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
45  const unsigned int ver); \
46  template void type save<archive##_oarchive>(archive##_oarchive& ar, \
47  BOOST_PP_IF(BOOST_PP_IS_EMPTY(arg),,const) arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
48  const unsigned int ver) BOOST_PP_IF(BOOST_PP_IS_EMPTY(arg),const,)
49 
50 #define _HPP_SERIALIZATION_IMPLEMENT(type,archive,arg) \
51  template void type serialize<archive##_iarchive>(archive##_iarchive& ar, \
52  arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
53  const unsigned int ver); \
54  template void type serialize<archive##_oarchive>(archive##_oarchive& ar, \
55  arg BOOST_PP_COMMA_IF(BOOST_PP_NOT(BOOST_PP_IS_EMPTY(arg))) \
56  const unsigned int ver)
57 
58 #if BOOST_VERSION >= 106500
59 #define HPP_SERIALIZATION_SPLIT_IMPLEMENT(type) \
60  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::polymorphic,); \
61  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::xml,); \
62  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::text,); \
63  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::binary,)
64 
65 #define HPP_SERIALIZATION_IMPLEMENT(type) \
66  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::polymorphic,); \
67  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::xml,); \
68  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::text,); \
69  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::binary,)
70 
71 #define HPP_SERIALIZATION_FREE_IMPLEMENT(type) \
72  namespace boost { namespace serialization { \
73  _HPP_SERIALIZATION_IMPLEMENT(,archive::polymorphic,type& t); \
74  _HPP_SERIALIZATION_IMPLEMENT(,archive::xml,type& t); \
75  _HPP_SERIALIZATION_IMPLEMENT(,archive::text,type& t); \
76  _HPP_SERIALIZATION_IMPLEMENT(,archive::binary,type& t); \
77  }}
78 
79 #define HPP_SERIALIZATION_SPLIT_FREE_IMPLEMENT(type) \
80  namespace boost { namespace serialization { \
81  template<class Archive> \
82  void serialize(Archive & ar, type& t, const unsigned int version) { \
83  split_free(ar, t, version); \
84  } \
85  _HPP_SERIALIZATION_IMPLEMENT(,archive::polymorphic,type& t); \
86  _HPP_SERIALIZATION_IMPLEMENT(,archive::xml,type& t); \
87  _HPP_SERIALIZATION_IMPLEMENT(,archive::text,type& t); \
88  _HPP_SERIALIZATION_IMPLEMENT(,archive::binary,type& t); \
89  }}
90 
91 #else // BOOST_VERSION >= 106500
92 
93 #define HPP_SERIALIZATION_SPLIT_IMPLEMENT(type) \
94  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::xml,); \
95  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::text,); \
96  _HPP_SERIALIZATION_SPLIT_IMPLEMENT(type::,boost::archive::binary,)
97 
98 #define HPP_SERIALIZATION_IMPLEMENT(type) \
99  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::xml,); \
100  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::text,); \
101  _HPP_SERIALIZATION_IMPLEMENT(type::,boost::archive::binary,)
102 
103 #define HPP_SERIALIZATION_FREE_IMPLEMENT(type) \
104  namespace boost { namespace serialization { \
105  _HPP_SERIALIZATION_IMPLEMENT(,archive::xml,type& t); \
106  _HPP_SERIALIZATION_IMPLEMENT(,archive::text,type& t); \
107  _HPP_SERIALIZATION_IMPLEMENT(,archive::binary,type& t); \
108  }}
109 
110 #define HPP_SERIALIZATION_SPLIT_FREE_IMPLEMENT(type) \
111  namespace boost { namespace serialization { \
112  template<class Archive> \
113  void serialize(Archive & ar, type& t, const unsigned int version) { \
114  split_free(ar, t, version); \
115  } \
116  _HPP_SERIALIZATION_IMPLEMENT(,archive::xml,type& t); \
117  _HPP_SERIALIZATION_IMPLEMENT(,archive::text,type& t); \
118  _HPP_SERIALIZATION_IMPLEMENT(,archive::binary,type& t); \
119  }}
120 
121 #endif // BOOST_VERSION >= 106500
122 
123 namespace hpp {
124 namespace serialization {
125 using boost::serialization::make_nvp;
126 } // namespace util
127 } // namespace hpp
128 
129 #endif // HPP_UTIL_SERIALIZATION_HH
Definition: assertion.hh:24