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-2022 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 Foam_IListStream_H
42 #define Foam_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 
128 /*---------------------------------------------------------------------------*\
129  Class IListStream Declaration
130 \*---------------------------------------------------------------------------*/
131 
132 //- An ISstream with internal List storage. Always UNCOMPRESSED.
133 class IListStream
134 :
136  public ISstream
137 {
138  typedef Detail::IListStreamAllocator allocator_type;
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,
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
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  (
221  )
222  :
224  {}
225 
226 
227  //- Move construct from List
229  (
230  ::Foam::List<char>&& buffer, // Fully qualify (issue #1521)
232  )
233  :
234  IListStream(std::move(buffer), IOstreamOption(fmt))
235  {}
236 
237 
238  //- Move construct from DynamicList
239  template<int SizeMin>
240  explicit IListStream
241  (
242  DynamicList<char,SizeMin>&& buffer,
244  )
245  :
246  IListStream(std::move(buffer), IOstreamOption(fmt))
247  {}
248 
249  #endif /* Foam_IOstream_extras */
250 };
251 
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 } // End namespace Foam
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 #endif
260 
261 // ************************************************************************* //
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:27
stream_type stream_
The stream.
Definition: UIListStream.H:141
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A simple container for options an IOstream can normally have.
label size() const
The list size.
Definition: UIListStream.H:194
void swap(UList< T > &list)
Swap content with another UList of the same type in constant time.
Definition: UListI.H:427
An stream/stream-buffer input allocator with List storage.
Definition: IListStream.H:54
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
void reset_gbuffer()
Convenience method to address the underlying List storage.
Definition: IListStream.H:97
const UList< char > list() const
Const UList access to the input characters (shallow copy).
Definition: UIListStream.H:178
An stream/stream-buffer input allocator for a externally allocated list.
Definition: UIListStream.H:125
std::streampos tellg() const
Position of the get buffer.
Definition: UIListStream.H:202
std::streampos pos() const
Return the current get position in the buffer.
Definition: IListStream.H:207
void clearStorage()
Clear storage.
Definition: IListStream.H:118
An ISstream with internal List storage. Always UNCOMPRESSED.
Definition: IListStream.H:144
Istream & operator()() const
A non-const reference to const Istream.
Definition: IListStream.H:235
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
IListStreamAllocator()
Default construct.
Definition: IListStream.H:66
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
versionNumber version() const noexcept
Get the stream version.
streamFormat
Data format (ascii | binary)
IListStream(IOstreamOption streamOpt=IOstreamOption())
Default construct with an empty list.
Definition: IListStream.H:159
void swap(List< char > &list)
Transfer contents to other List.
Definition: IListStream.H:127
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:167
void reset(char *buffer, size_t nbytes)
Reset buffer pointers.
Definition: UIListStream.H:161
void rewind()
Move to buffer start, clear errors.
Definition: UIListStream.H:210
streamFormat format() const noexcept
Get the current stream format.
Namespace for OpenFOAM.
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: IListStream.H:215