UOPstream.H 7.91 KB
Newer Older
mattijs's avatar
mattijs committed
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 9
    Copyright (C) 2011-2014 OpenFOAM Foundation
    Copyright (C) 2017-2018 OpenCFD Ltd.
mattijs's avatar
mattijs committed
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.
mattijs's avatar
mattijs committed
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/>.
mattijs's avatar
mattijs committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

Class
    Foam::UOPstream

Description
    Output inter-processor communications stream operating on external
    buffer.

SourceFiles
    UOPstream.C

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

#include "Pstream.H"

#ifndef UOPstream_H
#define UOPstream_H

#include "UPstream.H"
#include "Ostream.H"
#include "DynamicList.H"
#include "PstreamBuffers.H"

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
                          Class UOPstream Declaration
\*---------------------------------------------------------------------------*/

class UOPstream
:
    public UPstream,
    public Ostream
{
Mark Olesen's avatar
Mark Olesen committed
63
    // Private Data
mattijs's avatar
mattijs committed
64 65 66 67 68

        int toProcNo_;

        DynamicList<char>& sendBuf_;

69
        const int tag_;
mattijs's avatar
mattijs committed
70

71 72
        const label comm_;

mattijs's avatar
mattijs committed
73 74 75
        const bool sendAtDestruct_;


76
    // Private Member Functions
mattijs's avatar
mattijs committed
77

78 79 80 81
        //- Prepare buffer for count bytes of output at specified alignment.
        inline void prepareBuffer(const size_t count, const size_t align);

        //- Write data to the transfer buffer
mattijs's avatar
mattijs committed
82
        template<class T>
83
        inline void writeToBuffer(const T& val);
mattijs's avatar
mattijs committed
84 85

        //- Write a char to the transfer buffer
86
        inline void writeToBuffer(const char& c);
mattijs's avatar
mattijs committed
87

88 89 90 91 92 93 94 95 96 97 98 99
        //- Write count bytes of data to the transfer buffer
        //  using align byte alignment
        inline void writeToBuffer
        (
            const void* data,
            const size_t count,
            const size_t align
        );

        //- Write string length and content.
        //  The content includes the trailing nul char.
        inline void writeStringToBuffer(const std::string& str);
mattijs's avatar
mattijs committed
100 101 102 103 104 105 106 107 108 109 110 111 112


public:

    // Constructors

        //- Construct given process index to send to and optional buffer size,
        //  write format and IO version
        UOPstream
        (
            const commsTypes commsType,
            const int toProcNo,
            DynamicList<char>& sendBuf,
113
            const int tag = UPstream::msgType(),
114
            const label comm = UPstream::worldComm,
mattijs's avatar
mattijs committed
115 116 117 118 119 120
            const bool sendAtDestruct = true,
            streamFormat format=BINARY,
            versionNumber version=currentVersion
        );

        //- Construct given buffers
121
        UOPstream(const int toProcNo, PstreamBuffers& buffers);
mattijs's avatar
mattijs committed
122 123


124 125
    //- Destructor
    ~UOPstream();
mattijs's avatar
mattijs committed
126 127


128
    // Member Functions
mattijs's avatar
mattijs committed
129 130 131 132

        // Inquiry

            //- Return flags of output stream
133
            virtual ios_base::fmtflags flags() const
mattijs's avatar
mattijs committed
134 135 136 137 138
            {
                return ios_base::fmtflags(0);
            }


139
        // Write Functions
mattijs's avatar
mattijs committed
140 141 142 143 144 145 146

            //- Write given buffer to given processor
            static bool write
            (
                const commsTypes commsType,
                const int toProcNo,
                const char* buf,
147
                const std::streamsize bufSize,
148 149
                const int tag = UPstream::msgType(),
                const label communicator = 0
mattijs's avatar
mattijs committed
150 151
            );

152 153 154
            //- Write token to stream or otherwise handle it.
            //  \return false if the token type was not handled by this method
            virtual bool write(const token& tok);
mattijs's avatar
mattijs committed
155

156
            //- Write single character. Whitespace is suppressed.
157
            virtual Ostream& write(const char c);
mattijs's avatar
mattijs committed
158

159 160
            //- Write the word-characters of a character string.
            //  Sends as a single char, or as word.
161
            virtual Ostream& write(const char* str);
mattijs's avatar
mattijs committed
162 163

            //- Write word
164
            virtual Ostream& write(const word& str);
mattijs's avatar
mattijs committed
165 166

            //- Write string
167
            virtual Ostream& write(const string& str);
mattijs's avatar
mattijs committed
168 169 170

            //- Write std::string surrounded by quotes.
            //  Optional write without quotes.
171
            virtual Ostream& writeQuoted
mattijs's avatar
mattijs committed
172
            (
173
                const std::string& str,
mattijs's avatar
mattijs committed
174 175 176
                const bool quoted=true
            );

177 178
            //- Write int32_t as a label
            virtual Ostream& write(const int32_t val);
179

180
            //- Write int64_t as a label
181
            virtual Ostream& write(const int64_t val);
mattijs's avatar
mattijs committed
182 183

            //- Write floatScalar
184
            virtual Ostream& write(const floatScalar val);
mattijs's avatar
mattijs committed
185 186

            //- Write doubleScalar
187
            virtual Ostream& write(const doubleScalar val);
mattijs's avatar
mattijs committed
188

189
            //- Write binary block with 8-byte alignment.
190
            virtual Ostream& write(const char* data, std::streamsize count);
mattijs's avatar
mattijs committed
191

192
            //- Low-level raw binary output.
193
            virtual Ostream& writeRaw(const char* data, std::streamsize count);
194

195 196 197
            //- Begin marker for low-level raw binary output.
            //  The count indicates the number of bytes for subsequent
            //  writeRaw calls.
198
            virtual bool beginRawWrite(std::streamsize count);
199

200
            //- End marker for low-level raw binary output.
201
            virtual bool endRawWrite()
202
            {
203
                return true;
204 205
            }

mattijs's avatar
mattijs committed
206
            //- Add indentation characters
207
            virtual void indent()
mattijs's avatar
mattijs committed
208 209 210 211 212 213
            {}


        // Stream state functions

            //- Flush stream
214
            virtual void flush()
mattijs's avatar
mattijs committed
215 216 217
            {}

            //- Add newline and flush stream
218
            virtual void endl()
mattijs's avatar
mattijs committed
219 220
            {}

221 222 223 224 225 226 227 228 229 230 231 232 233
            //- Get the current padding character
            //  \return previous padding character
            virtual char fill() const
            {
                return 0;
            }

            //- Set padding character for formatted field up to field width
            virtual char fill(const char)
            {
                return 0;
            }

mattijs's avatar
mattijs committed
234
            //- Get width of output field
235
            virtual int width() const
mattijs's avatar
mattijs committed
236 237 238 239
            {
                return 0;
            }

240 241
            //- Set width of output field
            //  \return previous width
242
            virtual int width(const int)
mattijs's avatar
mattijs committed
243 244 245 246 247
            {
                 return 0;
            }

            //- Get precision of output field
248
            virtual int precision() const
mattijs's avatar
mattijs committed
249 250 251 252
            {
                 return 0;
            }

253 254
            //- Set precision of output field
            //  \return old precision
255
            virtual int precision(const int)
mattijs's avatar
mattijs committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269
            {
                 return 0;
            }


        // Edit

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


Mark Olesen's avatar
Mark Olesen committed
270
    // Print
mattijs's avatar
mattijs committed
271

Mark Olesen's avatar
Mark Olesen committed
272 273
        //- Print stream description to Ostream
        void print(Ostream& os) const;
mattijs's avatar
mattijs committed
274 275 276 277 278 279 280 281 282 283 284 285
};


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

} // End namespace Foam

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

#endif

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