Lunchbox  1.14.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
anySerialization.h
1 
2 /* Copyright (c) 2012, Daniel Nachbaur <daniel.nachbaur@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 3.0 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_ANYSERIALIZATION_H
19 #define LUNCHBOX_ANYSERIALIZATION_H
20 
21 #include <lunchbox/any.h>
22 #include <lunchbox/types.h>
23 #include <servus/uint128_t.h>
24 
25 #include <boost/mpl/list.hpp>
26 #include <boost/mpl/for_each.hpp>
27 
28 #include <boost/serialization/export.hpp>
29 #include <boost/serialization/extended_type_info.hpp>
30 
31 
37 #define SERIALIZABLEANY( CLASS ) \
38  BOOST_CLASS_EXPORT( lunchbox::Any::holder< CLASS > )
39 
40 namespace lunchbox
41 {
42 
44 typedef boost::mpl::list< int8_t, uint8_t,
45  int16_t, uint16_t,
46  int32_t, uint32_t,
47  int64_t, uint64_t,
48  float, double,
49  bool, std::string, servus::uint128_t > podTypes;
50 
56 template< class Archive >
57 struct registerWrapper
58 {
59  explicit registerWrapper( Archive& ar ) : ar_( ar ) {}
60  Archive& ar_;
61 
62  template< typename T >
63  void operator()( T )
64  {
65  ar_.template register_type< Any::holder< T > >();
66  }
67 };
74 template< class TypeList, class Archive >
75 void registerTypelist( Archive& ar )
76 {
77  boost::mpl::for_each< TypeList >( registerWrapper< Archive >( ar ) );
78 }
79 
84 template< class Archive, class Object, class Stream >
85 void serializeAny( Object& object, Stream& stream )
86 {
87  Archive ar( stream );
88  registerTypelist< podTypes >( ar );
89  ar & object;
90 }
91 
96 template< class Archive, class Object, class Stream >
97 void saveAny( Object& object, Stream& stream )
98 {
99  Archive ar( stream );
100  registerTypelist< podTypes >( ar );
101  ar << object;
102 }
103 
108 template< class Archive, class Object, class Stream >
109 void loadAny( Object& object, Stream& stream )
110 {
111  Archive ar( stream );
112  registerTypelist< podTypes >( ar );
113  ar >> object;
114 }
115 
116 }
117 
118 #endif
void registerTypelist(Archive &ar)
Registers the types from the given type list for serializing it inside a lunchbox::Any through the gi...
Basic type definitions not provided by the operating system.
boost::mpl::list< int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, double, bool, std::string, servus::uint128_t > podTypes
List of supported POD types for lunchbox::Any serialization.
void serializeAny(Object &object, Stream &stream)
Serializes the given object which can be a lunchbox::Any through the given archive type to/from the g...
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
void saveAny(Object &object, Stream &stream)
Saves the given object which can be a lunchbox::Any through the given archive type to/from the given ...
void loadAny(Object &object, Stream &stream)
Loads the given object which can be a lunchbox::Any through the given archive type to/from the given ...