FreeFOAM The Cross-Platform CFD Toolkit
IPstream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  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 Class
25  Foam::IPstream
26 
27 Description
28  Input inter-processor communications stream.
29 
30 SourceFiles
31  IPstream.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 // intentionally outside include guards!
36 #include <OpenFOAM/Pstream.H>
37 
38 #ifndef IPstream_H
39 #define IPstream_H
40 
41 #include <OpenFOAM/Istream.H>
42 #include "IPstreamImpl.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class IPstream Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 class IPstream
54 :
55  public Pstream,
56  public Istream
57 {
58  // Private data
59 
60  int fromProcNo_;
61  label messageSize_;
62 
63  static autoPtr<IPstreamImpl> impl_;
64 
65 
66  // Private member functions
67 
68  //- Check the bufferPosition_ against messageSize_ for EOF
69  inline void checkEof();
70 
71  //- Read a T from the transfer buffer
72  template<class T>
73  inline void readFromBuffer(T&);
74 
75  //- Read data from the transfer buffer
76  inline void readFromBuffer(void* data, size_t count, size_t align);
77 
78  //- Fetches the IPstreamImpl instance if necessary
79  static autoPtr<IPstreamImpl>& impl()
80  {
81  if(!impl_.valid())
82  {
83  impl_ = IPstreamImpl::New();
84  }
85  return impl_;
86  }
87 
88 public:
89 
90  // Constructors
91 
92  //- Construct given process index to read from and optional buffer size,
93  // read format and IO version
94  IPstream
95  (
96  const commsTypes commsType,
97  const int fromProcNo,
98  const label bufSize = 0,
101  );
102 
103 
104  // Destructor
105 
106  ~IPstream();
107 
108 
109  // Member functions
110 
111  // Inquiry
112 
113  //- Return flags of output stream
114  ios_base::fmtflags flags() const
115  {
116  return ios_base::fmtflags(0);
117  }
118 
119 
120  // Read functions
121 
122  //- Read into given buffer from given processor and return the
123  // message size
124  static label read
125  (
126  const commsTypes commsType,
127  const int fromProcNo,
128  char* buf,
129  const std::streamsize bufSize
130  )
131  {
132  return impl()->read(commsType, fromProcNo, buf, bufSize);
133  }
134 
135  //- Non-blocking receives: wait until all have finished.
136  static void waitRequests()
137  {
138  impl()->waitRequests();
139  }
140 
141  //- Non-blocking receives: has request i finished?
142  static bool finishedRequest(const label i)
143  {
144  return impl()->finishedRequest(i);
145  }
146 
147  //- Return next token from stream
148  Istream& read(token&);
149 
150  //- Read a character
151  Istream& read(char&);
152 
153  //- Read a word
154  Istream& read(word&);
155 
156  // Read a string (including enclosing double-quotes)
157  Istream& read(string&);
158 
159  //- Read a label
160  Istream& read(label&);
161 
162  //- Read a floatScalar
164 
165  //- Read a doubleScalar
167 
168  //- Read binary block
169  Istream& read(char*, std::streamsize);
170 
171  //- Rewind and return the stream so that it may be read again
172  Istream& rewind();
173 
174 
175  // Edit
176 
177  //- Set flags of stream
178  ios_base::fmtflags flags(const ios_base::fmtflags)
179  {
180  return ios_base::fmtflags(0);
181  }
182 
183 
184  // Print
185 
186  //- Print description of IOstream to Ostream
187  void print(Ostream&) const;
188 };
189 
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #endif
198 
199 // ************************ vim: set sw=4 sts=4 et: ************************ //