Panda3D
streamWriter.h
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.h
10  * @author drose
11  * @date 2002-08-04
12  */
13 
14 #ifndef STREAMWRITER_H
15 #define STREAMWRITER_H
16 
17 #include "dtoolbase.h"
18 #include "pnotify.h"
19 #include "numeric_types.h"
20 #include "littleEndian.h"
21 #include "bigEndian.h"
22 
23 /**
24  * A StreamWriter object is used to write sequential binary data directly to
25  * an ostream. Its interface is very similar to Datagram by design; it's
26  * primarily intended as a convenience to eliminate the overhead of writing
27  * bytes to a Datagram and then writing the Datagram to a stream.
28  */
29 class EXPCL_DTOOL_PRC StreamWriter {
30 public:
31  INLINE StreamWriter(std::ostream &out);
32 PUBLISHED:
33  INLINE explicit StreamWriter(std::ostream *out, bool owns_stream);
34  INLINE StreamWriter(const StreamWriter &copy);
35  INLINE void operator = (const StreamWriter &copy);
36  INLINE ~StreamWriter();
37 
38  INLINE std::ostream *get_ostream() const;
39  MAKE_PROPERTY(std::ostream, get_ostream);
40 
41  BLOCKING INLINE void add_bool(bool value);
42  BLOCKING INLINE void add_int8(int8_t value);
43  BLOCKING INLINE void add_uint8(uint8_t value);
44 
45  // The default numeric packing is little-endian.
46  BLOCKING INLINE void add_int16(int16_t value);
47  BLOCKING INLINE void add_int32(int32_t value);
48  BLOCKING INLINE void add_int64(int64_t value);
49  BLOCKING INLINE void add_uint16(uint16_t value);
50  BLOCKING INLINE void add_uint32(uint32_t value);
51  BLOCKING INLINE void add_uint64(uint64_t value);
52  BLOCKING INLINE void add_float32(float value);
53  BLOCKING INLINE void add_float64(PN_float64 value);
54 
55  // These functions pack numbers big-endian, in case that's desired.
56  BLOCKING INLINE void add_be_int16(int16_t value);
57  BLOCKING INLINE void add_be_int32(int32_t value);
58  BLOCKING INLINE void add_be_int64(int64_t value);
59  BLOCKING INLINE void add_be_uint16(uint16_t value);
60  BLOCKING INLINE void add_be_uint32(uint32_t value);
61  BLOCKING INLINE void add_be_uint64(uint64_t value);
62  BLOCKING INLINE void add_be_float32(float value);
63  BLOCKING INLINE void add_be_float64(PN_float64 value);
64 
65  BLOCKING INLINE void add_string(const std::string &str);
66  BLOCKING INLINE void add_string32(const std::string &str);
67  BLOCKING INLINE void add_z_string(std::string str);
68  BLOCKING INLINE void add_fixed_string(const std::string &str, size_t size);
69 
70  BLOCKING void pad_bytes(size_t size);
71  EXTENSION(void append_data(PyObject *data));
72 
73  BLOCKING INLINE void flush();
74 
75  BLOCKING INLINE void write(const std::string &str);
76 
77 public:
78  BLOCKING INLINE void append_data(const void *data, size_t size);
79  BLOCKING INLINE void append_data(const std::string &data);
80 
81 private:
82  std::ostream *_out;
83  bool _owns_stream;
84 
85 #ifdef HAVE_PYTHON
86 PUBLISHED:
87  // Python 2 needs this for printing to work correctly.
88  int softspace;
89 #endif
90 };
91 
92 #include "streamWriter.I"
93 
94 #endif
A StreamWriter object is used to write sequential binary data directly to an ostream.
Definition: streamWriter.h:29
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.