Panda3D

streamWriter.h

00001 // Filename: streamWriter.h
00002 // Created by:  drose (04Aug02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef STREAMWRITER_H
00016 #define STREAMWRITER_H
00017 
00018 #include "dtoolbase.h"
00019 #include "pnotify.h"
00020 #include "numeric_types.h"
00021 #include "littleEndian.h"
00022 #include "bigEndian.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //       Class : StreamWriter
00026 // Description : A StreamWriter object is used to write sequential
00027 //               binary data directly to an ostream.  Its interface is
00028 //               very similar to Datagram by design; it's primarily
00029 //               intended as a convenience to eliminate the overhead
00030 //               of writing bytes to a Datagram and then writing the
00031 //               Datagram to a stream.
00032 ////////////////////////////////////////////////////////////////////
00033 class EXPCL_DTOOLCONFIG StreamWriter {
00034 public:
00035   INLINE StreamWriter(ostream &out);
00036 PUBLISHED:
00037   INLINE StreamWriter(ostream *out, bool owns_stream);
00038   INLINE StreamWriter(const StreamWriter &copy);
00039   INLINE void operator = (const StreamWriter &copy);
00040   INLINE ~StreamWriter();
00041 
00042   INLINE ostream *get_ostream() const;
00043 
00044   BLOCKING INLINE void add_bool(bool value);
00045   BLOCKING INLINE void add_int8(PN_int8 value);
00046   BLOCKING INLINE void add_uint8(PN_uint8 value);
00047 
00048   // The default numeric packing is little-endian.
00049   BLOCKING INLINE void add_int16(PN_int16 value);
00050   BLOCKING INLINE void add_int32(PN_int32 value);
00051   BLOCKING INLINE void add_int64(PN_int64 value);
00052   BLOCKING INLINE void add_uint16(PN_uint16 value);
00053   BLOCKING INLINE void add_uint32(PN_uint32 value);
00054   BLOCKING INLINE void add_uint64(PN_uint64 value);
00055   BLOCKING INLINE void add_float32(float value);
00056   BLOCKING INLINE void add_float64(PN_float64 value);
00057 
00058   // These functions pack numbers big-endian, in case that's desired.
00059   BLOCKING INLINE void add_be_int16(PN_int16 value);
00060   BLOCKING INLINE void add_be_int32(PN_int32 value);
00061   BLOCKING INLINE void add_be_int64(PN_int64 value);
00062   BLOCKING INLINE void add_be_uint16(PN_uint16 value);
00063   BLOCKING INLINE void add_be_uint32(PN_uint32 value);
00064   BLOCKING INLINE void add_be_uint64(PN_uint64 value);
00065   BLOCKING INLINE void add_be_float32(float value);
00066   BLOCKING INLINE void add_be_float64(PN_float64 value);
00067 
00068   BLOCKING INLINE void add_string(const string &str);
00069   BLOCKING INLINE void add_string32(const string &str);
00070   BLOCKING INLINE void add_z_string(string str);
00071   BLOCKING INLINE void add_fixed_string(const string &str, size_t size);
00072 
00073   BLOCKING void pad_bytes(size_t size);
00074   BLOCKING INLINE void append_data(const void *data, size_t size);
00075   BLOCKING INLINE void append_data(const string &data);
00076 
00077   BLOCKING INLINE void flush();
00078 
00079   BLOCKING INLINE void write(const string &str);
00080 
00081 private:
00082   ostream *_out;
00083   bool _owns_stream;
00084 };
00085 
00086 #include "streamWriter.I"
00087 
00088 #endif
 All Classes Functions Variables Enumerations