LCOV - code coverage report
Current view: top level - lunchbox - anySerialization.h (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 17 17 100.0 %
Date: 2014-10-01 Functions: 64 64 100.0 %

          Line data    Source code
       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 <lunchbox/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             : 
      32             : /**
      33             :  * Declares the given class to be serializable within a lunchbox::Any.
      34             :  * User is supposed to use this macro on global scope and in the compilation
      35             :  * unit where this class is to be serialized.
      36             :  */
      37             : #define SERIALIZABLEANY( CLASS ) \
      38             :     BOOST_CLASS_EXPORT( lunchbox::Any::holder< CLASS > )
      39             : 
      40             : namespace lunchbox
      41             : {
      42             : 
      43             : /** List of supported POD types for lunchbox::Any serialization. */
      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, uint128_t > podTypes;
      50             : 
      51             : /** @cond IGNORE */
      52             : /**
      53             :  * @internal
      54             :  * Utility struct for registering types for lunchbox::Any from a type list.
      55             :  */
      56             : template< class Archive >
      57             : struct registerWrapper
      58             : {
      59          56 :     explicit registerWrapper( Archive& ar ) : ar_( ar ) {}
      60             :     Archive& ar_;
      61             : 
      62             :     template< typename T >
      63         728 :     void operator()( T )
      64             :     {
      65         728 :         ar_.template register_type< Any::holder< T > >();
      66         728 :     }
      67             : };
      68             : /** @endcond */
      69             : 
      70             : /**
      71             :  * Registers the types from the given type list for serializing it inside a
      72             :  * lunchbox::Any through the given archive.
      73             :  */
      74             : template< class TypeList, class Archive >
      75          56 : void registerTypelist( Archive& ar )
      76             : {
      77          56 :     boost::mpl::for_each< TypeList >( registerWrapper< Archive >( ar ) );
      78          56 : }
      79             : 
      80             : /**
      81             :  * Serializes the given object which can be a lunchbox::Any through the given
      82             :  * archive type to/from the given stream.
      83             :  */
      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             : 
      92             : /**
      93             :  * Saves the given object which can be a lunchbox::Any through the given archive
      94             :  * type to/from the given stream.
      95             :  */
      96             : template< class Archive, class Object, class Stream >
      97          28 : void saveAny( Object& object, Stream& stream )
      98             : {
      99          28 :     Archive ar( stream );
     100          28 :     registerTypelist< podTypes >( ar );
     101          28 :     ar << object;
     102          28 : }
     103             : 
     104             : /**
     105             :  * Loads the given object which can be a lunchbox::Any through the given archive
     106             :  * type to/from the given stream.
     107             :  */
     108             : template< class Archive, class Object, class Stream >
     109          28 : void loadAny( Object& object, Stream& stream )
     110             : {
     111          28 :     Archive ar( stream );
     112          28 :     registerTypelist< podTypes >( ar );
     113          28 :     ar >> object;
     114          28 : }
     115             : 
     116             : }
     117             : 
     118             : #endif

Generated by: LCOV version 1.10