00001 // This file is part of ff3d - http://www.freefem.org/ff3d 00002 // Copyright (C) 2007 Stéphane Del Pino 00003 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2, or (at your option) 00007 // any later version. 00008 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software Foundation, 00016 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 // $Id: EndianConverter.hpp,v 1.3 2008/05/25 11:57:52 delpinux Exp $ 00019 00020 #ifndef ENDIAN_CONVERTER_HPP 00021 #define ENDIAN_CONVERTER_HPP 00022 00023 #include <Vector.hpp> 00024 00025 template <typename T> 00026 void invertEndianess(T& value) 00027 { 00028 const size_t n = sizeof(T); 00029 char* p = reinterpret_cast<char*>(&value); 00030 for (size_t i=0; i<n/2; ++i) { 00031 std::swap(p[i],p[n-1-i]); 00032 } 00033 } 00034 00035 template <typename T> 00036 void invertEndianess(Vector<T>& values) 00037 { 00038 for (size_t i=0; i<values.size(); ++i) { 00039 invertEndianess(values[i]); 00040 } 00041 } 00042 00043 #ifdef WORDS_BIGENDIAN 00044 00045 #include <algorithm> 00046 00047 template <typename T> 00048 void littleEndianize(T& value) 00049 { 00050 invertEndianess(value); 00051 } 00052 00053 template <typename T> 00054 void littleEndianize(Vector<T>& values) 00055 { 00056 invertEndianess(values); 00057 } 00058 00059 template <typename T> 00060 void fromLittleEndian(T& value) 00061 { 00062 invertEndianess(value); 00063 } 00064 00065 template <typename T> 00066 void fromLittleEndian(Vector<T>& values) 00067 { 00068 invertEndianess(values); 00069 } 00070 00071 template <typename T> 00072 void bigEndianize(T& value) 00073 { 00074 ; 00075 } 00076 00077 template <typename T> 00078 void bigEndianize(Vector<T>& values) 00079 { 00080 ; 00081 } 00082 00083 template <typename T> 00084 void fromBigEndian(T& value) 00085 { 00086 ; 00087 } 00088 00089 template <typename T> 00090 void fromBigEndian(Vector<T>& values) 00091 { 00092 ; 00093 } 00094 00095 #else // WORDS_BIGENDIAN 00096 00097 template <typename T> 00098 void littleEndianize(T& value) 00099 { 00100 ; 00101 } 00102 00103 template <typename T> 00104 void littleEndianize(Vector<T>& values) 00105 { 00106 ; 00107 } 00108 00109 template <typename T> 00110 void fromLittleEndian(T& value) 00111 { 00112 ; 00113 } 00114 00115 template <typename T> 00116 void fromLittleEndian(Vector<T>& values) 00117 { 00118 ; 00119 } 00120 00121 template <typename T> 00122 void bigEndianize(T& value) 00123 { 00124 invertEndianess(value); 00125 } 00126 00127 template <typename T> 00128 void bigEndianize(Vector<T>& values) 00129 { 00130 invertEndianess(values); 00131 } 00132 00133 00134 template <typename T> 00135 void fromBigEndian(T& value) 00136 { 00137 invertEndianess(value); 00138 } 00139 00140 template <typename T> 00141 void fromBigEndian(Vector<T>& values) 00142 { 00143 invertEndianess(values); 00144 } 00145 00146 #endif // WORDS_BIGENDIAN 00147 00148 #endif // ENDIAN_CONVERTER_HPP
1.5.6