1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
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
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 "FixedList.H"
31 #include "contiguous.H"
33 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
35 template<class T, unsigned Size>
36 Foam::FixedList<T, Size>::FixedList(Istream& is)
38 operator>>(is, *this);
42 template<class T, unsigned Size>
43 Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
45 is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
47 if (is.format() == IOstream::ASCII || !contiguous<T>())
53 "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
56 if (firstToken.isCompound())
58 L = dynamicCast<token::Compound<List<T> > >
60 firstToken.transferCompoundToken()
63 else if (firstToken.isLabel())
65 label s = firstToken.labelToken();
67 // Set list length to that read
70 else if (!firstToken.isPunctuation())
72 FatalIOErrorIn("operator>>(Istream&, FixedList<T, Size>&)", is)
73 << "incorrect first token, expected <label> "
74 "or '(' or '{', found "
76 << exit(FatalIOError);
80 // Putback the opening bracket
81 is.putBack(firstToken);
84 // Read beginning of contents
85 char delimiter = is.readBeginList("FixedList");
87 if (delimiter == token::BEGIN_LIST)
89 for (register unsigned i=0; i<Size; i++)
95 "operator>>(Istream&, FixedList<T, Size>&) : "
107 "operator>>(Istream&, FixedList<T, Size>&) : "
108 "reading the single entry"
111 for (register unsigned i=0; i<Size; i++)
117 // Read end of contents
118 is.readEndList("FixedList");
122 is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
126 "operator>>(Istream&, FixedList<T, Size>&) : "
127 "reading the binary block"
135 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
137 template<class T, unsigned Size>
138 void Foam::FixedList<T, Size>::writeEntry(Ostream& os) const
143 && token::compound::isCompound
145 "List<" + word(pTraits<T>::typeName) + '>'
149 os << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
156 template<class T, unsigned Size>
157 void Foam::FixedList<T, Size>::writeEntry
163 os.writeKeyword(keyword);
165 os << token::END_STATEMENT << endl;
169 template<class T, unsigned Size>
170 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
172 // Write list contents depending on data format
173 if (os.format() == IOstream::ASCII || !contiguous<T>())
175 bool uniform = false;
177 if (Size > 1 && contiguous<T>())
193 // Write size (so it is valid dictionary entry) and start delimiter
194 os << L.size() << token::BEGIN_BLOCK;
199 // Write end delimiter
200 os << token::END_BLOCK;
202 else if (Size < 11 && contiguous<T>())
204 // Write start delimiter
205 os << token::BEGIN_LIST;
210 if (i > 0) os << token::SPACE;
214 // Write end delimiter
215 os << token::END_LIST;
219 // Write start delimiter
220 os << nl << token::BEGIN_LIST;
228 // Write end delimiter
229 os << nl << token::END_LIST << nl;
234 os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
237 // Check state of IOstream
238 os.check("Ostream& operator<<(Ostream&, const FixedList&)");
244 // ************************************************************************* //