Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
36template<class T, unsigned N>
38{
39 const word tag("List<" + word(pTraits<T>::typeName) + '>');
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
49 }
50 }
51 }
52 os << *this;
53}
54
55
56// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57
58template<class T, unsigned N>
60{
61 this->readList(is);
62}
63
64
65// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
66
67template<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
83template<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}"
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))
110 )
111 )
112 {
113 // Single-line output
114
115 // Start delimiter
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 }
145 os.check(FUNCTION_NAME);
146 return os;
147}
148
149
150template<class T, unsigned N>
152(
153 Istream& is
154)
155{
156 FixedList<T, N>& list = *this;
157
159
161 {
162 // Binary and contiguous. Length is non-zero
163
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// ************************************************************************* //
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition FixedListI.H:144
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition FixedListI.H:137
void fill(const T &val)
Assign all entries to the given value.
Definition FixedListI.H:342
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition FixedListI.H:252
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition FixedListI.H:129
void writeEntry(Ostream &os) const
Write the FixedList with its compound type.
Definition FixedListIO.C:30
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition FixedListIO.C:78
Istream & readList(Istream &is)
Read from Istream, discarding contents of existing List.
FixedList()=default
Default construct.
streamFormat format() const noexcept
Get the current stream format.
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition Istream.C:192
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition Istream.C:171
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition Istream.C:71
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
static bool isCompound(const word &compoundType)
True if a known (registered) compound type.
Definition token.C:104
A token holds an item read from Istream.
Definition token.H:70
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition tokenI.H:650
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ SPACE
Space [isspace].
Definition token.H:144
bool isLabel() const noexcept
Integral token is convertible to Foam::label.
Definition tokenI.H:843
label labelToken() const
Return integer type as label value or Error.
Definition tokenI.H:869
compound & transferCompoundToken(const Istream *is=nullptr)
Return reference to compound and mark internally as released.
Definition token.C:157
bool isCompound() const noexcept
Token is COMPOUND.
Definition tokenI.H:1096
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition token.H:1253
A class for handling words, derived from Foam::string.
Definition word.H:66
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
void readContiguous(Istream &is, char *data, std::streamsize byteCount)
Read binary block of contiguous data, possibly with conversion.
Definition Istream.H:322
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
constexpr bool is_contiguous_v
The is_contiguous value of Type (after stripping of qualifiers).
Definition contiguous.H:77
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
Can suppress additional line breaks separate ASCII data content when the data elements are primitives...
Definition ListPolicy.H:74
const Vector< label > N(dict.get< Vector< label > >("N"))