Developer Documentation
SR_rbo.hh
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 
50 //=============================================================================
51 //
52 // Helper Functions for binary reading / writing
53 //
54 //=============================================================================
55 
56 #ifndef OPENMESH_SR_RBO_HH
57 #define OPENMESH_SR_RBO_HH
58 
59 
60 //== INCLUDES =================================================================
61 
63 // -------------------- STL
64 #if defined(OM_CC_MIPS)
65 # include <stdio.h> // size_t
66 #else
67 # include <cstdio> // size_t
68 #endif
69 #include <algorithm>
70 #include <typeinfo>
71 // -------------------- OpenMesh
73 #include <OpenMesh/Core/IO/SR_types.hh>
74 #include <OpenMesh/Core/Utils/GenProg.hh>
75 
76 //== NAMESPACES ===============================================================
77 
78 namespace OpenMesh {
79 namespace IO {
80 
81 
82 //=============================================================================
83 
84 
89 
90 
91 //-----------------------------------------------------------------------------
92 
96 template < size_t N > inline
97 void _reverse_byte_order_N(uint8_t* _val);
98 
99 template <> inline
101 
102 
103 template <> inline
105 {
106  _val[0] ^= _val[1]; _val[1] ^= _val[0]; _val[0] ^= _val[1];
107 }
108 
109 
110 template <> inline
112 {
113  _val[0] ^= _val[3]; _val[3] ^= _val[0]; _val[0] ^= _val[3]; // 0 <-> 3
114  _val[1] ^= _val[2]; _val[2] ^= _val[1]; _val[1] ^= _val[2]; // 1 <-> 2
115 }
116 
117 
118 template <> inline
120 {
121  _val[0] ^= _val[7]; _val[7] ^= _val[0]; _val[0] ^= _val[7]; // 0 <-> 7
122  _val[1] ^= _val[6]; _val[6] ^= _val[1]; _val[1] ^= _val[6]; // 1 <-> 6
123  _val[2] ^= _val[5]; _val[5] ^= _val[2]; _val[2] ^= _val[5]; // 2 <-> 5
124  _val[3] ^= _val[4]; _val[4] ^= _val[3]; _val[3] ^= _val[4]; // 3 <-> 4
125 }
126 
127 
128 template <> inline
130 {
131  _val[0] ^= _val[11]; _val[11] ^= _val[0]; _val[0] ^= _val[11]; // 0 <-> 11
132  _val[1] ^= _val[10]; _val[10] ^= _val[1]; _val[1] ^= _val[10]; // 1 <-> 10
133  _val[2] ^= _val[ 9]; _val[ 9] ^= _val[2]; _val[2] ^= _val[ 9]; // 2 <-> 9
134  _val[3] ^= _val[ 8]; _val[ 8] ^= _val[3]; _val[3] ^= _val[ 8]; // 3 <-> 8
135  _val[4] ^= _val[ 7]; _val[ 7] ^= _val[4]; _val[4] ^= _val[ 7]; // 4 <-> 7
136  _val[5] ^= _val[ 6]; _val[ 6] ^= _val[5]; _val[5] ^= _val[ 6]; // 5 <-> 6
137 }
138 
139 
140 template <> inline
142 {
144  _reverse_byte_order_N<8>(_val+8);
145  std::swap(*(uint64_t*)_val, *(((uint64_t*)_val)+1));
146 }
147 
148 
149 //-----------------------------------------------------------------------------
150 // wrapper for byte reordering
151 
152 // reverting pointers makes no sense, hence forbid it.
155 template <typename T> inline T* reverse_byte_order(T* t);
156 // Should never reach this point. If so, then some operator were not
157 // overloaded. Especially check for IO::binary<> specialization on
158 // custom data types.
159 
160 
162 {
163  // we should never reach this point
164  assert(false);
165 }
166 
167 // default action for byte reversal: cause an error to avoid
168 // surprising behaviour!
169 template <typename T> T& reverse_byte_order( T& _t )
170 {
171  omerr() << "Not defined for type " << typeid(T).name() << std::endl;
173  return _t;
174 }
175 
176 template <> inline bool& reverse_byte_order(bool & _t) { return _t; }
177 template <> inline char& reverse_byte_order(char & _t) { return _t; }
178 #if defined(OM_CC_GCC)
179 template <> inline signed char& reverse_byte_order(signed char & _t) { return _t; }
180 #endif
181 template <> inline uchar& reverse_byte_order(uchar& _t) { return _t; }
182 
183 // Instead do specializations for the necessary types
184 #define REVERSE_FUNDAMENTAL_TYPE( T ) \
185  template <> inline T& reverse_byte_order( T& _t ) {\
186  _reverse_byte_order_N<sizeof(T)>( reinterpret_cast<uint8_t*>(&_t) ); \
187  return _t; \
188  }
189 
190 // REVERSE_FUNDAMENTAL_TYPE(bool)
191 // REVERSE_FUNDAMENTAL_TYPE(char)
192 // REVERSE_FUNDAMENTAL_TYPE(uchar)
193 REVERSE_FUNDAMENTAL_TYPE(int16_t)
194 REVERSE_FUNDAMENTAL_TYPE(uint16_t)
195 // REVERSE_FUNDAMENTAL_TYPE(int)
196 // REVERSE_FUNDAMENTAL_TYPE(uint)
197 
198 REVERSE_FUNDAMENTAL_TYPE(unsigned long)
199 REVERSE_FUNDAMENTAL_TYPE(int32_t)
200 REVERSE_FUNDAMENTAL_TYPE(uint32_t)
201 REVERSE_FUNDAMENTAL_TYPE(int64_t)
202 REVERSE_FUNDAMENTAL_TYPE(uint64_t)
203 REVERSE_FUNDAMENTAL_TYPE(float)
204 REVERSE_FUNDAMENTAL_TYPE(double)
205 REVERSE_FUNDAMENTAL_TYPE(long double)
206 
207 #undef REVERSE_FUNDAMENTAL_TYPE
208 
209 #if 0
210 
211 #define REVERSE_VECTORT_TYPE( T ) \
212  template <> inline T& reverse_byte_order(T& _v) {\
213  for (size_t i; i< T::size_; ++i) \
214  _reverse_byte_order_N< sizeof(T::value_type) >( reinterpret_cast<uint8_t*>(&_v[i])); \
215  return _v; \
216  }
217 
218 #define REVERSE_VECTORT_TYPES( N ) \
219  REVERSE_VECTORT_TYPE( Vec##N##c ) \
220  REVERSE_VECTORT_TYPE( Vec##N##uc ) \
221  REVERSE_VECTORT_TYPE( Vec##N##s ) \
222  REVERSE_VECTORT_TYPE( Vec##N##us ) \
223  REVERSE_VECTORT_TYPE( Vec##N##i ) \
224  REVERSE_VECTORT_TYPE( Vec##N##ui ) \
225  REVERSE_VECTORT_TYPE( Vec##N##f ) \
226  REVERSE_VECTORT_TYPE( Vec##N##d ) \
227 
228 REVERSE_VECTORT_TYPES(1)
229 REVERSE_VECTORT_TYPES(2)
230 REVERSE_VECTORT_TYPES(3)
231 REVERSE_VECTORT_TYPES(4)
232 REVERSE_VECTORT_TYPES(6)
233 
234 #undef REVERSE_VECTORT_TYPES
235 #undef REVERSE_VECTORT_TYPE
236 
237 #endif
238 
239 template <typename T> inline
240 T reverse_byte_order(const T& a)
241 {
242  compile_timer_error__const_means_const(a);
243  return a;
244 }
245 
246 
248 
249 
250 //=============================================================================
251 } // namespace IO
252 } // namespace OpenMesh
253 //=============================================================================
254 #endif // OPENMESH_SR_RBO_HH defined
255 //=============================================================================
256 
unsigned long long uint64_t
Definition: SR_types.hh:94
unsigned char uchar
Definition: SR_types.hh:81
void _reverse_byte_order_N< 2 >(uint8_t *_val)
Definition: SR_rbo.hh:104
short int16_t
Definition: SR_types.hh:86
void _reverse_byte_order_N< 8 >(uint8_t *_val)
Definition: SR_rbo.hh:119
unsigned short uint16_t
Definition: SR_types.hh:86
void _reverse_byte_order_N< 12 >(uint8_t *_val)
Definition: SR_rbo.hh:129
void compile_time_error__no_fundamental_type()
Definition: SR_rbo.hh:161
void _reverse_byte_order_N(uint8_t *_val)
unsigned char uint8_t
Definition: SR_types.hh:85
void _reverse_byte_order_N< 4 >(uint8_t *_val)
Definition: SR_rbo.hh:111
T * reverse_byte_order(T *t)
unsigned int uint32_t
Definition: SR_types.hh:90
long long int64_t
Definition: SR_types.hh:94
void _reverse_byte_order_N< 1 >(uint8_t *)
Definition: SR_rbo.hh:100
void _reverse_byte_order_N< 16 >(uint8_t *_val)
Definition: SR_rbo.hh:141