OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SR_rbo.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 /*===========================================================================*\
36 * *
37 * $Revision: 736 $ *
38 * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08. Okt 2012) $ *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // Helper Functions for binary reading / writing
46 //
47 //=============================================================================
48
49 #ifndef OPENMESH_SR_RBO_HH
50 #define OPENMESH_SR_RBO_HH
51
52
53 //== INCLUDES =================================================================
54
55 #include <OpenMesh/Core/System/config.h>
56 // -------------------- STL
57 #if defined(OM_CC_MIPS)
58 # include <stdio.h> // size_t
59 #else
60 # include <cstdio> // size_t
61 #endif
62 #include <algorithm>
63 #include <typeinfo>
64 // -------------------- OpenMesh
65 #include <OpenMesh/Core/System/omstream.hh>
66 #include <OpenMesh/Core/IO/SR_types.hh>
67 #include <OpenMesh/Core/Utils/GenProg.hh>
68
69 //== NAMESPACES ===============================================================
70
71 namespace OpenMesh {
72 namespace IO {
73
74
75 //=============================================================================
76
77
82
83
84 //-----------------------------------------------------------------------------
85
89 template < size_t N > inline
90 void _reverse_byte_order_N(uint8_t* _val);
91
92 template <> inline
93 void _reverse_byte_order_N<1>(uint8_t* /*_val*/) { }
94
95
96 template <> inline
97 void _reverse_byte_order_N<2>(uint8_t* _val)
98 {
99 _val[0] ^= _val[1]; _val[1] ^= _val[0]; _val[0] ^= _val[1];
100 }
101
102
103 template <> inline
104 void _reverse_byte_order_N<4>(uint8_t* _val)
105 {
106 _val[0] ^= _val[3]; _val[3] ^= _val[0]; _val[0] ^= _val[3]; // 0 <-> 3
107 _val[1] ^= _val[2]; _val[2] ^= _val[1]; _val[1] ^= _val[2]; // 1 <-> 2
108 }
109
110
111 template <> inline
112 void _reverse_byte_order_N<8>(uint8_t* _val)
113 {
114 _val[0] ^= _val[7]; _val[7] ^= _val[0]; _val[0] ^= _val[7]; // 0 <-> 7
115 _val[1] ^= _val[6]; _val[6] ^= _val[1]; _val[1] ^= _val[6]; // 1 <-> 6
116 _val[2] ^= _val[5]; _val[5] ^= _val[2]; _val[2] ^= _val[5]; // 2 <-> 5
117 _val[3] ^= _val[4]; _val[4] ^= _val[3]; _val[3] ^= _val[4]; // 3 <-> 4
118 }
119
120
121 template <> inline
122 void _reverse_byte_order_N<12>(uint8_t* _val)
123 {
124 _val[0] ^= _val[11]; _val[11] ^= _val[0]; _val[0] ^= _val[11]; // 0 <-> 11
125 _val[1] ^= _val[10]; _val[10] ^= _val[1]; _val[1] ^= _val[10]; // 1 <-> 10
126 _val[2] ^= _val[ 9]; _val[ 9] ^= _val[2]; _val[2] ^= _val[ 9]; // 2 <-> 9
127 _val[3] ^= _val[ 8]; _val[ 8] ^= _val[3]; _val[3] ^= _val[ 8]; // 3 <-> 8
128 _val[4] ^= _val[ 7]; _val[ 7] ^= _val[4]; _val[4] ^= _val[ 7]; // 4 <-> 7
129 _val[5] ^= _val[ 6]; _val[ 6] ^= _val[5]; _val[5] ^= _val[ 6]; // 5 <-> 6
130 }
131
132
133 template <> inline
134 void _reverse_byte_order_N<16>(uint8_t* _val)
135 {
136 _reverse_byte_order_N<8>(_val);
137 _reverse_byte_order_N<8>(_val+8);
138 std::swap(*(uint64_t*)_val, *(((uint64_t*)_val)+1));
139 }
140
141
142 //-----------------------------------------------------------------------------
143 // wrapper for byte reordering
144
145 // reverting pointers makes no sense, hence forbid it.
148 template <typename T> inline T* reverse_byte_order(T* t);
149 // Should never reach this point. If so, then some operator were not
150 // overloaded. Especially check for IO::binary<> specialization on
151 // custom data types.
152
153
154 inline void compile_time_error__no_fundamental_type()
155 {
156 // we should never reach this point
157 assert(false);
158 }
159
160 // default action for byte reversal: cause an error to avoid
161 // surprising behaviour!
162 template <typename T> T& reverse_byte_order( T& _t )
163 {
164 omerr() << "Not defined for type " << typeid(T).name() << std::endl;
165 compile_time_error__no_fundamental_type();
166 return _t;
167 }
168
169 template <> inline bool& reverse_byte_order(bool & _t) { return _t; }
170 template <> inline char& reverse_byte_order(char & _t) { return _t; }
171 #if defined(OM_CC_GCC)
172 template <> inline signed char& reverse_byte_order(signed char & _t) { return _t; }
173 #endif
174 template <> inline uchar& reverse_byte_order(uchar& _t) { return _t; }
175
176 // Instead do specializations for the necessary types
177 #define REVERSE_FUNDAMENTAL_TYPE( T ) \
178 template <> inline T& reverse_byte_order( T& _t ) {\
179 _reverse_byte_order_N<sizeof(T)>( reinterpret_cast<uint8_t*>(&_t) ); \
180 return _t; \
181 }
182
183 // REVERSE_FUNDAMENTAL_TYPE(bool)
184 // REVERSE_FUNDAMENTAL_TYPE(char)
185 // REVERSE_FUNDAMENTAL_TYPE(uchar)
186 REVERSE_FUNDAMENTAL_TYPE(int16_t)
187 REVERSE_FUNDAMENTAL_TYPE(uint16_t)
188 // REVERSE_FUNDAMENTAL_TYPE(int)
189 // REVERSE_FUNDAMENTAL_TYPE(uint)
190
191 REVERSE_FUNDAMENTAL_TYPE(unsigned long)
192 REVERSE_FUNDAMENTAL_TYPE(int32_t)
193 REVERSE_FUNDAMENTAL_TYPE(uint32_t)
194 REVERSE_FUNDAMENTAL_TYPE(int64_t)
195 REVERSE_FUNDAMENTAL_TYPE(uint64_t)
196 REVERSE_FUNDAMENTAL_TYPE(float)
197 REVERSE_FUNDAMENTAL_TYPE(double)
198 REVERSE_FUNDAMENTAL_TYPE(long double)
199
200 #undef REVERSE_FUNDAMENTAL_TYPE
201
202 #if 0
203
204 #define REVERSE_VECTORT_TYPE( T ) \
205 template <> inline T& reverse_byte_order(T& _v) {\
206 for (size_t i; i< T::size_; ++i) \
207 _reverse_byte_order_N< sizeof(T::value_type) >( reinterpret_cast<uint8_t*>(&_v[i])); \
208 return _v; \
209 }
210
211 #define REVERSE_VECTORT_TYPES( N ) \
212 REVERSE_VECTORT_TYPE( Vec##N##c ) \
213 REVERSE_VECTORT_TYPE( Vec##N##uc ) \
214 REVERSE_VECTORT_TYPE( Vec##N##s ) \
215 REVERSE_VECTORT_TYPE( Vec##N##us ) \
216 REVERSE_VECTORT_TYPE( Vec##N##i ) \
217 REVERSE_VECTORT_TYPE( Vec##N##ui ) \
218 REVERSE_VECTORT_TYPE( Vec##N##f ) \
219 REVERSE_VECTORT_TYPE( Vec##N##d ) \
220
221 REVERSE_VECTORT_TYPES(1)
222 REVERSE_VECTORT_TYPES(2)
223 REVERSE_VECTORT_TYPES(3)
224 REVERSE_VECTORT_TYPES(4)
225 REVERSE_VECTORT_TYPES(6)
226
227 #undef REVERSE_VECTORT_TYPES
228 #undef REVERSE_VECTORT_TYPE
229
230 #endif
231
232 template <typename T> inline
233 T reverse_byte_order(const T& a)
234 {
235 compile_timer_error__const_means_const(a);
236 return a;
237 }
238
239
241
242
243 //=============================================================================
244 } // namespace IO
245 } // namespace OpenMesh
246 //=============================================================================
247 #endif // OPENMESH_SR_RBO_HH defined
248 //=============================================================================
249