CircularBufferIO.C
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) 2022-2025 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 \*---------------------------------------------------------------------------*/
27 
28 #include "DynamicList.H"
29 #include "Istream.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class T>
35 {
36  this->readList(is);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
41 
42 template<class T>
44 {
45  os << "size=" << size() << '/' << capacity()
46  << " begin=" << begin_
47  << " end=" << end_
48  // << " one=" << this->range_one() << this->array_one()
49  // << " two=" << this->range_two() << this->array_two()
50  << nl;
51 
52  return os;
53 }
54 
55 
56 template<class T>
58 {
59  // Delegate to DynamicList for reading
60  DynamicList<T> elements(std::move(storage_));
61  elements.readList(is);
62 
63  // Reset the list addressing range
64  begin_ = 0;
65  end_ = elements.size();
66 
67  const label minLen = (end_ + min_size());
68 
69  if (!elements.empty() && (elements.capacity() < minLen))
70  {
71  // Avoid full buffer (beg/end ambiguity)
72  // Use setCapacity instead of resize to avoid additional doubling...
73  elements.setCapacity(minLen);
74  }
75 
76  // Use the entire storage
77  elements.resize(elements.capacity());
78 
79  storage_ = std::move(elements);
80 
81  return is;
82 }
83 
84 
85 template<class T>
87 (
88  Ostream& os,
89  const label shortLen
90 ) const
91 {
92  const label len = this->size();
93  const auto list1 = this->array_one();
94  const auto list2 = this->array_two();
95 
96  #ifdef FULLDEBUG
97  if (len != (list1.size() + list2.size()))
98  {
100  << "Size check failed"
101  << abort(FatalError);
102  }
103  #endif
104 
105  if (os.format() == IOstreamOption::BINARY && is_contiguous_v<T>)
106  {
107  // Binary and contiguous
108 
109  os << nl << len << nl;
110 
111  if (len)
112  {
113  // The TOTAL number of bytes to be written.
114  // - possibly add start delimiter
115  // This is much like IndirectListBase output
116 
117  os.beginRawWrite(len*sizeof(T));
118 
119  if (!list1.empty())
120  {
121  os.writeRaw(list1.cdata_bytes(), list1.size_bytes());
122  }
123  if (!list2.empty())
124  {
125  os.writeRaw(list2.cdata_bytes(), list2.size_bytes());
126  }
127 
128  // End delimiter and/or cleanup.
129  os.endRawWrite();
130  }
131  }
132  else if
133  (
134  (len <= 1 || !shortLen)
135  ||
136  (
137  (len <= shortLen)
138  && (is_contiguous_v<T> || Foam::ListPolicy::no_linebreak<T>::value)
139  )
140  )
141  {
142  // Single-line output
143 
144  // Size and start delimiter
145  os << len << token::BEGIN_LIST;
146 
147  // Contents
148  label i = 0;
149  for (const T& val : list1)
150  {
151  if (i++) os << token::SPACE;
152  os << val;
153  }
154  for (const T& val : list2)
155  {
156  if (i++) os << token::SPACE;
157  os << val;
158  }
159 
160  // End delimiter
161  os << token::END_LIST;
162  }
163  else
164  {
165  // Multi-line output
166 
167  // Size and start delimiter
168  os << nl << len << nl << token::BEGIN_LIST << nl;
169 
170  // Contents
171  for (const T& val : list1)
172  {
173  os << val << nl;
174  }
175  for (const T& val : list2)
176  {
177  os << val << nl;
178  }
179 
180  // End delimiter
181  os << token::END_LIST << nl;
182  }
183 
184  os.check(FUNCTION_NAME);
185  return os;
186 }
187 
188 
189 // ************************************************************************* //
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write buffer contents with line-breaks in ASCII when length exceeds shortLen.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
constexpr CircularBuffer() noexcept
Default construct, empty buffer without allocation.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:697
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content...
Definition: DynamicListI.H:366
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:308
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicList.H:236
Istream & readList(Istream &is)
Read buffer contents from Istream.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Istream & readList(Istream &is)
Read from Istream, discarding existing contents.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
const volScalarField & T
Ostream & info(Ostream &os) const
Print information.
Can suppress additional line breaks separate ASCII data content when the data elements are primitives...
Definition: ListPolicy.H:73