Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / OpenFOAM / db / IOstreams / Pstreams / IPstream.H
blobbd9384de385a5ff2c1119f65bb814687a4136b14
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::IPstream
28 Description
29     Input inter-processor communications stream.
31 SourceFiles
32     IPstream.C
34 \*---------------------------------------------------------------------------*/
36 // intentionally outside include guards!
37 #include <OpenFOAM/Pstream.H>
39 #ifndef IPstream_H
40 #define IPstream_H
42 #include <OpenFOAM/Istream.H>
43 #include "IPstreamImpl.H"
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 namespace Foam
50 /*---------------------------------------------------------------------------*\
51                            Class IPstream Declaration
52 \*---------------------------------------------------------------------------*/
54 class IPstream
56     public Pstream,
57     public Istream
59     // Private data
61         int fromProcNo_;
62         label messageSize_;
64         static autoPtr<IPstreamImpl> impl_;
67     // Private member functions
69         //- Check the bufferPosition_ against messageSize_ for EOF
70         inline void checkEof();
72         //- Read a T from the transfer buffer
73         template<class T>
74         inline void readFromBuffer(T&);
76         //- Read data from the transfer buffer
77         inline void readFromBuffer(void* data, size_t count, size_t align);
79         //- Fetches the IPstreamImpl instance if necessary
80         static autoPtr<IPstreamImpl>& impl()
81         {
82             if(!impl_.valid())
83             {
84                 impl_ = IPstreamImpl::New();
85             }
86             return impl_;
87         }
89 public:
91     // Constructors
93         //- Construct given process index to read from and optional buffer size,
94         //  read format and IO version
95         IPstream
96         (
97             const commsTypes commsType,
98             const int fromProcNo,
99             const label bufSize = 0,
100             streamFormat format=BINARY,
101             versionNumber version=currentVersion
102         );
105     // Destructor
107         ~IPstream();
110     // Member functions
112         // Inquiry
114             //- Return flags of output stream
115             ios_base::fmtflags flags() const
116             {
117                 return ios_base::fmtflags(0);
118             }
121         // Read functions
123             //- Read into given buffer from given processor and return the
124             //  message size
125             static label read
126             (
127                 const commsTypes commsType,
128                 const int fromProcNo,
129                 char* buf,
130                 const std::streamsize bufSize
131             )
132             {
133                 return impl()->read(commsType, fromProcNo, buf, bufSize);
134             }
136             //- Non-blocking receives: wait until all have finished.
137             static void waitRequests()
138             {
139                 impl()->waitRequests();
140             }
142             //- Non-blocking receives: has request i finished?
143             static bool finishedRequest(const label i)
144             {
145                 return impl()->finishedRequest(i);
146             }
148             //- Return next token from stream
149             Istream& read(token&);
151             //- Read a character
152             Istream& read(char&);
154             //- Read a word
155             Istream& read(word&);
157             // Read a string (including enclosing double-quotes)
158             Istream& read(string&);
160             //- Read a label
161             Istream& read(label&);
163             //- Read a floatScalar
164             Istream& read(floatScalar&);
166             //- Read a doubleScalar
167             Istream& read(doubleScalar&);
169             //- Read binary block
170             Istream& read(char*, std::streamsize);
172             //- Rewind and return the stream so that it may be read again
173             Istream& rewind();
176         // Edit
178             //- Set flags of stream
179             ios_base::fmtflags flags(const ios_base::fmtflags)
180             {
181                 return ios_base::fmtflags(0);
182             }
185         // Print
187             //- Print description of IOstream to Ostream
188             void print(Ostream&) const;
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
194 } // End namespace Foam
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
198 #endif
200 // ************************ vim: set sw=4 sts=4 et: ************************ //