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

FixedListIO.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 "FixedList.H"
00027 #include <OpenFOAM/Istream.H>
00028 #include <OpenFOAM/Ostream.H>
00029 #include <OpenFOAM/token.H>
00030 #include <OpenFOAM/contiguous.H>
00031 
00032 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00033 
00034 template<class T, unsigned Size>
00035 Foam::FixedList<T, Size>::FixedList(Istream& is)
00036 {
00037     operator>>(is, *this);
00038 }
00039 
00040 
00041 template<class T, unsigned Size>
00042 Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
00043 {
00044     is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
00045 
00046     if (is.format() == IOstream::ASCII || !contiguous<T>())
00047     {
00048         token firstToken(is);
00049 
00050         is.fatalCheck
00051         (
00052             "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
00053         );
00054 
00055         if (firstToken.isCompound())
00056         {
00057             L = dynamicCast<token::Compound<List<T> > >
00058             (
00059                 firstToken.transferCompoundToken()
00060             );
00061         }
00062         else if (firstToken.isLabel())
00063         {
00064             label s = firstToken.labelToken();
00065 
00066             // Set list length to that read
00067             L.checkSize(s);
00068         }
00069         else if (!firstToken.isPunctuation())
00070         {
00071             FatalIOErrorIn("operator>>(Istream&, FixedList<T, Size>&)", is)
00072                 << "incorrect first token, expected <label> "
00073                    "or '(' or '{', found "
00074                 << firstToken.info()
00075                 << exit(FatalIOError);
00076         }
00077         else
00078         {
00079             // Putback the opening bracket
00080             is.putBack(firstToken);
00081         }
00082 
00083         // Read beginning of contents
00084         char delimiter = is.readBeginList("FixedList");
00085 
00086         if (delimiter == token::BEGIN_LIST)
00087         {
00088             for (register unsigned i=0; i<Size; i++)
00089             {
00090                 is >> L[i];
00091 
00092                 is.fatalCheck
00093                 (
00094                     "operator>>(Istream&, FixedList<T, Size>&) : "
00095                     "reading entry"
00096                 );
00097             }
00098         }
00099         else
00100         {
00101             T element;
00102             is >> element;
00103 
00104             is.fatalCheck
00105             (
00106                 "operator>>(Istream&, FixedList<T, Size>&) : "
00107                 "reading the single entry"
00108             );
00109 
00110             for (register unsigned i=0; i<Size; i++)
00111             {
00112                 L[i] = element;
00113             }
00114         }
00115 
00116         // Read end of contents
00117         is.readEndList("FixedList");
00118     }
00119     else
00120     {
00121         is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
00122 
00123         is.fatalCheck
00124         (
00125             "operator>>(Istream&, FixedList<T, Size>&) : "
00126             "reading the binary block"
00127         );
00128     }
00129 
00130     return is;
00131 }
00132 
00133 
00134 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
00135 
00136 template<class T, unsigned Size>
00137 void Foam::FixedList<T, Size>::writeEntry(Ostream& os) const
00138 {
00139     if
00140     (
00141         size()
00142      && token::compound::isCompound
00143         (
00144             "List<" + word(pTraits<T>::typeName) + '>'
00145         )
00146     )
00147     {
00148         os  << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
00149     }
00150 
00151     os << *this;
00152 }
00153 
00154 
00155 template<class T, unsigned Size>
00156 void Foam::FixedList<T, Size>::writeEntry
00157 (
00158     const word& keyword,
00159     Ostream& os
00160 ) const
00161 {
00162     os.writeKeyword(keyword);
00163     writeEntry(os);
00164     os << token::END_STATEMENT << endl;
00165 }
00166 
00167 
00168 template<class T, unsigned Size>
00169 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
00170 {
00171     // Write list contents depending on data format
00172     if (os.format() == IOstream::ASCII || !contiguous<T>())
00173     {
00174         bool uniform = false;
00175 
00176         if (Size > 1 && contiguous<T>())
00177         {
00178             uniform = true;
00179 
00180             forAll(L, i)
00181             {
00182                 if (L[i] != L[0])
00183                 {
00184                     uniform = false;
00185                     break;
00186                 }
00187             }
00188         }
00189 
00190         if (uniform)
00191         {
00192             // Write size (so it is valid dictionary entry) and start delimiter
00193             os << L.size() << token::BEGIN_BLOCK;
00194 
00195             // Write contents
00196             os << L[0];
00197 
00198             // Write end delimiter
00199             os << token::END_BLOCK;
00200         }
00201         else if (Size < 11 && contiguous<T>())
00202         {
00203             // Write start delimiter
00204             os << token::BEGIN_LIST;
00205 
00206             // Write contents
00207             forAll(L, i)
00208             {
00209                 if (i > 0) os << token::SPACE;
00210                 os << L[i];
00211             }
00212 
00213             // Write end delimiter
00214             os << token::END_LIST;
00215         }
00216         else
00217         {
00218             // Write start delimiter
00219             os << nl << token::BEGIN_LIST;
00220 
00221             // Write contents
00222             forAll(L, i)
00223             {
00224                 os << nl << L[i];
00225             }
00226 
00227             // Write end delimiter
00228             os << nl << token::END_LIST << nl;
00229         }
00230     }
00231     else
00232     {
00233         os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
00234     }
00235 
00236     // Check state of IOstream
00237     os.check("Ostream& operator<<(Ostream&, const FixedList&)");
00238 
00239     return os;
00240 }
00241 
00242 
00243 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines