Developer Documentation

OMFormatT_impl.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
52 #define OPENMESH_IO_OMFORMAT_CC
53
54
55 //== INCLUDES =================================================================
56
57 #include <OpenMesh/Core/IO/OMFormat.hh>
58 #include <algorithm>
59 #include <iomanip>
60
61 //== NAMESPACES ===============================================================
62
63 namespace OpenMesh {
64 namespace IO {
65
66 // helper to store a an integer
67 template< typename T >
68 size_t
69 store( std::ostream& _os,
70 const T& _val,
71 OMFormat::Chunk::Integer_Size _b,
72 bool _swap,
73 t_signed)
74 {
75 assert( OMFormat::is_integer( _val ) );
76
77 switch( _b )
78 {
79 case OMFormat::Chunk::Integer_8:
80 {
81 OMFormat::int8 v = static_cast<OMFormat::int8>(_val);
82 return store( _os, v, _swap );
83 }
84 case OMFormat::Chunk::Integer_16:
85 {
86 OMFormat::int16 v = static_cast<OMFormat::int16>(_val);
87 return store( _os, v, _swap );
88 }
89 case OMFormat::Chunk::Integer_32:
90 {
91 OMFormat::int32 v = static_cast<OMFormat::int32>(_val);
92 return store( _os, v, _swap );
93 }
94 case OMFormat::Chunk::Integer_64:
95 {
96 OMFormat::int64 v = static_cast<OMFormat::int64>(_val);
97 return store( _os, v, _swap );
98 }
99 }
100 return 0;
101 }
102
103
104 // helper to store a an unsigned integer
105 template< typename T >
106 size_t
107 store( std::ostream& _os,
108 const T& _val,
109 OMFormat::Chunk::Integer_Size _b,
110 bool _swap,
111 t_unsigned)
112 {
113 assert( OMFormat::is_integer( _val ) );
114
115 switch( _b )
116 {
117 case OMFormat::Chunk::Integer_8:
118 {
119 OMFormat::uint8 v = static_cast<OMFormat::uint8>(_val);
120 return store( _os, v, _swap );
121 }
122 case OMFormat::Chunk::Integer_16:
123 {
124 OMFormat::uint16 v = static_cast<OMFormat::uint16>(_val);
125 return store( _os, v, _swap );
126 }
127 case OMFormat::Chunk::Integer_32:
128 {
129 OMFormat::uint32 v = static_cast<OMFormat::uint32>(_val);
130 return store( _os, v, _swap );
131 }
132
133 case OMFormat::Chunk::Integer_64:
134 {
135 OMFormat::uint64 v = static_cast<OMFormat::uint64>(_val);
136 return store( _os, v, _swap );
137 }
138 }
139 return 0;
140 }
141
142
143 // helper to restore a an integer
144 template< typename T >
145 size_t
146 restore( std::istream& _is,
147 T& _val,
148 OMFormat::Chunk::Integer_Size _b,
149 bool _swap,
150 t_signed)
151 {
152 assert( OMFormat::is_integer( _val ) );
153 size_t bytes = 0;
154
155 switch( _b )
156 {
157 case OMFormat::Chunk::Integer_8:
158 {
159 OMFormat::int8 v;
160 bytes = restore( _is, v, _swap );
161 _val = static_cast<T>(v);
162 break;
163 }
164 case OMFormat::Chunk::Integer_16:
165 {
166 OMFormat::int16 v;
167 bytes = restore( _is, v, _swap );
168 _val = static_cast<T>(v);
169 break;
170 }
171 case OMFormat::Chunk::Integer_32:
172 {
173 OMFormat::int32 v;
174 bytes = restore( _is, v, _swap );
175 _val = static_cast<T>(v);
176 break;
177 }
178 case OMFormat::Chunk::Integer_64:
179 {
180 OMFormat::int64 v;
181 bytes = restore( _is, v, _swap );
182 _val = static_cast<T>(v);
183 break;
184 }
185 }
186 return bytes;
187 }
188
189
190 // helper to restore a an unsigned integer
191 template< typename T >
192 size_t
193 restore( std::istream& _is,
194 T& _val,
195 OMFormat::Chunk::Integer_Size _b,
196 bool _swap,
197 t_unsigned)
198 {
199 assert( OMFormat::is_integer( _val ) );
200 size_t bytes = 0;
201
202 switch( _b )
203 {
204 case OMFormat::Chunk::Integer_8:
205 {
206 OMFormat::uint8 v;
207 bytes = restore( _is, v, _swap );
208 _val = static_cast<T>(v);
209 break;
210 }
211 case OMFormat::Chunk::Integer_16:
212 {
213 OMFormat::uint16 v;
214 bytes = restore( _is, v, _swap );
215 _val = static_cast<T>(v);
216 break;
217 }
218 case OMFormat::Chunk::Integer_32:
219 {
220 OMFormat::uint32 v;
221 bytes = restore( _is, v, _swap );
222 _val = static_cast<T>(v);
223 break;
224 }
225
226 case OMFormat::Chunk::Integer_64:
227 {
228 OMFormat::uint64 v;
229 bytes = restore( _is, v, _swap );
230 _val = static_cast<T>(v);
231 break;
232 }
233 }
234 return bytes;
235 }
236
237 //=============================================================================
238 } // namespace IO
239 } // namespace OpenMesh
240 //=============================================================================
OpenMesh
Definition: MeshItems.hh:59