Wiselib
wiselib.testing/util/serialization/math_vec.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_MATH_VEC_H
00020 #define __WISELIB_UTIL_SERIALIZATION_MATH_VEC_H
00021 
00022 #include "algorithms/localization/distance_based/math/vec.h"
00023 #include "util/serialization/serialization.h"
00024 #include "util/serialization/simple_types.h"
00025 #include "util/serialization/floating_point.h"
00026 #include "util/serialization/endian.h"
00027 
00028 namespace wiselib
00029 {
00030 
00031    template<typename OsModel_P,
00032             typename BlockData_P,
00033             typename Arithmatic_P>
00034 
00035    struct Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, Vec<Arithmatic_P> >
00036    {
00037       typedef OsModel_P OsModel;
00038       typedef BlockData_P BlockData;
00039       typedef Arithmatic_P Arithmatic;
00040 
00041       typedef typename OsModel::size_t size_t;
00042       // --------------------------------------------------------------------
00043       static inline Vec<Arithmatic> read( BlockData *target )
00044       {
00045          Vec<Arithmatic> x;
00046          read( target, x );
00047          return x;
00048       }
00049       // --------------------------------------------------------------------
00050       static inline void read( BlockData *target, Vec<Arithmatic>& value )
00051       {
00052         Arithmatic x = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Arithmatic>::read(
00053             target );
00054         Arithmatic y = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Arithmatic>::read(
00055             target + sizeof(Arithmatic) );
00056         Arithmatic z = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Arithmatic>::read(
00057             target + 2 * sizeof(Arithmatic) );
00058          value = Vec<Arithmatic>( x, y, z );
00059       }
00060       // --------------------------------------------------------------------
00061       static inline size_t write( BlockData *target, const Vec<Arithmatic>& value )
00062       {
00063         Arithmatic x = value.x();
00064         Arithmatic y = value.y();
00065         Arithmatic z = value.z();
00066          size_t bytes_x = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Arithmatic>::write(
00067             target, x );
00068          size_t bytes_y = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Arithmatic>::write(
00069             target + bytes_x, y );
00070          size_t bytes_z = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Arithmatic>::write(
00071             target + bytes_x + bytes_y, z );
00072          return bytes_x + bytes_y + bytes_z;
00073       }
00074    };
00075    // -----------------------------------------------------------------------
00076    // -----------------------------------------------------------------------
00077    // -----------------------------------------------------------------------
00078    template<typename OsModel_P,
00079             typename BlockData_P,
00080             typename Arithmatic_P>
00081    struct Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, Vec<Arithmatic_P> >
00082    {
00083       typedef OsModel_P OsModel;
00084       typedef BlockData_P BlockData;
00085       typedef Arithmatic_P Arithmatic;
00086       typedef typename OsModel::size_t size_t;
00087       // --------------------------------------------------------------------
00088       static inline Vec<Arithmatic> read( BlockData *target )
00089       {
00090          Vec<Arithmatic> x;
00091          read( target, x );
00092          return x;
00093       }
00094       // --------------------------------------------------------------------
00095       static inline void read( BlockData *target, Vec<Arithmatic>& value )
00096       {
00097         Arithmatic x = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Arithmatic>::read(
00098             target );
00099         Arithmatic y = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Arithmatic>::read(
00100             target + sizeof(Arithmatic) );
00101         Arithmatic z = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Arithmatic>::read(
00102             target + 2 * sizeof(Arithmatic) );
00103          value = Vec<>( x, y, z );
00104       }
00105       // --------------------------------------------------------------------
00106       static inline size_t write( BlockData *target, const Vec<Arithmatic>& value )
00107       {
00108         Arithmatic x = value.x();
00109         Arithmatic y = value.y();
00110         Arithmatic z = value.z();
00111          size_t bytes_x = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Arithmatic>::write(
00112             target, x );
00113          size_t bytes_y = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Arithmatic>::write(
00114             target + bytes_x, y );
00115          size_t bytes_z = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Arithmatic>::write(
00116             target + bytes_x + bytes_y, z );
00117          return bytes_x + bytes_y + bytes_z;
00118       }
00119    };
00120 
00121 }
00122 
00123 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines