FreeFOAM The Cross-Platform CFD Toolkit
LListIO.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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "LList.H"
29 #include <OpenFOAM/Istream.H>
30 #include <OpenFOAM/Ostream.H>
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class LListBase, class T>
36 {
37  operator>>(is, *this);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
42 
43 template<class LListBase, class T>
45 {
46  // Anull list
47  L.clear();
48 
49  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
50 
51  token firstToken(is);
52 
53  is.fatalCheck
54  (
55  " operator>>(Istream&, LList<LListBase, T>&) : reading first token"
56  );
57 
58  if (firstToken.isLabel())
59  {
60  label s = firstToken.labelToken();
61 
62  // Read beginning of contents
63  char delimiter = is.readBeginList("LList<LListBase, T>");
64 
65  if (s)
66  {
67  if (delimiter == token::BEGIN_LIST)
68  {
69  for (register label i=0; i<s; i++)
70  {
71  T element;
72  is >> element;
73  L.append(element);
74  }
75  }
76  else
77  {
78  T element;
79  is >> element;
80 
81  for (register label i=0; i<s; i++)
82  {
83  L.append(element);
84  }
85  }
86  }
87 
88  // Read end of contents
89  is.readEndList("LList");
90  }
91  else if (firstToken.isPunctuation())
92  {
93  if (firstToken.pToken() != token::BEGIN_LIST)
94  {
96  (
97  " operator>>(Istream&, LList<LListBase, T>&)",
98  is
99  ) << "incorrect first token, '(', found " << firstToken.info()
100  << exit(FatalIOError);
101  }
102 
103  token lastToken(is);
104  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
105 
106  while
107  (
108  !(
109  lastToken.isPunctuation()
110  && lastToken.pToken() == token::END_LIST
111  )
112  )
113  {
114  is.putBack(lastToken);
115  T element;
116  is >> element;
117  L.append(element);
118 
119  is >> lastToken;
120  is.fatalCheck(" operator>>(Istream&, LList<LListBase, T>&)");
121  }
122  }
123  else
124  {
125  FatalIOErrorIn(" operator>>(Istream&, LList<LListBase, T>&)", is)
126  << "incorrect first token, expected <int> or '(', found "
127  << firstToken.info()
128  << exit(FatalIOError);
129  }
130 
131  // Check state of IOstream
132  is.fatalCheck(" operator>>(Istream&, LList<LListBase,>&)");
133 
134  return is;
135 }
136 
137 
138 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
139 
140 template<class LListBase, class T>
141 Foam::Ostream& Foam::operator<<(Ostream& os, const LList<LListBase, T>& lst)
142 {
143  // Write size
144  os << nl << lst.size();
145 
146  // Write beginning of contents
147  os << nl << token::BEGIN_LIST << nl;
148 
149  // Write contents
150  for
151  (
152  typename LList<LListBase, T>::const_iterator iter = lst.begin();
153  iter != lst.end();
154  ++iter
155  )
156  {
157  os << iter() << nl;
158  }
159 
160  // Write end of contents
161  os << token::END_LIST;
162 
163  // Check state of IOstream
164  os.check("Ostream& operator<<(Ostream&, const LList<LListBase, T>&)");
165 
166  return os;
167 }
168 
169 
170 // ************************ vim: set sw=4 sts=4 et: ************************ //