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 | Copyright (C) 2011-2015 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  " operator>>(Istream&, LList<LListBase, T>&)",
96  is
97  ) << "incorrect first token, '(', found " << firstToken.info()
98  << exit(FatalIOError);
99  }
100 
101  token lastToken(is);
102  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
103 
104  while
105  (
106  !(
107  lastToken.isPunctuation()
108  && lastToken.pToken() == token::END_LIST
109  )
110  )
111  {
112  is.putBack(lastToken);
113  T element;
114  is >> element;
115  L.append(element);
116 
117  is >> lastToken;
118  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
119  }
120  }
121  else
122  {
123  FatalIOErrorIn(" operator>>(Istream&, LList<LListBase, T>&)", is)
124  << "incorrect first token, expected <int> or '(', found "
125  << firstToken.info()
126  << exit(FatalIOError);
127  }
128 
129  // Check state of IOstream
130  is.fatalCheck(" operator>>(Istream&, LList<LListBase,>&)");
131 
132  return is;
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
137 
138 template<class LListBase, class T>
139 Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst)
140 {
141  // Write size
142  os << nl << lst.size();
143 
144  // Write beginning of contents
145  os << nl << token::BEGIN_LIST << nl;
146 
147  // Write contents
148  for
149  (
150  typename LList<LListBase, T>::const_iterator iter = lst.begin();
151  iter != lst.end();
152  ++iter
153  )
154  {
155  os << iter() << nl;
156  }
157 
158  // Write end of contents
159  os << token::END_LIST;
160 
161  // Check state of IOstream
162  os.check("Ostream& operator<<(Ostream&, const LList<LListBase, T>&)");
163 
164  return os;
165 }
166 
167 
168 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:262
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 ))
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
char readBeginList(const char *funcName)
Definition: Istream.C:131
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
bool isPunctuation() const
Definition: tokenI.H:203
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
An STL-conforming const_iterator.
Definition: LList.H:284
LList()
Null construct.
Definition: LList.H:108
Template class for non-intrusive linked lists.
Definition: LList.H:51
const volScalarField & T
Definition: createFields.H:25
static const char nl
Definition: Ostream.H:260
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
char readEndList(const char *funcName)
Definition: Istream.C:152
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:107
A token holds items read from Istream.
Definition: token.H:67
label labelToken() const
Definition: tokenI.H:267
void append(const T &a)
Add at tail of list.
Definition: LList.H:166
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void clear()
Delete contents of list.
Definition: LList.C:53
punctuationToken pToken() const
Definition: tokenI.H:208
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325