Wiselib
|
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_STD_PAIR_H 00020 #define __WISELIB_UTIL_SERIALIZATION_STD_PAIR_H 00021 00022 #include "util/serialization/serialization.h" 00023 #include "util/serialization/simple_types.h" 00024 #include "util/serialization/endian.h" 00025 00026 namespace wiselib 00027 { 00028 00029 template<typename OsModel_P, 00030 typename BlockData_P, 00031 typename A, 00032 typename B> 00033 class Serialization<OsModel_P, WISELIB_BIG_ENDIAN, BlockData_P, std::pair<A, B> > 00034 { 00035 public: 00036 typedef OsModel_P OsModel; 00037 typedef BlockData_P BlockData; 00038 typedef std::pair<A, B> Type; 00039 typedef typename Type::first_type First; 00040 typedef typename Type::second_type Second; 00041 00042 typedef typename OsModel::size_t size_t; 00043 // -------------------------------------------------------------------- 00044 static inline Type read( BlockData *target ) 00045 { 00046 Type x; 00047 read( target, x ); 00048 return x; 00049 } 00050 // -------------------------------------------------------------------- 00051 static inline void read( BlockData *target, Type& value ) 00052 { 00053 // FIXME: The following const_cast is *very* ugly, but is there an 00054 // alternative? It is required when working with std::map as 00055 // routing table, and temporary values mut be stored. E.g., 00056 // look into dsdv_routing::update_routing_table() 00057 Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, First>::read( target, const_cast<First>(value.first) ); 00058 Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Second>::read( target + sizeof(A), value.second ); 00059 } 00060 // -------------------------------------------------------------------- 00061 static inline size_t write( BlockData *target, Type& value ) 00062 { 00063 size_t bytes_a = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, First>::write( target, value.first ); 00064 size_t bytes_b = Serialization<OsModel, WISELIB_BIG_ENDIAN, BlockData, Second>::write( target + bytes_a, value.second ); 00065 return bytes_a + bytes_b; 00066 } 00067 }; 00068 // ----------------------------------------------------------------------- 00069 // ----------------------------------------------------------------------- 00070 // ----------------------------------------------------------------------- 00071 template<typename OsModel_P, 00072 typename BlockData_P, 00073 typename A, 00074 typename B> 00075 class Serialization<OsModel_P, WISELIB_LITTLE_ENDIAN, BlockData_P, std::pair<A, B> > 00076 { 00077 public: 00078 typedef OsModel_P OsModel; 00079 typedef BlockData_P BlockData; 00080 typedef std::pair<A, B> Type; 00081 typedef typename Type::first_type First; 00082 typedef typename Type::second_type Second; 00083 00084 typedef typename OsModel::size_t size_t; 00085 // -------------------------------------------------------------------- 00086 static inline Type read( BlockData *target ) 00087 { 00088 Type x; 00089 read( target, x ); 00090 return x; 00091 } 00092 // -------------------------------------------------------------------- 00093 static inline void read( BlockData *target, Type& value ) 00094 { 00095 // FIXME: The following const_cast is *very* ugly, but is there an 00096 // alternative? It is required when working with std::map as 00097 // routing table, and temporary values mut be stored. E.g., 00098 // look into dsdv_routing::update_routing_table() 00099 Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, First>::read( target, const_cast<First>(value.first) ); 00100 Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Second>::read( target + sizeof(A), value.second ); 00101 } 00102 // -------------------------------------------------------------------- 00103 static inline size_t write( BlockData *target, Type& value ) 00104 { 00105 size_t bytes_a = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, First>::write( target, value.first ); 00106 size_t bytes_b = Serialization<OsModel, WISELIB_LITTLE_ENDIAN, BlockData, Second>::write( target + bytes_a, value.second ); 00107 return bytes_a + bytes_b; 00108 } 00109 }; 00110 00111 } 00112 00113 #endif