|
libGDF
|
00001 // 00002 // This file is part of libGDF. 00003 // 00004 // libGDF is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as 00006 // published by the Free Software Foundation, either version 3 of 00007 // the License, or (at your option) any later version. 00008 // 00009 // libGDF 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 Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with libGDF. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // Copyright 2010 Martin Billinger 00018 00019 #ifndef __TYPES_H_INCLUDED__ 00020 #define __TYPES_H_INCLUDED__ 00021 00022 #include "GDF/Exceptions.h" 00023 #include <boost/cstdint.hpp> 00024 #include <boost/detail/endian.hpp> 00025 #include <iostream> 00026 00027 namespace gdf 00028 { 00029 typedef boost::int8_t int8; 00030 typedef boost::uint8_t uint8; 00031 typedef boost::int16_t int16; 00032 typedef boost::uint16_t uint16; 00033 typedef boost::int32_t int32; 00034 typedef boost::uint32_t uint32; 00035 typedef boost::int64_t int64; 00036 typedef boost::uint64_t uint64; 00037 typedef float float32; 00038 typedef double float64; 00039 00040 enum type_id 00041 { 00042 INVALID_TYPE = 0, 00043 INT8 = 1, 00044 UINT8 = 2, 00045 INT16 = 3, 00046 UINT16 = 4, 00047 INT32 = 5, 00048 UINT32 = 6, 00049 INT64 = 7, 00050 UINT64 = 8, 00051 00052 FLOAT32 = 16, 00053 FLOAT64 = 17 00054 }; 00055 00056 size_t datatype_size( uint32 t ); 00057 00058 template<typename T> 00059 T switch_endian( const T &source ) 00060 { 00061 T dest; 00062 const char *A = reinterpret_cast<const char*>(&source); 00063 char *B = reinterpret_cast<char*>(&dest); 00064 00065 int a = 0; 00066 int b = sizeof(T)-1; 00067 00068 while( b >= 0 ) 00069 { 00070 B[b] = A[a]; 00071 } 00072 } 00073 00074 template<typename T> 00075 void writeLittleEndian( std::ostream &out, T item ) 00076 { 00077 #if defined(BOOST_LITTLE_ENDIAN) 00078 out.write( reinterpret_cast<const char*>(&item), sizeof(item) ); 00079 #elif defined(BOOST_BIG_ENDIAN) 00080 const char* p = reinterpret_cast<const char*>(&item) + sizeof(item)-1; 00081 for( size_t i=0; i<sizeof(item); i++ ) 00082 out.write( p--, 1 ); 00083 #else 00084 #error "Unable to determine system endianness." 00085 #endif 00086 } 00087 00088 template<typename T> 00089 void readLittleEndian( std::istream &in, T &item ) 00090 { 00091 #if defined(BOOST_LITTLE_ENDIAN) 00092 in.read( reinterpret_cast<char*>(&item), sizeof(item) ); 00093 #elif defined(BOOST_BIG_ENDIAN) 00094 char* p = reinterpret_cast<char*>(&item) + sizeof(item)-1; 00095 for( size_t i=0; i<sizeof(item); i++ ) 00096 in.read( p--, 1 ); 00097 #else 00098 #error "Unable to determine system endianness." 00099 #endif 00100 } 00101 00102 } 00103 00104 #endif