Panda3D
streamReader.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file streamReader.I
10  * @author drose
11  * @date 2002-08-04
12  */
13 
14 /**
15  *
16  */
17 INLINE StreamReader::
18 StreamReader(std::istream &in) :
19  _in(&in),
20  _owns_stream(false)
21 {
22 }
23 
24 /**
25  * If owns_stream is true, the stream pointer will be deleted when the
26  * StreamReader destructs.
27  */
28 INLINE StreamReader::
29 StreamReader(std::istream *in, bool owns_stream) :
30  _in(in),
31  _owns_stream(owns_stream)
32 {
33 }
34 
35 /**
36  * The copy constructor does not copy ownership of the stream.
37  */
38 INLINE StreamReader::
39 StreamReader(const StreamReader &copy) :
40  _in(copy._in),
41  _owns_stream(false)
42 {
43 }
44 
45 /**
46  * The copy constructor does not copy ownership of the stream.
47  */
48 INLINE void StreamReader::
49 operator = (const StreamReader &copy) {
50  if (_owns_stream) {
51  delete _in;
52  }
53  _in = copy._in;
54  _owns_stream = false;
55 }
56 
57 /**
58  *
59  */
60 INLINE StreamReader::
61 ~StreamReader() {
62  if (_owns_stream) {
63  delete _in;
64  }
65 }
66 
67 /**
68  * Returns the stream in use.
69  */
70 INLINE std::istream *StreamReader::
71 get_istream() const {
72  return _in;
73 }
74 
75 /**
76  * Extracts a boolean value.
77  */
78 INLINE bool StreamReader::
80  return (get_uint8() != 0);
81 }
82 
83 /**
84  * Extracts a signed 8-bit integer.
85  */
86 INLINE int8_t StreamReader::
88  return (int8_t)_in->get();
89 }
90 
91 /**
92  * Extracts an unsigned 8-bit integer.
93  */
94 INLINE uint8_t StreamReader::
96  return (uint8_t)_in->get();
97 }
98 
99 /**
100  * Extracts a signed 16-bit integer.
101  */
102 INLINE int16_t StreamReader::
104  int16_t readval, retval;
105  _in->read((char *)&readval, sizeof(readval));
106  LittleEndian s(&readval, 0, sizeof(readval));
107  s.store_value(&retval, sizeof(retval));
108  return retval;
109 }
110 
111 /**
112  * Extracts a signed 32-bit integer.
113  */
114 INLINE int32_t StreamReader::
116  int32_t readval, retval;
117  _in->read((char *)&readval, sizeof(readval));
118  LittleEndian s(&readval, 0, sizeof(readval));
119  s.store_value(&retval, sizeof(retval));
120  return retval;
121 }
122 
123 /**
124  * Extracts a signed 64-bit integer.
125  */
126 INLINE int64_t StreamReader::
128  int64_t readval, retval;
129  _in->read((char *)&readval, sizeof(readval));
130  LittleEndian s(&readval, 0, sizeof(readval));
131  s.store_value(&retval, sizeof(retval));
132  return retval;
133 }
134 
135 /**
136  * Extracts an unsigned 16-bit integer.
137  */
138 INLINE uint16_t StreamReader::
140  uint16_t readval, retval;
141  _in->read((char *)&readval, sizeof(readval));
142  LittleEndian s(&readval, 0, sizeof(readval));
143  s.store_value(&retval, sizeof(retval));
144  return retval;
145 }
146 
147 /**
148  * Extracts an unsigned 32-bit integer.
149  */
150 INLINE uint32_t StreamReader::
152  uint32_t readval, retval;
153  _in->read((char *)&readval, sizeof(readval));
154  LittleEndian s(&readval, 0, sizeof(readval));
155  s.store_value(&retval, sizeof(retval));
156  return retval;
157 }
158 
159 /**
160  * Extracts an unsigned 64-bit integer.
161  */
162 INLINE uint64_t StreamReader::
164  uint64_t readval, retval;
165  _in->read((char *)&readval, sizeof(readval));
166  LittleEndian s(&readval, 0, sizeof(readval));
167  s.store_value(&retval, sizeof(retval));
168  return retval;
169 }
170 
171 /**
172  * Extracts a 32-bit single-precision floating-point number. Since this kind
173  * of float is not necessarily portable across different architectures,
174  * special care is required.
175  */
176 INLINE float StreamReader::
178  // For now, we assume the float format is portable across all architectures
179  // we are concerned with. If we come across one that is different, we will
180  // have to convert.
181  nassertr(sizeof(float) == 4, 0.0f);
182 
183  float readval, retval;
184  _in->read((char *)&readval, sizeof(readval));
185  LittleEndian s(&readval, 0, sizeof(readval));
186  s.store_value(&retval, sizeof(retval));
187  return retval;
188 }
189 
190 /**
191  * Extracts a 64-bit floating-point number.
192  */
193 INLINE PN_float64 StreamReader::
195  PN_float64 readval, retval;
196  _in->read((char *)&readval, sizeof(readval));
197  LittleEndian s(&readval, 0, sizeof(readval));
198  s.store_value(&retval, sizeof(retval));
199  return retval;
200 }
201 
202 /**
203  * Extracts a signed big-endian 16-bit integer.
204  */
205 INLINE int16_t StreamReader::
207  int16_t readval, retval;
208  _in->read((char *)&readval, sizeof(readval));
209  BigEndian s(&readval, 0, sizeof(readval));
210  s.store_value(&retval, sizeof(retval));
211  return retval;
212 }
213 
214 /**
215  * Extracts a signed big-endian 32-bit integer.
216  */
217 INLINE int32_t StreamReader::
219  int32_t readval, retval;
220  _in->read((char *)&readval, sizeof(readval));
221  BigEndian s(&readval, 0, sizeof(readval));
222  s.store_value(&retval, sizeof(retval));
223  return retval;
224 }
225 
226 /**
227  * Extracts a signed big-endian 64-bit integer.
228  */
229 INLINE int64_t StreamReader::
231  int64_t readval, retval;
232  _in->read((char *)&readval, sizeof(readval));
233  BigEndian s(&readval, 0, sizeof(readval));
234  s.store_value(&retval, sizeof(retval));
235  return retval;
236 }
237 
238 /**
239  * Extracts an unsigned big-endian 16-bit integer.
240  */
241 INLINE uint16_t StreamReader::
243  uint16_t readval, retval;
244  _in->read((char *)&readval, sizeof(readval));
245  BigEndian s(&readval, 0, sizeof(readval));
246  s.store_value(&retval, sizeof(retval));
247  return retval;
248 }
249 
250 /**
251  * Extracts an unsigned big-endian 32-bit integer.
252  */
253 INLINE uint32_t StreamReader::
255  uint32_t readval, retval;
256  _in->read((char *)&readval, sizeof(readval));
257  BigEndian s(&readval, 0, sizeof(readval));
258  s.store_value(&retval, sizeof(retval));
259  return retval;
260 }
261 
262 /**
263  * Extracts an unsigned big-endian 64-bit integer.
264  */
265 INLINE uint64_t StreamReader::
267  uint64_t readval, retval;
268  _in->read((char *)&readval, sizeof(readval));
269  BigEndian s(&readval, 0, sizeof(readval));
270  s.store_value(&retval, sizeof(retval));
271  return retval;
272 }
273 
274 /**
275  * Extracts a 32-bit single-precision big-endian floating-point number. Since
276  * this kind of float is not necessarily portable across different
277  * architectures, special care is required.
278  */
279 INLINE float StreamReader::
281  // For now, we assume the float format is portable across all architectures
282  // we are concerned with. If we come across one that is different, we will
283  // have to convert.
284  nassertr(sizeof(float) == 4, 0.0f);
285 
286  float readval, retval;
287  _in->read((char *)&readval, sizeof(readval));
288  BigEndian s(&readval, 0, sizeof(readval));
289  s.store_value(&retval, sizeof(retval));
290  return retval;
291 }
292 
293 /**
294  * Extracts a 64-bit big-endian floating-point number.
295  */
296 INLINE PN_float64 StreamReader::
298  PN_float64 readval, retval;
299  _in->read((char *)&readval, sizeof(readval));
300  BigEndian s(&readval, 0, sizeof(readval));
301  s.store_value(&retval, sizeof(retval));
302  return retval;
303 }
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
Definition: streamReader.I:139
int64_t get_int64()
Extracts a signed 64-bit integer.
Definition: streamReader.I:127
int32_t get_int32()
Extracts a signed 32-bit integer.
Definition: streamReader.I:115
int16_t get_be_int16()
Extracts a signed big-endian 16-bit integer.
Definition: streamReader.I:206
uint16_t get_be_uint16()
Extracts an unsigned big-endian 16-bit integer.
Definition: streamReader.I:242
uint64_t get_uint64()
Extracts an unsigned 64-bit integer.
Definition: streamReader.I:163
int32_t get_be_int32()
Extracts a signed big-endian 32-bit integer.
Definition: streamReader.I:218
PN_float64 get_be_float64()
Extracts a 64-bit big-endian floating-point number.
Definition: streamReader.I:297
int64_t get_be_int64()
Extracts a signed big-endian 64-bit integer.
Definition: streamReader.I:230
float get_be_float32()
Extracts a 32-bit single-precision big-endian floating-point number.
Definition: streamReader.I:280
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
Definition: streamReader.I:151
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
uint32_t get_be_uint32()
Extracts an unsigned big-endian 32-bit integer.
Definition: streamReader.I:254
PN_float64 get_float64()
Extracts a 64-bit floating-point number.
Definition: streamReader.I:194
float get_float32()
Extracts a 32-bit single-precision floating-point number.
Definition: streamReader.I:177
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
void store_value(void *dest, size_t length) const
Copies the data, with byte reversal if appropriate, into the indicated numeric variable,...
int16_t get_int16()
Extracts a signed 16-bit integer.
Definition: streamReader.I:103
int8_t get_int8()
Extracts a signed 8-bit integer.
Definition: streamReader.I:87
void operator=(const StreamReader &copy)
The copy constructor does not copy ownership of the stream.
Definition: streamReader.I:49
A class to read sequential binary data directly from an istream.
Definition: streamReader.h:28
void store_value(void *dest, size_t length) const
Copies the data, with byte reversal if appropriate, into the indicated numeric variable,...
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
Definition: streamReader.I:95
uint64_t get_be_uint64()
Extracts an unsigned big-endian 64-bit integer.
Definition: streamReader.I:266
bool get_bool()
Extracts a boolean value.
Definition: streamReader.I:79