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

14
15
16
17
    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.
18
19
20
21
22
23
24

    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
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26
27
28
29
30

Class
    Foam::IOstream

Description
Mark Olesen's avatar
Mark Olesen committed
31
32
33
34
35
36
37
38
    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.
39
40

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

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

#ifndef IOstream_H
#define IOstream_H

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

#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
77
78
:
    public IOstreamOption
79
80
81
{
public:

82
    // Public Data Types
83

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


92
    // Public Static Data
93

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


98
protected:
99

100
    // Protected Data
101

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

105
        streamAccess openClosed_;
106

107
        ios_base::iostate ioState_;
108

109
110
111
112
113
114
115
        //- 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
116
        label lineNumber_;
117
118


119
    // Protected Member Functions
120

121
    // Access
122

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

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

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

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


148
public:
149

Mark Olesen's avatar
Mark Olesen committed
150
151
152
153
154
155
156
157
158
    // Generated Methods

        //- Copy construct
        IOstream(const IOstream&) = default;

        //- Destructor
        virtual ~IOstream() = default;


159
    // Constructors
160

161
162
163
164
165
166
        //- Construct with specified stream option
        explicit IOstream(const IOstreamOption option)
        :
            IOstreamOption(option),
            openClosed_(CLOSED),
            ioState_(ios_base::iostate(0)),
167
168
            labelByteSize_(sizeof(label)),
            scalarByteSize_(sizeof(scalar)),
169
170
171
172
            lineNumber_(0)
        {
            setBad();
        }
173

174
        //- Construct with format, version
175
176
177
178
179
180
181
182
183
        IOstream
        (
            streamFormat format,
            versionNumber version,
            compressionType compression=UNCOMPRESSED
        )
        :
            IOstream(IOstreamOption(format, version, compression))
        {}
184
185


186
    // Member Functions
187

188
    // Access
189

190
191
192
        //- Return the name of the stream
        //  Useful for Fstream to return the filename
        virtual const fileName& name() const;
193

194
195
196
        //- Return non-const access to the name of the stream
        //  Useful to alter the stream name
        virtual fileName& name();
197
198


199
    // Check
200

201
        //- Check IOstream status for given operation.
202
203
204
        //  Print IOstream state or generate a FatalIOError
        //  when an error has occurred.
        //  The base implementation is a fatalCheck
205
        virtual bool check(const char* operation) const;
206

207
        //- Check IOstream status for given operation.
208
209
        //  Generate a FatalIOError when an error has occurred.
        bool fatalCheck(const char* operation) const;
210

211
212
213
214
215
        //- Return true if stream has been opened
        bool opened() const
        {
            return openClosed_ == OPENED;
        }
216

217
218
219
220
221
        //- Return true if stream is closed
        bool closed() const
        {
            return openClosed_ == CLOSED;
        }
222

223
224
225
226
227
        //- Return true if next operation might succeed
        bool good() const
        {
            return ioState_ == 0;
        }
228

229
230
231
232
233
        //- Return true if end of input seen
        bool eof() const
        {
            return ioState_ & ios_base::eofbit;
        }
234

235
236
237
238
239
        //- Return true if next operation will fail
        bool fail() const
        {
            return ioState_ & (ios_base::badbit | ios_base::failbit);
        }
240

241
242
243
244
245
        //- Return true if stream is corrupted
        bool bad() const
        {
            return ioState_ & ios_base::badbit;
        }
246

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

253
254
255
256
257
        //- Return true if the stream has failed
        bool operator!() const
        {
            return fail();
        }
258
259


260
    // Element sizes (precision)
261

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
        //- 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;
        }

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

        //- Check if the label byte-size associated with the stream
        //- is the same as the given type
        template<class T = label>
        typename std::enable_if<std::is_integral<T>::value, bool>::type
        checkLabelSize() const
        {
            return labelByteSize_ == sizeof(T);
        }

        //- Check if the scalar byte-size associated with the stream
        //- is the same as the given type
        template<class T = scalar>
        typename std::enable_if<std::is_floating_point<T>::value, bool>::type
        checkScalarSize() const
        {
            return scalarByteSize_ == sizeof(T);
        }


    // Stream State Functions

308
309
310
311
312
        //- Const access to the current stream line number
        label lineNumber() const
        {
            return lineNumber_;
        }
313

314
315
316
317
318
        //- Non-const access to the current stream line number
        label& lineNumber()
        {
            return lineNumber_;
        }
319

320
321
322
323
        //- Set the stream line number
        //  \return the previous value
        label lineNumber(const label num)
        {
324
            const label old(lineNumber_);
325
326
327
            lineNumber_ = num;
            return old;
        }
328

329
330
        //- Return flags of stream
        virtual ios_base::fmtflags flags() const = 0;
331

332
333
334
335
336
        //- Return the default precision
        static unsigned int defaultPrecision()
        {
            return precision_;
        }
337

338
339
340
341
342
343
344
345
        //- Reset the default precision
        //  \return the previous value
        static unsigned int defaultPrecision(unsigned int prec)
        {
            unsigned int old(precision_);
            precision_ = prec;
            return old;
        }
346

347
348
349
350
351
        //- Set stream to have reached eof
        void setEof()
        {
            ioState_ |= ios_base::eofbit;
        }
352

353
354
355
356
357
        //- Set stream to have failed
        void setFail()
        {
            ioState_ |= ios_base::failbit;
        }
358

359
360
361
362
363
        //- Set stream to be bad
        void setBad()
        {
            ioState_ |= ios_base::badbit;
        }
364

365
366
367
368
369
370
371
372
373
374
375
        //- 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
376
        (
377
378
            const ios_base::fmtflags f,
            const ios_base::fmtflags mask
379
380
        )
        {
381
            return flags((flags() & ~mask) | (f & mask));
382
383
        }

384
385
386
387
388
        //- Unset flags of stream
        void unsetf(const ios_base::fmtflags f)
        {
            flags(flags() & ~f);
        }
389

390
391
392

    // Print

Mark Olesen's avatar
Mark Olesen committed
393
        //- Print stream description to Ostream
394
395
396
397
398
399
400
401
402
403
404
405
406
407
        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;
        }
408
409
410
};


411
// Ostream Operator
412

413
414
415
template<>
Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);

416
417
418
419
420

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

421
//- An IOstream manipulator
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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

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