Wiselib
wiselib.stable/util/serialization/simple_types.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_SIMPLE_TYPES_H
00020 #define __WISELIB_UTIL_SERIALIZATION_SIMPLE_TYPES_H
00021 
00022 #include "util/serialization/serialization.h"
00023 #include "util/serialization/endian.h"
00024 #include "util/serialization/floating_point.h"
00025 
00026 namespace wiselib
00027 {
00028 
00029    template <typename OsModel_P,
00030              typename BlockData_P>
00031    struct Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, bool>
00032    {
00033    public:
00034       typedef OsModel_P OsModel;
00035       typedef BlockData_P BlockData;
00036       typedef bool Type;
00037 
00038       typedef typename OsModel::size_t size_t;
00039       // --------------------------------------------------------------------
00040       static inline Type read( BlockData *target )
00041       {
00042          if (*target == 0x00)
00043             return false;
00044          else
00045             return true;
00046       }
00047       // --------------------------------------------------------------------
00048       static inline size_t write( BlockData *target, Type& value )
00049       {
00050          if (!value)
00051             *target = 0x00;
00052          else
00053             *target = 0x01;
00054 
00055          return 1;
00056       }
00057    };
00058    // -----------------------------------------------------------------------
00059    // -----------------------------------------------------------------------
00060    // -----------------------------------------------------------------------
00061    template <typename OsModel_P,
00062              typename BlockData_P>
00063    struct Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, bool>
00064    {
00065    public:
00066       typedef OsModel_P OsModel;
00067       typedef BlockData_P BlockData;
00068       typedef bool Type;
00069 
00070       typedef typename OsModel::size_t size_t;
00071       // --------------------------------------------------------------------
00072       static inline Type read( BlockData *target )
00073       {
00074          if (*target == 0x00)
00075             return false;
00076          else
00077             return true;
00078       }
00079       // --------------------------------------------------------------------
00080       static inline size_t write( BlockData *target, Type& value )
00081       {
00082          if (!value)
00083             *target = 0x00;
00084          else
00085             *target = 0x01;
00086 
00087          return 1;
00088       }
00089    };
00090    // -----------------------------------------------------------------------
00091    // -----------------------------------------------------------------------
00092    // -----------------------------------------------------------------------
00093    template <typename OsModel_P,
00094              typename BlockData_P>
00095    struct Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, uint16_t>
00096    {
00097    public:
00098       typedef OsModel_P OsModel;
00099       typedef BlockData_P BlockData;
00100       typedef uint16_t Type;
00101 
00102       typedef typename OsModel::size_t size_t;
00103       // --------------------------------------------------------------------
00104       static inline Type read( BlockData *target )
00105       {
00106          Type value;
00107          *((BlockData*)&value + 1) = *target;
00108          *((BlockData*)&value) = *(target + 1);
00109          return value;
00110       }
00111       // --------------------------------------------------------------------
00112       static inline size_t write( BlockData *target, Type& value )
00113       {
00114          *target = (value >> 8) & 0xff;
00115          *(target + 1) = value & 0xff;
00116          return sizeof(Type);
00117       }
00118    };
00119    // -----------------------------------------------------------------------
00120    // -----------------------------------------------------------------------
00121    // -----------------------------------------------------------------------
00122    template <typename OsModel_P,
00123              typename BlockData_P>
00124    struct Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, uint16_t>
00125    {
00126    public:
00127       typedef OsModel_P OsModel;
00128       typedef BlockData_P BlockData;
00129       typedef uint16_t Type;
00130 
00131       typedef typename OsModel::size_t size_t;
00132       // --------------------------------------------------------------------
00133       static inline Type read( BlockData *target )
00134       {
00135          Type value;
00136          *((BlockData*)&value) = *target;
00137          *((BlockData*)&value + 1) = *(target + 1);
00138          return value;
00139       }
00140       // --------------------------------------------------------------------
00141       static inline size_t write( BlockData *target, Type& value )
00142       {
00143          *target = (value >> 8) & 0xff;
00144          *(target + 1) = value & 0xff;
00145          return sizeof(Type);
00146       }
00147    };
00148    // -----------------------------------------------------------------------
00149    // -----------------------------------------------------------------------
00150    // -----------------------------------------------------------------------
00151    template <typename OsModel_P,
00152              typename BlockData_P>
00153    struct Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, int16_t>
00154    {
00155    public:
00156       typedef OsModel_P OsModel;
00157       typedef BlockData_P BlockData;
00158       typedef int16_t Type;
00159 
00160       typedef typename OsModel::size_t size_t;
00161       // --------------------------------------------------------------------
00162       static inline Type read( BlockData *target )
00163       {
00164          Type value;
00165          *((BlockData*)&value + 1) = *target;
00166          *((BlockData*)&value) = *(target + 1);
00167          return value;
00168       }
00169       // --------------------------------------------------------------------
00170       static inline size_t write( BlockData *target, Type& value )
00171       {
00172          *target = (value >> 8) & 0xff;
00173          *(target + 1) = value & 0xff;
00174          return sizeof(Type);
00175       }
00176    };
00177    // -----------------------------------------------------------------------
00178    // -----------------------------------------------------------------------
00179    // -----------------------------------------------------------------------
00180    template <typename OsModel_P,
00181              typename BlockData_P>
00182    struct Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, int16_t>
00183    {
00184    public:
00185       typedef OsModel_P OsModel;
00186       typedef BlockData_P BlockData;
00187       typedef int16_t Type;
00188 
00189       typedef typename OsModel::size_t size_t;
00190       // --------------------------------------------------------------------
00191       static inline Type read( BlockData *target )
00192       {
00193          Type value;
00194          *((BlockData*)&value) = *target;
00195          *((BlockData*)&value + 1) = *(target + 1);
00196          return value;
00197       }
00198       // --------------------------------------------------------------------
00199       static inline size_t write( BlockData *target, Type& value )
00200       {
00201          *target = (value >> 8) & 0xff;
00202          *(target + 1) = value & 0xff;
00203          return sizeof(Type);
00204       }
00205    };
00206    // -----------------------------------------------------------------------
00207    // -----------------------------------------------------------------------
00208    // -----------------------------------------------------------------------
00209    template <typename OsModel_P,
00210              typename BlockData_P>
00211    struct Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, double>
00212    {
00213    public:
00214       typedef OsModel_P OsModel;
00215       typedef BlockData_P BlockData;
00216       typedef double Type;
00217 
00218       typedef typename OsModel::size_t size_t;
00219       // --------------------------------------------------------------------
00220       static inline Type read( BlockData *target )
00221       {
00222          return FpSerialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, double, sizeof(double)>::read( target );
00223       }
00224       // --------------------------------------------------------------------
00225       static inline size_t write( BlockData *target, Type& value )
00226       {
00227          return FpSerialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, double, sizeof(double)>::write( target, value );
00228       }
00229    };
00230    // -----------------------------------------------------------------------
00231    // -----------------------------------------------------------------------
00232    // -----------------------------------------------------------------------
00233    template <typename OsModel_P,
00234              typename BlockData_P>
00235    struct Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, double>
00236    {
00237    public:
00238       typedef OsModel_P OsModel;
00239       typedef BlockData_P BlockData;
00240       typedef double Type;
00241 
00242       typedef typename OsModel::size_t size_t;
00243       // --------------------------------------------------------------------
00244       static inline Type read( BlockData *target )
00245       {
00246          return FpSerialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, double, sizeof(double)>::read( target );
00247       }
00248       // --------------------------------------------------------------------
00249       static inline size_t write( BlockData *target, Type& value )
00250       {
00251          return FpSerialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, double, sizeof(double)>::write( target, value );
00252       }
00253    };
00254 
00255 }
00256 
00257 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines