IListStream.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) 2017-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::IListStream
28 
29 Description
30  An input stream that reads from a List and manages the List storage.
31  Similar to IStringStream but with a List for its storage instead of
32  as string to allow reuse of List contents without copying.
33 
34 See Also
35  Foam::OListStream
36  Foam::UIListStream
37  Foam::UOListStream
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef IListStream_H
42 #define IListStream_H
43 
44 #include "List.H"
45 #include "UIListStream.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 namespace Detail
52 {
53 
54 /*---------------------------------------------------------------------------*\
55  Class Detail::IListStreamAllocator Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 //- An stream/stream-buffer input allocator with List storage
60 :
61  private List<char>,
63 {
64 protected:
65 
66  // Constructors
67 
68  //- Default construct
70  :
71  List<char>(),
72  UIListStreamAllocator(List<char>::data(), List<char>::size())
73  {}
74 
75  //- Move construct from List
77  :
78  List<char>(std::move(buffer)),
79  UIListStreamAllocator(List<char>::data(), List<char>::size())
80  {}
81 
82  //- Move construct from DynamicList
83  template<int SizeMin>
85  :
86  List<char>(std::move(buffer)),
87  UIListStreamAllocator(List<char>::data(), List<char>::size())
88  {}
89 
90 
91  // Protected Member Functions
92 
93  //- Convenience method to address the underlying List storage
94  inline void reset_gbuffer()
95  {
97  (
100  );
101  }
102 
103 public:
104 
105  // Member Functions
106 
107  //- The current get position in the buffer
109 
110  //- Clear storage
111  inline void clearStorage()
112  {
114  reset_gbuffer();
115  }
116 
117  //- Transfer contents to other List
118  inline void swap(List<char>& list)
119  {
121  reset_gbuffer();
122  }
123 };
124 
125 } // End namespace Detail
126 
127 
128 /*---------------------------------------------------------------------------*\
129  Class IListStream Declaration
130 \*---------------------------------------------------------------------------*/
131 
132 //- An ISstream with internal List storage. Always UNCOMPRESSED.
133 class IListStream
134 :
136  public ISstream
137 {
139 
140 public:
141 
142  // Constructors
143 
144  //- Default construct with an empty list
145  explicit IListStream
146  (
147  IOstreamOption streamOpt = IOstreamOption()
148  )
149  :
150  allocator_type(),
151  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
152  {}
153 
154  //- Move construct from List
155  explicit IListStream
156  (
157  ::Foam::List<char>&& buffer, // Fully qualify (issue #1521)
158  IOstreamOption streamOpt = IOstreamOption()
159  )
160  :
161  allocator_type(std::move(buffer)),
162  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
163  {}
164 
165 
166  //- Move construct from DynamicList
167  template<int SizeMin>
168  explicit IListStream
169  (
170  DynamicList<char,SizeMin>&& buffer,
171  IOstreamOption streamOpt = IOstreamOption()
172  )
173  :
174  allocator_type(std::move(buffer)),
175  ISstream(stream_, "input", streamOpt.format(), streamOpt.version())
176  {}
177 
178 
179  // Member Functions
180 
181  //- The current get position in the buffer
182  using allocator_type::size;
183 
184 
185  //- Return the current get position in the buffer
186  std::streampos pos() const
187  {
188  return allocator_type::tellg();
189  }
190 
191  //- Rewind the stream, clearing any old errors
192  virtual void rewind()
193  {
195  setGood(); // resynchronize with internal state
196  }
197 
198 
199  //- Print stream description to Ostream
200  virtual void print(Ostream& os) const;
201 
202 
203  // Member Operators
204 
205  //- A non-const reference to const Istream
206  // Needed for read-constructors where the stream argument is temporary
207  Istream& operator()() const
208  {
209  // Could also rewind
210  return const_cast<IListStream&>(*this);
211  }
212 
213 
214  // Additional constructors and methods (as per v2012 and earlier)
215  #ifdef Foam_IOstream_extras
216 
217  //- Construct with an empty list
218  explicit IListStream
219  (
222  )
223  :
224  IListStream(IOstreamOption(fmt, ver))
225  {}
226 
227 
228  //- Move construct from List
230  (
231  ::Foam::List<char>&& buffer, // Fully qualify (issue #1521)
233  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
234  )
235  :
236  IListStream(std::move(buffer), IOstreamOption(fmt, ver))
237  {}
238 
239 
240  //- Move construct from DynamicList
241  template<int SizeMin>
242  explicit IListStream
243  (
244  DynamicList<char,SizeMin>&& buffer,
246  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
247  )
248  :
249  IListStream(std::move(buffer), IOstreamOption(fmt, ver))
250  {}
251 
252  #endif /* Foam_IOstream_extras */
253 };
254 
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257 
258 } // End namespace Foam
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 #endif
263 
264 // ************************************************************************* //
Foam::IListStream::operator()
Istream & operator()() const
A non-const reference to const Istream.
Definition: IListStream.H:206
List.H
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::Detail::IListStreamAllocator
An stream/stream-buffer input allocator with List storage.
Definition: IListStream.H:58
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::Detail::UIListStreamAllocator::list
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: UIListStream.H:163
Foam::IListStream
An ISstream with internal List storage. Always UNCOMPRESSED.
Definition: IListStream.H:132
Foam::IOstream::setGood
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
Foam::Detail::IListStreamAllocator::IListStreamAllocator
IListStreamAllocator(List< char > &&buffer)
Move construct from List.
Definition: IListStream.H:75
Foam::Detail::UIListStreamAllocator
An stream/stream-buffer input allocator for a externally allocated list.
Definition: UIListStream.H:120
Foam::Detail::UIListStreamAllocator::tellg
std::streampos tellg() const
Position of the get buffer.
Definition: UIListStream.H:181
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
UIListStream.H
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
Foam::Detail::IListStreamAllocator::IListStreamAllocator
IListStreamAllocator()
Default construct.
Definition: IListStream.H:68
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::IListStream::IListStream
IListStream(IOstreamOption streamOpt=IOstreamOption())
Default construct with an empty list.
Definition: IListStream.H:145
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Detail::UIListStreamAllocator::reset
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:148
Foam::ISstream::ISstream
ISstream(std::istream &is, const string &streamName, IOstreamOption streamOpt=IOstreamOption())
Construct wrapper around std::istream, set stream status.
Definition: ISstreamI.H:32
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Detail::UIListStreamAllocator::stream_
stream_type stream_
The stream.
Definition: UIListStream.H:132
Foam::Detail::IListStreamAllocator::IListStreamAllocator
IListStreamAllocator(DynamicList< char, SizeMin > &&buffer)
Move construct from DynamicList.
Definition: IListStream.H:83
Foam::IListStream::rewind
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: IListStream.H:191
Foam::Detail::IListStreamAllocator::swap
void swap(List< char > &list)
Transfer contents to other List.
Definition: IListStream.H:117
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::IListStream::pos
std::streampos pos() const
Return the current get position in the buffer.
Definition: IListStream.H:185
Foam::IListStream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:34
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::Detail::IListStreamAllocator::reset_gbuffer
void reset_gbuffer()
Convenience method to address the underlying List storage.
Definition: IListStream.H:93
Foam::Detail::IListStreamAllocator::clearStorage
void clearStorage()
Clear storage.
Definition: IListStream.H:110
Foam::Detail::UIListStreamAllocator::size
label size() const
The list size.
Definition: UIListStream.H:175
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Detail::UIListStreamAllocator::rewind
void rewind()
Move to buffer start, clear errors.
Definition: UIListStream.H:187
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55