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_binary_spec.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: 842 $ *
38 * $Date: 2013-07-23 15:27:49 +0200 (Di, 23. Jul 2013) $ *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // Helper Functions for binary reading / writing
46 //
47 //=============================================================================
48
49 #ifndef OPENMESH_SR_BINARY_SPEC_HH
50 #define OPENMESH_SR_BINARY_SPEC_HH
51
52 //== INCLUDES =================================================================
53
54 #include <OpenMesh/Core/System/config.h>
55 // -------------------- STL
56 #include <iterator>
57 #include <string>
58 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
59 # include <OpenMesh/Tools/Utils/NumLimitsT.hh>
60 #else
61 # include <limits>
62 #endif
63 #include <vector>
64 #include <stdexcept> // logic_error
65 #include <numeric> // accumulate
66 // -------------------- OpenMesh
67 #include <OpenMesh/Core/Geometry/VectorT.hh>
68 #include <OpenMesh/Core/Mesh/Status.hh>
69 #include <OpenMesh/Core/IO/SR_types.hh>
70 #include <OpenMesh/Core/IO/SR_rbo.hh>
71 #include <OpenMesh/Core/IO/SR_binary.hh>
72
73 //== NAMESPACES ===============================================================
74
75 namespace OpenMesh {
76 namespace IO {
77
78
79 //=============================================================================
80
81 #ifndef DOXY_IGNORE_THIS
82
83 //-----------------------------------------------------------------------------
84 // struct binary, helper for storing/restoring
85
86 #define SIMPLE_BINARY( T ) \
87 template <> struct binary< T > { \
88 typedef T value_type; \
89 static const bool is_streamable = true; \
90 static size_t size_of(const value_type&) { return sizeof(value_type); } \
91 static size_t size_of(void) { return sizeof(value_type); } \
92 static size_t store( std::ostream& _os, const value_type& _val, \
93 bool _swap=false) { \
94 value_type tmp = _val; \
95 if (_swap) reverse_byte_order(tmp); \
96 _os.write( (const char*)&tmp, sizeof(value_type) ); \
97 return _os.good() ? sizeof(value_type) : 0; \
98 } \
99 \
100 static size_t restore( std::istream& _is, value_type& _val, \
101 bool _swap=false) { \
102 _is.read( (char*)&_val, sizeof(value_type) ); \
103 if (_swap) reverse_byte_order(_val); \
104 return _is.good() ? sizeof(value_type) : 0; \
105 } \
106 }
107
108 SIMPLE_BINARY(bool);
109 //SIMPLE_BINARY(int);
110
111 // Why is this needed? Should not be used as not 32 bit compatible
112 //SIMPLE_BINARY(unsigned long);
113 SIMPLE_BINARY(float);
114 SIMPLE_BINARY(double);
115 SIMPLE_BINARY(long double);
116
117 SIMPLE_BINARY(int8_t);
118 SIMPLE_BINARY(int16_t);
119 SIMPLE_BINARY(int32_t);
120 SIMPLE_BINARY(int64_t);
121 SIMPLE_BINARY(uint8_t);
122 SIMPLE_BINARY(uint16_t);
123 SIMPLE_BINARY(uint32_t);
124 SIMPLE_BINARY(uint64_t);
125
126 #undef SIMPLE_BINARY
127
128 // For unsigned long which is of size 64 bit on 64 bit
129 // architectures: convert into 32 bit unsigned integer value
130 // in order to stay compatible between 32/64 bit architectures.
131 // This allows cross reading BUT forbids storing unsigned longs
132 // as data type since higher order word (4 bytes) will be truncated.
133 // Does not work in case the data type that is to be stored
134 // exceeds the value range of unsigned int in size, which is improbable...
135
136 #define SIMPLE_BINARY( T ) \
137 template <> struct binary< T > { \
138 typedef T value_type; \
139 static const bool is_streamable = true; \
140 static size_t size_of(const value_type&) { return sizeof(value_type); } \
141 static size_t size_of(void) { return sizeof(value_type); } \
142 static size_t store( std::ostream& _os, const value_type& _val, \
143 bool _swap=false) { \
144 value_type tmp = _val; \
145 if (_swap) reverse_byte_order(tmp); \
146 /* Convert unsigned long to unsigned int for compatibility reasons */ \
147 unsigned int t1 = static_cast<unsigned int>(tmp); \
148 _os.write( (const char*)&t1, sizeof(unsigned int) ); \
149 return _os.good() ? sizeof(unsigned int) : 0; \
150 } \
151 \
152 static size_t restore( std::istream& _is, value_type& _val, \
153 bool _swap=false) { \
154 unsigned int t1; \
155 _is.read( (char*)&t1, sizeof(unsigned int) ); \
156 _val = t1; \
157 if (_swap) reverse_byte_order(_val); \
158 return _is.good() ? sizeof(unsigned int) : 0; \
159 } \
160 }
161
162 SIMPLE_BINARY(unsigned long);
163
164 #undef SIMPLE_BINARY
165
166 #define VECTORT_BINARY( T ) \
167 template <> struct binary< T > { \
168 typedef T value_type; \
169 static const bool is_streamable = true; \
170 static size_t size_of(void) { return sizeof(value_type); } \
171 static size_t size_of(const value_type&) { return size_of(); } \
172 static size_t store( std::ostream& _os, const value_type& _val, \
173 bool _swap=false) { \
174 value_type tmp = _val; \
175 size_t i, b = size_of(_val), N = value_type::size_; \
176 if (_swap) for (i=0; i<N; ++i) \
177 reverse_byte_order( tmp[i] ); \
178 _os.write( (const char*)&tmp[0], b ); \
179 return _os.good() ? b : 0; \
180 } \
181 \
182 static size_t restore( std::istream& _is, value_type& _val, \
183 bool _swap=false) { \
184 size_t i, N=value_type::size_; \
185 size_t b = N * sizeof(value_type::value_type); \
186 _is.read( (char*)&_val[0], b ); \
187 if (_swap) for (i=0; i<N; ++i) \
188 reverse_byte_order( _val[i] ); \
189 return _is.good() ? b : 0; \
190 } \
191 }
192
193 #define VECTORTS_BINARY( N ) \
194 VECTORT_BINARY( Vec##N##c ); \
195 VECTORT_BINARY( Vec##N##uc ); \
196 VECTORT_BINARY( Vec##N##s ); \
197 VECTORT_BINARY( Vec##N##us ); \
198 VECTORT_BINARY( Vec##N##i ); \
199 VECTORT_BINARY( Vec##N##ui ); \
200 VECTORT_BINARY( Vec##N##f ); \
201 VECTORT_BINARY( Vec##N##d );
202
203 VECTORTS_BINARY( 1 )
204 VECTORTS_BINARY( 2 )
205 VECTORTS_BINARY( 3 )
206 VECTORTS_BINARY( 4 )
207 VECTORTS_BINARY( 6 )
208
209 #undef VECTORTS_BINARY
210 #undef VECTORT_BINARY
211
212 template <> struct binary< std::string > {
213 typedef std::string value_type;
214 typedef uint16_t length_t;
215
216 static const bool is_streamable = true;
217
218 static size_t size_of() { return UnknownSize; }
219 static size_t size_of(const value_type &_v)
220 { return sizeof(length_t) + _v.size(); }
221
222 static
223 size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
224 {
225 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
226 if (_v.size() < Utils::NumLimitsT<length_t>::max() )
227 #else
228 if (_v.size() < std::numeric_limits<length_t>::max() )
229 #endif
230 {
231 length_t len = length_t(_v.size());
232
233 if (_swap) reverse_byte_order(len);
234
235 size_t bytes = binary<length_t>::store( _os, len, _swap );
236 _os.write( _v.data(), len );
237 return _os.good() ? len+bytes : 0;
238 }
239 throw std::runtime_error("Cannot store string longer than 64Kb");
240 }
241
242 static
243 size_t restore(std::istream& _is, value_type& _val, bool _swap=false)
244 {
245 length_t len;
246 size_t bytes = binary<length_t>::restore( _is, len, _swap );
247 if (_swap)
248 reverse_byte_order(len);
249 _val.resize(len);
250 _is.read( const_cast<char*>(_val.data()), len );
251
252 return _is.good() ? (len+bytes) : 0;
253 }
254 };
255
256
257 template <> struct binary<OpenMesh::Attributes::StatusInfo>
258 {
259 typedef OpenMesh::Attributes::StatusInfo value_type;
260 typedef value_type::value_type status_t;
261
262 static const bool is_streamable = true;
263
264 static size_t size_of() { return sizeof(status_t); }
265 static size_t size_of(const value_type&) { return size_of(); }
266
267 static size_t n_bytes(size_t _n_elem)
268 { return _n_elem*sizeof(status_t); }
269
270 static
271 size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
272 {
273 status_t v=_v.bits();
274 return binary<status_t>::store(_os, v, _swap);
275 }
276
277 static
278 size_t restore( std::istream& _os, value_type& _v, bool _swap=false)
279 {
280 status_t v;
281 size_t b = binary<status_t>::restore(_os, v, _swap);
282 _v.set_bits(v);
283 return b;
284 }
285 };
286
287
288 //-----------------------------------------------------------------------------
289 // std::vector<T> specializations for struct binary<>
290
291 template <typename T>
292 struct FunctorStore {
293 FunctorStore( std::ostream& _os, bool _swap) : os_(_os), swap_(_swap) { }
294 size_t operator () ( size_t _v1, const T& _s2 )
295 { return _v1+binary<T>::store(os_, _s2, swap_ ); }
296
297 std::ostream& os_;
298 bool swap_;
299 };
300
301
302 template <typename T>
303 struct FunctorRestore {
304 FunctorRestore( std::istream& _is, bool _swap) : is_(_is), swap_(_swap) { }
305 size_t operator () ( size_t _v1, T& _s2 )
306 { return _v1+binary<T>::restore(is_, _s2, swap_ ); }
307 std::istream& is_;
308 bool swap_;
309 };
310
311 #include <OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl>
312 #include <OpenMesh/Core/IO/SR_binary_vector_of_string.inl>
313 #include <OpenMesh/Core/IO/SR_binary_vector_of_bool.inl>
314
315 // ----------------------------------------------------------------------------
316
317 #endif // DOXY_IGNORE_THIS
318
319 //=============================================================================
320 } // namespace IO
321 } // namespace OpenMesh
322 //=============================================================================
323 #endif // OPENMESH_SR_BINARY_SPEC_HH defined
324 //=============================================================================
325