Panda3D

streamWrapper.h

00001 // Filename: streamWrapper.h
00002 // Created by:  drose (11Nov08)
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 STREAMWRAPPER_H
00016 #define STREAMWRAPPER_H
00017 
00018 #include "dtoolbase.h"
00019 #include "mutexImpl.h"
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //       Class : StreamWrapperBase
00023 // Description : The base class for both IStreamWrapper and
00024 //               OStreamWrapper, this provides the common locking
00025 //               interface.
00026 ////////////////////////////////////////////////////////////////////
00027 class EXPCL_DTOOLCONFIG StreamWrapperBase {
00028 protected:
00029   INLINE StreamWrapperBase();
00030 
00031 PUBLISHED:
00032   INLINE void acquire();
00033   INLINE void release();
00034 
00035 private:
00036   MutexImpl _lock;
00037 #ifdef SIMPLE_THREADS
00038   // In the SIMPLE_THREADS case, we need to use a bool flag, because
00039   // MutexImpl defines to nothing in this case--but we still need to
00040   // achieve a form of locking, since I/O operations can cause the
00041   // thread to swap without warning.
00042   bool _lock_flag;
00043 #endif
00044 };
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //       Class : IStreamWrapper
00048 // Description : This class provides a locking wrapper around an
00049 //               arbitrary istream pointer.  A thread may use this
00050 //               class to perform an atomic seek/read/gcount
00051 //               operation.
00052 ////////////////////////////////////////////////////////////////////
00053 class EXPCL_DTOOLCONFIG IStreamWrapper : virtual public StreamWrapperBase {
00054 public:
00055   INLINE IStreamWrapper(istream *stream, bool owns_pointer);
00056 PUBLISHED:
00057   INLINE IStreamWrapper(istream &stream);
00058   ~IStreamWrapper();
00059 
00060   INLINE istream *get_istream() const;
00061 
00062 public:
00063   void read(char *buffer, streamsize num_bytes);
00064   void read(char *buffer, streamsize num_bytes, streamsize &read_bytes);
00065   void read(char *buffer, streamsize num_bytes, streamsize &read_bytes, bool &eof);
00066   void seek_read(streamsize pos, char *buffer, streamsize num_bytes, streamsize &read_bytes, bool &eof);
00067   INLINE int get();
00068   streamsize seek_gpos_eof();
00069 
00070 private:
00071   istream *_istream;
00072   bool _owns_pointer;
00073 };
00074 
00075 ////////////////////////////////////////////////////////////////////
00076 //       Class : OStreamWrapper
00077 // Description : This class provides a locking wrapper around an
00078 //               arbitrary ostream pointer.  A thread may use this
00079 //               class to perform an atomic seek/write operation.
00080 ////////////////////////////////////////////////////////////////////
00081 class EXPCL_DTOOLCONFIG OStreamWrapper : virtual public StreamWrapperBase {
00082 public:
00083   INLINE OStreamWrapper(ostream *stream, bool owns_pointer);
00084 PUBLISHED:
00085   INLINE OStreamWrapper(ostream &stream);
00086   ~OStreamWrapper();
00087 
00088   INLINE ostream *get_ostream() const;
00089 
00090 public:
00091   void write(const char *buffer, streamsize num_bytes);
00092   void write(const char *buffer, streamsize num_bytes, bool &fail);
00093   void seek_write(streamsize pos, const char *buffer, streamsize num_bytes, bool &fail);
00094   INLINE bool put(char c);
00095 
00096 private:
00097   ostream *_ostream;
00098   bool _owns_pointer;
00099 };
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //       Class : StreamWrapper
00103 // Description : This class provides a locking wrapper around a
00104 //               combination ostream/istream pointer.
00105 ////////////////////////////////////////////////////////////////////
00106 class EXPCL_DTOOLCONFIG StreamWrapper : public IStreamWrapper, public OStreamWrapper {
00107 public:
00108   INLINE StreamWrapper(iostream *stream, bool owns_pointer);
00109 PUBLISHED:
00110   INLINE StreamWrapper(iostream &stream);
00111   ~StreamWrapper();
00112 
00113   INLINE iostream *get_iostream() const;
00114 
00115 private:
00116   iostream *_iostream;
00117   bool _owns_pointer;
00118 };
00119 
00120 #include "streamWrapper.I"
00121 
00122 #endif
 All Classes Functions Variables Enumerations