Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / containers / Lists / PtrList / PtrListIO.C
blobcdd89c15b65c1a6171db9b493311af79d6f64d81
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "PtrList.H"
27 #include "SLList.H"
28 #include "Istream.H"
29 #include "Ostream.H"
30 #include "INew.H"
32 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
34 template<class T>
35 template<class INew>
36 void Foam::PtrList<T>::read(Istream& is, const INew& inewt)
38     is.fatalCheck("PtrList<T>::read(Istream&, const INew&)");
40     token firstToken(is);
42     is.fatalCheck
43     (
44         "PtrList<T>::read(Istream&, const INew&) : "
45         "reading first token"
46     );
48     if (firstToken.isLabel())
49     {
50         // Read size of list
51         label s = firstToken.labelToken();
53         setSize(s);
55         // Read beginning of contents
56         char delimiter = is.readBeginList("PtrList");
58         if (s)
59         {
60             if (delimiter == token::BEGIN_LIST)
61             {
62                 forAll(*this, i)
63                 {
64                     set(i, inewt(is));
66                     is.fatalCheck
67                     (
68                         "PtrList<T>::read(Istream&, const INew&) : "
69                         "reading entry"
70                     );
71                 }
72             }
73             else
74             {
75                 T* tPtr = inewt(is).ptr();
76                 set(0, tPtr);
78                 is.fatalCheck
79                 (
80                     "PtrList<T>::read(Istream&, const INew&) : "
81                     "reading the single entry"
82                 );
84                 for (label i=1; i<s; i++)
85                 {
86                     set(i, tPtr->clone());
87                 }
88             }
89         }
91         // Read end of contents
92         is.readEndList("PtrList");
93     }
94     else if (firstToken.isPunctuation())
95     {
96         if (firstToken.pToken() != token::BEGIN_LIST)
97         {
98             FatalIOErrorIn
99             (
100                 "PtrList<T>::read(Istream&, const INew&)",
101                 is
102             )   << "incorrect first token, '(', found " << firstToken.info()
103                 << exit(FatalIOError);
104         }
106         SLList<T*> sllPtrs;
108         token lastToken(is);
109         while
110         (
111            !(
112                 lastToken.isPunctuation()
113              && lastToken.pToken() == token::END_LIST
114             )
115         )
116         {
117             is.putBack(lastToken);
118             sllPtrs.append(inewt(is).ptr());
119             is >> lastToken;
120         }
122         setSize(sllPtrs.size());
124         label i = 0;
125         for
126         (
127             typename SLList<T*>::iterator iter = sllPtrs.begin();
128             iter != sllPtrs.end();
129             ++iter
130         )
131         {
132             set(i++, iter());
133         }
134     }
135     else
136     {
137         FatalIOErrorIn
138         (
139             "PtrList<T>::read(Istream&, const INew&)",
140             is
141         )   << "incorrect first token, expected <int> or '(', found "
142             << firstToken.info()
143             << exit(FatalIOError);
144     }
148 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
150 template<class T>
151 template<class INew>
152 Foam::PtrList<T>::PtrList(Istream& is, const INew& inewt)
154     read(is, inewt);
158 template<class T>
159 Foam::PtrList<T>::PtrList(Istream& is)
161     read(is, INew<T>());
165 // * * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * //
167 template<class T>
168 Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& L)
170     L.clear();
171     L.read(is, INew<T>());
173     return is;
177 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
179 template<class T>
180 Foam::Ostream& Foam::operator<<(Ostream& os, const PtrList<T>& L)
182     // Write size and start delimiter
183     os << nl << L.size() << nl << token::BEGIN_LIST;
185     // Write contents
186     forAll(L, i)
187     {
188         os << nl << L[i];
189     }
191     // Write end delimiter
192     os << nl << token::END_LIST << nl;
194     // Check state of IOstream
195     os.check("Ostream& operator<<(Ostream&, const PtrList&)");
197     return os;
201 // ************************************************************************* //