PtrListIO.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 "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  "PtrList<T>::read(Istream&, const INew&)",
101  is
102  ) << "incorrect first token, '(', found " << firstToken.info()
103  << exit(FatalIOError);
104  }
105 
106  SLList<T*> sllPtrs;
107 
108  token lastToken(is);
109  while
110  (
111  !(
112  lastToken.isPunctuation()
113  && lastToken.pToken() == token::END_LIST
114  )
115  )
116  {
117  is.putBack(lastToken);
118 
119  if (is.eof())
120  {
122  (
123  "PtrList<T>::read(Istream&, const INew&)",
124  is
125  ) << "Premature EOF after reading " << lastToken.info()
126  << exit(FatalIOError);
127  }
128 
129  sllPtrs.append(inewt(is).ptr());
130  is >> lastToken;
131  }
132 
133  setSize(sllPtrs.size());
134 
135  label i = 0;
136  for
137  (
138  typename SLList<T*>::iterator iter = sllPtrs.begin();
139  iter != sllPtrs.end();
140  ++iter
141  )
142  {
143  set(i++, iter());
144  }
145  }
146  else
147  {
149  (
150  "PtrList<T>::read(Istream&, const INew&)",
151  is
152  ) << "incorrect first token, expected <int> or '(', found "
153  << firstToken.info()
154  << exit(FatalIOError);
155  }
156 }
157 
158 
159 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
160 
161 template<class T>
162 template<class INew>
164 {
165  read(is, inewt);
166 }
167 
168 
169 template<class T>
171 {
172  read(is, INew<T>());
173 }
174 
175 
176 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
177 
178 template<class T>
180 {
181  L.clear();
182  L.read(is, INew<T>());
183 
184  return is;
185 }
186 
187 
188 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
189 
190 template<class T>
191 Foam::Ostream& Foam::operator<<(Ostream& os, const PtrList<T>& L)
192 {
193  // Write size and start delimiter
194  os << nl << L.size() << nl << token::BEGIN_LIST;
195 
196  // Write contents
197  forAll(L, i)
198  {
199  os << nl << L[i];
200  }
201 
202  // Write end delimiter
203  os << nl << token::END_LIST << nl;
204 
205  // Check state of IOstream
206  os.check("Ostream& operator<<(Ostream&, const PtrList&)");
207 
208  return os;
209 }
210 
211 
212 // ************************************************************************* //
Non-intrusive singly-linked list.
Definition: SLList.H:47
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
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 ))
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
const iterator & end()
Definition: LList.H:273
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:185
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
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
const volScalarField & T
Definition: createFields.H:25
PtrList()
Null Constructor.
Definition: PtrList.C:34
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
static const char nl
Definition: Ostream.H:260
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
An STL-conforming iterator.
Definition: SLListBase.H:163
void read(Istream &, const INew &inewt)
Read from Istream using given Istream constructor class.
Definition: PtrListIO.C:36
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
#define forAll(list, i)
Definition: UList.H:421
A token holds items read from Istream.
Definition: token.H:67
label labelToken() const
Definition: tokenI.H:267
points setSize(newPointi)
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
bool read(const char *, int32_t &)
Definition: int32IO.C:87
punctuationToken pToken() const
Definition: tokenI.H:208
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
label size() const
Return number of elements in list.
Definition: SLListBaseI.H:67