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

spl/io/ThreadSafeStream.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 _threadsafestream_h
00018 #define _threadsafestream_h
00019 
00020 #include <stdio.h>
00021 
00022 #include <spl/types.h>
00023 #include <spl/Debug.h>
00024 #include <spl/threading/Event.h>
00025 #include <spl/threading/Mutex.h>
00026 #include <spl/io/IStream.h>
00027 #include <spl/Memory.h>
00028 #include <spl/RefCountPtrCast.h>
00029 #include <spl/io/Stream.h>
00030 #include <spl/String.h>
00031 #include <spl/collection/Vector.h>
00032 
00044 class ThreadSafeStream;
00045 typedef RefCountPtrCast<ThreadSafeStream, spl::IStream, spl::IStreamPtr> ThreadSafeStreamPtr;
00046 typedef WeakReference<ThreadSafeStream, spl::IStreamPtr> ThreadSafeStreamRef;
00047 
00048 REGISTER_TYPEOF(148, ThreadSafeStreamPtr);
00049 REGISTER_TYPEOF( 470, ThreadSafeStreamRef );
00050 
00052 class ThreadSafeStream : public spl::IStream
00053 {
00054 protected:
00055         spl::IStreamPtr m_strm;
00056         Mutex m_lock;
00057 
00058 public:
00059         ThreadSafeStream( spl::IStreamPtr strm );
00060         virtual ~ThreadSafeStream();
00061 
00062         inline void Lock() { m_lock.Lock(); }
00063         inline void Unlock() { m_lock.Unlock(); }
00064 
00065         virtual void Close();
00066         virtual void Flush();
00067         virtual int Read(Array<byte>& buffer, const int offset, int count);
00068         virtual int ReadByte();
00069         virtual long Seek(const long offset, const SeekOrigin origin);
00070         virtual void Write(const Array<byte>& buffer, const int offset, const int count);
00071         virtual void WriteByte(byte value);
00072 
00073         virtual bool CanRead() const;
00074         virtual bool CanSeek() const;
00075         virtual bool CanWrite() const;
00076 
00077         virtual long Length() const;
00078         virtual long Position() const;
00079 
00080 #ifdef DEBUG
00081         virtual void ValidateMem() const;
00082         virtual void CheckMem() const;
00083 #endif
00084 };
00085 
00086 REGISTER_TYPEOF(150, ThreadSafeStream);
00087 
00091 #endif