IOstream.H 9.78 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
6
7
8
    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
     \\/     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 whether the stream open or closed
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
        label lineNumber_;
109
110


111
    // Protected Member Functions
112

113
    // Access
114

115
116
117
118
119
        //- Set stream opened
        void setOpened()
        {
            openClosed_ = OPENED;
        }
120

121
122
123
124
125
        //- Set stream closed
        void setClosed()
        {
            openClosed_ = CLOSED;
        }
126

127
128
129
130
131
        //- Set stream state
        void setState(ios_base::iostate state)
        {
            ioState_ = state;
        }
132

133
134
135
136
137
        //- Set stream to be good
        void setGood()
        {
            ioState_ = ios_base::iostate(0);
        }
138
139


140
public:
141

142
    // Constructors
143

144
145
146
147
148
149
150
151
152
153
        //- Construct with specified stream option
        explicit IOstream(const IOstreamOption option)
        :
            IOstreamOption(option),
            openClosed_(CLOSED),
            ioState_(ios_base::iostate(0)),
            lineNumber_(0)
        {
            setBad();
        }
154

155
156
157
158
159
160
161
162
163
164
        //- Construct setting format and version
        IOstream
        (
            streamFormat format,
            versionNumber version,
            compressionType compression=UNCOMPRESSED
        )
        :
            IOstream(IOstreamOption(format, version, compression))
        {}
165
166


167
168
    //- Destructor
    virtual ~IOstream() = default;
169
170


171
    // Member Functions
172

173
    // Access
174

175
176
177
        //- Return the name of the stream
        //  Useful for Fstream to return the filename
        virtual const fileName& name() const;
178

179
180
181
        //- Return non-const access to the name of the stream
        //  Useful to alter the stream name
        virtual fileName& name();
182
183


184
    // Check
185

186
187
        //- Check IOstream status for given operation.
        //  Print IOstream state if error has occurred
188
        virtual bool check(const char* operation) const;
189

190
191
        //- Check IOstream status for given operation.
        //  Print IOstream state if error has occurred and exit
192
        void fatalCheck(const char* operation) const;
193

194
195
196
197
198
        //- Return true if stream has been opened
        bool opened() const
        {
            return openClosed_ == OPENED;
        }
199

200
201
202
203
204
        //- Return true if stream is closed
        bool closed() const
        {
            return openClosed_ == CLOSED;
        }
205

206
207
208
209
210
        //- Return true if next operation might succeed
        bool good() const
        {
            return ioState_ == 0;
        }
211

212
213
214
215
216
        //- Return true if end of input seen
        bool eof() const
        {
            return ioState_ & ios_base::eofbit;
        }
217

218
219
220
221
222
        //- Return true if next operation will fail
        bool fail() const
        {
            return ioState_ & (ios_base::badbit | ios_base::failbit);
        }
223

224
225
226
227
228
        //- Return true if stream is corrupted
        bool bad() const
        {
            return ioState_ & ios_base::badbit;
        }
229

230
231
        //- Return true if the stream has not failed
        explicit operator bool() const
232
        {
233
            return !fail();
234
        }
235

236
237
238
239
240
        //- Return true if the stream has failed
        bool operator!() const
        {
            return fail();
        }
241
242


243
    // Stream State Functions
244

245
246
247
248
249
        //- Const access to the current stream line number
        label lineNumber() const
        {
            return lineNumber_;
        }
250

251
252
253
254
255
        //- Non-const access to the current stream line number
        label& lineNumber()
        {
            return lineNumber_;
        }
256

257
258
259
260
261
262
263
264
        //- Set the stream line number
        //  \return the previous value
        label lineNumber(const label num)
        {
            label old(lineNumber_);
            lineNumber_ = num;
            return old;
        }
265

266
267
        //- Return flags of stream
        virtual ios_base::fmtflags flags() const = 0;
268

269
270
271
272
273
        //- Return the default precision
        static unsigned int defaultPrecision()
        {
            return precision_;
        }
274

275
276
277
278
279
280
281
282
        //- Reset the default precision
        //  \return the previous value
        static unsigned int defaultPrecision(unsigned int prec)
        {
            unsigned int old(precision_);
            precision_ = prec;
            return old;
        }
283

284
285
286
287
288
        //- Set stream to have reached eof
        void setEof()
        {
            ioState_ |= ios_base::eofbit;
        }
289

290
291
292
293
294
        //- Set stream to have failed
        void setFail()
        {
            ioState_ |= ios_base::failbit;
        }
295

296
297
298
299
300
        //- Set stream to be bad
        void setBad()
        {
            ioState_ |= ios_base::badbit;
        }
301

302
303
304
305
306
307
308
309
310
311
312
        //- 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
313
        (
314
315
            const ios_base::fmtflags f,
            const ios_base::fmtflags mask
316
317
        )
        {
318
            return flags((flags() & ~mask) | (f & mask));
319
320
        }

321
322
323
324
325
        //- Unset flags of stream
        void unsetf(const ios_base::fmtflags f)
        {
            flags(flags() & ~f);
        }
326

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

    // 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;
        }
345
346
347
};


348
// Ostream operator
349

350
351
352
template<>
Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

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

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

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