initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / HashTables / HashPtrTable / HashPtrTableIO.C
blobe4eb63f06e6c929976445306f94e99cc39ceb010
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "HashPtrTable.H"
28 #include "Istream.H"
29 #include "Ostream.H"
30 #include "INew.H"
32 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
34 template<class T, class Key, class Hash>
35 template<class INew>
36 void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
38     is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
40     token firstToken(is);
42     is.fatalCheck
43     (
44         "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
45         "reading first token"
46     );
48     if (firstToken.isLabel())
49     {
50         label s = firstToken.labelToken();
52         // Read beginning of contents
53         char delimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
55         if (s)
56         {
57             if (2*s > this->tableSize_)
58             {
59                 this->resize(2*s);
60             }
62             if (delimiter == token::BEGIN_LIST)
63             {
64                 for (label i=0; i<s; i++)
65                 {
66                     Key key;
67                     is >> key;
68                     insert(key, inewt(key, is).ptr());
70                     is.fatalCheck
71                     (
72                         "HashPtrTable<T, Key, Hash>::"
73                         "read(Istream&, const INew&) : reading entry"
74                     );
75                 }
76             }
77             else
78             {
79                 FatalIOErrorIn
80                 (
81                     "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
82                     is
83                 )   << "incorrect first token, '(', found " << firstToken.info()
84                     << exit(FatalIOError);
85             }
86         }
88         // Read end of contents
89         is.readEndList("HashPtrTable");
90     }
91     else if (firstToken.isPunctuation())
92     {
93         if (firstToken.pToken() != token::BEGIN_LIST)
94         {
95             FatalIOErrorIn
96             (
97                 "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
98                 is
99             )   << "incorrect first token, '(', found " << firstToken.info()
100                 << exit(FatalIOError);
101         }
103         token lastToken(is);
104         while
105         (
106            !(
107                 lastToken.isPunctuation()
108              && lastToken.pToken() == token::END_LIST
109             )
110         )
111         {
112             is.putBack(lastToken);
113             Key key;
114             is >> key;
115             insert(key, inewt(key, is).ptr());
117             is.fatalCheck
118             (
119                 "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
120                 "reading entry"
121             );
123             is >> lastToken;
124         }
125     }
126     else
127     {
128         FatalIOErrorIn
129         (
130             "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
131             is
132         )   << "incorrect first token, expected <int> or '(', found "
133             << firstToken.info()
134             << exit(FatalIOError);
135     }
137     is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
141 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
143 template<class T, class Key, class Hash>
144 template<class INew>
145 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inewt)
147     read(is, inewt);
151 template<class T, class Key, class Hash>
152 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is)
154     read(is, INew<T>());
158 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
160 template<class T, class Key, class Hash>
161 Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
163     L.clear();
164     L.read(is, INew<T>());
166     return is;
170 template<class T, class Key, class Hash>
171 Foam::Ostream& Foam::operator<<
173     Ostream& os,
174     const HashPtrTable<T, Key, Hash>& L
177     // Write size and start delimiter
178     os << nl << L.size() << nl << token::BEGIN_LIST << nl;
180     // Write contents
181     for
182     (
183         typename HashPtrTable<T, Key, Hash>::const_iterator iter = L.begin();
184         iter != L.end();
185         ++iter
186     )
187     {
188         os << iter.key() << token::SPACE << *iter() << nl;
189     }
191     // Write end delimiter
192     os << token::END_LIST;
194     // Check state of IOstream
195     os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)");
197     return os;
201 // ************************************************************************* //