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

11
12
13
14
    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.
15
16
17
18
19
20
21

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

Class
    Foam::IOstream

Description
Mark Olesen's avatar
Mark Olesen committed
28
29
30
31
32
33
34
35
    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.
36
37

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

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

#ifndef IOstream_H
#define IOstream_H

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

#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
74
75
:
    public IOstreamOption
76
77
78
{
public:

79
    // Public Data Types
80
81

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


89
    // Public Static Data
90

91
92
        //- Default precision
        static unsigned int precision_;
93
94


95
protected:
96

97
    // Protected Data
98

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

102
        streamAccess openClosed_;
103

104
        ios_base::iostate ioState_;
105

106
        label lineNumber_;
107
108


109
    // Protected Member Functions
110

111
    // Access
112

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

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

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

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


138
public:
139

140
    // Constructors
141

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

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


165
166
    //- Destructor
    virtual ~IOstream() = default;
167
168


169
    // Member Functions
170

171
    // Access
172

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

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


182
    // Check
183

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

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

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

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

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

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

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

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

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

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


241
    // Stream State Functions
242

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

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

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

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

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

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

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

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

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

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

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

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

    // 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;
        }
343
344
345
};


346
// Ostream operator
347

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

351
352
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

// --------------------------------------------------------------------
// ------ 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

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