stream.h

Engine/source/core/stream/stream.h

More...

Classes:

class

Base stream class for streaming data across a specific media.

Namespaces:

namespace

Public Defines

define
DECLARE_OVERLOADED_READ(type) bool read(type* out_read);
define
DECLARE_OVERLOADED_WRITE(type) bool write(type in_write);

Detailed Description

Public Defines

DECLARE_OVERLOADED_READ(type) bool read(type* out_read);
DECLARE_OVERLOADED_WRITE(type) bool write(type in_write);
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _STREAM_H_
 25#define _STREAM_H_
 26
 27#ifndef _TORQUE_TYPES_H_
 28#include "platform/types.h"
 29#endif
 30#ifndef _ENDIAN_H_
 31#include "core/util/endian.h"
 32#endif
 33#ifndef _STRINGFUNCTIONS_H_
 34#include "core/strings/stringFunctions.h"
 35#endif
 36
 37/// @defgroup stream_overload Primitive Type Stream Operation Overloads
 38/// These macros declare the read and write functions for all primitive types.
 39/// @{
 40#define DECLARE_OVERLOADED_READ(type)  bool read(type* out_read);
 41#define DECLARE_OVERLOADED_WRITE(type) bool write(type in_write);
 42/// @}
 43
 44class ColorI;
 45class LinearColorF;
 46struct NetAddress;
 47class RawData;
 48class String;
 49class NetSocket;
 50
 51namespace Torque {
 52   class ByteBuffer;
 53}
 54
 55//------------------------------------------------------------------------------
 56//-------------------------------------- Base Stream class
 57//
 58/// Base stream class for streaming data across a specific media
 59class Stream
 60{
 61   // Public structs and enumerations...
 62public:
 63     /// Status constants for the stream
 64   enum StreamStatus {
 65      Ok = 0,           ///< Ok!
 66      IOError,          ///< Read or Write error
 67      EOS,              ///< End of Stream reached (mostly for reads)
 68      IllegalCall,      ///< An unsupported operation used.  Always w/ accompanied by AssertWarn
 69      Closed,           ///< Tried to operate on a closed stream (or detached filter)
 70      UnknownError      ///< Catchall
 71   };
 72
 73   enum Capability {
 74      StreamWrite    = BIT(0), ///< Can this stream write?
 75      StreamRead     = BIT(1), ///< Can this stream read?
 76      StreamPosition = BIT(2)  ///< Can this stream position?
 77   };
 78
 79   // Accessible only through inline accessors
 80private:
 81   StreamStatus m_streamStatus;
 82
 83   // Derived accessible data modifiers...
 84protected:
 85   void setStatus(const StreamStatus in_newStatus) { m_streamStatus = in_newStatus; }
 86
 87public:
 88   Stream();
 89   virtual ~Stream() {}
 90
 91   /// Gets the status of the stream
 92   Stream::StreamStatus getStatus() const { return m_streamStatus; }
 93   /// Gets a printable string form of the status
 94   static const char* getStatusString(const StreamStatus in_status);
 95
 96   // Derived classes must override these...
 97protected:
 98   virtual bool _read(const U32 in_numBytes,  void* out_pBuffer)      = 0;
 99   virtual bool _write(const U32 in_numBytes, const void* in_pBuffer) = 0;
100
101   virtual void _write(const String & str);
102   virtual void _read(String * str);
103
104
105public:
106     /// Checks to see if this stream has the capability of a given function
107   virtual bool hasCapability(const Capability caps) const = 0;
108
109   /// Gets the position in the stream
110   virtual U32  getPosition() const                      = 0;
111   /// Sets the position of the stream.  Returns if the new position is valid or not
112   virtual bool setPosition(const U32 in_newPosition) = 0;
113   /// Gets the size of the stream
114   virtual U32  getStreamSize() = 0;
115
116   /// Reads a line from the stream.
117   /// @param buffer buffer to be read into
118   /// @param bufferSize max size of the buffer.  Will not read more than the "bufferSize"
119   void readLine(U8 *buffer, U32 bufferSize);
120   /// writes a line to the stream
121   void writeLine(const U8 *buffer);
122
123   /// Reads a string and inserts it into the StringTable
124   /// @see StringTable
125   const char *readSTString(bool casesens = false);
126   /// Reads a string of maximum 255 characters long
127   virtual void readString(char stringBuf[256]);
128   /// Reads a string that could potentially be more than 255 characters long.
129   /// @param maxStringLen Maximum length to read.  If the string is longer than maxStringLen, only maxStringLen bytes will be read.
130   /// @param stringBuf buffer where data is read into
131   void readLongString(U32 maxStringLen, char *stringBuf);
132   /// Writes a string to the stream.  This function is slightly unstable.
133   /// Only use this if you have a valid string that is not empty.
134   /// writeString is safer.
135   void writeLongString(U32 maxStringLen, const char *string);
136
137   inline bool Put(char character) { return write(character); }
138
139   /// Write raw text to the stream
140   void writeText(const char *text);
141
142   /// Writes a string to the stream.
143   virtual void writeString(const char *stringBuf, S32 maxLen=255);
144
145   /// Writes a formatted buffer to the stream.
146   /// NOTE: A maximum string length of 4K is allowed.
147   bool writeFormattedBuffer(const char *format, ...);
148
149   /// Writes a NULL terminated string buffer.
150   bool writeStringBuffer(const char* buffer) { return write(dStrlen(buffer), buffer); }
151
152   // read/write real strings
153   void write(const String & str) { _write(str); }
154   void read(String * str) { _read(str); }
155
156   /// Write an integral color to the stream.
157   bool write(const ColorI&);
158   /// Write a floating point color to the stream.
159   bool write(const LinearColorF&);
160   /// Read an integral color from the stream.
161   bool read(ColorI*);
162   /// Read a floating point color from the stream.
163   bool read(LinearColorF*);
164
165   /// Write a network address to the stream.
166   bool write(const NetAddress &);
167   /// Read a network address from the stream.
168   bool read(NetAddress*);
169
170   /// Write a network socket to the stream.
171   bool write(const NetSocket &);
172   /// Read a network socket from the stream.
173   bool read(NetSocket*);
174
175   /// Write some raw data onto the stream.
176   bool write(const RawData &);
177   /// Read some raw data from the stream.
178   bool read(RawData *);
179
180   /// Write some raw data onto the stream.
181   bool write(const Torque::ByteBuffer &);
182   /// Read some raw data from the stream.
183   bool read(Torque::ByteBuffer *);
184
185   // Overloaded write and read ops..
186  public:
187   bool read(const U32 in_numBytes,  void* out_pBuffer) {
188      return _read(in_numBytes, out_pBuffer);
189   }
190   bool write(const U32 in_numBytes, const void* in_pBuffer) {
191      return _write(in_numBytes, in_pBuffer);
192   }
193
194   DECLARE_OVERLOADED_WRITE(S8)
195   DECLARE_OVERLOADED_WRITE(U8)
196
197   DECLARE_OVERLOADED_WRITE(S16)
198   DECLARE_OVERLOADED_WRITE(S32)
199   DECLARE_OVERLOADED_WRITE(U16)
200   DECLARE_OVERLOADED_WRITE(U32)
201   DECLARE_OVERLOADED_WRITE(U64)
202   DECLARE_OVERLOADED_WRITE(F32)
203   DECLARE_OVERLOADED_WRITE(F64)
204
205   DECLARE_OVERLOADED_READ(S8)
206   DECLARE_OVERLOADED_READ(U8)
207
208   DECLARE_OVERLOADED_READ(S16)
209   DECLARE_OVERLOADED_READ(S32)
210   DECLARE_OVERLOADED_READ(U16)
211   DECLARE_OVERLOADED_READ(U32)
212   DECLARE_OVERLOADED_READ(U64)
213   DECLARE_OVERLOADED_READ(F32)
214   DECLARE_OVERLOADED_READ(F64)
215
216   // We have to do the bool's by hand, since they are different sizes
217   //  on different compilers...
218   //
219   bool read(bool* out_pRead) {
220      U8 translate;
221      bool success = read(&translate);
222      if (success == false)
223         return false;
224
225      *out_pRead = translate != 0;
226      return true;
227   }
228   bool write(const bool& in_rWrite) {
229      U8 translate = in_rWrite ? U8(1) : U8(0);
230      return write(translate);
231   }
232
233   /// Copy the contents of another stream into this one
234   bool copyFrom(Stream *other);
235
236   /// Write a number of tabs to this stream
237   void writeTabs(U32 count)
238   {
239      char tab[] = "   ";
240      while(count--)
241         write(3, (void*)tab);
242   }
243
244   /// Create an exact replica of this stream.
245   /// Return NULL if the cloning fails.
246   virtual Stream* clone() const;
247};
248
249// This interface is used to provide the amount of bytes actually written/read when using the stream as a 
250// file.  The Stream interface does not provide this information.  This is a lame workaround.  
251class IStreamByteCount
252{
253public:
254   virtual ~IStreamByteCount() {}
255
256   virtual U32 getLastBytesRead() = 0;
257   virtual U32 getLastBytesWritten() = 0;
258};
259
260#endif //_STREAM_H_
261