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-2020 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  {
129  buf_.printBufInfo(os);
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,
195  )
196  :
197  allocator_type(buffer, nbytes),
198  OSstream(stream_, "output", format, version)
199  {}
200 
201  //- Construct using data area from a List and number of bytes
203  (
204  UList<char>& buffer,
205  size_t size,
208  )
209  :
210  UOListStream(buffer.data(), size, format, version)
211  {}
212 
213  //- Construct using data area from a FixedList
214  template<unsigned N>
215  explicit UOListStream
216  (
217  FixedList<char, N>& buffer,
220  )
221  :
222  UOListStream(buffer.data(), N, format, version)
223  {}
224 
225  //- Construct using data area from a List and its inherent storage size
226  explicit UOListStream
227  (
228  UList<char>& buffer,
231  )
232  :
233  UOListStream(buffer.data(), buffer.size(), format, version)
234  {}
235 
236 
237  //- Construct using data area from a DynamicList and its capacity
238  template<int SizeMin>
239  explicit UOListStream
240  (
244  )
245  :
246  UOListStream(buffer.data(), buffer.capacity(), format, version)
247  {}
248 
249 
250  // Member Functions
251 
252  //- Rewind the stream, clearing any old errors
253  virtual void rewind()
254  {
256  setGood(); // resynchronize with internal state
257  }
258 
259  //- Print stream description to Ostream
260  virtual void print(Ostream& os) const;
261 };
262 
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 } // End namespace Foam
267 
268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269 
270 #endif
271 
272 // ************************************************************************* //
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::UOListStream::UOListStream
UOListStream(char *buffer, size_t nbytes, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct using specified buffer and number of bytes.
Definition: UOListStream.H:189
Foam::FixedList::data
T * data() noexcept
Return a pointer to the first data element.
Definition: FixedListI.H:181
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::UList::data
T * data()
Return a pointer to the first data element.
Definition: UListI.H:205
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:289
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:168
Foam::DynamicList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:224
Foam::Detail::UOListStreamAllocator::UOListStreamAllocator
UOListStreamAllocator(char *buffer, size_t nbytes)
Construct for character array and number of bytes.
Definition: UOListStream.H:120
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::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:341
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
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::UOListStream::rewind
virtual void rewind()
Rewind the stream, clearing any old errors.
Definition: UOListStream.H:252
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
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::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
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::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