Lunchbox  1.6.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 
00040 
00041 namespace lunchbox
00042 {
00043 
00045 typedef boost::mpl::list< int8_t, uint8_t,
00046                           int16_t, uint16_t,
00047                           int32_t, uint32_t,
00048                           int64_t, uint64_t,
00049                           float, double,
00050                           bool, std::string, uint128_t > podTypes;
00051 
00056 template< class Archive >
00057 struct registerWrapper
00058 {
00059     registerWrapper( Archive& ar ) : ar_( ar ) {}
00060     Archive& ar_;
00061 
00062     template< typename T >
00063     void operator()( T )
00064     {
00065         ar_.template register_type< Any::holder< T > >();
00066     }
00067 };
00068 
00073 template< class TypeList, class Archive >
00074 void registerTypelist( Archive& ar )
00075 {
00076     boost::mpl::for_each< TypeList >( registerWrapper< Archive >( ar ) );
00077 }
00078 
00083 template< class Archive, class Object, class Stream >
00084 void serializeAny( Object& object, Stream& stream )
00085 {
00086     Archive ar( stream );
00087     registerTypelist< podTypes >( ar );
00088     ar & object;
00089 }
00090 
00095 template< class Archive, class Object, class Stream >
00096 void saveAny( Object& object, Stream& stream )
00097 {
00098     Archive ar( stream );
00099     registerTypelist< podTypes >( ar );
00100     ar << object;
00101 }
00102 
00107 template< class Archive, class Object, class Stream >
00108 void loadAny( Object& object, Stream& stream )
00109 {
00110     Archive ar( stream );
00111     registerTypelist< podTypes >( ar );
00112     ar >> object;
00113 }
00114 
00115 }
00116 
00117 #endif