Istream.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  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 Class
28  Foam::Istream
29 
30 Description
31  An Istream is an abstract base class for all input systems
32  (streams, files, token lists etc). The basic operations
33  are construct, close, read token, read primitive and read binary
34  block.
35 
36  In addition, version control and line number counting is incorporated.
37  Usually one would use the read primitive member functions, but if one
38  were reading a stream on unknown data sequence one can read token by
39  token, and then analyse.
40 
41 SourceFiles
42  Istream.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Istream_H
47 #define Istream_H
48 
49 #include "IOstream.H"
50 #include "token.H"
51 #include "contiguous.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 /*---------------------------------------------------------------------------*\
59  Class Istream Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class Istream
63 :
64  public IOstream
65 {
66  // Private Data
67 
68  //- Has a token been put back on the stream?
69  bool putBack_;
70 
71  //- The last token put back on the stream
72  token putBackToken_;
73 
74 
75 public:
76 
77  // Generated Methods
78 
79  //- Copy construct
80  Istream(const Istream&) = default;
81 
82  //- Destructor
83  virtual ~Istream() = default;
84 
85 
86  // Constructors
87 
88  //- Default construct (ASCII, uncompressed),
89  //- construct with specified stream option.
90  explicit Istream(IOstreamOption streamOpt = IOstreamOption())
91  :
92  IOstream(streamOpt),
93  putBack_(false)
94  {}
95 
96  //- Construct with format, version (compression)
97  explicit Istream
98  (
99  streamFormat fmt,
101  compressionType comp = compressionType::UNCOMPRESSED
102  )
103  :
104  Istream(IOstreamOption(fmt, comp, ver))
105  {}
106 
107 
108  // Member Functions
109 
110  // Read Functions
111 
112  //- Put back token
113  // Only a single put back is permitted
114  void putBack(const token& tok);
115 
116  //- Get the put back token if there is one and return true.
117  // Return false if no put back token is available.
118  bool getBack(token& tok);
119 
120  //- Peek at the put back token without removing it.
121  // Returns false if no put back token is available and set the
122  // token to undefined.
123  bool peekBack(token& tok);
124 
125  //- Return next token from stream
126  virtual Istream& read(token&) = 0;
127 
128  //- Read a character
129  virtual Istream& read(char&) = 0;
130 
131  //- Read a word
132  virtual Istream& read(word&) = 0;
133 
134  //- Read a string (including enclosing double-quotes)
135  virtual Istream& read(string&) = 0;
136 
137  //- Read a label
138  virtual Istream& read(label&) = 0;
139 
140  //- Read a floatScalar
141  virtual Istream& read(floatScalar&) = 0;
142 
143  //- Read a doubleScalar
144  virtual Istream& read(doubleScalar&) = 0;
145 
146  //- Read binary block
147  virtual Istream& read(char*, std::streamsize) = 0;
148 
149  //- Start of low-level raw binary read
150  virtual bool beginRawRead() = 0;
151 
152  //- End of low-level raw binary read
153  virtual bool endRawRead() = 0;
154 
155  //- Low-level raw binary read
156  virtual Istream& readRaw(char*, std::streamsize) = 0;
157 
158  //- Rewind the stream so that it may be read again
159  virtual void rewind() = 0;
160 
161 
162  // Read List punctuation tokens
163 
164  //- Begin read of data chunk, starts with '('.
165  // \return true or FatalIOError
166  bool readBegin(const char* funcName);
167 
168  //- End read of data chunk, ends with ')'
169  // \return true or FatalIOError
170  bool readEnd(const char* funcName);
171 
172  //- Begin read of list data, starts with '(' or '{'
173  // \return starting delimiter or FatalIOError
174  char readBeginList(const char* funcName);
175 
176  //- End read of list data, ends with ')' or '}'
177  // \return closing delimiter or FatalIOError
178  char readEndList(const char* funcName);
179 
180 
181  // Member Operators
182 
183  //- Return a non-const reference to const Istream
184  // Needed for read-constructors where the stream argument is temporary:
185  // e.g. thing thisThing(IFstream("thingFileName")());
186  Istream& operator()() const;
187 };
188 
189 
190 // --------------------------------------------------------------------
191 // ------ Manipulators (not taking arguments)
192 // --------------------------------------------------------------------
193 
194 //- An Istream manipulator
195 typedef Istream& (*IstreamManip)(Istream&);
196 
197 //- operator>> handling for manipulators without arguments
199 {
200  return f(is);
201 }
202 
203 //- operator>> handling for manipulators without arguments
205 {
206  f(is);
207  return is;
208 }
209 
210 
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 
213 namespace Detail
214 {
215  //- Read binary block of contiguous data, possibly with conversion
216  template<class T>
217  void readContiguous(Istream& is, char* data, std::streamsize byteCount)
218  {
219  is.beginRawRead();
220 
222  {
224  (
225  is,
226  reinterpret_cast<label*>(data),
227  byteCount/sizeof(label)
228  );
229  }
231  {
232  readRawScalar
233  (
234  is,
235  reinterpret_cast<scalar*>(data),
236  byteCount/sizeof(scalar)
237  );
238  }
239  else
240  {
241  is.readRaw(data, byteCount);
242  }
243 
244  is.endRawRead();
245  }
246 
247 } // End namespace Detail
248 
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 } // End namespace Foam
253 
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257 #ifdef NoRepository
258  #include "HashTable.C"
259 #endif
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 #endif
264 
265 // ************************************************************************* //
token.H
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::Istream::Istream
Istream(IOstreamOption streamOpt=IOstreamOption())
Definition: Istream.H:89
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::Istream::Istream
Istream(const Istream &)=default
Copy construct.
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
HashTable.C
Foam::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:422
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:167
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Istream::readEnd
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:127
Foam::Istream::readRaw
virtual Istream & readRaw(char *, std::streamsize)=0
Low-level raw binary read.
Foam::is_contiguous_label
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:83
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:109
Foam::Istream::endRawRead
virtual bool endRawRead()=0
End of low-level raw binary read.
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::Istream::rewind
virtual void rewind()=0
Rewind the stream so that it may be read again.
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Istream::~Istream
virtual ~Istream()=default
Destructor.
IOstream.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::Istream::operator()
Istream & operator()() const
Return a non-const reference to const Istream.
Definition: Istream.C:189
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::is_contiguous_scalar
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:91
Foam::Istream::getBack
bool getBack(token &tok)
Get the put back token if there is one and return true.
Definition: Istream.C:75
Foam::Istream::beginRawRead
virtual bool beginRawRead()=0
Start of low-level raw binary read.
f
labelList f(nPoints)
contiguous.H
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::readRawLabel
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
Foam::IstreamManip
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition: Istream.H:194
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::Istream::peekBack
bool peekBack(token &tok)
Peek at the put back token without removing it.
Definition: Istream.C:94
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.
Foam::Detail::readContiguous
void readContiguous(Istream &is, char *data, std::streamsize byteCount)
Read binary block of contiguous data, possibly with conversion.
Definition: Istream.H:216