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,2024 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 
27 class FXObject;
28 
29 
31 enum FXStreamDirection {
32  FXStreamDead=0,
33  FXStreamSave=1,
34  FXStreamLoad=2
35  };
36 
37 
39 enum FXStreamStatus {
40  FXStreamOK=0,
41  FXStreamEnd=1,
42  FXStreamFull=2,
43  FXStreamNoWrite=3,
44  FXStreamNoRead=4,
45  FXStreamFormat=5,
46  FXStreamUnknown=6,
47  FXStreamAlloc=7,
48  FXStreamFailure=8
49  };
50 
51 
53 enum FXWhence {
54  FXFromStart=0,
55  FXFromCurrent=1,
56  FXFromEnd=2
57  };
58 
59 
81 class FXAPI FXStream {
82 protected:
83  FXHash hash; // Hash table
84  const FXObject *parent; // Parent object
85  FXuchar *begptr; // Begin of buffer
86  FXuchar *endptr; // End of buffer
87  FXuchar *wrptr; // Write pointer
88  FXuchar *rdptr; // Read pointer
89  FXlong pos; // Position
90  FXStreamDirection dir; // Direction of current transfer
91  FXStreamStatus code; // Status code
92  FXuint seq; // Sequence number
93  FXbool owns; // Stream owns buffer
94  FXbool swap; // Swap bytes on readin
95 protected:
96 
101  virtual FXuval writeBuffer(FXuval count);
102 
107  virtual FXuval readBuffer(FXuval count);
108 
109 private:
110  FXStream(const FXStream&);
111  FXStream &operator=(const FXStream&);
112 public:
113 
120  FXStream(const FXObject* cont=nullptr);
121 
128  FXbool open(FXStreamDirection save_or_load,FXuchar* data=nullptr,FXuval size=8192UL,FXbool owned=false);
129 
131  virtual FXbool flush();
132 
134  virtual FXbool close();
135 
137  FXuval getSpace() const;
138 
140  void setSpace(FXuval sp);
141 
143  void setOwned(FXbool owned){ owns=owned; }
144 
146  FXbool isOwned() const { return owns; }
147 
149  FXStreamStatus status() const { return code; }
150 
152  FXbool eof() const { return code!=FXStreamOK; }
153 
155  void setError(FXStreamStatus err);
156 
158  FXStreamDirection direction() const { return dir; }
159 
161  const FXObject* container() const { return parent; }
162 
164  FXlong position() const { return pos; }
165 
167  virtual FXbool position(FXlong offset,FXWhence whence=FXFromStart);
168 
172  void swapBytes(FXbool s){ swap=s; }
173 
177  FXbool swapBytes() const { return swap; }
178 
184  void setBigEndian(FXbool big);
185 
189  FXbool isBigEndian() const;
190 
192  FXStream& operator<<(const FXuchar& v);
193  FXStream& operator<<(const FXchar& v){ return *this << reinterpret_cast<const FXuchar&>(v); }
194  FXStream& operator<<(const FXbool& v);
195  FXStream& operator<<(const FXushort& v);
196  FXStream& operator<<(const FXshort& v){ return *this << reinterpret_cast<const FXushort&>(v); }
197  FXStream& operator<<(const FXuint& v);
198  FXStream& operator<<(const FXint& v){ return *this << reinterpret_cast<const FXuint&>(v); }
199  FXStream& operator<<(const FXfloat& v){ return *this << reinterpret_cast<const FXuint&>(v); }
200  FXStream& operator<<(const FXdouble& v);
201  FXStream& operator<<(const FXlong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
202  FXStream& operator<<(const FXulong& v){ return *this << reinterpret_cast<const FXdouble&>(v); }
203 
205  FXStream& save(const FXuchar* p,FXuval n);
206  FXStream& save(const FXchar* p,FXuval n){ return save(reinterpret_cast<const FXuchar*>(p),n); }
207  FXStream& save(const FXbool* p,FXuval n);
208  FXStream& save(const FXushort* p,FXuval n);
209  FXStream& save(const FXshort* p,FXuval n){ return save(reinterpret_cast<const FXushort*>(p),n); }
210  FXStream& save(const FXuint* p,FXuval n);
211  FXStream& save(const FXint* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
212  FXStream& save(const FXfloat* p,FXuval n){ return save(reinterpret_cast<const FXuint*>(p),n); }
213  FXStream& save(const FXdouble* p,FXuval n);
214  FXStream& save(const FXlong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
215  FXStream& save(const FXulong* p,FXuval n){ return save(reinterpret_cast<const FXdouble*>(p),n); }
216 
218  FXStream& operator>>(FXuchar& v);
219  FXStream& operator>>(FXchar& v){ return *this >> reinterpret_cast<FXuchar&>(v); }
220  FXStream& operator>>(FXbool& v);
221  FXStream& operator>>(FXushort& v);
222  FXStream& operator>>(FXshort& v){ return *this >> reinterpret_cast<FXushort&>(v); }
223  FXStream& operator>>(FXuint& v);
224  FXStream& operator>>(FXint& v){ return *this >> reinterpret_cast<FXuint&>(v); }
225  FXStream& operator>>(FXfloat& v){ return *this >> reinterpret_cast<FXuint&>(v); }
226  FXStream& operator>>(FXdouble& v);
227  FXStream& operator>>(FXlong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
228  FXStream& operator>>(FXulong& v){ return *this >> reinterpret_cast<FXdouble&>(v); }
229 
231  FXStream& load(FXuchar* p,FXuval n);
232  FXStream& load(FXchar* p,FXuval n){ return load(reinterpret_cast<FXuchar*>(p),n); }
233  FXStream& load(FXbool* p,FXuval n);
234  FXStream& load(FXushort* p,FXuval n);
235  FXStream& load(FXshort* p,FXuval n){ return load(reinterpret_cast<FXushort*>(p),n); }
236  FXStream& load(FXuint* p,FXuval n);
237  FXStream& load(FXint* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
238  FXStream& load(FXfloat* p,FXuval n){ return load(reinterpret_cast<FXuint*>(p),n); }
239  FXStream& load(FXdouble* p,FXuval n);
240  FXStream& load(FXlong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
241  FXStream& load(FXulong* p,FXuval n){ return load(reinterpret_cast<FXdouble*>(p),n); }
242 
244  FXStream& saveObject(const FXObject* v);
245 
247  FXStream& loadObject(FXObject*& v);
248 
250  FXStream& addObject(const FXObject* v);
251 
253  template<class TYPE>
254  FXStream& operator>>(TYPE*& obj){ return loadObject(reinterpret_cast<FXObject*&>(obj)); }
255 
257  template<class TYPE>
258  FXStream& operator<<(const TYPE* obj){ return saveObject(static_cast<const FXObject*>(obj)); }
259 
261  virtual ~FXStream();
262  };
263 
264 }
265 
266 #endif
FXbool isOwned() const
Get buffer ownership flag.
Definition: FXStream.h:146
void setOwned(FXbool owned)
Set buffer ownership flag.
Definition: FXStream.h:143
FXStream & operator<<(const TYPE *obj)
Save object.
Definition: FXStream.h:258
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:81
void swapBytes(FXbool s)
Change swap bytes flag.
Definition: FXStream.h:172
FXStreamStatus status() const
Get status code.
Definition: FXStream.h:149
FXStreamDirection direction() const
Obtain stream direction.
Definition: FXStream.h:158
const FXObject * container() const
Get parent object.
Definition: FXStream.h:161
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:138
FXbool swapBytes() const
Get state of the swap bytes flag.
Definition: FXStream.h:177
FXbool eof() const
Return true if at end of file or error.
Definition: FXStream.h:152
FXlong position() const
Get position.
Definition: FXStream.h:164
FXStream & operator>>(TYPE *&obj)
Load object.
Definition: FXStream.h:254

Copyright © 1997-2022 Jeroen van der Zijp