Wiselib
wiselib.stable/util/serialization/serialization.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef __WISELIB_UTIL_SERIALIZATION_SERIALIZATION_H
00020 #define __WISELIB_UTIL_SERIALIZATION_SERIALIZATION_H
00021 
00022 #include "util/serialization/endian.h"
00023 
00024 namespace wiselib
00025 {
00026 
00030    template <typename OsModel_P,
00031              Endianness,
00032              typename BlockData_P,
00033              typename Type_P>
00034    struct Serialization
00035    {
00036       typedef OsModel_P OsModel;
00037       typedef BlockData_P BlockData;
00038       typedef Type_P Type;
00039 
00040       typedef typename OsModel::size_t size_t;
00041       // --------------------------------------------------------------------
00042       static inline size_t write( BlockData *target, Type& value )
00043       {
00044          for ( unsigned int i = 0; i < sizeof(Type); i++ )
00045             target[sizeof(Type) - 1 - i] = *((BlockData*)&value + i);
00046          return sizeof(Type);
00047       }
00048       // --------------------------------------------------------------------
00049       static Type read( BlockData *target )
00050       {
00051          Type value;
00052          for ( unsigned int i = 0; i < sizeof(Type); i++ )
00053             *((BlockData*)&value + i) = target[sizeof(Type) - 1 - i];
00054          return value;
00055       }
00056 
00057    };
00058    // -----------------------------------------------------------------------
00062    template <typename OsModel_P,
00063              typename BlockData_P,
00064              typename Type_P>
00065    struct Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, Type_P>
00066    {
00067       typedef OsModel_P OsModel;
00068       typedef BlockData_P BlockData;
00069       typedef Type_P Type;
00070 
00071       typedef typename OsModel::size_t size_t;
00072       // --------------------------------------------------------------------
00073       static inline size_t write( BlockData *target, Type& value )
00074       {
00075          for ( unsigned int i = 0; i < sizeof(Type); i++ )
00076             target[i] = *((BlockData*)&value + i);
00077          return sizeof(Type);
00078       }
00079       // --------------------------------------------------------------------
00080       static Type read( BlockData *target )
00081       {
00082          Type value;
00083          for ( unsigned int i = 0; i < sizeof(Type); i++ )
00084             *((BlockData*)&value + i) = target[i];
00085          return value;
00086       }
00087 
00088    };
00089    // -----------------------------------------------------------------------
00090    // -----------------------------------------------------------------------
00091    // -----------------------------------------------------------------------
00092    template<typename OsModel_P,
00093             typename BlockData_P,
00094             typename Type_P>
00095    inline Type_P read( BlockData_P *target )
00096    {
00097       return Serialization<OsModel_P, OsModel_P::endianness, BlockData_P, Type_P>::read( target );
00098    }
00099    // -----------------------------------------------------------------------
00100    template<typename OsModel_P,
00101             typename BlockData_P,
00102             typename Type_P>
00103    inline void read( BlockData_P *target, Type_P& value )
00104    {
00105       value = Serialization<OsModel_P, OsModel_P::endianness, BlockData_P, Type_P>::read( target );
00106    }
00107    // -----------------------------------------------------------------------
00108    template<typename OsModel_P,
00109             typename BlockData_P,
00110             typename Type_P>
00111    inline typename OsModel_P::size_t write( BlockData_P *target, Type_P& value )
00112    {
00113       return Serialization<OsModel_P, OsModel_P::endianness, BlockData_P, Type_P>::write( target, value );
00114    }
00115 
00116 }
00117 
00118 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines