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-2020 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 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
49 
50 template<class T, unsigned N>
52 (
53  const word& keyword,
54  Ostream& os
55 ) const
56 {
57  if (keyword.size())
58  {
59  os.writeKeyword(keyword);
60  }
61  writeEntry(os);
62  os << token::END_STATEMENT << endl;
63 }
64 
65 
66 template<class T, unsigned N>
68 (
69  Ostream& os,
70  const label shortLen
71 ) const
72 {
73  const FixedList<T, N>& list = *this;
74 
75  // Write list contents depending on data format
76 
77  // Unlike UList, no compact output form since a FixedList is generally
78  // small and we desire a consistent appearance.
79  // Eg, FixedList<T,2> or Pair<T> as "(-1 -1)", not as "2{-1}"
80 
81  if (os.format() == IOstream::ASCII || !is_contiguous<T>::value)
82  {
83  if
84  (
85  (N <= 1 || !shortLen)
86  ||
87  (
88  (N <= unsigned(shortLen))
89  &&
90  (
93  )
94  )
95  )
96  {
97  // Start delimiter
98  os << token::BEGIN_LIST;
99 
100  // Contents
101  for (unsigned i=0; i<N; ++i)
102  {
103  if (i) os << token::SPACE;
104  os << list[i];
105  }
106 
107  // End delimiter
108  os << token::END_LIST;
109  }
110  else
111  {
112  // Start delimiter
113  os << nl << token::BEGIN_LIST << nl;
114 
115  // Contents
116  for (unsigned i=0; i<N; ++i)
117  {
118  os << list[i] << nl;
119  }
120 
121  // End delimiter
122  os << token::END_LIST << nl;
123  }
124  }
125  else
126  {
127  // Binary, contiguous
128 
129  // write(...) includes surrounding start/end delimiters
130  os.write(reinterpret_cast<const char*>(list.cdata()), N*sizeof(T));
131  }
132 
133  os.check(FUNCTION_NAME);
134  return os;
135 }
136 
137 
138 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
139 
140 template<class T, unsigned N>
142 {
143  operator>>(is, *this);
144 }
145 
146 
147 template<class T, unsigned N>
149 {
151 
152  if (is.format() == IOstream::ASCII || !is_contiguous<T>::value)
153  {
154  token firstToken(is);
155 
156  is.fatalCheck
157  (
158  "operator>>(Istream&, FixedList<T, N>&) : "
159  "reading first token"
160  );
161 
162  if (firstToken.isCompound())
163  {
164  list = dynamicCast<token::Compound<List<T>>>
165  (
166  firstToken.transferCompoundToken(is)
167  );
168  }
169  else if (firstToken.isLabel())
170  {
171  const label len = firstToken.labelToken();
172 
173  // List lengths must match
174  list.checkSize(len);
175  }
176  else if (!firstToken.isPunctuation())
177  {
179  << "incorrect first token, expected <label> "
180  "or '(' or '{', found "
181  << firstToken.info()
182  << exit(FatalIOError);
183  }
184  else
185  {
186  // Putback the opening bracket
187  is.putBack(firstToken);
188  }
189 
190  // Read beginning of contents
191  const char delimiter = is.readBeginList("FixedList");
192 
193  if (delimiter == token::BEGIN_LIST)
194  {
195  for (unsigned i=0; i<N; ++i)
196  {
197  is >> list[i];
198 
199  is.fatalCheck
200  (
201  "operator>>(Istream&, FixedList<T, N>&) : "
202  "reading entry"
203  );
204  }
205  }
206  else
207  {
208  T val;
209  is >> val;
210 
211  is.fatalCheck
212  (
213  "operator>>(Istream&, FixedList<T, N>&) : "
214  "reading the single entry"
215  );
216 
217  for (unsigned i=0; i<N; ++i)
218  {
219  list[i] = val; // Copy the value
220  }
221  }
222 
223  // Read end of contents
224  is.readEndList("FixedList");
225  }
226  else
227  {
228  // Binary and contiguous
229 
230  Detail::readContiguous<T>
231  (
232  is,
233  reinterpret_cast<char*>(list.data()),
234  N*sizeof(T)
235  );
236 
237  is.fatalCheck
238  (
239  "operator>>(Istream&, FixedList<T, N>&) : "
240  "reading the binary block"
241  );
242  }
243 
244  return is;
245 }
246 
247 
248 // ************************************************************************* //
token.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::FixedList::writeEntry
void writeEntry(Ostream &os) const
Write the FixedList with its compound type.
Definition: FixedListIO.C:37
Foam::FixedList::data
T * data() noexcept
Return a pointer to the first data element.
Definition: FixedListI.H:181
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::FixedList::cdata
const T * cdata() const noexcept
Return a const pointer to the first data element.
Definition: FixedListI.H:173
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:57
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:167
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Istream.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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:68
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
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:270
Foam::FixedList::FixedList
FixedList()=default
Default construct.
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
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:271
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75