Panda3D
 All Classes Functions Variables Enumerations
streamWriter.I
1 // Filename: streamWriter.I
2 // Created by: drose (04Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: StreamWriter::Constructor
18 // Access: Public
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE StreamWriter::
22 StreamWriter(ostream &out) :
23  _out(&out),
24  _owns_stream(false)
25 {
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: StreamWriter::Constructor
30 // Access: Published
31 // Description:
32 ////////////////////////////////////////////////////////////////////
33 INLINE StreamWriter::
34 StreamWriter(ostream *out, bool owns_stream) :
35  _out(out),
36  _owns_stream(owns_stream)
37 {
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: StreamWriter::Copy Constructor
42 // Access: Published
43 // Description: The copy constructor does not copy ownership of the
44 // stream.
45 ////////////////////////////////////////////////////////////////////
46 INLINE StreamWriter::
47 StreamWriter(const StreamWriter &copy) :
48  _out(copy._out),
49  _owns_stream(false)
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: StreamWriter::Copy Assignment Operator
55 // Access: Published
56 // Description: The copy constructor does not copy ownership of the
57 // stream.
58 ////////////////////////////////////////////////////////////////////
59 INLINE void StreamWriter::
60 operator = (const StreamWriter &copy) {
61  if (_owns_stream) {
62  delete _out;
63  }
64  _out = copy._out;
65  _owns_stream = false;
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: StreamWriter::Destructor
70 // Access: Published
71 // Description:
72 ////////////////////////////////////////////////////////////////////
73 INLINE StreamWriter::
74 ~StreamWriter() {
75  if (_owns_stream) {
76  delete _out;
77  }
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: StreamWriter::get_ostream
82 // Access: Published
83 // Description: Returns the stream in use.
84 ////////////////////////////////////////////////////////////////////
85 INLINE ostream *StreamWriter::
86 get_ostream() const {
87  return _out;
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: StreamWriter::add_bool
92 // Access: Published
93 // Description: Adds a boolean value to the stream.
94 ////////////////////////////////////////////////////////////////////
95 INLINE void StreamWriter::
96 add_bool(bool b) {
97  add_uint8(b);
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: StreamWriter::add_int8
102 // Access: Published
103 // Description: Adds a signed 8-bit integer to the stream.
104 ////////////////////////////////////////////////////////////////////
105 INLINE void StreamWriter::
106 add_int8(PN_int8 value) {
107  append_data(&value, 1);
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: StreamWriter::add_uint8
112 // Access: Published
113 // Description: Adds an unsigned 8-bit integer to the stream.
114 ////////////////////////////////////////////////////////////////////
115 INLINE void StreamWriter::
116 add_uint8(PN_uint8 value) {
117  append_data(&value, 1);
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: StreamWriter::add_int16
122 // Access: Published
123 // Description: Adds a signed 16-bit integer to the stream.
124 ////////////////////////////////////////////////////////////////////
125 INLINE void StreamWriter::
126 add_int16(PN_int16 value) {
127  LittleEndian s(&value, sizeof(value));
128  append_data(s.get_data(), sizeof(value));
129 }
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: StreamWriter::add_int32
133 // Access: Published
134 // Description: Adds a signed 32-bit integer to the stream.
135 ////////////////////////////////////////////////////////////////////
136 INLINE void StreamWriter::
137 add_int32(PN_int32 value) {
138  LittleEndian s(&value, sizeof(value));
139  append_data(s.get_data(), sizeof(value));
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: StreamWriter::add_int64
144 // Access: Published
145 // Description: Adds a signed 64-bit integer to the stream.
146 ////////////////////////////////////////////////////////////////////
147 INLINE void StreamWriter::
148 add_int64(PN_int64 value) {
149  LittleEndian s(&value, sizeof(value));
150  append_data(s.get_data(), sizeof(value));
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: StreamWriter::add_uint16
155 // Access: Published
156 // Description: Adds an unsigned 16-bit integer to the stream.
157 ////////////////////////////////////////////////////////////////////
158 INLINE void StreamWriter::
159 add_uint16(PN_uint16 value) {
160  LittleEndian s(&value, sizeof(value));
161  append_data(s.get_data(), sizeof(value));
162 }
163 
164 ////////////////////////////////////////////////////////////////////
165 // Function: StreamWriter::add_uint32
166 // Access: Published
167 // Description: Adds an unsigned 32-bit integer to the stream.
168 ////////////////////////////////////////////////////////////////////
169 INLINE void StreamWriter::
170 add_uint32(PN_uint32 value) {
171  LittleEndian s(&value, sizeof(value));
172  append_data(s.get_data(), sizeof(value));
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: StreamWriter::add_uint64
177 // Access: Published
178 // Description: Adds an unsigned 64-bit integer to the stream.
179 ////////////////////////////////////////////////////////////////////
180 INLINE void StreamWriter::
181 add_uint64(PN_uint64 value) {
182  LittleEndian s(&value, sizeof(value));
183  append_data(s.get_data(), sizeof(value));
184 }
185 
186 ////////////////////////////////////////////////////////////////////
187 // Function: StreamWriter::add_float32
188 // Access: Published
189 // Description: Adds a 32-bit single-precision floating-point number
190 // to the stream. Since this kind of float is not
191 // necessarily portable across different architectures,
192 // special care is required.
193 ////////////////////////////////////////////////////////////////////
194 INLINE void StreamWriter::
195 add_float32(float value) {
196  // For now, we assume the float format is portable across all
197  // architectures we are concerned with. If we come across one that
198  // is different, we will have to convert.
199  nassertv(sizeof(value) == 4);
200  LittleEndian s(&value, sizeof(value));
201  append_data(s.get_data(), sizeof(value));
202 }
203 
204 ////////////////////////////////////////////////////////////////////
205 // Function: StreamWriter::add_float64
206 // Access: Published
207 // Description: Adds a 64-bit floating-point number to the stream.
208 ////////////////////////////////////////////////////////////////////
209 INLINE void StreamWriter::
210 add_float64(PN_float64 value) {
211  LittleEndian s(&value, sizeof(value));
212  append_data(s.get_data(), sizeof(value));
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: StreamWriter::add_be_int16
217 // Access: Published
218 // Description: Adds a signed 16-bit big-endian integer to the
219 // streamWriter.
220 ////////////////////////////////////////////////////////////////////
221 INLINE void StreamWriter::
222 add_be_int16(PN_int16 value) {
223  BigEndian s(&value, sizeof(value));
224  append_data(s.get_data(), sizeof(value));
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: StreamWriter::add_be_int32
229 // Access: Published
230 // Description: Adds a signed 32-bit big-endian integer to the
231 // streamWriter.
232 ////////////////////////////////////////////////////////////////////
233 INLINE void StreamWriter::
234 add_be_int32(PN_int32 value) {
235  BigEndian s(&value, sizeof(value));
236  append_data(s.get_data(), sizeof(value));
237 }
238 
239 ////////////////////////////////////////////////////////////////////
240 // Function: StreamWriter::add_be_int64
241 // Access: Published
242 // Description: Adds a signed 64-bit big-endian integer to the
243 // streamWriter.
244 ////////////////////////////////////////////////////////////////////
245 INLINE void StreamWriter::
246 add_be_int64(PN_int64 value) {
247  BigEndian s(&value, sizeof(value));
248  append_data(s.get_data(), sizeof(value));
249 }
250 
251 ////////////////////////////////////////////////////////////////////
252 // Function: StreamWriter::add_be_uint16
253 // Access: Published
254 // Description: Adds an unsigned 16-bit big-endian integer to the
255 // streamWriter.
256 ////////////////////////////////////////////////////////////////////
257 INLINE void StreamWriter::
258 add_be_uint16(PN_uint16 value) {
259  BigEndian s(&value, sizeof(value));
260  append_data(s.get_data(), sizeof(value));
261 }
262 
263 ////////////////////////////////////////////////////////////////////
264 // Function: StreamWriter::add_be_uint32
265 // Access: Published
266 // Description: Adds an unsigned 32-bit big-endian integer to the
267 // streamWriter.
268 ////////////////////////////////////////////////////////////////////
269 INLINE void StreamWriter::
270 add_be_uint32(PN_uint32 value) {
271  BigEndian s(&value, sizeof(value));
272  append_data(s.get_data(), sizeof(value));
273 }
274 
275 ////////////////////////////////////////////////////////////////////
276 // Function: StreamWriter::add_be_uint64
277 // Access: Published
278 // Description: Adds an unsigned 64-bit big-endian integer to the
279 // streamWriter.
280 ////////////////////////////////////////////////////////////////////
281 INLINE void StreamWriter::
282 add_be_uint64(PN_uint64 value) {
283  BigEndian s(&value, sizeof(value));
284  append_data(s.get_data(), sizeof(value));
285 }
286 
287 ////////////////////////////////////////////////////////////////////
288 // Function: StreamWriter::add_be_float32
289 // Access: Published
290 // Description: Adds a 32-bit single-precision big-endian
291 // floating-point number to the stream. Since this
292 // kind of float is not necessarily portable across
293 // different architectures, special care is required.
294 ////////////////////////////////////////////////////////////////////
295 INLINE void StreamWriter::
296 add_be_float32(float value) {
297  // For now, we assume the float format is portable across all
298  // architectures we are concerned with. If we come across one that
299  // is different, we will have to convert.
300  nassertv(sizeof(value) == 4);
301  BigEndian s(&value, sizeof(value));
302  append_data(s.get_data(), sizeof(value));
303 }
304 
305 ////////////////////////////////////////////////////////////////////
306 // Function: StreamWriter::add_be_float64
307 // Access: Published
308 // Description: Adds a 64-bit big-endian floating-point number to the
309 // streamWriter.
310 ////////////////////////////////////////////////////////////////////
311 INLINE void StreamWriter::
312 add_be_float64(PN_float64 value) {
313  BigEndian s(&value, sizeof(value));
314  append_data(s.get_data(), sizeof(value));
315 }
316 
317 ////////////////////////////////////////////////////////////////////
318 // Function: StreamWriter::add_string
319 // Access: Published
320 // Description: Adds a variable-length string to the stream. This
321 // actually adds a count followed by n bytes.
322 ////////////////////////////////////////////////////////////////////
323 INLINE void StreamWriter::
324 add_string(const string &str) {
325  // The max sendable length for a string is 2^16.
326  nassertv(str.length() <= (PN_uint16)0xffff);
327 
328  // Strings always are preceded by their length
329  add_uint16(str.length());
330 
331  // Add the string
332  append_data(str);
333 }
334 
335 ////////////////////////////////////////////////////////////////////
336 // Function: StreamWriter::add_string32
337 // Access: Published
338 // Description: Adds a variable-length string to the stream, using a
339 // 32-bit length field.
340 ////////////////////////////////////////////////////////////////////
341 INLINE void StreamWriter::
342 add_string32(const string &str) {
343  // Strings always are preceded by their length
344  add_uint32(str.length());
345 
346  // Add the string
347  append_data(str);
348 }
349 
350 ////////////////////////////////////////////////////////////////////
351 // Function: StreamWriter::add_z_string
352 // Access: Published
353 // Description: Adds a variable-length string to the stream, as a
354 // NULL-terminated string.
355 ////////////////////////////////////////////////////////////////////
356 INLINE void StreamWriter::
357 add_z_string(string str) {
358  // We must not have any nested null characters in the string.
359  size_t null_pos = str.find('\0');
360  // Add the string (sans the null character).
361  append_data(str.substr(0, null_pos));
362 
363  // And the null character.
364  add_uint8('\0');
365 }
366 
367 ////////////////////////////////////////////////////////////////////
368 // Function: StreamWriter::add_fixed_string
369 // Access: Published
370 // Description: Adds a fixed-length string to the stream. If the
371 // string given is less than the requested size, this
372 // will pad the string out with zeroes; if it is greater
373 // than the requested size, this will silently truncate
374 // the string.
375 ////////////////////////////////////////////////////////////////////
376 INLINE void StreamWriter::
377 add_fixed_string(const string &str, size_t size) {
378  if (str.length() < size) {
379  append_data(str);
380  pad_bytes(size - str.length());
381 
382  } else { // str.length() >= size
383  append_data(str.substr(0, size));
384  }
385 }
386 
387 ////////////////////////////////////////////////////////////////////
388 // Function: StreamWriter::append_data
389 // Access: Published
390 // Description: Appends some more raw data to the end of the
391 // streamWriter.
392 ////////////////////////////////////////////////////////////////////
393 INLINE void StreamWriter::
394 append_data(const void *data, size_t size) {
395  _out->write((const char *)data, size);
396 }
397 
398 ////////////////////////////////////////////////////////////////////
399 // Function: StreamWriter::append_data
400 // Access: Published
401 // Description: Appends some more raw data to the end of the
402 // streamWriter.
403 ////////////////////////////////////////////////////////////////////
404 INLINE void StreamWriter::
405 append_data(const string &data) {
406  append_data(data.data(), data.length());
407 }
408 
409 ////////////////////////////////////////////////////////////////////
410 // Function: StreamWriter::flush
411 // Access: Published
412 // Description: Calls flush() on the underlying stream.
413 ////////////////////////////////////////////////////////////////////
414 INLINE void StreamWriter::
415 flush() {
416  _out->flush();
417 }
418 
419 ////////////////////////////////////////////////////////////////////
420 // Function: StreamWriter::write
421 // Access: Published
422 // Description: A synonym of append_data(). This is useful when
423 // assigning the StreamWriter to sys.stderr and/or
424 // sys.stdout in Python.
425 ////////////////////////////////////////////////////////////////////
426 INLINE void StreamWriter::
427 write(const string &data) {
428  append_data(data.data(), data.length());
429 }
A StreamWriter object is used to write sequential binary data directly to an ostream.
Definition: streamWriter.h:33
void add_float32(float value)
Adds a 32-bit single-precision floating-point number to the stream.
Definition: streamWriter.I:195
void append_data(const void *data, size_t size)
Appends some more raw data to the end of the streamWriter.
Definition: streamWriter.I:394
void add_be_uint64(PN_uint64 value)
Adds an unsigned 64-bit big-endian integer to the streamWriter.
Definition: streamWriter.I:282
void add_be_uint16(PN_uint16 value)
Adds an unsigned 16-bit big-endian integer to the streamWriter.
Definition: streamWriter.I:258
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the streamWriter.
Definition: streamWriter.I:312
void add_int8(PN_int8 value)
Adds a signed 8-bit integer to the stream.
Definition: streamWriter.I:106
void add_be_int32(PN_int32 value)
Adds a signed 32-bit big-endian integer to the streamWriter.
Definition: streamWriter.I:234
void add_int64(PN_int64 value)
Adds a signed 64-bit integer to the stream.
Definition: streamWriter.I:148
void add_z_string(string str)
Adds a variable-length string to the stream, as a NULL-terminated string.
Definition: streamWriter.I:357
const void * get_data() const
Returns the pointer to the first byte of the data, either reversed or nonreversed, as appropriate.
void operator=(const StreamWriter &copy)
The copy constructor does not copy ownership of the stream.
Definition: streamWriter.I:60
void add_be_int16(PN_int16 value)
Adds a signed 16-bit big-endian integer to the streamWriter.
Definition: streamWriter.I:222
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the stream.
Definition: streamWriter.I:116
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the stream.
Definition: streamWriter.I:159
void add_be_int64(PN_int64 value)
Adds a signed 64-bit big-endian integer to the streamWriter.
Definition: streamWriter.I:246
void add_be_float32(float value)
Adds a 32-bit single-precision big-endian floating-point number to the stream.
Definition: streamWriter.I:296
void flush()
Calls flush() on the underlying stream.
Definition: streamWriter.I:415
void add_bool(bool value)
Adds a boolean value to the stream.
Definition: streamWriter.I:96
void add_int16(PN_int16 value)
Adds a signed 16-bit integer to the stream.
Definition: streamWriter.I:126
void add_be_uint32(PN_uint32 value)
Adds an unsigned 32-bit big-endian integer to the streamWriter.
Definition: streamWriter.I:270
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the stream.
Definition: streamWriter.I:170
NativeNumericData and ReversedNumericData work together to provide a sneaky interface for automatical...
void write(const string &str)
A synonym of append_data().
Definition: streamWriter.I:427
ostream * get_ostream() const
Returns the stream in use.
Definition: streamWriter.I:86
void add_string32(const string &str)
Adds a variable-length string to the stream, using a 32-bit length field.
Definition: streamWriter.I:342
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the stream.
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the stream.
Definition: streamWriter.I:210
void add_uint64(PN_uint64 value)
Adds an unsigned 64-bit integer to the stream.
Definition: streamWriter.I:181
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the stream.
Definition: streamWriter.I:137
void add_fixed_string(const string &str, size_t size)
Adds a fixed-length string to the stream.
Definition: streamWriter.I:377
void add_string(const string &str)
Adds a variable-length string to the stream.
Definition: streamWriter.I:324
const void * get_data() const
Returns the pointer to the first byte of the data, either reversed or nonreversed, as appropriate.