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

FXStream.h
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,2021 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (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 *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXSTREAM_H
22 #define FXSTREAM_H
23 
24 namespace FX {
25 
26 
28 enum FXStreamDirection {
29  FXStreamDead=0,
30  FXStreamSave=1,
31  FXStreamLoad=2
32  };
33 
34 
36 enum FXStreamStatus {
37  FXStreamOK=0,
38  FXStreamEnd=1,
39  FXStreamFull=2,
40  FXStreamNoWrite=3,
41  FXStreamNoRead=4,
42  FXStreamFormat=5,
43  FXStreamUnknown=6,
44  FXStreamAlloc=7,
45  FXStreamFailure=8
46  };
47 
48 
50 enum FXWhence {
51  FXFromStart=0,
52  FXFromCurrent=1,
53  FXFromEnd=2
54  };
55 
56 
78 class FXAPI FXStream {
79 protected:
80  FXHash hash; // Hash table
81  const FXObject *parent; // Parent object
82  FXuchar *begptr; // Begin of buffer
83  FXuchar *endptr; // End of buffer
84  FXuchar *wrptr; // Write pointer
85  FXuchar *rdptr; // Read pointer
86  FXlong pos; // Position
87  FXStreamDirection dir; // Direction of current transfer
88  FXStreamStatus code; // Status code
89  FXuint seq; // Sequence number
90  FXbool owns; // Stream owns buffer
91  FXbool swap; // Swap bytes on readin
92 protected:
93 
98  virtual FXuval writeBuffer(FXuval count);
99 
104  virtual FXuval readBuffer(FXuval count);
105 
106 private:
107  FXStream(const FXStream&);
108  FXStream &operator=(const FXStream&);
109 public:
110 
117  FXStream(const FXObject* cont=NULL);
118 
125  FXbool open(FXStreamDirection save_or_load,FXuchar* data=NULL,FXuval size=8192UL,FXbool owned=false);
126 
128  virtual FXbool flush();
129 
131  virtual FXbool close();
132 
134  FXuval getSpace() const;
135 
137  void setSpace(FXuval sp);
138 
140  void setOwned(FXbool owned){ owns=owned; }
141 
143  FXbool isOwned() const { return owns; }
144 
146  FXStreamStatus status() const { return code; }
147 
149  FXbool eof() const { return code!=FXStreamOK; }
150 
152  void setError(FXStreamStatus err);
153 
155  FXStreamDirection direction() const { return dir; }
156 
158  const FXObject* container() const { return parent; }
159 
161  FXlong position() const { return pos; }
162 
164  virtual FXbool position(FXlong offset,FXWhence whence=FXFromStart);
165 
169  void swapBytes(FXbool s){ swap=s; }
170 
174  FXbool swapBytes() const { return swap; }
175 
181  void setBigEndian(FXbool big);
182 
186  FXbool isBigEndian() const;
187 
189  FXStream& operator<<(const FXuchar& v);
190  FXStream& operator<<(const FXchar& v){ return *this << reinterpret_cast<const FXuchar&>(v); }
191  FXStream& operator<<(const FXbool& v){ return *this << reinterpret_cast<const FXuchar&>(v); }
192  FXStream& operator<<(const FXushort& v);
193  FXStream& operator<<(const FXshort& v){ return *this << reinterpret_cast<const FXushort&>(v); }
194  FXStream& operator<<(const FXuint& v);
195  FXStream& operator<<(const FXint& v){ return *this << reinterpret_cast<const FXuint&>(v); }
196  FXStream& operator<<(const FXfloat& v){ return *this << reinterpret_cast<const FXuint&>(v); }
197  FXStream& operator<<(const FXdouble& v);
198  FXStream& operator<<(const FXlong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
199  FXStream& operator<<(const FXulong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
200 
202  FXStream& save(const FXuchar* p,FXuval n);
203  FXStream& save(const FXchar* p,FXuval n){ return save(reinterpret_cast<const FXuchar*>(p),n); }
204  FXStream& save(const FXbool* p,FXuval n){ return save(reinterpret_cast<const FXuchar*>(p),n); }
205  FXStream& save(const FXushort* p,FXuval n);
206  FXStream& save(const FXshort* p,FXuval n){ return save(reinterpret_cast<const FXushort*>(p),n); }
207  FXStream& save(const FXuint* p,FXuval n);
208  FXStream& save(const FXint* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
209  FXStream& save(const FXfloat* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
210  FXStream& save(const FXdouble* p,FXuval n);
211  FXStream& save(const FXlong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
212  FXStream& save(const FXulong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
213 
215  FXStream& operator>>(FXuchar& v);
216  FXStream& operator>>(FXchar& v){ return *this >> reinterpret_cast<FXuchar&>(v); }
217  FXStream& operator>>(FXbool& v){ return *this >> reinterpret_cast<FXuchar&>(v); }
218  FXStream& operator>>(FXushort& v);
219  FXStream& operator>>(FXshort& v){ return *this >> reinterpret_cast<FXushort&>(v); }
220  FXStream& operator>>(FXuint& v);
221  FXStream& operator>>(FXint& v){ return *this >> reinterpret_cast<FXuint&>(v); }
222  FXStream& operator>>(FXfloat& v){ return *this >> reinterpret_cast<FXuint&>(v); }
223  FXStream& operator>>(FXdouble& v);
224  FXStream& operator>>(FXlong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
225  FXStream& operator>>(FXulong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
226 
228  FXStream& load(FXuchar* p,FXuval n);
229  FXStream& load(FXchar* p,FXuval n){ return load(reinterpret_cast<FXuchar*>(p),n); }
230  FXStream& load(FXbool* p,FXuval n){ return load(reinterpret_cast<FXuchar*>(p),n); }
231  FXStream& load(FXushort* p,FXuval n);
232  FXStream& load(FXshort* p,FXuval n){ return load(reinterpret_cast<FXushort*>(p),n); }
233  FXStream& load(FXuint* p,FXuval n);
234  FXStream& load(FXint* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
235  FXStream& load(FXfloat* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
236  FXStream& load(FXdouble* p,FXuval n);
237  FXStream& load(FXlong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
238  FXStream& load(FXulong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
239 
241  FXStream& saveObject(const FXObject* v);
242 
244  FXStream& loadObject(FXObject*& v);
245 
247  FXStream& addObject(const FXObject* v);
248 
250  template<class TYPE>
251  FXStream& operator>>(TYPE*& obj){ return loadObject(reinterpret_cast<FXObject*&>(obj)); }
252 
254  template<class TYPE>
255  FXStream& operator<<(const TYPE* obj){ return saveObject(static_cast<const FXObject*>(obj)); }
256 
258  virtual ~FXStream();
259  };
260 
261 }
262 
263 #endif
FXbool isOwned() const
Get buffer ownership flag.
Definition: FXStream.h:143
void setOwned(FXbool owned)
Set buffer ownership flag.
Definition: FXStream.h:140
FXStream & operator<<(const TYPE *obj)
Save object.
Definition: FXStream.h:255
A hash table for mapping pointers to pointers.
Definition: FXHash.h:33
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:78
void swapBytes(FXbool s)
Change swap bytes flag.
Definition: FXStream.h:169
FXStreamStatus status() const
Get status code.
Definition: FXStream.h:146
FXStreamDirection direction() const
Obtain stream direction.
Definition: FXStream.h:155
const FXObject * container() const
Get parent object.
Definition: FXStream.h:158
Definition: FX4Splitter.h:28
Object is the base class for all objects in FOX; in order to receive messages from the user interface...
Definition: FXObject.h:103
FXbool swapBytes() const
Get state of the swap bytes flag.
Definition: FXStream.h:174
FXbool eof() const
Return true if at end of file or error.
Definition: FXStream.h:149
FXlong position() const
Get position.
Definition: FXStream.h:161
FXStream & operator>>(TYPE *&obj)
Load object.
Definition: FXStream.h:251

Copyright © 1997-2020 Jeroen van der Zijp