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.h>
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 #include <OpenMesh/Core/Utils/GenProg.hh>
00057
00058
00059
00060 namespace OpenMesh {
00061 namespace IO {
00062
00063
00064
00065
00066
00071
00072
00073
00074
00078 template < size_t N > inline
00079 void _reverse_byte_order_N(uint8_t* _val)
00080 {
00081 assert_compile(false);
00082
00083 }
00084
00085
00086 template <> inline
00087 void _reverse_byte_order_N<1>(uint8_t* ) { };
00088
00089
00090 template <> inline
00091 void _reverse_byte_order_N<2>(uint8_t* _val)
00092 {
00093 _val[0] ^= _val[1]; _val[1] ^= _val[0]; _val[0] ^= _val[1];
00094 }
00095
00096
00097 template <> inline
00098 void _reverse_byte_order_N<4>(uint8_t* _val)
00099 {
00100 _val[0] ^= _val[3]; _val[3] ^= _val[0]; _val[0] ^= _val[3];
00101 _val[1] ^= _val[2]; _val[2] ^= _val[1]; _val[1] ^= _val[2];
00102 }
00103
00104
00105 template <> inline
00106 void _reverse_byte_order_N<8>(uint8_t* _val)
00107 {
00108 _val[0] ^= _val[7]; _val[7] ^= _val[0]; _val[0] ^= _val[7];
00109 _val[1] ^= _val[6]; _val[6] ^= _val[1]; _val[1] ^= _val[6];
00110 _val[2] ^= _val[5]; _val[5] ^= _val[2]; _val[2] ^= _val[5];
00111 _val[3] ^= _val[4]; _val[4] ^= _val[3]; _val[3] ^= _val[4];
00112 }
00113
00114
00115 template <> inline
00116 void _reverse_byte_order_N<12>(uint8_t* _val)
00117 {
00118 _val[0] ^= _val[11]; _val[11] ^= _val[0]; _val[0] ^= _val[11];
00119 _val[1] ^= _val[10]; _val[10] ^= _val[1]; _val[1] ^= _val[10];
00120 _val[2] ^= _val[ 9]; _val[ 9] ^= _val[2]; _val[2] ^= _val[ 9];
00121 _val[3] ^= _val[ 8]; _val[ 8] ^= _val[3]; _val[3] ^= _val[ 8];
00122 _val[4] ^= _val[ 7]; _val[ 7] ^= _val[4]; _val[4] ^= _val[ 7];
00123 _val[5] ^= _val[ 6]; _val[ 6] ^= _val[5]; _val[5] ^= _val[ 6];
00124 }
00125
00126
00127 template <> inline
00128 void _reverse_byte_order_N<16>(uint8_t* _val)
00129 {
00130 _reverse_byte_order_N<8>(_val);
00131 _reverse_byte_order_N<8>(_val+8);
00132 std::swap(*(uint64_t*)_val, *(((uint64_t*)_val)+1));
00133 }
00134
00135
00136
00137
00138
00139
00142 template <typename T> inline T* reverse_byte_order(T* t)
00143 {
00144
00145
00146
00147
00148 assert_compile(false);
00149 return t;
00150 }
00151
00152 inline void compile_time_error__no_fundamental_type()
00153 {
00154
00155 assert(false);
00156 }
00157
00158
00159
00160 template <typename T> T& reverse_byte_order( T& _t )
00161 {
00162 omerr() << "Not defined for type " << typeid(T).name() << std::endl;
00163 compile_time_error__no_fundamental_type();
00164 return _t;
00165 }
00166
00167 template <> inline bool& reverse_byte_order(bool & _t) { return _t; }
00168 template <> inline char& reverse_byte_order(char & _t) { return _t; }
00169 #if defined(OM_CC_GCC)
00170 template <> inline signed char& reverse_byte_order(signed char & _t) { return _t; }
00171 #endif
00172 template <> inline uchar& reverse_byte_order(uchar& _t) { return _t; }
00173
00174
00175 #define REVERSE_FUNDAMENTAL_TYPE( T ) \
00176 template <> inline T& reverse_byte_order( T& _t ) {\
00177 _reverse_byte_order_N<sizeof(T)>( reinterpret_cast<uint8_t*>(&_t) ); \
00178 return _t; \
00179 }
00180
00181
00182
00183
00184 REVERSE_FUNDAMENTAL_TYPE(int16_t);
00185 REVERSE_FUNDAMENTAL_TYPE(uint16_t);
00186 REVERSE_FUNDAMENTAL_TYPE(int);
00187 REVERSE_FUNDAMENTAL_TYPE(uint);
00188 REVERSE_FUNDAMENTAL_TYPE(int32_t);
00189 REVERSE_FUNDAMENTAL_TYPE(uint32_t);
00190 REVERSE_FUNDAMENTAL_TYPE(int64_t);
00191 REVERSE_FUNDAMENTAL_TYPE(uint64_t);
00192 REVERSE_FUNDAMENTAL_TYPE(float);
00193 REVERSE_FUNDAMENTAL_TYPE(double);
00194 REVERSE_FUNDAMENTAL_TYPE(long double);
00195
00196 #undef REVERSE_FUNDAMENTAL_TYPE
00197
00198 #if 0
00199
00200 #define REVERSE_VECTORT_TYPE( T ) \
00201 template <> inline T& reverse_byte_order(T& _v) {\
00202 for (size_t i; i< T::size_; ++i) \
00203 _reverse_byte_order_N< sizeof(T::value_type) >( reinterpret_cast<uint8_t*>(&_v[i])); \
00204 return _v; \
00205 }
00206
00207 #define REVERSE_VECTORT_TYPES( N ) \
00208 REVERSE_VECTORT_TYPE( Vec##N##c ) \
00209 REVERSE_VECTORT_TYPE( Vec##N##uc ) \
00210 REVERSE_VECTORT_TYPE( Vec##N##s ) \
00211 REVERSE_VECTORT_TYPE( Vec##N##us ) \
00212 REVERSE_VECTORT_TYPE( Vec##N##i ) \
00213 REVERSE_VECTORT_TYPE( Vec##N##ui ) \
00214 REVERSE_VECTORT_TYPE( Vec##N##f ) \
00215 REVERSE_VECTORT_TYPE( Vec##N##d ) \
00216
00217 REVERSE_VECTORT_TYPES(1)
00218 REVERSE_VECTORT_TYPES(2)
00219 REVERSE_VECTORT_TYPES(3)
00220 REVERSE_VECTORT_TYPES(4)
00221 REVERSE_VECTORT_TYPES(6)
00222
00223 #undef REVERSE_VECTORT_TYPES
00224 #undef REVERSE_VECTORT_TYPE
00225
00226 #endif
00227
00228 template <typename T> inline
00229 T reverse_byte_order(const T& a)
00230 {
00231 compile_timer_error__const_means_const(a);
00232 return a;
00233 }
00234
00235
00237
00238
00239
00240 }
00241 }
00242
00243 #endif // OPENMESH_SR_RBO_HH defined
00244
00245