• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/io/MemoryStream.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _memorystream_h
00018 #define _memorystream_h
00019 
00020 #include <spl/Debug.h>
00021 #include <spl/collection/Array.h>
00022 #include <spl/io/IStream.h>
00023 #include <spl/RefCountPtr.h>
00024 #include <spl/collection/Vector.h>
00025 #include <spl/WeakReference.h>
00026 
00033 class MemoryStream;
00034 typedef RefCountPtrCast<MemoryStream, spl::IStream, spl::IStreamPtr> MemoryStreamPtr;
00035 typedef WeakReference<MemoryStream, spl::IStreamPtr> MemoryStreamRef;
00036 
00037 REGISTER_TYPEOF(110, MemoryStreamPtr);
00038 REGISTER_TYPEOF( 459, MemoryStreamRef );
00039 
00043 class MemoryStream : public spl::IStream
00044 {
00045 protected:
00046         // not excessively efficient
00047         Vector<byte> m_buf;
00048         int m_ptr;
00049 
00050 public:
00051         MemoryStream();
00052         
00053         inline MemoryStream(const MemoryStream& ms)
00054         : m_buf(ms.m_buf), m_ptr(ms.m_ptr)
00055         {
00056         }
00057 
00058         virtual ~MemoryStream();
00059 
00060         inline MemoryStream& operator =(const MemoryStream &ms)
00061         {
00062                 m_buf = ms.m_buf;
00063                 m_ptr = ms.m_ptr;
00064                 return *this;
00065         }
00066 
00067         virtual void Close();
00068         virtual void Flush();
00069         virtual int Read(Array<byte>& buffer, const int offset, int count);
00070         inline int Read(Array<byte>& buffer) { return Read(buffer, 0, buffer.Length()); }
00071         virtual int ReadByte();
00072         virtual long Seek(const long offset, const SeekOrigin origin);
00073         virtual void Write(const Array<byte>& buffer, const int offset, const int count);
00074         inline void Write(const Array<byte>& buffer) { Write(buffer, 0, buffer.Length()); }
00075         virtual void WriteByte(byte value);
00076 
00077         virtual bool CanRead() const;
00078         virtual bool CanSeek() const;
00079         virtual bool CanWrite() const;
00080 
00081         virtual long Length() const;
00082         virtual long Position() const;
00083 
00084         StringPtr ToString();
00085         
00086 #ifdef DEBUG
00087         virtual void ValidateMem() const;
00088         virtual void CheckMem() const;
00089 #endif
00090 };
00091 
00092 REGISTER_TYPEOF(112, MemoryStream);
00093 
00096 #endif