FreeFOAM The Cross-Platform CFD Toolkit
HashPtrTableIO.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 "HashPtrTable.H"
27 #include <OpenFOAM/Istream.H>
28 #include <OpenFOAM/Ostream.H>
29 #include <OpenFOAM/INew.H>
30 
31 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32 
33 template<class T, class Key, class Hash>
34 template<class INew>
35 void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
36 {
37  is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
38 
39  token firstToken(is);
40 
41  is.fatalCheck
42  (
43  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
44  "reading first token"
45  );
46 
47  if (firstToken.isLabel())
48  {
49  label s = firstToken.labelToken();
50 
51  // Read beginning of contents
52  char delimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
53 
54  if (s)
55  {
56  if (2*s > this->tableSize_)
57  {
58  this->resize(2*s);
59  }
60 
61  if (delimiter == token::BEGIN_LIST)
62  {
63  for (label i=0; i<s; i++)
64  {
65  Key key;
66  is >> key;
67  insert(key, inewt(key, is).ptr());
68 
69  is.fatalCheck
70  (
71  "HashPtrTable<T, Key, Hash>::"
72  "read(Istream&, const INew&) : reading entry"
73  );
74  }
75  }
76  else
77  {
79  (
80  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
81  is
82  ) << "incorrect first token, '(', found " << firstToken.info()
83  << exit(FatalIOError);
84  }
85  }
86 
87  // Read end of contents
88  is.readEndList("HashPtrTable");
89  }
90  else if (firstToken.isPunctuation())
91  {
92  if (firstToken.pToken() != token::BEGIN_LIST)
93  {
95  (
96  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
97  is
98  ) << "incorrect first token, '(', found " << firstToken.info()
99  << exit(FatalIOError);
100  }
101 
102  token lastToken(is);
103  while
104  (
105  !(
106  lastToken.isPunctuation()
107  && lastToken.pToken() == token::END_LIST
108  )
109  )
110  {
111  is.putBack(lastToken);
112  Key key;
113  is >> key;
114  insert(key, inewt(key, is).ptr());
115 
116  is.fatalCheck
117  (
118  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
119  "reading entry"
120  );
121 
122  is >> lastToken;
123  }
124  }
125  else
126  {
128  (
129  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
130  is
131  ) << "incorrect first token, expected <int> or '(', found "
132  << firstToken.info()
133  << exit(FatalIOError);
134  }
135 
136  is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
141 
142 template<class T, class Key, class Hash>
143 template<class INew>
145 {
146  read(is, inewt);
147 }
148 
149 
150 template<class T, class Key, class Hash>
152 {
153  read(is, INew<T>());
154 }
155 
156 
157 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
158 
159 template<class T, class Key, class Hash>
161 {
162  L.clear();
163  L.read(is, INew<T>());
164 
165  return is;
166 }
167 
168 
169 template<class T, class Key, class Hash>
170 Foam::Ostream& Foam::operator<<
171 (
172  Ostream& os,
174 )
175 {
176  // Write size and start delimiter
177  os << nl << L.size() << nl << token::BEGIN_LIST << nl;
178 
179  // Write contents
180  for
181  (
182  typename HashPtrTable<T, Key, Hash>::const_iterator iter = L.begin();
183  iter != L.end();
184  ++iter
185  )
186  {
187  os << iter.key() << token::SPACE << *iter() << nl;
188  }
189 
190  // Write end delimiter
191  os << token::END_LIST;
192 
193  // Check state of IOstream
194  os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)");
195 
196  return os;
197 }
198 
199 
200 // ************************ vim: set sw=4 sts=4 et: ************************ //