Lunchbox  1.4.0
anySerialization.h
00001 
00002 /* Copyright (c) 2012, Daniel Nachbaur <daniel.nachbaur@gmail.com>
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 3.0 as published
00006  * by the Free Software Foundation.
00007  *
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef LUNCHBOX_ANYSERIALIZATION_H
00019 #define LUNCHBOX_ANYSERIALIZATION_H
00020 
00021 #include <lunchbox/any.h>
00022 #include <lunchbox/types.h>
00023 #include <lunchbox/uint128_t.h>
00024 
00025 #include <boost/mpl/list.hpp>
00026 #include <boost/mpl/for_each.hpp>
00027 
00028 #include <boost/serialization/export.hpp>
00029 #include <boost/serialization/extended_type_info.hpp>
00030 
00031 
00037 #define SERIALIZABLEANY( CLASS ) \
00038     BOOST_CLASS_EXPORT( lunchbox::Any::holder< CLASS > )
00039 
00045 #define ANYARCHIVE( ar )   \
00046     lunchbox::registerAnyPodTypes( ar );
00047 
00048 
00049 namespace lunchbox
00050 {
00051 
00053 typedef boost::mpl::list< int8_t, uint8_t,
00054                           int16_t, uint16_t,
00055                           int32_t, uint32_t,
00056                           int64_t, uint64_t,
00057                           float, double,
00058                           bool, std::string, uint128_t > podTypes;
00059 
00064 template< class T, class Archive >
00065 void registerAnyType( Archive& ar )
00066 {
00067     ar.template register_type< Any::holder< T > >();
00068 }
00069 
00074 template< class Archive >
00075 struct registerWrapper
00076 {
00077     registerWrapper( Archive& ar ) : ar_( ar ) {}
00078     Archive& ar_;
00079 
00080     template< typename U >
00081     void operator()( U )
00082     {
00083         registerAnyType< U >( ar_ );
00084     }
00085 };
00086 
00091 template< class Archive, class TypeList >
00092 void registerAnyTypes( Archive& ar )
00093 {
00094     boost::mpl::for_each< TypeList >( registerWrapper< Archive >( ar ) );
00095 }
00096 
00101 template< class Archive >
00102 void registerAnyPodTypes( Archive& ar )
00103 {
00104     registerAnyTypes< Archive, podTypes >( ar );
00105 }
00106 
00111 template< class Archive, class Object, class Stream >
00112 void serializeAny( Object& object, Stream& stream )
00113 {
00114     Archive ar( stream );
00115     ANYARCHIVE( ar );
00116     ar & object;
00117 }
00118 
00119 }
00120 
00121 #endif