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

FXStream.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *       P e r s i s t e n t   S t o r a g e   S t r e a m   C l a s s e s       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2010 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (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                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXSTREAM_H
00022 #define FXSTREAM_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00028 /// Stream data flow direction
00029 enum FXStreamDirection {
00030   FXStreamDead=0,               /// Unopened stream
00031   FXStreamSave=1,               /// Saving stuff to stream
00032   FXStreamLoad=2                /// Loading stuff from stream
00033   };
00034 
00035 
00036 /// Stream status codes
00037 enum FXStreamStatus {
00038   FXStreamOK=0,                 /// OK
00039   FXStreamEnd=1,                /// Try read past end of stream
00040   FXStreamFull=2,               /// Filled up stream buffer or disk full
00041   FXStreamNoWrite=3,            /// Unable to open for write
00042   FXStreamNoRead=4,             /// Unable to open for read
00043   FXStreamFormat=5,             /// Stream format error
00044   FXStreamUnknown=6,            /// Trying to read unknown class
00045   FXStreamAlloc=7,              /// Alloc failed
00046   FXStreamFailure=8             /// General failure
00047   };
00048 
00049 
00050 /// Stream seeking
00051 enum FXWhence {
00052   FXFromStart=0,                /// Seek from start position
00053   FXFromCurrent=1,              /// Seek from current position
00054   FXFromEnd=2                   /// Seek from end position
00055   };
00056 
00057 
00058 /**
00059 * A stream is a way to serialize data and objects into a byte stream.
00060 * Each item of data that is saved or loaded from the stream may be byte-swapped,
00061 * thus allowing little-endian machines to read data produced on big endian ones
00062 * and vice-versa.
00063 * Data is serialized exactly as-is.  There are no tags or other markers
00064 * inserted into the stream; thus, the stream may be used to save or load arbitrary
00065 * binary data.
00066 * Objects derived from FXObjects may be serialized also; whenever a reference to an
00067 * object is serialized, a table is consulted to determine if the same object has
00068 * been encountered previously; if not, the object is added to the table and then
00069 * its contents are serialized.  If the object has been encountered before, only a
00070 * reference to the object is serialized.
00071 * When loading back a serialized object, new instances are constructed using
00072 * the default constructor, and subsequently the object's contents are loaded.
00073 * A special container object may be passed in which is placed in the table
00074 * as if it had been encountered before; this will cause only references to this
00075 * object to be saved.  The container object is typically the top-level document
00076 * object which manages all objects contained by it.  Additional objects may be
00077 * added using addObject(); these will not be actually saved or loaded.
00078 */
00079 class FXAPI FXStream {
00080 protected:
00081   FXHash             hash;      // Hash table
00082   const FXObject    *parent;    // Parent object
00083   FXuchar           *begptr;    // Begin of buffer
00084   FXuchar           *endptr;    // End of buffer
00085   FXuchar           *wrptr;     // Write pointer
00086   FXuchar           *rdptr;     // Read pointer
00087   FXlong             pos;       // Position
00088   FXStreamDirection  dir;       // Direction of current transfer
00089   FXStreamStatus     code;      // Status code
00090   FXuint             seq;       // Sequence number
00091   FXbool             owns;      // Stream owns buffer
00092   FXbool             swap;      // Swap bytes on readin
00093 protected:
00094 
00095   /**
00096   * Write at least count bytes from the buffer;
00097   * returns number of bytes available to be written.
00098   */
00099   virtual FXuval writeBuffer(FXuval count);
00100 
00101   /**
00102   * Read at least count bytes into the buffer;
00103   * returns number of bytes available to be read.
00104   */
00105   virtual FXuval readBuffer(FXuval count);
00106 
00107 public:
00108 
00109   /**
00110   * Construct stream with given container object.  The container object
00111   * is an object that will itself not be saved to or loaded from the stream,
00112   * but which may be referenced by other objects.  These references will be
00113   * properly saved and restored.
00114   */
00115   FXStream(const FXObject* cont=NULL);
00116 
00117   /**
00118   * Open stream for reading (FXStreamLoad) or for writing (FXStreamSave).
00119   * An initial buffer size may be given, which must be at least 16 bytes.
00120   * If data is not NULL, it is expected to point to an external data buffer
00121   * of length size; otherwise stream will use an internally managed buffer.
00122   */
00123   FXbool open(FXStreamDirection save_or_load,FXuchar* data=NULL,FXuval size=8192UL,FXbool owned=false);
00124 
00125   /// Flush buffer
00126   virtual FXbool flush();
00127 
00128   /// Close; return true if OK
00129   virtual FXbool close();
00130 
00131   /// Get available buffer space
00132   FXuval getSpace() const;
00133 
00134   /// Set available buffer space
00135   void setSpace(FXuval sp);
00136 
00137   /// Set buffer ownership flag
00138   void setOwned(FXbool owned){ owns=owned; }
00139 
00140   /// Get buffer ownership flag
00141   FXbool isOwned() const { return owns; }
00142 
00143   /// Get status code
00144   FXStreamStatus status() const { return code; }
00145 
00146   /// Return true if at end of file or error
00147   FXbool eof() const { return code!=FXStreamOK; }
00148 
00149   /// Set status code
00150   void setError(FXStreamStatus err);
00151 
00152   /// Obtain stream direction
00153   FXStreamDirection direction() const { return dir; }
00154 
00155   /// Get parent object
00156   const FXObject* container() const { return parent; }
00157 
00158   /// Get position
00159   FXlong position() const { return pos; }
00160 
00161   /// Move to position relative to head, tail, or current location
00162   virtual FXbool position(FXlong offset,FXWhence whence=FXFromStart);
00163 
00164   /**
00165   * Change swap bytes flag.
00166   */
00167   void swapBytes(FXbool s){ swap=s; }
00168 
00169   /**
00170   * Get state of the swap bytes flag.
00171   */
00172   FXbool swapBytes() const { return swap; }
00173 
00174   /**
00175   * Set stream to big endian mode if true.  Byte swapping will
00176   * be enabled if the machine native byte order is not equal to
00177   * the desired byte order.
00178   */
00179   void setBigEndian(FXbool big);
00180 
00181   /**
00182   * Return true if big endian mode.
00183   */
00184   FXbool isBigEndian() const;
00185 
00186   /// Save single items to stream
00187   FXStream& operator<<(const FXuchar& v);
00188   FXStream& operator<<(const FXchar& v){ return *this << reinterpret_cast<const FXuchar&>(v); }
00189   FXStream& operator<<(const FXbool& v){ return *this << reinterpret_cast<const FXuchar&>(v); }
00190   FXStream& operator<<(const FXushort& v);
00191   FXStream& operator<<(const FXshort& v){ return *this << reinterpret_cast<const FXushort&>(v); }
00192   FXStream& operator<<(const FXuint& v);
00193   FXStream& operator<<(const FXint& v){ return *this << reinterpret_cast<const FXuint&>(v); }
00194   FXStream& operator<<(const FXfloat& v){ return *this << reinterpret_cast<const FXuint&>(v); }
00195   FXStream& operator<<(const FXdouble& v);
00196   FXStream& operator<<(const FXlong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
00197   FXStream& operator<<(const FXulong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
00198 
00199   /// Save arrays of items to stream
00200   FXStream& save(const FXuchar* p,FXuval n);
00201   FXStream& save(const FXchar* p,FXuval n){ return save(reinterpret_cast<const FXuchar*>(p),n); }
00202   FXStream& save(const FXbool* p,FXuval n){ return save(reinterpret_cast<const FXuchar*>(p),n); }
00203   FXStream& save(const FXushort* p,FXuval n);
00204   FXStream& save(const FXshort* p,FXuval n){ return save(reinterpret_cast<const FXushort*>(p),n); }
00205   FXStream& save(const FXuint* p,FXuval n);
00206   FXStream& save(const FXint* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
00207   FXStream& save(const FXfloat* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
00208   FXStream& save(const FXdouble* p,FXuval n);
00209   FXStream& save(const FXlong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
00210   FXStream& save(const FXulong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
00211 
00212   /// Load single items from stream
00213   FXStream& operator>>(FXuchar& v);
00214   FXStream& operator>>(FXchar& v){ return *this >> reinterpret_cast<FXuchar&>(v); }
00215   FXStream& operator>>(FXbool& v){ return *this >> reinterpret_cast<FXuchar&>(v); }
00216   FXStream& operator>>(FXushort& v);
00217   FXStream& operator>>(FXshort& v){ return *this >> reinterpret_cast<FXushort&>(v); }
00218   FXStream& operator>>(FXuint& v);
00219   FXStream& operator>>(FXint& v){ return *this >> reinterpret_cast<FXuint&>(v); }
00220   FXStream& operator>>(FXfloat& v){ return *this >> reinterpret_cast<FXuint&>(v); }
00221   FXStream& operator>>(FXdouble& v);
00222   FXStream& operator>>(FXlong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
00223   FXStream& operator>>(FXulong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
00224 
00225   /// Load arrays of items from stream
00226   FXStream& load(FXuchar* p,FXuval n);
00227   FXStream& load(FXchar* p,FXuval n){ return load(reinterpret_cast<FXuchar*>(p),n); }
00228   FXStream& load(FXbool* p,FXuval n){ return load(reinterpret_cast<FXuchar*>(p),n); }
00229   FXStream& load(FXushort* p,FXuval n);
00230   FXStream& load(FXshort* p,FXuval n){ return load(reinterpret_cast<FXushort*>(p),n); }
00231   FXStream& load(FXuint* p,FXuval n);
00232   FXStream& load(FXint* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
00233   FXStream& load(FXfloat* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
00234   FXStream& load(FXdouble* p,FXuval n);
00235   FXStream& load(FXlong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
00236   FXStream& load(FXulong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
00237 
00238   /// Save object
00239   FXStream& saveObject(const FXObject* v);
00240 
00241   /// Load object
00242   FXStream& loadObject(FXObject*& v);
00243 
00244   /// Add object without saving or loading
00245   FXStream& addObject(const FXObject* v);
00246 
00247   /// Destructor
00248   virtual ~FXStream();
00249   };
00250 
00251 }
00252 
00253 #endif

Copyright © 1997-2010 Jeroen van der Zijp