Panda3D
streamWriter.h
1 // Filename: streamWriter.h
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 #ifndef STREAMWRITER_H
16 #define STREAMWRITER_H
17 
18 #include "dtoolbase.h"
19 #include "pnotify.h"
20 #include "numeric_types.h"
21 #include "littleEndian.h"
22 #include "bigEndian.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : StreamWriter
26 // Description : A StreamWriter object is used to write sequential
27 // binary data directly to an ostream. Its interface is
28 // very similar to Datagram by design; it's primarily
29 // intended as a convenience to eliminate the overhead
30 // of writing bytes to a Datagram and then writing the
31 // Datagram to a stream.
32 ////////////////////////////////////////////////////////////////////
33 class EXPCL_DTOOLCONFIG StreamWriter {
34 public:
35  INLINE StreamWriter(ostream &out);
36 PUBLISHED:
37  INLINE StreamWriter(ostream *out, bool owns_stream);
38  INLINE StreamWriter(const StreamWriter &copy);
39  INLINE void operator = (const StreamWriter &copy);
40  INLINE ~StreamWriter();
41 
42  INLINE ostream *get_ostream() const;
43 
44  BLOCKING INLINE void add_bool(bool value);
45  BLOCKING INLINE void add_int8(PN_int8 value);
46  BLOCKING INLINE void add_uint8(PN_uint8 value);
47 
48  // The default numeric packing is little-endian.
49  BLOCKING INLINE void add_int16(PN_int16 value);
50  BLOCKING INLINE void add_int32(PN_int32 value);
51  BLOCKING INLINE void add_int64(PN_int64 value);
52  BLOCKING INLINE void add_uint16(PN_uint16 value);
53  BLOCKING INLINE void add_uint32(PN_uint32 value);
54  BLOCKING INLINE void add_uint64(PN_uint64 value);
55  BLOCKING INLINE void add_float32(float value);
56  BLOCKING INLINE void add_float64(PN_float64 value);
57 
58  // These functions pack numbers big-endian, in case that's desired.
59  BLOCKING INLINE void add_be_int16(PN_int16 value);
60  BLOCKING INLINE void add_be_int32(PN_int32 value);
61  BLOCKING INLINE void add_be_int64(PN_int64 value);
62  BLOCKING INLINE void add_be_uint16(PN_uint16 value);
63  BLOCKING INLINE void add_be_uint32(PN_uint32 value);
64  BLOCKING INLINE void add_be_uint64(PN_uint64 value);
65  BLOCKING INLINE void add_be_float32(float value);
66  BLOCKING INLINE void add_be_float64(PN_float64 value);
67 
68  BLOCKING INLINE void add_string(const string &str);
69  BLOCKING INLINE void add_string32(const string &str);
70  BLOCKING INLINE void add_z_string(string str);
71  BLOCKING INLINE void add_fixed_string(const string &str, size_t size);
72 
73  BLOCKING void pad_bytes(size_t size);
74  BLOCKING INLINE void append_data(const void *data, size_t size);
75  BLOCKING INLINE void append_data(const string &data);
76 
77  BLOCKING INLINE void flush();
78 
79  BLOCKING INLINE void write(const string &str);
80 
81 private:
82  ostream *_out;
83  bool _owns_stream;
84 };
85 
86 #include "streamWriter.I"
87 
88 #endif
A StreamWriter object is used to write sequential binary data directly to an ostream.
Definition: streamWriter.h:33