initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / UList / UListIO.C
blob967462fb171fc91c511d2fbde76db74424b9329f
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 "UList.H"
28 #include "Ostream.H"
29 #include "token.H"
30 #include "contiguous.H"
32 // * * * * * * * * * * * * * * * Ostream Operator *  * * * * * * * * * * * * //
34 template<class T>
35 void Foam::UList<T>::writeEntry(Ostream& os) const
37     if
38     (
39         size()
40      && token::compound::isCompound
41         (
42             "List<" + word(pTraits<T>::typeName) + '>'
43         )
44     )
45     {
46         os  << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
47     }
49     os << *this;
53 template<class T>
54 void Foam::UList<T>::writeEntry(const word& keyword, Ostream& os) const
56     os.writeKeyword(keyword);
57     writeEntry(os);
58     os << token::END_STATEMENT << endl;
62 template<class T>
63 Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
65     // Write list contents depending on data format
66     if (os.format() == IOstream::ASCII || !contiguous<T>())
67     {
68         bool uniform = false;
70         if (L.size() > 1 && contiguous<T>())
71         {
72             uniform = true;
74             forAll(L, i)
75             {
76                 if (L[i] != L[0])
77                 {
78                     uniform = false;
79                     break;
80                 }
81             }
82         }
84         if (uniform)
85         {
86             // Write size and start delimiter
87             os << L.size() << token::BEGIN_BLOCK;
89             // Write contents
90             os << L[0];
92             // Write end delimiter
93             os << token::END_BLOCK;
94         }
95         else if (L.size() < 11 && contiguous<T>())
96         {
97             // Write size and start delimiter
98             os << L.size() << token::BEGIN_LIST;
100             // Write contents
101             forAll(L, i)
102             {
103                 if (i > 0) os << token::SPACE;
104                 os << L[i];
105             }
107             // Write end delimiter
108             os << token::END_LIST;
109         }
110         else
111         {
112             // Write size and start delimiter
113             os << nl << L.size() << nl << token::BEGIN_LIST;
115             // Write contents
116             forAll(L, i)
117             {
118                 os << nl << L[i];
119             }
121             // Write end delimiter
122             os << nl << token::END_LIST << nl;
123         }
124     }
125     else
126     {
127         os << nl << L.size() << nl;
128         if (L.size())
129         {
130             os.write(reinterpret_cast<const char*>(L.v_), L.byteSize());
131         }
132     }
134     // Check state of IOstream
135     os.check("Ostream& operator<<(Ostream&, const UList&)");
137     return os;
141 // ************************************************************************* //