initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / PtrList / PtrListIO.C
blob564b22c2bccc3891272d40a0e33b914be00cb9a6
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 "PtrList.H"
28 #include "SLList.H"
29 #include "Istream.H"
30 #include "Ostream.H"
31 #include "INew.H"
33 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
35 template<class T>
36 template<class INew>
37 void Foam::PtrList<T>::read(Istream& is, const INew& inewt)
39     is.fatalCheck("PtrList<T>::read(Istream&, const INew&)");
41     token firstToken(is);
43     is.fatalCheck
44     (
45         "PtrList<T>::read(Istream&, const INew&) : "
46         "reading first token"
47     );
49     if (firstToken.isLabel())
50     {
51         // Read size of list
52         label s = firstToken.labelToken();
54         setSize(s);
56         // Read beginning of contents
57         char delimiter = is.readBeginList("PtrList");
59         if (s)
60         {
61             if (delimiter == token::BEGIN_LIST)
62             {
63                 forAll(*this, i)
64                 {
65                     set(i, inewt(is));
67                     is.fatalCheck
68                     (
69                         "PtrList<T>::read(Istream&, const INew&) : "
70                         "reading entry"
71                     );
72                 }
73             }
74             else
75             {
76                 T* tPtr = inewt(is).ptr();
77                 set(0, tPtr);
79                 is.fatalCheck
80                 (
81                     "PtrList<T>::read(Istream&, const INew&) : "
82                     "reading the single entry"
83                 );
85                 for (label i=1; i<s; i++)
86                 {
87                     set(i, tPtr->clone());
88                 }
89             }
90         }
92         // Read end of contents
93         is.readEndList("PtrList");
94     }
95     else if (firstToken.isPunctuation())
96     {
97         if (firstToken.pToken() != token::BEGIN_LIST)
98         {
99             FatalIOErrorIn
100             (
101                 "PtrList<T>::read(Istream&, const INew&)",
102                 is
103             )   << "incorrect first token, '(', found " << firstToken.info()
104                 << exit(FatalIOError);
105         }
107         SLList<T*> sllPtrs;
109         token lastToken(is);
110         while
111         (
112            !(
113                 lastToken.isPunctuation()
114              && lastToken.pToken() == token::END_LIST
115             )
116         )
117         {
118             is.putBack(lastToken);
119             sllPtrs.append(inewt(is).ptr());
120             is >> lastToken;
121         }
123         setSize(sllPtrs.size());
125         label i = 0;
126         for
127         (
128             typename SLList<T*>::iterator iter = sllPtrs.begin();
129             iter != sllPtrs.end();
130             ++iter
131         )
132         {
133             set(i++, iter());
134         }
135     }
136     else
137     {
138         FatalIOErrorIn
139         (
140             "PtrList<T>::read(Istream&, const INew&)",
141             is
142         )   << "incorrect first token, expected <int> or '(', found "
143             << firstToken.info()
144             << exit(FatalIOError);
145     }
149 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
151 template<class T>
152 template<class INew>
153 Foam::PtrList<T>::PtrList(Istream& is, const INew& inewt)
155     read(is, inewt);
159 template<class T>
160 Foam::PtrList<T>::PtrList(Istream& is)
162     read(is, INew<T>());
166 // * * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * //
168 template<class T>
169 Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& L)
171     L.clear();
172     L.read(is, INew<T>());
174     return is;
178 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
180 template<class T>
181 Foam::Ostream& Foam::operator<<(Ostream& os, const PtrList<T>& L)
183     // Write size and start delimiter
184     os << nl << L.size() << nl << token::BEGIN_LIST;
186     // Write contents
187     forAll(L, i)
188     {
189         os << nl << L[i];
190     }
192     // Write end delimiter
193     os << nl << token::END_LIST << nl;
195     // Check state of IOstream
196     os.check("Ostream& operator<<(Ostream&, const PtrList&)");
198     return os;
202 // ************************************************************************* //