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-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::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 Foam_UOListStream_H
83 #define Foam_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 {
182  typedef Detail::UOListStreamAllocator allocator_type;
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,
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
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,
266  )
267  :
268  UOListStream(buffer, nbytes, IOstreamOption(fmt))
269  {}
270 
271  //- Construct using data area from a List and number of bytes
273  (
274  UList<char>& buffer,
275  size_t nbytes,
277  )
278  :
279  UOListStream(buffer.data(), nbytes, IOstreamOption(fmt))
280  {}
281 
282  //- Construct using data area from a List and its inherent storage size
284  (
285  UList<char>& buffer,
287  )
288  :
289  UOListStream(buffer.data(), buffer.size(), IOstreamOption(fmt))
290  {}
291 
292  //- Construct using data area from a FixedList
293  template<unsigned N>
295  (
296  FixedList<char, N>& buffer,
298  )
299  :
300  UOListStream(buffer.data(), N, IOstreamOption(fmt))
301  {}
302 
303  //- Construct using data area from a DynamicList and its capacity
304  template<int SizeMin>
306  (
307  DynamicList<char,SizeMin>& buf,
309  )
310  :
311  UOListStream(buf.data(), buf.capacity(), IOstreamOption(fmt))
312  {}
313 
314  #endif /* Foam_IOstream_extras */
315 };
316 
317 
318 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 
320 } // End namespace Foam
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 #endif
325 
326 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
const UList< char > list() const
Const UList access to the characters written (shallow copy).
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:101
An OSstream attached to an unallocated external buffer.
Definition: UOListStream.H:190
stream_type stream_
The stream.
Definition: UOListStream.H:114
void printBufInfo(Ostream &os) const
Definition: UOListStream.H:128
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: ListStream.C:51
A simple container for options an IOstream can normally have.
label size() const
The current output position in the buffer, which is also the addressed list size. ...
Definition: UOListStream.H:165
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
std::streamsize capacity() const
The buffer capacity.
label capacity() const
The current list output capacity.
Definition: UOListStream.H:156
UOListStreamAllocator(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UOListStream.H:122
An output streambuf for memory access.
UOListStream(char *buffer, size_t nbytes, IOstreamOption streamOpt=IOstreamOption())
Construct using specified buffer and number of bytes.
Definition: UOListStream.H:205
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:99
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
void rewind()
Move to buffer start, clear errors.
Definition: UOListStream.H:173
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
memorybuf::out buf_
The stream buffer.
Definition: UOListStream.H:109
const Vector< label > N(dict.get< Vector< label >>("N"))
An stream/stream-buffer allocator for external buffers.
Definition: UOListStream.H:98
streamFormat
Data format (ascii | binary)
void printBufInfo(Ostream &os) const
Some information about the output buffer position/capacity.
const UList< char > list() const
Const UList access to the characters written (shallow copy).
Definition: UOListStream.H:140
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UOListStream.H:272
std::streamsize tellp() const
The buffer put position.
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:167
OSstream(const OSstream &)=default
Copy construct.
Namespace for OpenFOAM.