LListIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "LList.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class LListBase, class T>
34 {
35  operator>>(is, *this);
36 }
37 
38 
39 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
40 
41 template<class LListBase, class T>
43 {
44  // Anull list
45  L.clear();
46 
47  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
48 
49  token firstToken(is);
50 
51  is.fatalCheck
52  (
53  " operator>>(Istream&, LList<LListBase, T>&) : reading first token"
54  );
55 
56  if (firstToken.isLabel())
57  {
58  label s = firstToken.labelToken();
59 
60  // Read beginning of contents
61  char delimiter = is.readBeginList("LList<LListBase, T>");
62 
63  if (s)
64  {
65  if (delimiter == token::BEGIN_LIST)
66  {
67  for (label i=0; i<s; ++i)
68  {
69  T element;
70  is >> element;
71  L.append(element);
72  }
73  }
74  else
75  {
76  T element;
77  is >> element;
78 
79  for (label i=0; i<s; ++i)
80  {
81  L.append(element);
82  }
83  }
84  }
85 
86  // Read end of contents
87  is.readEndList("LList");
88  }
89  else if (firstToken.isPunctuation())
90  {
91  if (firstToken.pToken() != token::BEGIN_LIST)
92  {
94  (
95  is
96  ) << "incorrect first token, '(', found " << firstToken.info()
97  << exit(FatalIOError);
98  }
99 
100  token lastToken(is);
101  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
102 
103  while
104  (
105  !(
106  lastToken.isPunctuation()
107  && lastToken.pToken() == token::END_LIST
108  )
109  )
110  {
111  is.putBack(lastToken);
112  T element;
113  is >> element;
114  L.append(element);
115 
116  is >> lastToken;
117  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
118  }
119  }
120  else
121  {
123  << "incorrect first token, expected <int> or '(', found "
124  << firstToken.info()
125  << exit(FatalIOError);
126  }
127 
128  // Check state of IOstream
129  is.fatalCheck(" operator>>(Istream&, LList<LListBase,>&)");
130 
131  return is;
132 }
133 
134 
135 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
136 
137 template<class LListBase, class T>
138 Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst)
139 {
140  // Write size
141  os << nl << lst.size();
142 
143  // Write beginning of contents
144  os << nl << token::BEGIN_LIST << nl;
145 
146  // Write contents
147  for (const T& val : lst)
148  {
149  os << val << nl;
150  }
151 
152  // Write end of contents
153  os << token::END_LIST;
154 
155  // Check state of IOstream
156  os.check("Ostream& operator<<(Ostream&, const LList<LListBase, T>&)");
157 
158  return os;
159 }
160 
161 
162 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
punctuationToken pToken() const
Definition: tokenI.H:248
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
LList()
Null construct.
Definition: LList.H:108
char readEndList(const char *funcName)
Definition: Istream.C:148
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:391
Template class for non-intrusive linked lists.
Definition: LList.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:72
void putBack(const token &)
Put back token.
Definition: Istream.C:30
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Istream & operator>>(Istream &, directionInfo &)
char readBeginList(const char *funcName)
Definition: Istream.C:127
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
void clear()
Delete contents of list.
Definition: LList.C:73
void append(const T &a)
Add at tail of list.
Definition: LList.H:172
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
label labelToken() const
Definition: tokenI.H:397
bool isPunctuation() const
Definition: tokenI.H:243
IOerror FatalIOError