Lunchbox  1.16.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/for_each.hpp>
26 #include <boost/mpl/list.hpp>
27 
28 #include <boost/serialization/export.hpp>
29 #include <boost/serialization/extended_type_info.hpp>
30 
36 #define SERIALIZABLEANY(CLASS) BOOST_CLASS_EXPORT(lunchbox::Any::holder<CLASS>)
37 
38 namespace lunchbox
39 {
41 typedef boost::mpl::list<int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t,
42  int64_t, uint64_t, float, double, bool, std::string,
43  servus::uint128_t>
45 
51 template <class Archive>
52 struct registerWrapper
53 {
54  explicit registerWrapper(Archive& ar)
55  : ar_(ar)
56  {
57  }
58  Archive& ar_;
59 
60  template <typename T>
61  void operator()(T)
62  {
63  ar_.template register_type<Any::holder<T> >();
64  }
65 };
72 template <class TypeList, class Archive>
73 void registerTypelist(Archive& ar)
74 {
75  boost::mpl::for_each<TypeList>(registerWrapper<Archive>(ar));
76 }
77 
82 template <class Archive, class Object, class Stream>
83 void serializeAny(Object& object, Stream& stream)
84 {
85  Archive ar(stream);
86  registerTypelist<podTypes>(ar);
87  ar& object;
88 }
89 
94 template <class Archive, class Object, class Stream>
95 void saveAny(Object& object, Stream& stream)
96 {
97  Archive ar(stream);
98  registerTypelist<podTypes>(ar);
99  ar << object;
100 }
101 
106 template <class Archive, class Object, class Stream>
107 void loadAny(Object& object, Stream& stream)
108 {
109  Archive ar(stream);
110  registerTypelist<podTypes>(ar);
111  ar >> object;
112 }
113 }
114 
115 #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:29
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 ...