PtrListIO.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 "PtrList.H"
27 #include "SLList.H"
28 #include "Istream.H"
29 #include "Ostream.H"
30 #include "INew.H"
31 
32 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
33 
34 template<class T>
35 template<class INew>
36 void Foam::PtrList<T>::read(Istream& is, const INew& inewt)
37 {
38  is.fatalCheck("PtrList<T>::read(Istream&, const INew&)");
39 
40  token firstToken(is);
41 
42  is.fatalCheck
43  (
44  "PtrList<T>::read(Istream&, const INew&) : "
45  "reading first token"
46  );
47 
48  if (firstToken.isLabel())
49  {
50  // Read size of list
51  label s = firstToken.labelToken();
52 
53  setSize(s);
54 
55  // Read beginning of contents
56  char delimiter = is.readBeginList("PtrList");
57 
58  if (s)
59  {
60  if (delimiter == token::BEGIN_LIST)
61  {
62  forAll(*this, i)
63  {
64  set(i, inewt(is));
65 
66  is.fatalCheck
67  (
68  "PtrList<T>::read(Istream&, const INew&) : "
69  "reading entry"
70  );
71  }
72  }
73  else
74  {
75  T* tPtr = inewt(is).ptr();
76  set(0, tPtr);
77 
78  is.fatalCheck
79  (
80  "PtrList<T>::read(Istream&, const INew&) : "
81  "reading the single entry"
82  );
83 
84  for (label i=1; i<s; i++)
85  {
86  set(i, tPtr->clone());
87  }
88  }
89  }
90 
91  // Read end of contents
92  is.readEndList("PtrList");
93  }
94  else if (firstToken.isPunctuation())
95  {
96  if (firstToken.pToken() != token::BEGIN_LIST)
97  {
99  (
100  is
101  ) << "incorrect first token, '(', found " << firstToken.info()
102  << exit(FatalIOError);
103  }
104 
105  SLList<T*> sllPtrs;
106 
107  token lastToken(is);
108  while
109  (
110  !(
111  lastToken.isPunctuation()
112  && lastToken.pToken() == token::END_LIST
113  )
114  )
115  {
116  is.putBack(lastToken);
117 
118  if (is.eof())
119  {
121  (
122  is
123  ) << "Premature EOF after reading " << lastToken.info()
124  << exit(FatalIOError);
125  }
126 
127  sllPtrs.append(inewt(is).ptr());
128  is >> lastToken;
129  }
130 
131  setSize(sllPtrs.size());
132 
133  label i = 0;
134  for
135  (
136  typename SLList<T*>::iterator iter = sllPtrs.begin();
137  iter != sllPtrs.end();
138  ++iter
139  )
140  {
141  set(i++, iter());
142  }
143  }
144  else
145  {
147  (
148  is
149  ) << "incorrect first token, expected <int> or '(', found "
150  << firstToken.info()
151  << exit(FatalIOError);
152  }
153 }
154 
155 
156 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
157 
158 template<class T>
159 template<class INew>
161 {
162  read(is, inewt);
163 }
164 
165 
166 template<class T>
168 {
169  read(is, INew<T>());
170 }
171 
172 
173 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
174 
175 template<class T>
177 {
178  L.clear();
179  L.read(is, INew<T>());
180 
181  return is;
182 }
183 
184 
185 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
punctuationToken pToken() const
Definition: tokenI.H:248
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const iterator & end()
Definition: LList.H:286
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
An STL-conforming iterator.
Definition: LList.H:246
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
void read(Istream &, const INew &inewt)
Read from Istream using given Istream constructor class.
Definition: PtrListIO.C:36
points setSize(newPointi)
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
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
iterator begin()
Definition: LList.H:281
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:318
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
label labelToken() const
Definition: tokenI.H:397
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:174
PtrList()
Null Constructor.
Definition: PtrList.C:32
Non-intrusive singly-linked list.
bool isPunctuation() const
Definition: tokenI.H:243
IOerror FatalIOError