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-2021 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  }
44  os << *this;
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 template<class T, unsigned N>
52 {
53  this->readList(is);
54 }
55 
56 
57 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
58 
59 template<class T, unsigned N>
61 (
62  const word& keyword,
63  Ostream& os
64 ) const
65 {
66  if (keyword.size())
67  {
68  os.writeKeyword(keyword);
69  }
70  writeEntry(os);
71  os << token::END_STATEMENT << endl;
72 }
73 
74 
75 template<class T, unsigned N>
77 (
78  Ostream& os,
79  const label shortLen
80 ) const
81 {
82  const FixedList<T, N>& list = *this;
83 
84  // Unlike UList, no compact ascii output since a FixedList is generally
85  // small and we prefer a consistent appearance.
86  // Eg, FixedList<T,2> or Pair<T> as "(-1 -1)", never as "2{-1}"
87 
88  if (os.format() == IOstream::BINARY && is_contiguous<T>::value)
89  {
90  // Binary and contiguous. Size is always non-zero
91 
92  // write(...) includes surrounding start/end delimiters
93  os.write(list.cdata_bytes(), list.size_bytes());
94  }
95  else if
96  (
97  (N <= 1 || !shortLen)
98  ||
99  (
100  (N <= unsigned(shortLen))
101  &&
102  (
105  )
106  )
107  )
108  {
109  // Single-line output
110 
111  // Start delimiter
112  os << token::BEGIN_LIST;
113 
114  // Contents
115  for (unsigned i=0; i<N; ++i)
116  {
117  if (i) os << token::SPACE;
118  os << list[i];
119  }
120 
121  // End delimiter
122  os << token::END_LIST;
123  }
124  else
125  {
126  // Multi-line output
127 
128  // Start delimiter
129  os << nl << token::BEGIN_LIST << nl;
130 
131  // Contents
132  for (unsigned i=0; i<N; ++i)
133  {
134  os << list[i] << nl;
135  }
136 
137  // End delimiter
138  os << token::END_LIST << nl;
139  }
140 
141  os.check(FUNCTION_NAME);
142  return os;
143 }
144 
145 
146 template<class T, unsigned N>
148 (
149  Istream& is
150 )
151 {
152  FixedList<T, N>& list = *this;
153 
155 
156  if (is.format() == IOstream::BINARY && is_contiguous<T>::value)
157  {
158  // Binary and contiguous
159 
160  Detail::readContiguous<T>
161  (
162  is,
163  list.data_bytes(),
164  list.size_bytes()
165  );
166 
167  is.fatalCheck
168  (
169  "FixedList<T, N>::readList(Istream&) : "
170  "reading the binary block"
171  );
172  }
173  else
174  {
175  token tok(is);
176 
177  is.fatalCheck
178  (
179  "FixedList<T, N>::readList(Istream&) : "
180  "reading first token"
181  );
182 
183  if (tok.isCompound())
184  {
185  // Compound: transfer contents
186  list = dynamicCast<token::Compound<List<T>>>
187  (
188  tok.transferCompoundToken(is)
189  );
190  }
191  else if (tok.isLabel())
192  {
193  const label len = tok.labelToken();
194 
195  // List lengths must match
196  list.checkSize(len);
197  }
198  else if (!tok.isPunctuation())
199  {
201  << "incorrect first token, expected <label> "
202  "or '(' or '{', found "
203  << tok.info() << nl
204  << exit(FatalIOError);
205  }
206  else
207  {
208  // Putback the opening bracket
209  is.putBack(tok);
210  }
211 
212  // Begin of contents marker
213  const char delimiter = is.readBeginList("FixedList");
214 
215  if (delimiter == token::BEGIN_LIST)
216  {
217  for (unsigned i=0; i<N; ++i)
218  {
219  is >> list[i];
220 
221  is.fatalCheck
222  (
223  "FixedList<T, N>::readList(Istream&) : "
224  "reading entry"
225  );
226  }
227  }
228  else
229  {
230  // Uniform content (delimiter == token::BEGIN_BLOCK)
231 
232  T val;
233  is >> val;
234 
235  is.fatalCheck
236  (
237  "FixedList<T, N>::readList(Istream&) : "
238  "reading the single entry"
239  );
240 
241  for (unsigned i=0; i<N; ++i)
242  {
243  list[i] = val; // Copy the value
244  }
245  }
246 
247  // End of contents marker
248  is.readEndList("FixedList");
249  }
250 
251  return is;
252 }
253 
254 
255 // ************************************************************************* //
token.H
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
Foam::FixedList::readList
Istream & readList(Istream &is)
Read from Istream, discarding contents of existing List.
Definition: FixedListIO.C:148
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::FixedList::writeEntry
void writeEntry(Ostream &os) const
Write the FixedList with its compound type.
Definition: FixedListIO.C:37
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
Foam::FixedList::cdata_bytes
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:185
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::FixedList::size_bytes
static std::streamsize size_bytes() noexcept
Number of contiguous bytes for the list data,.
Definition: FixedListI.H:200
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
Istream.H
os
OBJstream os(runTime.globalPath()/outputName)
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::token::transferCompoundToken
compound & transferCompoundToken()
Return reference to compound and mark internally as released.
Definition: token.C:90
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::FixedList::data_bytes
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: FixedListI.H:193
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Detail::ListPolicy::no_linebreak
Definition: ListPolicy.H:76
Foam::FixedList::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: FixedListIO.C:77
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::isCompound
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:716
Foam::FixedList::FixedList
FixedList()=default
Default construct.
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
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::FixedList::checkSize
void checkSize(const label size) const
Check size is identical to template parameter N.
Definition: FixedListI.H:290
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75