LCOV - code coverage report
Current view: top level - tests - serialize.h (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 43 44 97.7 %
Date: 2016-03-29 17:09:06 Functions: 17 17 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2012, Daniel Nachbaur <danielnachbaur@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 2.1 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_SERIALIZE_H
      19             : #define LUNCHBOX_SERIALIZE_H
      20             : 
      21             : #include "test.h"
      22             : 
      23             : #include <lunchbox/anySerialization.h>
      24             : 
      25             : #include <sstream>
      26             : 
      27             : #include <boost/archive/text_oarchive.hpp>
      28             : #include <boost/archive/text_iarchive.hpp>
      29             : 
      30             : #pragma warning( push )
      31             : #pragma warning( disable: 4996 )
      32             : #include <boost/archive/binary_oarchive.hpp>
      33             : #include <boost/archive/binary_iarchive.hpp>
      34             : #pragma warning( pop )
      35             : 
      36             : 
      37             : template< class T >
      38          14 : void textSave( const T& object, std::ostream& os )
      39             : {
      40          14 :     lunchbox::saveAny< boost::archive::text_oarchive >( object, os );
      41          14 : }
      42             : 
      43             : template< class T >
      44          14 : void textLoad( T& object, std::istream& is )
      45             : {
      46          14 :     lunchbox::loadAny< boost::archive::text_iarchive >( object, is );
      47          14 : }
      48             : 
      49             : template< class T >
      50          14 : void binarySave( const T& object, std::ostream& os )
      51             : {
      52          14 :     lunchbox::saveAny< boost::archive::binary_oarchive >( object, os );
      53          14 : }
      54             : 
      55             : template< class T >
      56          14 : void binaryLoad( T& object, std::istream& is )
      57             : {
      58          14 :     lunchbox::loadAny< boost::archive::binary_iarchive >( object, is );
      59          14 : }
      60             : 
      61             : template< class T >
      62          14 : void textSerialize( const T& object, T& loadedObject )
      63             : {
      64          14 :     std::stringstream stream;
      65          14 :     textSave( object, stream );
      66          14 :     textLoad( loadedObject, stream );
      67          14 : }
      68             : 
      69             : template< class T >
      70          14 : void textSerializeAndTest( const T& object )
      71             : {
      72          14 :     T loadedObject;
      73          14 :     textSerialize( object, loadedObject );
      74          14 :     TEST( object == loadedObject );
      75          14 : }
      76             : 
      77             : template< class T >
      78          14 : void binarySerialize( const T& object, T& loadedObject )
      79             : {
      80          14 :     std::stringstream stream;
      81          14 :     binarySave( object, stream );
      82          14 :     binaryLoad( loadedObject, stream );
      83          14 : }
      84             : 
      85             : template< class T >
      86          14 : void binarySerializeAndTest( const T& object )
      87             : {
      88          14 :     T loadedObject;
      89          14 :     binarySerialize( object, loadedObject );
      90          14 :     TEST( object == loadedObject );
      91          14 : }
      92             : 
      93             : 
      94           9 : struct Foo
      95             : {
      96           2 :     bool operator == ( const Foo& rhs ) const
      97             :     {
      98           2 :         if( this == &rhs )
      99           0 :             return true;
     100             : 
     101           2 :         return i == rhs.i && f == rhs.f && b == rhs.b && s == rhs.s;
     102             :     }
     103             : 
     104             :     bool operator != ( const Foo& rhs ) const
     105             :     {
     106             :         return !( *this == rhs );
     107             :     }
     108             : 
     109             :     int i;
     110             :     float f;
     111             :     bool b;
     112             :     std::string s;
     113             : 
     114             :     template< class Archive >
     115           4 :     void serialize( Archive & ar, const unsigned int /*version*/ )
     116             :     {
     117           4 :         ar & i;
     118           4 :         ar & f;
     119           4 :         ar & b;
     120           4 :         ar & s;
     121           4 :     }
     122             : };
     123             : 
     124           2 : SERIALIZABLEANY( Foo )
     125             : 
     126             : 
     127             : #endif

Generated by: LCOV version 1.11