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