Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

misc/endian.h

Go to the documentation of this file.
00001 #ifndef MISC_ENDIAN_H
00002 #define MISC_ENDIAN_H
00003 
00004 /*
00005 
00006   GLT OpenGL C++ Toolkit (LGPL)
00007   Copyright (C) 2000-2002 Nigel Stewart  
00008 
00009   Email: nigels.com@gmail.com   
00010   WWW:   http://www.nigels.com/glt/
00011 
00012   This library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU Lesser General Public
00014   License as published by the Free Software Foundation; either
00015   version 2.1 of the License, or (at your option) any later version.
00016 
00017   This library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020   Lesser General Public License for more details.
00021 
00022   You should have received a copy of the GNU Lesser General Public
00023   License along with this library; if not, write to the Free Software
00024   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 */
00027 
00035 #include <glt/config.h>
00036 
00037 #if !defined(GLT_LITTLE_ENDIAN) && !defined(GLT_BIG_ENDIAN)
00038 #error GLT_LITTLE_ENDIAN or GLT_BIG_ENDIAN is not defined!
00039 #endif
00040 
00041 #if defined(GLT_LITTLE_ENDIAN) && defined(GLT_BIG_ENDIAN)
00042 #error GLT_LITTLE_ENDIAN and GLT_BIG_ENDIAN are both defined!
00043 #endif
00044 
00049 inline uint32 flip(const uint32 *v)
00050 {
00051     const byte *b = (const byte *) v;
00052     return b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
00053 }
00054 
00055 inline uint32 flip(const uint32 &v)
00056 {
00057     return flip(&v);
00058 }
00059 
00060 inline uint32 flip(const int32 &v)
00061 {
00062     return flip(reinterpret_cast<const uint32 *>(&v));
00063 }
00064 
00069 inline uint16 flip(const uint16 *v)
00070 {
00071     const byte *b = (const byte *) v;
00072     return b[0]<<8 | b[1];
00073 }
00074 
00075 inline uint16 flip(const uint16 &v)
00076 {
00077     return flip(&v);
00078 }
00079 
00080 inline int16 flip(const int16 &v)
00081 {
00082     return flip(reinterpret_cast<const uint32 *>(&v));
00083 }
00084 
00090 inline uint32 littleEndian(const uint32 *v)
00091 {
00092     #ifdef GLT_LITTLE_ENDIAN
00093     return *v;
00094     #endif
00095 
00096     #ifdef GLT_BIG_ENDIAN
00097     return flip(v);
00098     #endif
00099 }
00100 
00106 inline uint16 littleEndian(const uint16 *v)
00107 {
00108     #ifdef GLT_LITTLE_ENDIAN
00109     return *v;
00110     #endif
00111 
00112     #ifdef GLT_BIG_ENDIAN
00113     return flip(v);
00114     #endif
00115 }
00116 
00122 inline uint32 littleEndian(const uint32 &v)
00123 {
00124     #ifdef GLT_LITTLE_ENDIAN
00125     return v;
00126     #endif
00127 
00128     #ifdef GLT_BIG_ENDIAN
00129     return flip(&v);
00130     #endif
00131 }
00132 
00138 inline uint16 littleEndian(const uint16 &v)
00139 {
00140     #ifdef GLT_LITTLE_ENDIAN
00141     return v;
00142     #endif
00143 
00144     #ifdef GLT_BIG_ENDIAN
00145     return flip(&v);
00146     #endif
00147 }
00148 
00154 inline uint32 bigEndian(const uint32 *v)
00155 {
00156     #ifdef GLT_BIG_ENDIAN
00157     return *v;
00158     #endif
00159 
00160     #ifdef GLT_LITTLE_ENDIAN
00161     return flip(v);
00162     #endif
00163 }
00164 
00170 inline uint16 bigEndian(const uint16 *v)
00171 {
00172     #ifdef GLT_BIG_ENDIAN
00173     return *v;
00174     #endif
00175 
00176     #ifdef GLT_LITTLE_ENDIAN
00177     return flip(v);
00178     #endif
00179 }
00180 
00186 inline uint32 bigEndian(const uint32 &v)
00187 {
00188     #ifdef GLT_BIG_ENDIAN
00189     return v;
00190     #endif
00191 
00192     #ifdef GLT_LITTLE_ENDIAN
00193     return flip(&v);
00194     #endif
00195 }
00196 
00202 inline uint32 bigEndian(const uint16 &v)
00203 {
00204     #ifdef GLT_BIG_ENDIAN
00205     return v;
00206     #endif
00207 
00208     #ifdef GLT_LITTLE_ENDIAN
00209     return flip(&v);
00210     #endif
00211 }
00212 
00213 #endif

Generated on Tue Nov 5 11:11:04 2002 for GLT by doxygen1.2.18