FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/SLList.H>
28 #include <OpenFOAM/Istream.H>
29 #include <OpenFOAM/Ostream.H>
30 #include <OpenFOAM/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  sllPtrs.append(inewt(is).ptr());
119  is >> lastToken;
120  }
121 
122  setSize(sllPtrs.size());
123 
124  label i = 0;
125  for
126  (
127  typename SLList<T*>::iterator iter = sllPtrs.begin();
128  iter != sllPtrs.end();
129  ++iter
130  )
131  {
132  set(i++, iter());
133  }
134  }
135  else
136  {
138  (
139  "PtrList<T>::read(Istream&, const INew&)",
140  is
141  ) << "incorrect first token, expected <int> or '(', found "
142  << firstToken.info()
143  << exit(FatalIOError);
144  }
145 }
146 
147 
148 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
149 
150 template<class T>
151 template<class INew>
153 {
154  read(is, inewt);
155 }
156 
157 
158 template<class T>
160 {
161  read(is, INew<T>());
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
166 
167 template<class T>
169 {
170  L.clear();
171  L.read(is, INew<T>());
172 
173  return is;
174 }
175 
176 
177 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
178 
179 template<class T>
180 Foam::Ostream& Foam::operator<<(Ostream& os, const PtrList<T>& L)
181 {
182  // Write size and start delimiter
183  os << nl << L.size() << nl << token::BEGIN_LIST;
184 
185  // Write contents
186  forAll(L, i)
187  {
188  os << nl << L[i];
189  }
190 
191  // Write end delimiter
192  os << nl << token::END_LIST << nl;
193 
194  // Check state of IOstream
195  os.check("Ostream& operator<<(Ostream&, const PtrList&)");
196 
197  return os;
198 }
199 
200 
201 // ************************ vim: set sw=4 sts=4 et: ************************ //