LListIO.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-2022 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 "LList.H"
30 #include "Istream.H"
31 #include "Ostream.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 template<class LListBase, class T>
37 {
38  operator>>(is, *this);
39 }
40 
41 
42 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
43 
44 template<class LListBase, class T>
46 {
47  LList<LListBase, T>& list = *this;
48 
49  // Anull list
50  list.clear();
51 
53 
54  token tok(is);
55 
56  is.fatalCheck("LList::readList : reading first token");
57 
58  if (tok.isLabel())
59  {
60  const label len = tok.labelToken();
61 
62  // Begin of contents marker
63  const char delimiter = is.readBeginList("LList");
64 
65  if (len)
66  {
67  if (delimiter == token::BEGIN_LIST)
68  {
69  for (label i=0; i<len; ++i)
70  {
71  T elem;
72  is >> elem;
73  list.push_back(std::move(elem));
74  }
75  }
76  else
77  {
78  // Uniform content (delimiter == token::BEGIN_BLOCK)
79 
80  T elem;
81  is >> elem;
82 
83  for (label i=0; i<len; ++i)
84  {
85  list.push_back(elem);
86  }
87  }
88  }
89 
90  // End of contents marker
91  is.readEndList("LList");
92  }
93  else if (tok.isPunctuation(token::BEGIN_LIST))
94  {
95  is >> tok;
97 
98  while (!tok.isPunctuation(token::END_LIST))
99  {
100  is.putBack(tok);
101 
102  T elem;
103  is >> elem;
104  list.push_back(std::move(elem));
105 
106  is >> tok;
108  }
109  }
110  else
111  {
113  << "incorrect first token, expected <int> or '(', found "
114  << tok.info()
115  << exit(FatalIOError);
116  }
117 
119  return is;
120 }
121 
122 
123 template<class LListBase, class T>
125 (
126  Ostream& os,
127  const label shortLen
128 ) const
129 {
130  // NB: no binary, contiguous output
131 
132  const label len = this->size();
133 
134  if
135  (
136  (len <= 1 || !shortLen)
137  || (len <= shortLen)
138  )
139  {
140  // Size and start delimiter
141  os << len << token::BEGIN_LIST;
142 
143  // Contents
144  bool space = false;
145  for (const T& val : *this)
146  {
147  if (space) os << token::SPACE;
148  os << val;
149  space = true;
150  }
151 
152  // End delimiter
153  os << token::END_LIST;
154  }
155  else
156  {
157  // Size and start delimiter
158  os << nl << len << nl << token::BEGIN_LIST << nl;
159 
160  // Contents
161  for (const T& val : *this)
162  {
163  os << val << nl;
164  }
165 
166  // End delimiter
167  os << token::END_LIST;
168  }
169 
170  os.check(FUNCTION_NAME);
171  return os;
172 }
173 
174 
175 template<class LListBase, class T>
176 Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& list)
177 {
178  return list.readList(is);
179 }
180 
181 
182 template<class LListBase, class T>
183 Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& list)
184 {
185  return list.writeList(os, -1); // Always with line breaks
186 }
187 
188 
189 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:192
Template class for non-intrusive linked lists.
Definition: LList.H:46
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 token holds an item read from Istream.
Definition: token.H:65
Istream & readList(Istream &is)
Read list from Istream.
Definition: LListIO.C:38
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write LList with line-breaks when length exceeds shortLen.
Definition: LListIO.C:118
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
Istream & operator>>(Istream &, directionInfo &)
LList()=default
Default construct.
void push_back(const T &elem)
Add copy at back of list.
Definition: LList.H:299
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
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
void clear()
Delete contents of list.
Definition: LList.C:88
const volScalarField & T
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:627
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...