FixedListIO.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "FixedList.H"
30 #include "Istream.H"
31 #include "Ostream.H"
32 #include "token.H"
33 
34 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
35 
36 template<class T, unsigned N>
38 {
39  const word tag("List<" + word(pTraits<T>::typeName) + '>');
40  if (token::compound::isCompound(tag))
41  {
42  os << tag << token::SPACE;
43  if constexpr (is_contiguous_v<T>)
44  {
45  if (os.format() == IOstreamOption::BINARY)
46  {
47  // Need size too so that List<Type>::readList parses correctly
48  os << static_cast<label>(N);
49  }
50  }
51  }
52  os << *this;
53 }
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
58 template<class T, unsigned N>
60 {
61  this->readList(is);
62 }
63 
64 
65 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
66 
67 template<class T, unsigned N>
69 (
70  const word& keyword,
71  Ostream& os
72 ) const
73 {
74  if (keyword.size())
75  {
76  os.writeKeyword(keyword);
77  }
78  writeEntry(os);
79  os.endEntry();
80 }
81 
82 
83 template<class T, unsigned N>
85 (
86  Ostream& os,
87  const label shortLen
88 ) const
89 {
90  const FixedList<T, N>& list = *this;
91 
92  // Unlike UList, no compact ascii output since a FixedList is generally
93  // small and we prefer a consistent appearance.
94  // Eg, FixedList<T,2> or Pair<T> as "(-1 -1)", never as "2{-1}"
95 
96  if (os.format() == IOstreamOption::BINARY && is_contiguous_v<T>)
97  {
98  // Binary and contiguous. Size is always non-zero
99 
100  // write(...) includes surrounding start/end delimiters
101  os.write(list.cdata_bytes(), list.size_bytes());
102  }
103  else if
104  (
105  (N <= 1 || !shortLen)
106  ||
107  (
108  (N <= unsigned(shortLen))
109  && (is_contiguous_v<T> || Foam::ListPolicy::no_linebreak<T>::value)
110  )
111  )
112  {
113  // Single-line output
114 
115  // Start delimiter
116  os << token::BEGIN_LIST;
117 
118  // Contents
119  for (unsigned i=0; i<N; ++i)
120  {
121  if (i) os << token::SPACE;
122  os << list[i];
123  }
124 
125  // End delimiter
126  os << token::END_LIST;
127  }
128  else
129  {
130  // Multi-line output
131 
132  // Start delimiter
133  os << nl << token::BEGIN_LIST << nl;
134 
135  // Contents
136  for (unsigned i=0; i<N; ++i)
137  {
138  os << list[i] << nl;
139  }
140 
141  // End delimiter
142  os << token::END_LIST << nl;
143  }
144 
146  return os;
147 }
148 
149 
150 template<class T, unsigned N>
152 (
153  Istream& is
154 )
155 {
156  FixedList<T, N>& list = *this;
157 
159 
160  if (is.format() == IOstreamOption::BINARY && is_contiguous_v<T>)
161  {
162  // Binary and contiguous. Length is non-zero
163 
164  Detail::readContiguous<T>
165  (
166  is,
167  list.data_bytes(),
168  list.size_bytes()
169  );
170 
171  is.fatalCheck
172  (
173  "FixedList<T, N>::readList(Istream&) : "
174  "reading the binary block"
175  );
176  return is;
177  }
178  else
179  {
180  token tok(is);
181 
182  is.fatalCheck
183  (
184  "FixedList<T, N>::readList(Istream&) : "
185  "reading first token"
186  );
187 
188  if (tok.isCompound())
189  {
190  // Compound: transfer contents
191  // - in practice probably never reach this branch
192  list = tok.transferCompoundToken<List<T>>(is);
193  return is;
194  }
195  else if (tok.isLabel())
196  {
197  // List lengths must match
198  list.checkSize(tok.labelToken());
199  }
200  else if (!tok.isPunctuation())
201  {
203  << "incorrect first token, expected <label> or '(' , found "
204  << tok.info() << nl
205  << exit(FatalIOError);
206  }
207  else
208  {
209  // Putback the opening bracket
210  is.putBack(tok);
211  }
212 
213  // Begin of contents marker
214  const char delimiter = is.readBeginList("FixedList");
215 
216  if (delimiter == token::BEGIN_LIST)
217  {
218  for (unsigned i=0; i<N; ++i)
219  {
220  is >> list[i];
221 
222  is.fatalCheck
223  (
224  "FixedList<T, N>::readList(Istream&) : "
225  "reading entry"
226  );
227  }
228  }
229  else
230  {
231  // Uniform content (delimiter == token::BEGIN_BLOCK)
232  // - compatibility for v1812 and earlier (see issue #1160)
233 
234  T elem;
235  is >> elem;
236 
237  is.fatalCheck
238  (
239  "FixedList<T, N>::readList(Istream&) : "
240  "reading the single entry"
241  );
242 
243  // Fill with the value
244  this->fill(elem);
245  }
246 
247  // End of contents marker
248  is.readEndList("FixedList");
249  }
250 
251  return is;
252 }
253 
254 
255 // ************************************************************************* //
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:129
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:192
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
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:61
void writeEntry(Ostream &os) const
Write the FixedList with its compound type.
Definition: FixedListIO.C:30
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
A class for handling words, derived from Foam::string.
Definition: word.H:63
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition: FixedListI.H:144
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: FixedListIO.C:78
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:137
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:252
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
const volScalarField & T
const Vector< label > N(dict.get< Vector< label >>("N"))
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:629
Istream & readList(Istream &is)
Read from Istream, discarding contents of existing List.
Definition: FixedListIO.C:145
streamFormat format() const noexcept
Get the current stream format.
Can suppress additional line breaks separate ASCII data content when the data elements are primitives...
Definition: ListPolicy.H:73
FixedList()=default
Default construct.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...