00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef OPENMESH_SR_RBO_HH
00039 #define OPENMESH_SR_RBO_HH
00040
00041
00042
00043
00044 #include <OpenMesh/Core/System/config.hh>
00045
00046 #if defined(OM_CC_MIPS)
00047 # include <stdio.h>
00048 #else
00049 # include <cstdio>
00050 #endif
00051 #include <algorithm>
00052 #include <typeinfo>
00053
00054 #include <OpenMesh/Core/System/omstream.hh>
00055 #include <OpenMesh/Core/IO/SR_types.hh>
00056
00057
00058
00059 namespace OpenMesh {
00060 namespace IO {
00061
00062
00063
00064
00065
00070
00071
00072
00073
00074 template < size_t N > inline
00075 void _reverse_byte_order_N(uint8_t* _val)
00076 {
00077
00078 }
00079
00080
00081 template <> inline
00082 void _reverse_byte_order_N<1>(uint8_t* _val) { };
00083
00084
00085 template <> inline
00086 void _reverse_byte_order_N<2>(uint8_t* _val)
00087 {
00088 _val[0] ^= _val[1]; _val[1] ^= _val[0]; _val[0] ^= _val[1];
00089 }
00090
00091
00092 template <> inline
00093 void _reverse_byte_order_N<4>(uint8_t* _val)
00094 {
00095 _val[0] ^= _val[3]; _val[3] ^= _val[0]; _val[0] ^= _val[3];
00096 _val[1] ^= _val[2]; _val[2] ^= _val[1]; _val[1] ^= _val[2];
00097 }
00098
00099
00100 template <> inline
00101 void _reverse_byte_order_N<8>(uint8_t* _val)
00102 {
00103 _val[0] ^= _val[7]; _val[7] ^= _val[0]; _val[0] ^= _val[7];
00104 _val[1] ^= _val[6]; _val[6] ^= _val[1]; _val[1] ^= _val[6];
00105 _val[2] ^= _val[5]; _val[5] ^= _val[2]; _val[2] ^= _val[5];
00106 _val[3] ^= _val[4]; _val[4] ^= _val[3]; _val[3] ^= _val[4];
00107 }
00108
00109
00110 template <> inline
00111 void _reverse_byte_order_N<12>(uint8_t* _val)
00112 {
00113 _val[0] ^= _val[11]; _val[11] ^= _val[0]; _val[0] ^= _val[11];
00114 _val[1] ^= _val[10]; _val[10] ^= _val[1]; _val[1] ^= _val[10];
00115 _val[2] ^= _val[ 9]; _val[ 9] ^= _val[2]; _val[2] ^= _val[ 9];
00116 _val[3] ^= _val[ 8]; _val[ 8] ^= _val[3]; _val[3] ^= _val[ 8];
00117 _val[4] ^= _val[ 7]; _val[ 7] ^= _val[4]; _val[4] ^= _val[ 7];
00118 _val[5] ^= _val[ 6]; _val[ 6] ^= _val[5]; _val[5] ^= _val[ 6];
00119 }
00120
00121
00122 template <> inline
00123 void _reverse_byte_order_N<16>(uint8_t* _val)
00124 {
00125 _reverse_byte_order_N<8>(_val);
00126 _reverse_byte_order_N<8>(_val+8);
00127 std::swap(*(uint64_t*)_val, *(((uint64_t*)_val)+1));
00128 }
00129
00130
00131
00132
00133
00134
00135 template <typename T> inline T* reverse_byte_order(T* t)
00136 {
00137
00138
00139
00140
00141 return t;
00142 }
00143
00144
00145 inline void compile_time_error__no_fundamental_type()
00146 {
00147
00148 assert(false);
00149 }
00150
00151
00152
00153
00154 template <typename T> T& reverse_byte_order( T& _t )
00155 {
00156 omerr() << "Not defined for type " << typeid(T).name() << std::endl;
00157 compile_time_error__no_fundamental_type();
00158 return _t;
00159 }
00160
00161 template <> inline bool& reverse_byte_order(bool & _t) { return _t; }
00162 template <> inline char& reverse_byte_order(char & _t) { return _t; }
00163 #if defined(OM_CC_GCC)
00164 template <> inline signed char& reverse_byte_order(signed char & _t) { return _t; }
00165 #endif
00166 template <> inline uchar& reverse_byte_order(uchar& _t) { return _t; }
00167
00168
00169 #define REVERSE_FUNDAMENTAL_TYPE( T ) \
00170 template <> inline T& reverse_byte_order( T& _t ) {\
00171 _reverse_byte_order_N<sizeof(T)>( reinterpret_cast<uint8_t*>(&_t) ); \
00172 return _t; \
00173 }
00174
00175
00176
00177
00178 REVERSE_FUNDAMENTAL_TYPE(int16_t);
00179 REVERSE_FUNDAMENTAL_TYPE(uint16_t);
00180 REVERSE_FUNDAMENTAL_TYPE(int);
00181 REVERSE_FUNDAMENTAL_TYPE(uint);
00182 REVERSE_FUNDAMENTAL_TYPE(int32_t);
00183 REVERSE_FUNDAMENTAL_TYPE(uint32_t);
00184 REVERSE_FUNDAMENTAL_TYPE(int64_t);
00185 REVERSE_FUNDAMENTAL_TYPE(uint64_t);
00186 REVERSE_FUNDAMENTAL_TYPE(float);
00187 REVERSE_FUNDAMENTAL_TYPE(double);
00188 REVERSE_FUNDAMENTAL_TYPE(long double);
00189
00190 #undef REVERSE_FUNDAMENTAL_TYPE
00191
00192 #if 0
00193
00194 #define REVERSE_VECTORT_TYPE( T ) \
00195 template <> inline T& reverse_byte_order(T& _v) {\
00196 for (size_t i; i< T::size_; ++i) \
00197 _reverse_byte_order_N< sizeof(T::value_type) >( reinterpret_cast<uint8_t*>(&_v[i])); \
00198 return _v; \
00199 }
00200
00201 #define REVERSE_VECTORT_TYPES( N ) \
00202 REVERSE_VECTORT_TYPE( Vec##N##c ) \
00203 REVERSE_VECTORT_TYPE( Vec##N##uc ) \
00204 REVERSE_VECTORT_TYPE( Vec##N##s ) \
00205 REVERSE_VECTORT_TYPE( Vec##N##us ) \
00206 REVERSE_VECTORT_TYPE( Vec##N##i ) \
00207 REVERSE_VECTORT_TYPE( Vec##N##ui ) \
00208 REVERSE_VECTORT_TYPE( Vec##N##f ) \
00209 REVERSE_VECTORT_TYPE( Vec##N##d ) \
00210
00211 REVERSE_VECTORT_TYPES(1)
00212 REVERSE_VECTORT_TYPES(2)
00213 REVERSE_VECTORT_TYPES(3)
00214 REVERSE_VECTORT_TYPES(4)
00215 REVERSE_VECTORT_TYPES(6)
00216
00217 #undef REVERSE_VECTORT_TYPES
00218 #undef REVERSE_VECTORT_TYPE
00219
00220 #endif
00221
00222 template <typename T> inline
00223 T reverse_byte_order(const T& a)
00224 {
00225 compile_timer_error__const_means_const(a);
00226 return a;
00227 }
00228
00229
00231
00232
00233
00234 }
00235 }
00236
00237 #endif // OPENMESH_SR_RBO_HH defined
00238
00239