memStream.h

Engine/source/core/stream/memStream.h

More...

Classes:

class

The MemStream class is used to read and write to a memory buffer.

Detailed Description

  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 _MEMSTREAM_H_
 25#define _MEMSTREAM_H_
 26
 27#ifndef _STREAM_H_
 28#include "core/stream/stream.h"
 29#endif
 30
 31
 32/// The MemStream class is used to read and write to a memory buffer.
 33class MemStream : public Stream
 34{
 35   typedef Stream Parent;
 36
 37   protected:
 38
 39      /// The amount to grow the buffer when we run out of
 40      /// space writing to the stream.
 41      U32 mGrowSize;
 42
 43      /// The actual size of the memory buffer.  It is always
 44      /// greater than or equal to the mStreamSize.
 45      U32 mBufferSize;
 46
 47      /// The size of the data in the stream. It is always
 48      /// less than or equal to the mBufferSize.
 49      U32 mStreamSize;
 50
 51      /// The memory buffer.
 52      void *mBufferBase;
 53
 54      /// If true the memory is owned by the steam and it
 55      /// will be deleted in the destructor.
 56      bool mOwnsMemory;
 57
 58      ///
 59      U32 mInstCaps;
 60
 61      /// Our current read/write position within the buffer.
 62      U32 mCurrentPosition;
 63
 64   public:
 65
 66      /// This constructs an empty memory stream that will grow 
 67      /// in increments as needed.
 68      MemStream(  U32 growSize,
 69                  bool allowRead  = true,
 70                  bool allowWrite = true );
 71
 72      /// This constructs the stream with a fixed size memory buffer.  If
 73      /// buffer is null then it will be allocated for you.
 74      MemStream(  U32 bufferSize,
 75                  void *buffer,
 76                  bool allowRead  = true,
 77                  bool allowWrite = true );
 78
 79      /// The destructor.
 80      virtual ~MemStream();
 81
 82   protected:
 83
 84      // Stream
 85      bool _read( const U32 in_numBytes,  void *out_pBuffer );
 86      bool _write( const U32 in_numBytes, const void *in_pBuffer );
 87
 88   public:
 89
 90      // Stream
 91      bool hasCapability( const Capability caps ) const;
 92      U32 getPosition() const;
 93      bool setPosition( const U32 in_newPosition );
 94      U32 getStreamSize();
 95
 96      /// Returns the memory buffer.
 97      void *getBuffer() { return mBufferBase; }
 98      const void *getBuffer() const { return mBufferBase; }
 99
100      /// Takes the memory buffer reseting the stream.
101      void *takeBuffer();
102
103};
104
105#endif //_MEMSTREAM_H_
106