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-2019 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  // Constructors
78 
79  //- Construct and set stream status
81  (
85  )
86  :
88  putBack_(false)
89  {}
90 
91 
92  //- Destructor
93  virtual ~Istream() = default;
94 
95 
96  // Member Functions
97 
98  // Read Functions
99 
100  //- Put back token
101  // Only a single put back is permitted
102  void putBack(const token& tok);
103 
104  //- Get the put back token if there is one and return true.
105  // Return false if no put back token is available.
106  bool getBack(token& tok);
107 
108  //- Peek at the put back token without removing it.
109  // Returns false if no put back token is available and set the
110  // token to undefined.
111  bool peekBack(token& tok);
112 
113  //- Return next token from stream
114  virtual Istream& read(token&) = 0;
115 
116  //- Read a character
117  virtual Istream& read(char&) = 0;
118 
119  //- Read a word
120  virtual Istream& read(word&) = 0;
121 
122  //- Read a string (including enclosing double-quotes)
123  virtual Istream& read(string&) = 0;
124 
125  //- Read a label
126  virtual Istream& read(label&) = 0;
127 
128  //- Read a floatScalar
129  virtual Istream& read(floatScalar&) = 0;
130 
131  //- Read a doubleScalar
132  virtual Istream& read(doubleScalar&) = 0;
133 
134  //- Read binary block
135  virtual Istream& read(char*, std::streamsize) = 0;
136 
137  //- Start of low-level raw binary read
138  virtual bool beginRawRead() = 0;
139 
140  //- End of low-level raw binary read
141  virtual bool endRawRead() = 0;
142 
143  //- Low-level raw binary read
144  virtual Istream& readRaw(char*, std::streamsize) = 0;
145 
146  //- Rewind the stream so that it may be read again
147  virtual void rewind() = 0;
148 
149 
150  // Read List punctuation tokens
151 
152  //- Begin read of data chunk, starts with '('.
153  // \return true or FatalIOError
154  bool readBegin(const char* funcName);
155 
156  //- End read of data chunk, ends with ')'
157  // \return true or FatalIOError
158  bool readEnd(const char* funcName);
159 
160  //- Begin read of list data, starts with '(' or '{'
161  // \return starting delimiter or FatalIOError
162  char readBeginList(const char* funcName);
163 
164  //- End read of list data, ends with ')' or '}'
165  // \return closing delimiter or FatalIOError
166  char readEndList(const char* funcName);
167 
168 
169  // Member Operators
170 
171  //- Return a non-const reference to const Istream
172  // Needed for read-constructors where the stream argument is temporary:
173  // e.g. thing thisThing(IFstream("thingFileName")());
174  Istream& operator()() const;
175 };
176 
177 
178 // --------------------------------------------------------------------
179 // ------ Manipulators (not taking arguments)
180 // --------------------------------------------------------------------
181 
182 //- An Istream manipulator
183 typedef Istream& (*IstreamManip)(Istream&);
184 
185 //- operator>> handling for manipulators without arguments
187 {
188  return f(is);
189 }
190 
191 //- operator>> handling for manipulators without arguments
193 {
194  f(is);
195  return is;
196 }
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 namespace Detail
202 {
203  //- Read binary block of contiguous data, possibly with conversion
204  template<class T>
205  void readContiguous(Istream& is, char* data, std::streamsize byteCount)
206  {
207  is.beginRawRead();
208 
210  {
212  (
213  is,
214  reinterpret_cast<label*>(data),
215  byteCount/sizeof(label)
216  );
217  }
219  {
220  readRawScalar
221  (
222  is,
223  reinterpret_cast<scalar*>(data),
224  byteCount/sizeof(scalar)
225  );
226  }
227  else
228  {
229  is.readRaw(data, byteCount);
230  }
231 
232  is.endRawRead();
233  }
234 
235 } // End namespace Detail
236 
237 
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 
240 } // End namespace Foam
241 
242 
243 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244 
245 #ifdef NoRepository
246  #include "HashTable.C"
247 #endif
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:73
token.H
Foam::doubleScalar
double doubleScalar
Floating-point double precision scalar type.
Definition: doubleScalar.H:52
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::floatScalar
float floatScalar
Floating-point single precision scalar type.
Definition: floatScalar.H:52
HashTable.C
Foam::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:414
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number.
Definition: IOstreamOption.H:193
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:228
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
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::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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:79
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.
Foam::IOstream::IOstream
IOstream(const IOstreamOption option)
Construct with specified stream option.
Definition: IOstream.H:152
IOstream.H
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
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::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
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:71
Foam::readRawLabel
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:297
Foam::Istream::Istream
Istream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct and set stream status.
Definition: Istream.H:80
Foam::IstreamManip
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition: Istream.H:182
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
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:204