UOListStream.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) 2016-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::UOListStream
28 
29 Description
30  Similar to OStringStream but using an externally managed buffer for
31  its output.
32 
33  This allows the output buffer to be reused and can make it easier when
34  writing out data. It is the user's responsibility to ensure proper
35  synchronization in the sizes. Provided that the external buffer is large
36  enough that overflow does not occur, the following usage pattern
37  works.
38 
39  \code
40  DynamicList<char> buffer(4096); // allocate some large buffer
41 
42  {
43  UOListStream os(buffer);
44  os << "content1" << " and more content";
45  buffer.resize(os.size()); // synchronize sizes
46  }
47 
48  something.write(buffer, buffer.size());
49  \endcode
50 
51  Although the UOListStream is quite lightweight, there may be cases
52  where it is preferable to reuse the stream as well.
53  \code
54  DynamicList<char> buffer(4096); // allocate some large buffer
55 
56  UOListStream os(buffer);
57  os << "content1" << " and more content";
58  buffer.resize(os.size()); // synchronize sizes
59 
60  something.write(buffer, buffer.size());
61 
62  os.rewind();
63  os << "content2";
64  buffer.resize(os.size()); // synchronize sizes
65 
66  something.write(buffer, buffer.size());
67 
68  // or simply using the output size directly (without sync)
69  os.rewind();
70  os << "content3";
71 
72  something.write(buffer, os.size());
73  \endcode
74 
75 See Also
76  Foam::IListStream
77  Foam::OListStream
78  Foam::UIListStream
79 
80 \*---------------------------------------------------------------------------*/
81 
82 #ifndef UOListStream_H
83 #define UOListStream_H
84 
85 #include "DynamicList.H"
86 #include "FixedList.H"
87 #include "OSstream.H"
88 #include "memoryStreamBuffer.H"
89 
90 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
91 
92 namespace Foam
93 {
94 
95 namespace Detail
96 {
97 
98 /*---------------------------------------------------------------------------*\
99  Class Detail::UOListStreamAllocator Declaration
100 \*---------------------------------------------------------------------------*/
101 
102 //- An stream/stream-buffer allocator for external buffers
104 {
105 protected:
106 
107  // Protected Data
108 
109  typedef std::ostream stream_type;
110 
111  //- The stream buffer
113 
114  //- The stream
116 
117 
118  // Constructors
119 
120  //- Construct for character array and number of bytes
121  UOListStreamAllocator(char* buffer, size_t nbytes)
122  :
123  buf_(buffer, nbytes),
124  stream_(&buf_)
125  {}
126 
127  void printBufInfo(Ostream& os) const
128  {
130  }
131 
132 public:
133 
134  // Member Functions
135 
136  //- Const UList access to the characters written (shallow copy).
137  inline const UList<char> list() const
138  {
139  return buf_.list();
140  }
141 
142  //- Non-const UList access to the characters written (shallow copy).
143  inline UList<char> list()
144  {
145  return buf_.list();
146  }
147 
148  //- The current list output capacity
149  inline label capacity() const
150  {
151  return buf_.capacity();
152  }
153 
154  //- The current output position in the buffer,
155  //- which is also the addressed list size
156  inline label size() const
157  {
158  return buf_.tellp();
159  }
160 
161  //- Move to buffer start, clear errors
162  void rewind()
163  {
164  buf_.pubseekpos(0, std::ios_base::out);
165  stream_.clear(); // for safety, clear any old errors
166  }
167 };
168 
169 } // End namespace Detail
170 
171 
172 /*---------------------------------------------------------------------------*\
173  Class UOListStream Declaration
174 \*---------------------------------------------------------------------------*/
175 
176 //- An OSstream attached to an unallocated external buffer
177 class UOListStream
178 :
180  public OSstream
181 {
183 
184 public:
185 
186  // Constructors
187 
188  //- Construct using specified buffer and number of bytes
190  (
191  char* buffer,
192  size_t nbytes,
193  IOstreamOption streamOpt = IOstreamOption()
194  )
195  :
196  allocator_type(buffer, nbytes),
197  OSstream(stream_, "output", streamOpt.format(), streamOpt.version())
198  {}
199 
200  //- Construct using data area from a List and number of bytes
202  (
203  UList<char>& buffer,
204  size_t nbytes,
205  IOstreamOption streamOpt = IOstreamOption()
206  )
207  :
208  UOListStream(buffer.data(), nbytes, streamOpt)
209  {}
210 
211  //- Construct using data area from a List and its inherent storage size
212  explicit UOListStream
213  (
214  UList<char>& buffer,
215  IOstreamOption streamOpt = IOstreamOption()
216  )
217  :
218  UOListStream(buffer.data(), buffer.size(), streamOpt)
219  {}
220 
221  //- Construct using data area from a FixedList
222  template<unsigned N>
223  explicit UOListStream
224  (
225  FixedList<char, N>& buffer,
226  IOstreamOption streamOpt = IOstreamOption()
227  )
228  :
229  UOListStream(buffer.data(), N, streamOpt)
230  {}
231 
232  //- Construct using data area from a DynamicList and its capacity
233  template<int SizeMin>
234  explicit UOListStream
235  (
237  IOstreamOption streamOpt = IOstreamOption()
238  )
239  :
240  UOListStream(buffer.data(), buffer.capacity(), streamOpt)
241  {}
242 
243 
244  // Member Functions
245 
246  //- Rewind the stream, clearing any old errors
247  virtual void rewind()
248  {
250  setGood(); // resynchronize with internal state
251  }
252 
253  //- Print stream description to Ostream
254  virtual void print(Ostream& os) const;
255 
256 
257  // Additional constructors and methods (as per v2012 and earlier)
258  #ifdef Foam_IOstream_extras
259 
260  //- Construct using specified buffer and number of bytes
262  (
263  char* buffer,
264  size_t nbytes,
267  )
268  :
269  UOListStream(buffer, nbytes, IOstreamOption(fmt, ver))
270  {}
271 
272  //- Construct using data area from a List and number of bytes
274  (
275  UList<char>& buffer,
276  size_t nbytes,
278  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
279  )
280  :
281  UOListStream(buffer.data(), nbytes, IOstreamOption(fmt, ver))
282  {}
283 
284  //- Construct using data area from a List and its inherent storage size
286  (
287  UList<char>& buffer,
289  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
290  )
291  :
292  UOListStream(buffer.data(), buffer.size(), IOstreamOption(fmt, ver))
293  {}
294 
295  //- Construct using data area from a FixedList
296  template<unsigned N>
298  (
299  FixedList<char, N>& buffer,
301  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
302  )
303  :
304  UOListStream(buffer.data(), N, IOstreamOption(fmt, ver))
305  {}
306 
307  //- Construct using data area from a DynamicList and its capacity
308  template<int SizeMin>
310  (
311  DynamicList<char,SizeMin>& buf,
313  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
314  )
315  :
316  UOListStream(buf.data(), buf.capacity(), IOstreamOption(fmt, ver))
317  {}
318 
319  #endif /* Foam_IOstream_extras */
320 };
321 
322 
323 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
324 
325 } // End namespace Foam
326 
327 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328 
329 #endif
330 
331 // ************************************************************************* //
Foam::Detail::UOListStreamAllocator::size
label size() const
Definition: UOListStream.H:155
Foam::memorybuf::out::list
const UList< char > list() const
Const UList access to the characters written (shallow copy).
Definition: memoryStreamBuffer.H:295
Foam::memorybuf::out::capacity
std::streamsize capacity() const
The buffer capacity.
Definition: memoryStreamBuffer.H:289
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::FixedList::data
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: FixedListI.H:177
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::UOListStreamAllocator::stream_type
std::ostream stream_type
Definition: UOListStream.H:108
Foam::Detail::UOListStreamAllocator::stream_
stream_type stream_
The stream.
Definition: UOListStream.H:114
Foam::Detail::UOListStreamAllocator::list
UList< char > list()
Non-const UList access to the characters written (shallow copy).
Definition: UOListStream.H:142
Foam::Detail::UOListStreamAllocator::printBufInfo
void printBufInfo(Ostream &os) const
Definition: UOListStream.H:126
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::DynamicList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:287
Foam::Detail::UOListStreamAllocator::UOListStreamAllocator
UOListStreamAllocator(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UOListStream.H:120
Foam::IOstream::setGood
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
Foam::memorybuf::out::tellp
std::streamsize tellp() const
The buffer put position.
Definition: memoryStreamBuffer.H:140
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::memorybuf::out
An output streambuf for memory access.
Definition: memoryStreamBuffer.H:240
Foam::Detail::UOListStreamAllocator
An stream/stream-buffer allocator for external buffers.
Definition: UOListStream.H:102
Foam::OSstream::OSstream
OSstream(const OSstream &)=default
Copy construct.
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::UOListStream
An OSstream attached to an unallocated external buffer.
Definition: UOListStream.H:176
memoryStreamBuffer.H
Foam::UList::data
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UOListStream::rewind
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UOListStream.H:246
Foam::Detail::UOListStreamAllocator::list
const UList< char > list() const
Const UList access to the characters written (shallow copy).
Definition: UOListStream.H:136
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::Detail::UOListStreamAllocator::rewind
void rewind()
Move to buffer start, clear errors.
Definition: UOListStream.H:161
Foam::UOListStream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:58
Foam::memorybuf::out::printBufInfo
void printBufInfo(Ostream &os) const
Some information about the output buffer position/capacity.
Definition: memoryStreamBuffer.H:307
DynamicList.H
Foam::UOListStream::UOListStream
UOListStream(char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
Definition: UOListStream.H:189
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
N
const Vector< label > N(dict.get< Vector< label >>("N"))
FixedList.H
Foam::Detail::UOListStreamAllocator::capacity
label capacity() const
The current list output capacity.
Definition: UOListStream.H:148
Foam::Detail::UOListStreamAllocator::buf_
memorybuf::out buf_
The stream buffer.
Definition: UOListStream.H:111
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
OSstream.H