FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

HashPtrTableIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "HashPtrTable.H"
00027 #include <OpenFOAM/Istream.H>
00028 #include <OpenFOAM/Ostream.H>
00029 #include <OpenFOAM/INew.H>
00030 
00031 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00032 
00033 template<class T, class Key, class Hash>
00034 template<class INew>
00035 void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
00036 {
00037     is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
00038 
00039     token firstToken(is);
00040 
00041     is.fatalCheck
00042     (
00043         "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
00044         "reading first token"
00045     );
00046 
00047     if (firstToken.isLabel())
00048     {
00049         label s = firstToken.labelToken();
00050 
00051         // Read beginning of contents
00052         char delimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
00053 
00054         if (s)
00055         {
00056             if (2*s > this->tableSize_)
00057             {
00058                 this->resize(2*s);
00059             }
00060 
00061             if (delimiter == token::BEGIN_LIST)
00062             {
00063                 for (label i=0; i<s; i++)
00064                 {
00065                     Key key;
00066                     is >> key;
00067                     this->insert(key, inewt(key, is).ptr());
00068 
00069                     is.fatalCheck
00070                     (
00071                         "HashPtrTable<T, Key, Hash>::"
00072                         "read(Istream&, const INew&) : reading entry"
00073                     );
00074                 }
00075             }
00076             else
00077             {
00078                 FatalIOErrorIn
00079                 (
00080                     "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
00081                     is
00082                 )   << "incorrect first token, '(', found " << firstToken.info()
00083                     << exit(FatalIOError);
00084             }
00085         }
00086 
00087         // Read end of contents
00088         is.readEndList("HashPtrTable");
00089     }
00090     else if (firstToken.isPunctuation())
00091     {
00092         if (firstToken.pToken() != token::BEGIN_LIST)
00093         {
00094             FatalIOErrorIn
00095             (
00096                 "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
00097                 is
00098             )   << "incorrect first token, '(', found " << firstToken.info()
00099                 << exit(FatalIOError);
00100         }
00101 
00102         token lastToken(is);
00103         while
00104         (
00105            !(
00106                 lastToken.isPunctuation()
00107              && lastToken.pToken() == token::END_LIST
00108             )
00109         )
00110         {
00111             is.putBack(lastToken);
00112             Key key;
00113             is >> key;
00114             this->insert(key, inewt(key, is).ptr());
00115 
00116             is.fatalCheck
00117             (
00118                 "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
00119                 "reading entry"
00120             );
00121 
00122             is >> lastToken;
00123         }
00124     }
00125     else
00126     {
00127         FatalIOErrorIn
00128         (
00129             "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
00130             is
00131         )   << "incorrect first token, expected <int> or '(', found "
00132             << firstToken.info()
00133             << exit(FatalIOError);
00134     }
00135 
00136     is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
00137 }
00138 
00139 
00140 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00141 
00142 template<class T, class Key, class Hash>
00143 template<class INew>
00144 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inewt)
00145 {
00146     read(is, inewt);
00147 }
00148 
00149 
00150 template<class T, class Key, class Hash>
00151 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is)
00152 {
00153     read(is, INew<T>());
00154 }
00155 
00156 
00157 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00158 
00159 template<class T, class Key, class Hash>
00160 Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
00161 {
00162     L.clear();
00163     L.read(is, INew<T>());
00164 
00165     return is;
00166 }
00167 
00168 
00169 template<class T, class Key, class Hash>
00170 Foam::Ostream& Foam::operator<<
00171 (
00172     Ostream& os,
00173     const HashPtrTable<T, Key, Hash>& L
00174 )
00175 {
00176     // Write size and start delimiter
00177     os << nl << L.size() << nl << token::BEGIN_LIST << nl;
00178 
00179     // Write contents
00180     for
00181     (
00182         typename HashPtrTable<T, Key, Hash>::const_iterator iter = L.begin();
00183         iter != L.end();
00184         ++iter
00185     )
00186     {
00187         os << iter.key() << token::SPACE << *iter() << nl;
00188     }
00189 
00190     // Write end delimiter
00191     os << token::END_LIST;
00192 
00193     // Check state of IOstream
00194     os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)");
00195 
00196     return os;
00197 }
00198 
00199 
00200 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines