IOstream.H 11.7 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
        //- Default construct (ASCII, uncompressed),
        //- construct with specified stream option
        explicit IOstream(IOstreamOption streamOpt = IOstreamOption())
164
        :
165
            IOstreamOption(streamOpt),
166 167
            openClosed_(CLOSED),
            ioState_(ios_base::iostate(0)),
168 169
            labelByteSize_(sizeof(label)),
            scalarByteSize_(sizeof(scalar)),
170 171 172 173
            lineNumber_(0)
        {
            setBad();
        }
174

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


187
    // Member Functions
188

189
    // Access
190

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

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


200
    // Check
201

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

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

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

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

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

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

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

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

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

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


261
    // Element sizes (precision)
262

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

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

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

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

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

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

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

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

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

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

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

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

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

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

391 392 393

    // Print

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


412
// Ostream Operator
413

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

417 418 419 420 421

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

422
//- An IOstream manipulator
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 471
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

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