OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "HashPtrTable.H"
00028 #include "Istream.H"
00029 #include "Ostream.H"
00030 #include "INew.H"
00031 
00032 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00033 
00034 template<class T, class Key, class Hash>
00035 template<class INew>
00036 void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
00037 {
00038     is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
00039 
00040     token firstToken(is);
00041 
00042     is.fatalCheck
00043     (
00044         "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
00045         "reading first token"
00046     );
00047 
00048     if (firstToken.isLabel())
00049     {
00050         label s = firstToken.labelToken();
00051 
00052         // Read beginning of contents
00053         char delimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
00054 
00055         if (s)
00056         {
00057             if (2*s > this->tableSize_)
00058             {
00059                 this->resize(2*s);
00060             }
00061 
00062             if (delimiter == token::BEGIN_LIST)
00063             {
00064                 for (label i=0; i<s; i++)
00065                 {
00066                     Key key;
00067                     is >> key;
00068                     insert(key, inewt(key, is).ptr());
00069 
00070                     is.fatalCheck
00071                     (
00072                         "HashPtrTable<T, Key, Hash>::"
00073                         "read(Istream&, const INew&) : reading entry"
00074                     );
00075                 }
00076             }
00077             else
00078             {
00079                 FatalIOErrorIn
00080                 (
00081                     "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
00082                     is
00083                 )   << "incorrect first token, '(', found " << firstToken.info()
00084                     << exit(FatalIOError);
00085             }
00086         }
00087 
00088         // Read end of contents
00089         is.readEndList("HashPtrTable");
00090     }
00091     else if (firstToken.isPunctuation())
00092     {
00093         if (firstToken.pToken() != token::BEGIN_LIST)
00094         {
00095             FatalIOErrorIn
00096             (
00097                 "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
00098                 is
00099             )   << "incorrect first token, '(', found " << firstToken.info()
00100                 << exit(FatalIOError);
00101         }
00102 
00103         token lastToken(is);
00104         while
00105         (
00106            !(
00107                 lastToken.isPunctuation()
00108              && lastToken.pToken() == token::END_LIST
00109             )
00110         )
00111         {
00112             is.putBack(lastToken);
00113             Key key;
00114             is >> key;
00115             insert(key, inewt(key, is).ptr());
00116 
00117             is.fatalCheck
00118             (
00119                 "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
00120                 "reading entry"
00121             );
00122 
00123             is >> lastToken;
00124         }
00125     }
00126     else
00127     {
00128         FatalIOErrorIn
00129         (
00130             "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)",
00131             is
00132         )   << "incorrect first token, expected <int> or '(', found "
00133             << firstToken.info()
00134             << exit(FatalIOError);
00135     }
00136 
00137     is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
00138 }
00139 
00140 
00141 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00142 
00143 template<class T, class Key, class Hash>
00144 template<class INew>
00145 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inewt)
00146 {
00147     read(is, inewt);
00148 }
00149 
00150 
00151 template<class T, class Key, class Hash>
00152 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is)
00153 {
00154     read(is, INew<T>());
00155 }
00156 
00157 
00158 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00159 
00160 template<class T, class Key, class Hash>
00161 Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
00162 {
00163     L.clear();
00164     L.read(is, INew<T>());
00165 
00166     return is;
00167 }
00168 
00169 
00170 template<class T, class Key, class Hash>
00171 Foam::Ostream& Foam::operator<<
00172 (
00173     Ostream& os,
00174     const HashPtrTable<T, Key, Hash>& L
00175 )
00176 {
00177     // Write size and start delimiter
00178     os << nl << L.size() << nl << token::BEGIN_LIST << nl;
00179 
00180     // Write contents
00181     for
00182     (
00183         typename HashPtrTable<T, Key, Hash>::const_iterator iter = L.begin();
00184         iter != L.end();
00185         ++iter
00186     )
00187     {
00188         os << iter.key() << token::SPACE << *iter() << nl;
00189     }
00190 
00191     // Write end delimiter
00192     os << token::END_LIST;
00193 
00194     // Check state of IOstream
00195     os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)");
00196 
00197     return os;
00198 }
00199 
00200 
00201 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd