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

FXStream.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * 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 *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXStream.h,v 1.42 2006/01/22 17:58:10 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXSTREAM_H
25 #define FXSTREAM_H
26 
27 
28 namespace FX {
29 
30 
31 /// Stream data flow direction
32 enum FXStreamDirection {
33  FXStreamDead=0, /// Unopened stream
34  FXStreamSave=1, /// Saving stuff to stream
35  FXStreamLoad=2 /// Loading stuff from stream
36  };
37 
38 
39 /// Stream status codes
40 enum FXStreamStatus {
41  FXStreamOK=0, /// OK
42  FXStreamEnd=1, /// Try read past end of stream
43  FXStreamFull=2, /// Filled up stream buffer or disk full
44  FXStreamNoWrite=3, /// Unable to open for write
45  FXStreamNoRead=4, /// Unable to open for read
46  FXStreamFormat=5, /// Stream format error
47  FXStreamUnknown=6, /// Trying to read unknown class
48  FXStreamAlloc=7, /// Alloc failed
49  FXStreamFailure=8 /// General failure
50  };
51 
52 
53 /// Stream seeking
54 enum FXWhence {
55  FXFromStart=0, /// Seek from start position
56  FXFromCurrent=1, /// Seek from current position
57  FXFromEnd=2 /// Seek from end position
58  };
59 
60 
61 /**
62 * A stream is a way to serialize data and objects into a byte stream.
63 * Each item of data that is saved or loaded from the stream may be byte-swapped,
64 * thus allowing little-endian machines to read data produced on big endian ones
65 * and vice-versa.
66 * Data is serialized exactly as-is. There are no tags or other markers
67 * inserted into the stream; thus, the stream may be used to save or load arbitrary
68 * binary data.
69 * Objects derived from FXObjects may be serialized also; whenever a reference to an
70 * object is serialized, a table is consulted to determine if the same object has
71 * been encountered previously; if not, the object is added to the table and then
72 * its contents are serialized. If the object has been encountered before, only a
73 * reference to the object is serialized.
74 * When loading back a serialized object, new instances are constructed using
75 * the default constructor, and subsequently the object's contents are loaded.
76 * A special container object may be passed in which is placed in the table
77 * as if it had been encountered before; this will cause only references to this
78 * object to be saved. The container object is typically the top-level document
79 * object which manages all objects contained by it. Additional objects may be
80 * added using addObject(); these will not be actually saved or loaded.
81 */
82 class FXAPI FXStream {
83 protected:
84  FXHash hash; // Hash table
85  const FXObject *parent; // Parent object
86  FXuchar *begptr; // Begin of buffer
87  FXuchar *endptr; // End of buffer
88  FXuchar *wrptr; // Write pointer
89  FXuchar *rdptr; // Read pointer
90  FXlong pos; // Position
91  FXStreamDirection dir; // Direction of current transfer
92  FXStreamStatus code; // Status code
93  FXuint seq; // Sequence number
94  bool owns; // Stream owns buffer
95  bool swap; // Swap bytes on readin
96 protected:
97 
98  /**
99  * Write at least count bytes from the buffer;
100  * returns number of bytes available to be written.
101  */
102  virtual FXuval writeBuffer(FXuval count);
103 
104  /**
105  * Read at least count bytes into the buffer;
106  * returns number of bytes available to be read.
107  */
108  virtual FXuval readBuffer(FXuval count);
109 
110 public:
111 
112  /**
113  * Construct stream with given container object. The container object
114  * is an object that will itself not be saved to or loaded from the stream,
115  * but which may be referenced by other objects. These references will be
116  * properly saved and restored.
117  */
118  FXStream(const FXObject* cont=NULL);
119 
120  /**
121  * Open stream for reading (FXStreamLoad) or for writing (FXStreamSave).
122  * An initial buffer size may be given, which must be at least 16 bytes.
123  * If data is not NULL, it is expected to point to an external data buffer
124  * of length size; otherwise stream will use an internally managed buffer.
125  */
126  bool open(FXStreamDirection save_or_load,FXuval size=8192,FXuchar* data=NULL);
127 
128  /// Flush buffer
129  virtual bool flush();
130 
131  /// Close; return true if OK
132  virtual bool close();
133 
134  /// Get available buffer space
135  FXuval getSpace() const;
136 
137  /// Set available buffer space
138  void setSpace(FXuval sp);
139 
140  /// Get status code
141  FXStreamStatus status() const { return code; }
142 
143  /// Return true if at end of file or error
144  bool eof() const { return code!=FXStreamOK; }
145 
146  /// Set status code
147  void setError(FXStreamStatus err);
148 
149  /// Obtain stream direction
150  FXStreamDirection direction() const { return dir; }
151 
152  /// Get parent object
153  const FXObject* container() const { return parent; }
155  /// Get position
156  FXlong position() const { return pos; }
157 
158  /// Move to position relative to head, tail, or current location
159  virtual bool position(FXlong offset,FXWhence whence=FXFromStart);
160 
161  /**
162  * Change swap bytes flag.
163  */
164  void swapBytes(bool s){ swap=s; }
165 
166  /**
167  * Get state of the swap bytes flag.
168  */
169  bool swapBytes() const { return swap; }
170 
171  /**
172  * Set stream to big endian mode if true. Byte swapping will
173  * be enabled if the machine native byte order is not equal to
174  * the desired byte order.
175  */
176  void setBigEndian(bool big);
178  /**
179  * Return true if big endian mode.
180  */
181  bool isBigEndian() const;
183  /// Save single items to stream
184  FXStream& operator<<(const FXuchar& v);
185  FXStream& operator<<(const FXchar& v){ return *this << reinterpret_cast<const FXuchar&>(v); }
186  FXStream& operator<<(const FXushort& v);
187  FXStream& operator<<(const FXshort& v){ return *this << reinterpret_cast<const FXushort&>(v); }
188  FXStream& operator<<(const FXuint& v);
189  FXStream& operator<<(const FXint& v){ return *this << reinterpret_cast<const FXuint&>(v); }
190  FXStream& operator<<(const FXfloat& v){ return *this << reinterpret_cast<const FXuint&>(v); }
192  FXStream& operator<<(const FXlong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
193  FXStream& operator<<(const FXulong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
195  /// Save arrays of items to stream
196  FXStream& save(const FXuchar* p,FXuval n);
197  FXStream& save(const FXchar* p,FXuval n){ return save(reinterpret_cast<const FXuchar*>(p),n); }
198  FXStream& save(const FXushort* p,FXuval n);
199  FXStream& save(const FXshort* p,FXuval n){ return save(reinterpret_cast<const FXushort*>(p),n); }
200  FXStream& save(const FXuint* p,FXuval n);
201  FXStream& save(const FXint* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
202  FXStream& save(const FXfloat* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
203  FXStream& save(const FXdouble* p,FXuval n);
204  FXStream& save(const FXlong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
205  FXStream& save(const FXulong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
207  /// Load single items from stream
209  FXStream& operator>>(FXchar& v){ return *this >> reinterpret_cast<FXuchar&>(v); }
211  FXStream& operator>>(FXshort& v){ return *this >> reinterpret_cast<FXushort&>(v); }
213  FXStream& operator>>(FXint& v){ return *this >> reinterpret_cast<FXuint&>(v); }
214  FXStream& operator>>(FXfloat& v){ return *this >> reinterpret_cast<FXuint&>(v); }
216  FXStream& operator>>(FXlong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
217  FXStream& operator>>(FXulong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
219  /// Load arrays of items from stream
220  FXStream& load(FXuchar* p,FXuval n);
221  FXStream& load(FXchar* p,FXuval n){ return load(reinterpret_cast<FXuchar*>(p),n); }
222  FXStream& load(FXushort* p,FXuval n);
223  FXStream& load(FXshort* p,FXuval n){ return load(reinterpret_cast<FXushort*>(p),n); }
224  FXStream& load(FXuint* p,FXuval n);
225  FXStream& load(FXint* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
226  FXStream& load(FXfloat* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
227  FXStream& load(FXdouble* p,FXuval n);
228  FXStream& load(FXlong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
229  FXStream& load(FXulong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
230 
231  /// Save object
232  FXStream& saveObject(const FXObject* v);
233 
234  /// Load object
235  FXStream& loadObject(FXObject*& v);
236 
237  /// Add object without saving or loading
238  FXStream& addObject(const FXObject* v);
239 
240  /// Destructor
241  virtual ~FXStream();
242  };
243 
244 }
245 
246 #endif
FXStream & operator>>(FXStream &store, FXDate &d)
unsigned short FXushort
Definition: fxdefs.h:394
char FXchar
Definition: fxdefs.h:387
Unable to open for read.
Definition: FXStream.h:52
short FXshort
Definition: fxdefs.h:395
General failure.
Definition: FXStream.h:56
Loading stuff from stream.
Definition: FXStream.h:38
unsigned int FXuint
Definition: fxdefs.h:396
Unable to open for write.
Definition: FXStream.h:51
#define FXAPI
Definition: fxdefs.h:122
Seek from start position.
Definition: FXStream.h:66
Filled up stream buffer or disk full.
Definition: FXStream.h:50
#define NULL
Definition: fxdefs.h:41
Try read past end of stream.
Definition: FXStream.h:49
A hash table for associating pointers to pointers.
Definition: FXHash.h:33
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
Alloc failed.
Definition: FXStream.h:55
OK.
Definition: FXStream.h:48
FXWhence
Stream seeking.
Definition: FXStream.h:65
double FXdouble
Definition: fxdefs.h:399
Seek from current position.
Definition: FXStream.h:67
int FXint
Definition: fxdefs.h:397
void swap(FXString &a, FXString &b)
Definition: FXString.h:628
Trying to read unknown class.
Definition: FXStream.h:54
Object is the base class for all objects in FOX; in order to receive messages from the user interface...
Definition: FXObject.h:166
Seek from end position.
Definition: FXStream.h:68
Saving stuff to stream.
Definition: FXStream.h:37
unsigned char FXuchar
Definition: fxdefs.h:392
float FXfloat
Definition: fxdefs.h:398
FXStreamDirection
Stream data flow direction.
Definition: FXStream.h:35
unsigned long FXuval
Definition: fxdefs.h:436
FXStream & operator<<(FXStream &store, const FXDate &d)
FXStreamStatus
Stream status codes.
Definition: FXStream.h:47
Unopened stream.
Definition: FXStream.h:36
Stream format error.
Definition: FXStream.h:53

Copyright © 1997-2005 Jeroen van der Zijp