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 
45 //=============================================================================
46 //
47 // Helper Functions for binary reading / writing
48 //
49 //=============================================================================
50 
51 #ifndef OPENMESH_SR_RBO_HH
52 #define OPENMESH_SR_RBO_HH
53 
54 
55 //== INCLUDES =================================================================
56 
58 // -------------------- STL
59 #if defined(OM_CC_MIPS)
60 # include <stdio.h> // size_t
61 #else
62 # include <cstdio> // size_t
63 #endif
64 #include <algorithm>
65 #include <typeinfo>
66 // -------------------- OpenMesh
68 #include <OpenMesh/Core/IO/SR_types.hh>
69 #include <OpenMesh/Core/Utils/GenProg.hh>
70 
71 //== NAMESPACES ===============================================================
72 
73 namespace OpenMesh {
74 namespace IO {
75 
76 
77 //=============================================================================
78 
79 
84 
85 
86 //-----------------------------------------------------------------------------
87 
91 template < size_t N > inline
92 void _reverse_byte_order_N(uint8_t* _val);
93 
94 template <> inline
95 void _reverse_byte_order_N<1>(uint8_t* /*_val*/) { }
96 
97 
98 template <> inline
100 {
101  _val[0] ^= _val[1]; _val[1] ^= _val[0]; _val[0] ^= _val[1];
102 }
103 
104 
105 template <> inline
107 {
108  _val[0] ^= _val[3]; _val[3] ^= _val[0]; _val[0] ^= _val[3]; // 0 <-> 3
109  _val[1] ^= _val[2]; _val[2] ^= _val[1]; _val[1] ^= _val[2]; // 1 <-> 2
110 }
111 
112 
113 template <> inline
115 {
116  _val[0] ^= _val[7]; _val[7] ^= _val[0]; _val[0] ^= _val[7]; // 0 <-> 7
117  _val[1] ^= _val[6]; _val[6] ^= _val[1]; _val[1] ^= _val[6]; // 1 <-> 6
118  _val[2] ^= _val[5]; _val[5] ^= _val[2]; _val[2] ^= _val[5]; // 2 <-> 5
119  _val[3] ^= _val[4]; _val[4] ^= _val[3]; _val[3] ^= _val[4]; // 3 <-> 4
120 }
121 
122 
123 template <> inline
125 {
126  _val[0] ^= _val[11]; _val[11] ^= _val[0]; _val[0] ^= _val[11]; // 0 <-> 11
127  _val[1] ^= _val[10]; _val[10] ^= _val[1]; _val[1] ^= _val[10]; // 1 <-> 10
128  _val[2] ^= _val[ 9]; _val[ 9] ^= _val[2]; _val[2] ^= _val[ 9]; // 2 <-> 9
129  _val[3] ^= _val[ 8]; _val[ 8] ^= _val[3]; _val[3] ^= _val[ 8]; // 3 <-> 8
130  _val[4] ^= _val[ 7]; _val[ 7] ^= _val[4]; _val[4] ^= _val[ 7]; // 4 <-> 7
131  _val[5] ^= _val[ 6]; _val[ 6] ^= _val[5]; _val[5] ^= _val[ 6]; // 5 <-> 6
132 }
133 
134 
135 template <> inline
137 {
139  _reverse_byte_order_N<8>(_val+8);
140  std::swap(*(uint64_t*)_val, *(((uint64_t*)_val)+1));
141 }
142 
143 
144 //-----------------------------------------------------------------------------
145 // wrapper for byte reordering
146 
147 // reverting pointers makes no sense, hence forbid it.
150 template <typename T> inline T* reverse_byte_order(T* t);
151 // Should never reach this point. If so, then some operator were not
152 // overloaded. Especially check for IO::binary<> specialization on
153 // custom data types.
154 
155 
157 {
158  // we should never reach this point
159  assert(false);
160 }
161 
162 // default action for byte reversal: cause an error to avoid
163 // surprising behaviour!
164 template <typename T> T& reverse_byte_order( T& _t )
165 {
166  omerr() << "Not defined for type " << typeid(T).name() << std::endl;
168  return _t;
169 }
170 
171 template <> inline bool& reverse_byte_order(bool & _t) { return _t; }
172 template <> inline char& reverse_byte_order(char & _t) { return _t; }
173 #if defined(OM_CC_GCC)
174 template <> inline signed char& reverse_byte_order(signed char & _t) { return _t; }
175 #endif
176 template <> inline uchar& reverse_byte_order(uchar& _t) { return _t; }
177 
178 // Instead do specializations for the necessary types
179 #define REVERSE_FUNDAMENTAL_TYPE( T ) \
180  template <> inline T& reverse_byte_order( T& _t ) {\
181  _reverse_byte_order_N<sizeof(T)>( reinterpret_cast<uint8_t*>(&_t) ); \
182  return _t; \
183  }
184 
185 // REVERSE_FUNDAMENTAL_TYPE(bool)
186 // REVERSE_FUNDAMENTAL_TYPE(char)
187 // REVERSE_FUNDAMENTAL_TYPE(uchar)
188 REVERSE_FUNDAMENTAL_TYPE(int16_t)
189 REVERSE_FUNDAMENTAL_TYPE(uint16_t)
190 // REVERSE_FUNDAMENTAL_TYPE(int)
191 // REVERSE_FUNDAMENTAL_TYPE(uint)
192 
193 REVERSE_FUNDAMENTAL_TYPE(unsigned long)
194 REVERSE_FUNDAMENTAL_TYPE(int32_t)
195 REVERSE_FUNDAMENTAL_TYPE(uint32_t)
196 REVERSE_FUNDAMENTAL_TYPE(int64_t)
197 REVERSE_FUNDAMENTAL_TYPE(uint64_t)
198 REVERSE_FUNDAMENTAL_TYPE(float)
199 REVERSE_FUNDAMENTAL_TYPE(double)
200 REVERSE_FUNDAMENTAL_TYPE(long double)
201 
202 #undef REVERSE_FUNDAMENTAL_TYPE
203 
204 #if 0
205 
206 #define REVERSE_VECTORT_TYPE( T ) \
207  template <> inline T& reverse_byte_order(T& _v) {\
208  for (size_t i; i< T::size_; ++i) \
209  _reverse_byte_order_N< sizeof(T::value_type) >( reinterpret_cast<uint8_t*>(&_v[i])); \
210  return _v; \
211  }
212 
213 #define REVERSE_VECTORT_TYPES( N ) \
214  REVERSE_VECTORT_TYPE( Vec##N##c ) \
215  REVERSE_VECTORT_TYPE( Vec##N##uc ) \
216  REVERSE_VECTORT_TYPE( Vec##N##s ) \
217  REVERSE_VECTORT_TYPE( Vec##N##us ) \
218  REVERSE_VECTORT_TYPE( Vec##N##i ) \
219  REVERSE_VECTORT_TYPE( Vec##N##ui ) \
220  REVERSE_VECTORT_TYPE( Vec##N##f ) \
221  REVERSE_VECTORT_TYPE( Vec##N##d ) \
222 
223 REVERSE_VECTORT_TYPES(1)
224 REVERSE_VECTORT_TYPES(2)
225 REVERSE_VECTORT_TYPES(3)
226 REVERSE_VECTORT_TYPES(4)
227 REVERSE_VECTORT_TYPES(6)
228 
229 #undef REVERSE_VECTORT_TYPES
230 #undef REVERSE_VECTORT_TYPE
231 
232 #endif
233 
234 template <typename T> inline
235 T reverse_byte_order(const T& a)
236 {
237  compile_timer_error__const_means_const(a);
238  return a;
239 }
240 
241 
243 
244 
245 //=============================================================================
246 } // namespace IO
247 } // namespace OpenMesh
248 //=============================================================================
249 #endif // OPENMESH_SR_RBO_HH defined
250 //=============================================================================
251 
T * reverse_byte_order(T *t)
unsigned char uint8_t
Definition: SR_types.hh:80
void _reverse_byte_order_N(uint8_t *_val)
unsigned long long uint64_t
Definition: SR_types.hh:89
unsigned int uint32_t
Definition: SR_types.hh:85
void compile_time_error__no_fundamental_type()
Definition: SR_rbo.hh:156
long long int64_t
Definition: SR_types.hh:89
void _reverse_byte_order_N< 1 >(uint8_t *)
Definition: SR_rbo.hh:95
void _reverse_byte_order_N< 4 >(uint8_t *_val)
Definition: SR_rbo.hh:106
unsigned short uint16_t
Definition: SR_types.hh:81
unsigned char uchar
Definition: SR_types.hh:76
void _reverse_byte_order_N< 12 >(uint8_t *_val)
Definition: SR_rbo.hh:124
short int16_t
Definition: SR_types.hh:81
void _reverse_byte_order_N< 2 >(uint8_t *_val)
Definition: SR_rbo.hh:99
void _reverse_byte_order_N< 16 >(uint8_t *_val)
Definition: SR_rbo.hh:136
void _reverse_byte_order_N< 8 >(uint8_t *_val)
Definition: SR_rbo.hh:114