IOstream.H 10.8 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
8
     \\/     M anipulation  |
-------------------------------------------------------------------------------
                            | Copyright (C) 2011-2015 OpenFOAM Foundation
9
10
11
12
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

13
14
15
16
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
17
18
19
20
21
22
23

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
24
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
25
26
27
28
29

Class
    Foam::IOstream

Description
Mark Olesen's avatar
Mark Olesen committed
30
31
32
33
34
35
36
37
    An IOstream is an abstract base class for all input/output systems; be
    they streams, files, token lists etc.

    The basic operations are construct, close, read token, read primitive
    and read binary block.  In addition version control and line number
    counting is incorporated.  Usually one would use the read primitive
    member functions, but if one were reading a stream on unknown data
    sequence one can read token by token, and then analyse.
38
39

SourceFiles
Mark Olesen's avatar
Mark Olesen committed
40
    IOstream.C
41
42
43
44
45
46
47
48
49

\*---------------------------------------------------------------------------*/

#ifndef IOstream_H
#define IOstream_H

#include "char.H"
#include "bool.H"
#include "label.H"
Mark Olesen's avatar
Mark Olesen committed
50
#include "uLabel.H"
51
52
53
#include "scalar.H"
#include "fileName.H"
#include "InfoProxy.H"
54
#include "IOstreamOption.H"
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include <iostream>

using std::ios_base;
using std::istream;
using std::ostream;

using std::cin;
using std::cout;
using std::cerr;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

/*---------------------------------------------------------------------------*\
                           Class IOstream Declaration
\*---------------------------------------------------------------------------*/

class IOstream
76
77
:
    public IOstreamOption
78
79
80
{
public:

81
    // Public Data Types
82

83
        //- Enumeration for stream open/closed state
84
        enum streamAccess : char
85
        {
86
87
            CLOSED = 0,         //!< stream not open
            OPENED              //!< stream is open
88
89
90
        };


91
    // Public Static Data
92

93
94
        //- Default precision
        static unsigned int precision_;
95
96


97
protected:
98

99
    // Protected Data
100

101
102
        //- Name for any generic stream - normally treat as readonly
        static fileName staticName_;
103

104
        streamAccess openClosed_;
105

106
        ios_base::iostate ioState_;
107

108
109
110
111
112
113
114
        //- The label byte-size (could also be stored as byte)
        unsigned short labelByteSize_;

        //- The scalar byte-size (could also be stored as byte)
        unsigned short scalarByteSize_;

        //- The file line
115
        label lineNumber_;
116
117


118
    // Protected Member Functions
119

120
    // Access
121

122
123
124
125
126
        //- Set stream opened
        void setOpened()
        {
            openClosed_ = OPENED;
        }
127

128
129
130
131
132
        //- Set stream closed
        void setClosed()
        {
            openClosed_ = CLOSED;
        }
133

134
135
136
137
138
        //- Set stream state
        void setState(ios_base::iostate state)
        {
            ioState_ = state;
        }
139

140
141
142
143
144
        //- Set stream to be good
        void setGood()
        {
            ioState_ = ios_base::iostate(0);
        }
145
146


147
public:
148

149
    // Constructors
150

151
152
153
154
155
156
        //- Construct with specified stream option
        explicit IOstream(const IOstreamOption option)
        :
            IOstreamOption(option),
            openClosed_(CLOSED),
            ioState_(ios_base::iostate(0)),
157
158
            labelByteSize_(sizeof(label)),
            scalarByteSize_(sizeof(scalar)),
159
160
161
162
            lineNumber_(0)
        {
            setBad();
        }
163

164
        //- Construct with format, version
165
166
167
168
169
170
171
172
173
        IOstream
        (
            streamFormat format,
            versionNumber version,
            compressionType compression=UNCOMPRESSED
        )
        :
            IOstream(IOstreamOption(format, version, compression))
        {}
174
175


176
177
    //- Destructor
    virtual ~IOstream() = default;
178
179


180
    // Member Functions
181

182
    // Access
183

184
185
186
        //- Return the name of the stream
        //  Useful for Fstream to return the filename
        virtual const fileName& name() const;
187

188
189
190
        //- Return non-const access to the name of the stream
        //  Useful to alter the stream name
        virtual fileName& name();
191
192


193
    // Check
194

195
196
        //- Check IOstream status for given operation.
        //  Print IOstream state if error has occurred
197
        virtual bool check(const char* operation) const;
198

199
200
        //- Check IOstream status for given operation.
        //  Print IOstream state if error has occurred and exit
201
        void fatalCheck(const char* operation) const;
202

203
204
205
206
207
        //- Return true if stream has been opened
        bool opened() const
        {
            return openClosed_ == OPENED;
        }
208

209
210
211
212
213
        //- Return true if stream is closed
        bool closed() const
        {
            return openClosed_ == CLOSED;
        }
214

215
216
217
218
219
        //- Return true if next operation might succeed
        bool good() const
        {
            return ioState_ == 0;
        }
220

221
222
223
224
225
        //- Return true if end of input seen
        bool eof() const
        {
            return ioState_ & ios_base::eofbit;
        }
226

227
228
229
230
231
        //- Return true if next operation will fail
        bool fail() const
        {
            return ioState_ & (ios_base::badbit | ios_base::failbit);
        }
232

233
234
235
236
237
        //- Return true if stream is corrupted
        bool bad() const
        {
            return ioState_ & ios_base::badbit;
        }
238

239
240
        //- Return true if the stream has not failed
        explicit operator bool() const
241
        {
242
            return !fail();
243
        }
244

245
246
247
248
249
        //- Return true if the stream has failed
        bool operator!() const
        {
            return fail();
        }
250
251


252
    // Stream State Functions
253

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        //- The label byte-size associated with the stream
        unsigned labelByteSize() const
        {
            return labelByteSize_;
        }

        //- The scalar byte-size associated with the stream
        unsigned scalarByteSize() const
        {
            return scalarByteSize_;
        }

        //- Set the label byte-size associated with the stream
        void setLabelByteSize(unsigned nbytes)
        {
            labelByteSize_ = nbytes;
        }

        //- Set the scalar byte-size associated with the stream
        void setScalarByteSize(unsigned nbytes)
        {
            scalarByteSize_ = nbytes;
        }

278
279
280
281
282
        //- Const access to the current stream line number
        label lineNumber() const
        {
            return lineNumber_;
        }
283

284
285
286
287
288
        //- Non-const access to the current stream line number
        label& lineNumber()
        {
            return lineNumber_;
        }
289

290
291
292
293
        //- Set the stream line number
        //  \return the previous value
        label lineNumber(const label num)
        {
294
            const label old(lineNumber_);
295
296
297
            lineNumber_ = num;
            return old;
        }
298

299
300
        //- Return flags of stream
        virtual ios_base::fmtflags flags() const = 0;
301

302
303
304
305
306
        //- Return the default precision
        static unsigned int defaultPrecision()
        {
            return precision_;
        }
307

308
309
310
311
312
313
314
315
        //- Reset the default precision
        //  \return the previous value
        static unsigned int defaultPrecision(unsigned int prec)
        {
            unsigned int old(precision_);
            precision_ = prec;
            return old;
        }
316

317
318
319
320
321
        //- Set stream to have reached eof
        void setEof()
        {
            ioState_ |= ios_base::eofbit;
        }
322

323
324
325
326
327
        //- Set stream to have failed
        void setFail()
        {
            ioState_ |= ios_base::failbit;
        }
328

329
330
331
332
333
        //- Set stream to be bad
        void setBad()
        {
            ioState_ |= ios_base::badbit;
        }
334

335
336
337
338
339
340
341
342
343
344
345
        //- Set flags of stream
        virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;

        //- Set flags of stream
        ios_base::fmtflags setf(const ios_base::fmtflags f)
        {
            return flags(flags() | f);
        }

        //- Set flags of given field of stream
        ios_base::fmtflags setf
346
        (
347
348
            const ios_base::fmtflags f,
            const ios_base::fmtflags mask
349
350
        )
        {
351
            return flags((flags() & ~mask) | (f & mask));
352
353
        }

354
355
356
357
358
        //- Unset flags of stream
        void unsetf(const ios_base::fmtflags f)
        {
            flags(flags() & ~f);
        }
359

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377

    // Print

        //- Print description of IOstream to Ostream
        virtual void print(Ostream& os) const;

        //- Print information about the stream state bits
        void print(Ostream& os, const int streamState) const;


    // Info

        //- Return info proxy.
        //  Used to print IOstream information to a stream
        InfoProxy<IOstream> info() const
        {
            return *this;
        }
378
379
380
};


381
// Ostream Operator
382

383
384
385
template<>
Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);

386
387
388
389
390

// --------------------------------------------------------------------
// ------ Manipulators (not taking arguments)
// --------------------------------------------------------------------

391
//- An IOstream manipulator
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
typedef IOstream& (*IOstreamManip)(IOstream&);

//- operator<< handling for manipulators without arguments
inline IOstream& operator<<(IOstream& io, IOstreamManip f)
{
    return f(io);
}


inline IOstream& dec(IOstream& io)
{
    io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
    return io;
}

inline IOstream& hex(IOstream& io)
{
    io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
    return io;
}

inline IOstream& oct(IOstream& io)
{
    io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
    return io;
}

inline IOstream& fixed(IOstream& io)
{
    io.setf(ios_base::fixed, ios_base::floatfield);
    return io;
}

inline IOstream& scientific(IOstream& io)
{
    io.setf(ios_base::scientific, ios_base::floatfield);
    return io;
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //