Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

SR_rbo.hh

00001 //=============================================================================
00002 //                                                                            
00003 //                               OpenMesh                                     
00004 //      Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen       
00005 //                           www.openmesh.org                                 
00006 //                                                                            
00007 //-----------------------------------------------------------------------------
00008 //                                                                            
00009 //                                License                                     
00010 //                                                                            
00011 //   This library is free software; you can redistribute it and/or modify it 
00012 //   under the terms of the GNU Library General Public License as published  
00013 //   by the Free Software Foundation, version 2.                             
00014 //                                                                             
00015 //   This library is distributed in the hope that it will be useful, but       
00016 //   WITHOUT ANY WARRANTY; without even the implied warranty of                
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00018 //   Library General Public License for more details.                          
00019 //                                                                            
00020 //   You should have received a copy of the GNU Library General Public         
00021 //   License along with this library; if not, write to the Free Software       
00022 //   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 
00023 //                                                                            
00024 //-----------------------------------------------------------------------------
00025 //                                                                            
00026 //   $Revision: 1.3 $
00027 //   $Date: 2005/04/18 09:07:09 $
00028 //                                                                            
00029 //=============================================================================
00030 
00031 
00032 //=============================================================================
00033 //
00034 //  Helper Functions for binary reading / writing
00035 //
00036 //=============================================================================
00037 
00038 #ifndef OPENMESH_SR_RBO_HH
00039 #define OPENMESH_SR_RBO_HH
00040 
00041 
00042 //== INCLUDES =================================================================
00043 
00044 #include <OpenMesh/Core/System/config.hh>
00045 // -------------------- STL
00046 #if defined(OM_CC_MIPS)
00047 #  include <stdio.h> // size_t
00048 #else
00049 #  include <cstdio>  // size_t
00050 #endif
00051 #include <algorithm>
00052 #include <typeinfo>
00053 // -------------------- OpenMesh
00054 #include <OpenMesh/Core/System/omstream.hh>
00055 #include <OpenMesh/Core/IO/SR_types.hh>
00056 
00057 //== NAMESPACES ===============================================================
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 //  compile_time_error__only_for_fundamental_types(_val);
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]; // 0 <-> 3
00096    _val[1] ^= _val[2]; _val[2] ^= _val[1]; _val[1] ^= _val[2]; // 1 <-> 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]; // 0 <-> 7
00104    _val[1] ^= _val[6]; _val[6] ^= _val[1]; _val[1] ^= _val[6]; // 1 <-> 6
00105    _val[2] ^= _val[5]; _val[5] ^= _val[2]; _val[2] ^= _val[5]; // 2 <-> 5
00106    _val[3] ^= _val[4]; _val[4] ^= _val[3]; _val[3] ^= _val[4]; // 3 <-> 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]; // 0 <-> 11
00114    _val[1] ^= _val[10]; _val[10] ^= _val[1]; _val[1] ^= _val[10]; // 1 <-> 10
00115    _val[2] ^= _val[ 9]; _val[ 9] ^= _val[2]; _val[2] ^= _val[ 9]; // 2 <->  9
00116    _val[3] ^= _val[ 8]; _val[ 8] ^= _val[3]; _val[3] ^= _val[ 8]; // 3 <->  8
00117    _val[4] ^= _val[ 7]; _val[ 7] ^= _val[4]; _val[4] ^= _val[ 7]; // 4 <->  7
00118    _val[5] ^= _val[ 6]; _val[ 6] ^= _val[5]; _val[5] ^= _val[ 6]; // 5 <->  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 // wrapper for byte reordering
00133 
00134 // reverting pointers makes no sense, hence forbid it.
00135 template <typename T> inline T* reverse_byte_order(T* t)
00136 {
00137   // Should never reach this point. If so, then some operator were not
00138   // overloaded. Especially check for IO::binary<> specialization on
00139   // custom data types.
00140 //  compile_time_error__cannot_do_that(a);
00141   return t;
00142 }
00143 
00144 //#if defined(OM_CC_MSVC)
00145 inline void compile_time_error__no_fundamental_type()
00146 {
00147   // we should never reach this point
00148   assert(false);
00149 }
00150 //#endif
00151 
00152 // default action for byte reversal: cause an error to avoid
00153 // surprising behaviour!
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 // Instead do specializations for the necessary types
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 // REVERSE_FUNDAMENTAL_TYPE(bool);
00176 // REVERSE_FUNDAMENTAL_TYPE(char);
00177 // REVERSE_FUNDAMENTAL_TYPE(uchar);
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 } // namespace IO
00235 } // namespace OpenMesh
00236 //=============================================================================
00237 #endif // OPENMESH_SR_RBO_HH defined
00238 //=============================================================================
00239 

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .