LPtrListIO.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 "LPtrList.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 #include "INew.H"
30 
31 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32 
33 template<class LListBase, class T>
34 template<class INew>
35 void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& iNew)
36 {
37  is.fatalCheck
38  (
39  "LPtrList<LListBase, T>::read(Istream&, const INew&)"
40  );
41 
42  token firstToken(is);
43 
44  is.fatalCheck
45  (
46  "LPtrList<LListBase, T>::read(Istream&, const INew&) : "
47  "reading first token"
48  );
49 
50  if (firstToken.isLabel())
51  {
52  label s = firstToken.labelToken();
53 
54  // Read beginning of contents
55  char delimiter = is.readBeginList("LPtrList<LListBase, T>");
56 
57  if (s)
58  {
59  if (delimiter == token::BEGIN_LIST)
60  {
61  for (label i=0; i<s; ++i)
62  {
63  this->append(iNew(is).ptr());
64 
65  is.fatalCheck
66  (
67  "LPtrList<LListBase, T>::read(Istream&, const INew&) : "
68  "reading entry"
69  );
70  }
71  }
72  else
73  {
74  T* tPtr = iNew(is).ptr();
75  this->append(tPtr);
76 
77  is.fatalCheck
78  (
79  "LPtrList<LListBase, T>::read(Istream&, const INew&) : "
80  "reading entry"
81  );
82 
83  for (label i=1; i<s; ++i)
84  {
85  this->append(tPtr->clone().ptr());
86  }
87  }
88  }
89 
90  // Read end of contents
91  is.readEndList("LPtrList<LListBase, T>");
92  }
93  else if (firstToken.isPunctuation())
94  {
95  if (firstToken.pToken() != token::BEGIN_LIST)
96  {
98  (
99  is
100  ) << "incorrect first token, '(', found " << firstToken.info()
101  << exit(FatalIOError);
102  }
103 
104  token lastToken(is);
105  is.fatalCheck("LPtrList<LListBase, T>::read(Istream&, const INew&)");
106 
107  while
108  (
109  !(
110  lastToken.isPunctuation()
111  && lastToken.pToken() == token::END_LIST
112  )
113  )
114  {
115  is.putBack(lastToken);
116  this->append(iNew(is).ptr());
117 
118  is >> lastToken;
119  is.fatalCheck
120  (
121  "LPtrList<LListBase, T>::read(Istream&, const INew&)"
122  );
123  }
124  }
125  else
126  {
128  (
129  is
130  ) << "incorrect first token, expected <int> or '(', found "
131  << firstToken.info()
132  << exit(FatalIOError);
133  }
134 
135  is.fatalCheck("LPtrList<LListBase, T>::read(Istream&, const INew&)");
136 }
137 
138 
139 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
140 
141 template<class LListBase, class T>
142 template<class INew>
144 {
145  this->read(is, iNew);
146 }
147 
148 
149 template<class LListBase, class T>
151 {
152  this->read(is, INew<T>());
153 }
154 
155 
156 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
157 
158 template<class LListBase, class T>
160 {
161  L.clear();
162  L.read(is, INew<T>());
163 
164  return is;
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
169 
170 template<class LListBase, class T>
171 Foam::Ostream& Foam::operator<<(Ostream& os, const LPtrList<LListBase, T>& lst)
172 {
173  // Write size
174  os << nl << lst.size();
175 
176  // Write beginning of contents
177  os << nl << token::BEGIN_LIST << nl;
178 
179  // Write contents
180  for
181  (
182  typename LPtrList<LListBase, T>::const_iterator iter = lst.begin();
183  iter != lst.end();
184  ++iter
185  )
186  {
187  os << iter() << nl;
188  }
189 
190  // Write end of contents
191  os << token::END_LIST;
192 
193  // Check state of IOstream
194  os.check("Ostream& operator<<(Ostream&, const LPtrList<LListBase, T>&)");
195 
196  return os;
197 }
198 
199 // ************************************************************************* //
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
An STL-conforming const_iterator.
Definition: LPtrList.H:221
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:47
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 &)
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()
Clear the contents of the list.
Definition: LPtrList.C:75
const volScalarField & T
LPtrList()
Null construct.
Definition: LPtrList.H:94
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
IOerror FatalIOError