Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members

FXBufferedStream.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  Stream Buffering object                                      *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson.   All Rights Reserved.                *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************/
00022 #ifndef FXBUFFEREDSTREAM_H
00023 #define FXBUFFEREDSTREAM_H
00024 
00025 #ifndef FXSTREAM
00026 #include <fox/FXStream.h>
00027 using namespace FX;
00028 #endif
00029 namespace FXEX {
00030 
00031 /**
00032  * This class add support to FXStream's for:
00033  * 1. buffering
00034  * 2. stream enhancement such as compression, data munging, etc
00035  *
00036  *
00037  * This functionality is kinda cool, we can do something like this:
00038  * 
00039  * FXFileStream fstream = new FXFileStream(...);
00040  * fstream.open(...);
00041  * FXBufferedStream bstream = new FXBufferedStream(fstream);
00042  * ...
00043  * bstream << object;
00044  * ...
00045  * // when buffer becomes full (or closed), bstream automatically flushes into fstream
00046  *
00047  *
00048  * Or even better:
00049  *
00050  * FXSocketStream fstream = new FXSocketStream(...);
00051  * fstream.open(...);
00052  * FXBzipStream bstream = new FXBzipStream(fstream,compression_level);
00053  * ...
00054  * bstream << object;
00055  * ...
00056  * // when the buffer becomes full (or closed), bstream automatically flushes bzip
00057  * // compressed data over the socket!
00058  *
00059  *
00060  * Child should subclass this class and overload the bufferLoad() and bufferSave()
00061  * method so that they can mung the data when the buffer is full/empty.
00062  */
00063 class FXAPI FXBufferedStream : public FXMemoryStream {
00064 private:
00065   FXStream  *stream;     // embellish this stream
00066 
00067 private:
00068   /// These are overloaded to generate the buffer full/empty events
00069   virtual void saveItems(const void *buf,unsigned long n);
00070   virtual void loadItems(void *buf,unsigned long n);
00071 
00072 protected:
00073   /// overloaded by subclasses to handle a full buffer during a save condition
00074   virtual FXbool bufferSave();
00075 
00076   /// overloaded by subclasses to handle a buffer which has completed loading the current buffer
00077   virtual FXbool bufferLoad();
00078 
00079 public:
00080   /// Point this enhanced stream at the base stream
00081   FXBufferedStream(FXStream* s,const FXObject* cont=NULL);
00082 
00083   /// Create an enhanced stream
00084   /// - the subclass should figure out how much space to allocate in the buffer (using setSpace())
00085   FXbool open(FXStreamDirection save_or_load);
00086 
00087   /// Create an enhanced stream of a specific buffer size
00088   FXbool open(FXuint sp,FXStreamDirection save_or_load);
00089 
00090   /// Stop using an enhanced stream
00091   FXbool close();
00092 
00093   /// Put the buffer into the base stream
00094   void flush();
00095 
00096   /// Save to stream
00097   virtual FXStream& operator<<(const FXuchar& v);
00098   virtual FXStream& operator<<(const FXchar& v);
00099 
00100   /// Load from stream
00101   virtual FXStream& operator>>(FXuchar& v);
00102   virtual FXStream& operator>>(FXchar& v);
00103 
00104   /// Destructor
00105   virtual ~FXBufferedStream();
00106   };
00107 
00108 } // namespace FXEX
00109 #endif // FXBUFFEREDSTREAM_H