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-2023 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 #include "contiguous.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class T>
36 {
37  this->readList(is);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
42 
43 template<class T>
45 {
46  os << "size=" << size() << '/' << capacity()
47  << " begin=" << begin_
48  << " end=" << end_
49  // << " one=" << this->range_one() << this->array_one()
50  // << " two=" << this->range_two() << this->array_two()
51  << nl;
52 
53  return os;
54 }
55 
56 
57 template<class T>
59 {
60  // Delegate to DynamicList for reading
61  DynamicList<T> elements(std::move(storage_));
62  elements.readList(is);
63 
64  // Reset the list addressing range
65  begin_ = 0;
66  end_ = elements.size();
67 
68  const label minLen = (end_ + min_size());
69 
70  if (!elements.empty() && (elements.capacity() < minLen))
71  {
72  // Avoid full buffer (beg/end ambiguity)
73  // Use setCapacity instead of resize to avoid additional doubling...
74  elements.setCapacity(minLen);
75  }
76 
77  // Use the entire storage
78  elements.resize(elements.capacity());
79 
80  storage_ = std::move(elements);
81 
82  return is;
83 }
84 
85 
86 template<class T>
88 (
89  Ostream& os,
90  const label shortLen
91 ) const
92 {
93  const label len = this->size();
94  const auto list1 = this->array_one();
95  const auto list2 = this->array_two();
96 
97  #ifdef FULLDEBUG
98  if (len != (list1.size() + list2.size()))
99  {
101  << "Size check failed"
102  << abort(FatalError);
103  }
104  #endif
105 
106  if (os.format() == IOstreamOption::BINARY && is_contiguous<T>::value)
107  {
108  // Binary and contiguous
109 
110  os << nl << len << nl;
111 
112  if (len)
113  {
114  // The TOTAL number of bytes to be written.
115  // - possibly add start delimiter
116  // This is much like IndirectListBase output
117 
118  os.beginRawWrite(len*sizeof(T));
119 
120  if (!list1.empty())
121  {
122  os.writeRaw(list1.cdata_bytes(), list1.size_bytes());
123  }
124  if (!list2.empty())
125  {
126  os.writeRaw(list2.cdata_bytes(), list2.size_bytes());
127  }
128 
129  // End delimiter and/or cleanup.
130  os.endRawWrite();
131  }
132  }
133  else if
134  (
135  (len <= 1 || !shortLen)
136  ||
137  (
138  (len <= shortLen)
139  &&
140  (
141  is_contiguous<T>::value
142  || Detail::ListPolicy::no_linebreak<T>::value
143  )
144  )
145  )
146  {
147  // Single-line output
148 
149  // Size and start delimiter
150  os << len << token::BEGIN_LIST;
151 
152  // Contents
153  label i = 0;
154  for (const T& val : list1)
155  {
156  if (i++) os << token::SPACE;
157  os << val;
158  }
159  for (const T& val : list2)
160  {
161  if (i++) os << token::SPACE;
162  os << val;
163  }
164 
165  // End delimiter
166  os << token::END_LIST;
167  }
168  else
169  {
170  // Multi-line output
171 
172  // Size and start delimiter
173  os << nl << len << nl << token::BEGIN_LIST << nl;
174 
175  // Contents
176  for (const T& val : list1)
177  {
178  os << val << nl;
179  }
180  for (const T& val : list2)
181  {
182  os << val << nl;
183  }
184 
185  // End delimiter
186  os << token::END_LIST << nl;
187  }
188 
189  os.check(FUNCTION_NAME);
190  return os;
191 }
192 
193 
194 // ************************************************************************* //
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:116
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:598
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:666
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content...
Definition: DynamicListI.H:353
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicList.H:225
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.