LPtrListIO.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 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  "LPtrList<LListBase, T>::read(Istream&, const INew&)",
100  is
101  ) << "incorrect first token, '(', found " << firstToken.info()
102  << exit(FatalIOError);
103  }
104 
105  token lastToken(is);
106  is.fatalCheck("LPtrList<LListBase, T>::read(Istream&, const INew&)");
107 
108  while
109  (
110  !(
111  lastToken.isPunctuation()
112  && lastToken.pToken() == token::END_LIST
113  )
114  )
115  {
116  is.putBack(lastToken);
117  this->append(iNew(is).ptr());
118 
119  is >> lastToken;
120  is.fatalCheck
121  (
122  "LPtrList<LListBase, T>::read(Istream&, const INew&)"
123  );
124  }
125  }
126  else
127  {
129  (
130  "LPtrList<LListBase, T>::read(Istream&, const INew&)",
131  is
132  ) << "incorrect first token, expected <int> or '(', found "
133  << firstToken.info()
134  << exit(FatalIOError);
135  }
136 
137  is.fatalCheck("LPtrList<LListBase, T>::read(Istream&, const INew&)");
138 }
139 
140 
141 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
142 
143 template<class LListBase, class T>
144 template<class INew>
146 {
147  this->read(is, iNew);
148 }
149 
150 
151 template<class LListBase, class T>
153 {
154  this->read(is, INew<T>());
155 }
156 
157 
158 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
159 
160 template<class LListBase, class T>
162 {
163  L.clear();
164  L.read(is, INew<T>());
165 
166  return is;
167 }
168 
169 
170 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
171 
172 template<class LListBase, class T>
173 Foam::Ostream& Foam::operator<<(Ostream& os, const LPtrList<LListBase, T>& lst)
174 {
175  // Write size
176  os << nl << lst.size();
177 
178  // Write beginning of contents
179  os << nl << token::BEGIN_LIST << nl;
180 
181  // Write contents
182  for
183  (
184  typename LPtrList<LListBase, T>::const_iterator iter = lst.begin();
185  iter != lst.end();
186  ++iter
187  )
188  {
189  os << iter() << nl;
190  }
191 
192  // Write end of contents
193  os << token::END_LIST;
194 
195  // Check state of IOstream
196  os.check("Ostream& operator<<(Ostream&, const LPtrList<LListBase, T>&)");
197 
198  return os;
199 }
200 
201 // ************************************************************************* //
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:47
An STL-conforming const_iterator.
Definition: LPtrList.H:215
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 ))
void clear()
Clear the contents of the list.
Definition: LPtrList.C:70
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
const volScalarField & T
Definition: createFields.H:25
static const char nl
Definition: Ostream.H:260
IOerror FatalIOError
LPtrList()
Null construct.
Definition: LPtrList.H:94
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
bool read(const char *, int32_t &)
Definition: int32IO.C:87
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325