Panda3D

Loading...
Searching...
No Matches
streamWriter.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 streamWriter.I
10 * @author drose
11 * @date 2002-08-04
12 */
13
14/**
15 *
16 */
17INLINE StreamWriter::
18StreamWriter(std::ostream &out) :
19#ifdef HAVE_PYTHON
20 softspace(0),
21#endif
22 _out(&out),
23 _owns_stream(false)
24{
25}
26
27/**
28 *
29 */
30INLINE StreamWriter::
31StreamWriter(std::ostream *out, bool owns_stream) :
32#ifdef HAVE_PYTHON
33 softspace(0),
34#endif
35 _out(out),
36 _owns_stream(owns_stream)
37{
38}
39
40/**
41 * The copy constructor does not copy ownership of the stream.
42 */
43INLINE StreamWriter::
44StreamWriter(const StreamWriter &copy) :
45#ifdef HAVE_PYTHON
46 softspace(0),
47#endif
48 _out(copy._out),
49 _owns_stream(false)
50{
51}
52
53/**
54 * The copy constructor does not copy ownership of the stream.
55 */
56INLINE void StreamWriter::
57 operator = (const StreamWriter &copy) {
58 if (_owns_stream) {
59 delete _out;
60 }
61 _out = copy._out;
62 _owns_stream = false;
63}
64
65/**
66 *
67 */
68INLINE StreamWriter::
69~StreamWriter() {
70 if (_owns_stream) {
71 delete _out;
72 }
73}
74
75/**
76 * Returns the stream in use.
77 */
78INLINE std::ostream *StreamWriter::
79 get_ostream() const {
80 return _out;
81}
82
83/**
84 * Adds a boolean value to the stream.
85 */
86INLINE void StreamWriter::
87 add_bool(bool b) {
88 add_uint8(b);
89}
90
91/**
92 * Adds a signed 8-bit integer to the stream.
93 */
94INLINE void StreamWriter::
95 add_int8(int8_t value) {
96 append_data(&value, 1);
97}
98
99/**
100 * Adds an unsigned 8-bit integer to the stream.
101 */
102INLINE void StreamWriter::
103 add_uint8(uint8_t value) {
104 append_data(&value, 1);
105}
106
107/**
108 * Adds a signed 16-bit integer to the stream.
109 */
110INLINE void StreamWriter::
111 add_int16(int16_t value) {
112 LittleEndian s(&value, sizeof(value));
113 append_data(s.get_data(), sizeof(value));
114}
115
116/**
117 * Adds a signed 32-bit integer to the stream.
118 */
119INLINE void StreamWriter::
120 add_int32(int32_t value) {
121 LittleEndian s(&value, sizeof(value));
122 append_data(s.get_data(), sizeof(value));
123}
124
125/**
126 * Adds a signed 64-bit integer to the stream.
127 */
128INLINE void StreamWriter::
129 add_int64(int64_t value) {
130 LittleEndian s(&value, sizeof(value));
131 append_data(s.get_data(), sizeof(value));
132}
133
134/**
135 * Adds an unsigned 16-bit integer to the stream.
136 */
137INLINE void StreamWriter::
138 add_uint16(uint16_t value) {
139 LittleEndian s(&value, sizeof(value));
140 append_data(s.get_data(), sizeof(value));
141}
142
143/**
144 * Adds an unsigned 32-bit integer to the stream.
145 */
146INLINE void StreamWriter::
147 add_uint32(uint32_t value) {
148 LittleEndian s(&value, sizeof(value));
149 append_data(s.get_data(), sizeof(value));
150}
151
152/**
153 * Adds an unsigned 64-bit integer to the stream.
154 */
155INLINE void StreamWriter::
156 add_uint64(uint64_t value) {
157 LittleEndian s(&value, sizeof(value));
158 append_data(s.get_data(), sizeof(value));
159}
160
161/**
162 * Adds a 32-bit single-precision floating-point number to the stream. Since
163 * this kind of float is not necessarily portable across different
164 * architectures, special care is required.
165 */
166INLINE void StreamWriter::
167 add_float32(float value) {
168 // For now, we assume the float format is portable across all architectures
169 // we are concerned with. If we come across one that is different, we will
170 // have to convert.
171 nassertv(sizeof(value) == 4);
172 LittleEndian s(&value, sizeof(value));
173 append_data(s.get_data(), sizeof(value));
174}
175
176/**
177 * Adds a 64-bit floating-point number to the stream.
178 */
179INLINE void StreamWriter::
180 add_float64(PN_float64 value) {
181 LittleEndian s(&value, sizeof(value));
182 append_data(s.get_data(), sizeof(value));
183}
184
185/**
186 * Adds a signed 16-bit big-endian integer to the streamWriter.
187 */
188INLINE void StreamWriter::
189 add_be_int16(int16_t value) {
190 BigEndian s(&value, sizeof(value));
191 append_data(s.get_data(), sizeof(value));
192}
193
194/**
195 * Adds a signed 32-bit big-endian integer to the streamWriter.
196 */
197INLINE void StreamWriter::
198 add_be_int32(int32_t value) {
199 BigEndian s(&value, sizeof(value));
200 append_data(s.get_data(), sizeof(value));
201}
202
203/**
204 * Adds a signed 64-bit big-endian integer to the streamWriter.
205 */
206INLINE void StreamWriter::
207 add_be_int64(int64_t value) {
208 BigEndian s(&value, sizeof(value));
209 append_data(s.get_data(), sizeof(value));
210}
211
212/**
213 * Adds an unsigned 16-bit big-endian integer to the streamWriter.
214 */
215INLINE void StreamWriter::
216 add_be_uint16(uint16_t value) {
217 BigEndian s(&value, sizeof(value));
218 append_data(s.get_data(), sizeof(value));
219}
220
221/**
222 * Adds an unsigned 32-bit big-endian integer to the streamWriter.
223 */
224INLINE void StreamWriter::
225 add_be_uint32(uint32_t value) {
226 BigEndian s(&value, sizeof(value));
227 append_data(s.get_data(), sizeof(value));
228}
229
230/**
231 * Adds an unsigned 64-bit big-endian integer to the streamWriter.
232 */
233INLINE void StreamWriter::
234 add_be_uint64(uint64_t value) {
235 BigEndian s(&value, sizeof(value));
236 append_data(s.get_data(), sizeof(value));
237}
238
239/**
240 * Adds a 32-bit single-precision big-endian floating-point number to the
241 * stream. Since this kind of float is not necessarily portable across
242 * different architectures, special care is required.
243 */
244INLINE void StreamWriter::
245 add_be_float32(float value) {
246 // For now, we assume the float format is portable across all architectures
247 // we are concerned with. If we come across one that is different, we will
248 // have to convert.
249 nassertv(sizeof(value) == 4);
250 BigEndian s(&value, sizeof(value));
251 append_data(s.get_data(), sizeof(value));
252}
253
254/**
255 * Adds a 64-bit big-endian floating-point number to the streamWriter.
256 */
257INLINE void StreamWriter::
258 add_be_float64(PN_float64 value) {
259 BigEndian s(&value, sizeof(value));
260 append_data(s.get_data(), sizeof(value));
261}
262
263/**
264 * Adds a variable-length string to the stream. This actually adds a count
265 * followed by n bytes.
266 */
267INLINE void StreamWriter::
268 add_string(const std::string &str) {
269 // The max sendable length for a string is 2^16.
270 nassertv(str.length() <= (uint16_t)0xffff);
271
272 // Strings always are preceded by their length
273 add_uint16((uint16_t)str.length());
274
275 // Add the string
276 append_data(str);
277}
278
279/**
280 * Adds a variable-length string to the stream, using a 32-bit length field.
281 */
282INLINE void StreamWriter::
283 add_string32(const std::string &str) {
284 // Strings always are preceded by their length
285 add_uint32((uint32_t)str.length());
286
287 // Add the string
288 append_data(str);
289}
290
291/**
292 * Adds a variable-length string to the stream, as a NULL-terminated string.
293 */
294INLINE void StreamWriter::
295 add_z_string(std::string str) {
296 // We must not have any nested null characters in the string.
297 size_t null_pos = str.find('\0');
298 // Add the string (sans the null character).
299 append_data(str.substr(0, null_pos));
300
301 // And the null character.
302 add_uint8('\0');
303}
304
305/**
306 * Adds a fixed-length string to the stream. If the string given is less than
307 * the requested size, this will pad the string out with zeroes; if it is
308 * greater than the requested size, this will silently truncate the string.
309 */
310INLINE void StreamWriter::
311 add_fixed_string(const std::string &str, size_t size) {
312 if (str.length() < size) {
313 append_data(str);
314 pad_bytes(size - str.length());
315
316 } else { // str.length() >= size
317 append_data(str.substr(0, size));
318 }
319}
320
321/**
322 * Appends some more raw data to the end of the streamWriter.
323 */
324INLINE void StreamWriter::
325 append_data(const void *data, size_t size) {
326 _out->write((const char *)data, size);
327}
328
329/**
330 * Appends some more raw data to the end of the streamWriter.
331 */
332INLINE void StreamWriter::
333 append_data(const std::string &data) {
334 append_data(data.data(), data.length());
335}
336
337/**
338 * Calls flush() on the underlying stream.
339 */
340INLINE void StreamWriter::
341 flush() {
342 _out->flush();
343}
344
345/**
346 * A synonym of append_data(). This is useful when assigning the StreamWriter
347 * to sys.stderr and/or sys.stdout in Python.
348 */
349INLINE void StreamWriter::
350 write(const std::string &data) {
351 append_data(data.data(), data.length());
352}
BigEndian
ReversedNumericData BigEndian
BigEndian is a special class that automatically reverses the byte-order of numeric values for little-...
Definition bigEndian.h:32
NativeNumericData::get_data
const void * get_data() const
Returns the pointer to the first byte of the data, either reversed or nonreversed,...
Definition nativeNumericData.I:51
ReversedNumericData::get_data
const void * get_data() const
Returns the pointer to the first byte of the data, either reversed or nonreversed,...
Definition reversedNumericData.I:50
StreamWriter::append_data
void append_data(const void *data, size_t size)
Appends some more raw data to the end of the streamWriter.
Definition streamWriter.I:325
StreamWriter::write
void write(const std::string &str)
A synonym of append_data().
Definition streamWriter.I:350
StreamWriter::add_string32
void add_string32(const std::string &str)
Adds a variable-length string to the stream, using a 32-bit length field.
Definition streamWriter.I:283
StreamWriter::flush
void flush()
Calls flush() on the underlying stream.
Definition streamWriter.I:341
StreamWriter::add_float64
void add_float64(PN_float64 value)
Adds a 64-bit floating-point number to the stream.
Definition streamWriter.I:180
StreamWriter::add_be_uint32
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the streamWriter.
Definition streamWriter.I:225
StreamWriter::pad_bytes
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the stream.
Definition streamWriter.cxx:20
StreamWriter::add_int32
void add_int32(int32_t value)
Adds a signed 32-bit integer to the stream.
Definition streamWriter.I:120
StreamWriter::add_be_int32
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the streamWriter.
Definition streamWriter.I:198
StreamWriter::add_be_float32
void add_be_float32(float value)
Adds a 32-bit single-precision big-endian floating-point number to the stream.
Definition streamWriter.I:245
StreamWriter::add_float32
void add_float32(float value)
Adds a 32-bit single-precision floating-point number to the stream.
Definition streamWriter.I:167
StreamWriter::add_uint8
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the stream.
Definition streamWriter.I:103
StreamWriter::add_be_int64
void add_be_int64(int64_t value)
Adds a signed 64-bit big-endian integer to the streamWriter.
Definition streamWriter.I:207
StreamWriter::add_uint16
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the stream.
Definition streamWriter.I:138
StreamWriter::add_int64
void add_int64(int64_t value)
Adds a signed 64-bit integer to the stream.
Definition streamWriter.I:129
StreamWriter::add_z_string
void add_z_string(std::string str)
Adds a variable-length string to the stream, as a NULL-terminated string.
Definition streamWriter.I:295
StreamWriter::add_fixed_string
void add_fixed_string(const std::string &str, size_t size)
Adds a fixed-length string to the stream.
Definition streamWriter.I:311
StreamWriter::operator=
void operator=(const StreamWriter &copy)
The copy constructor does not copy ownership of the stream.
Definition streamWriter.I:57
StreamWriter::add_uint32
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the stream.
Definition streamWriter.I:147
StreamWriter::add_int16
void add_int16(int16_t value)
Adds a signed 16-bit integer to the stream.
Definition streamWriter.I:111
StreamWriter::add_be_uint64
void add_be_uint64(uint64_t value)
Adds an unsigned 64-bit big-endian integer to the streamWriter.
Definition streamWriter.I:234
StreamWriter::add_be_int16
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the streamWriter.
Definition streamWriter.I:189
StreamWriter::add_be_float64
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the streamWriter.
Definition streamWriter.I:258
StreamWriter::add_be_uint16
void add_be_uint16(uint16_t value)
Adds an unsigned 16-bit big-endian integer to the streamWriter.
Definition streamWriter.I:216
StreamWriter::add_int8
void add_int8(int8_t value)
Adds a signed 8-bit integer to the stream.
Definition streamWriter.I:95
StreamWriter::add_uint64
void add_uint64(uint64_t value)
Adds an unsigned 64-bit integer to the stream.
Definition streamWriter.I:156
StreamWriter::add_bool
void add_bool(bool value)
Adds a boolean value to the stream.
Definition streamWriter.I:87
StreamWriter::get_ostream
get_ostream
Returns the stream in use.
Definition streamWriter.h:39
StreamWriter::add_string
void add_string(const std::string &str)
Adds a variable-length string to the stream.
Definition streamWriter.I:268
LittleEndian
NativeNumericData LittleEndian
LittleEndian is a special class that automatically reverses the byte-order of numeric values for big-...
Definition littleEndian.h:32