initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / LinkedLists / accessTypes / LPtrList / LPtrListIO.C
blob735dd8d31b7819b2d1456abed56e606e814591ae
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 "LPtrList.H"
28 #include "Istream.H"
29 #include "Ostream.H"
30 #include "INew.H"
32 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
34 template<class LListBase, class T>
35 template<class INew>
36 void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& iNew)
38     is.fatalCheck
39     (
40         "LPtrList<LListBase, T>::read(Istream&, const INew&)"
41     );
43     token firstToken(is);
45     is.fatalCheck
46     (
47         "LPtrList<LListBase, T>::read(Istream&, const INew&) : "
48         "reading first token"
49     );
51     if (firstToken.isLabel())
52     {
53         label s = firstToken.labelToken();
55         // Read beginning of contents
56         char delimiter = is.readBeginList("LPtrList<LListBase, T>");
58         if (s)
59         {
60             if (delimiter == token::BEGIN_LIST)
61             {
62                 for (label i=0; i<s; i++)
63                 {
64                     append(iNew(is).ptr());
66                     is.fatalCheck
67                     (
68                         "LPtrList<LListBase, T>::read(Istream&, const INew&) : "
69                         "reading entry"
70                     );
71                 }
72             }
73             else
74             {
75                 T* tPtr = iNew(is).ptr();
76                 append(tPtr);
78                 is.fatalCheck
79                 (
80                     "LPtrList<LListBase, T>::read(Istream&, const INew&) : "
81                     "reading entry"
82                 );
84                 for (label i=1; i<s; i++)
85                 {
86                     append(tPtr->clone().ptr());
87                 }
88             }
89         }
91         // Read end of contents
92         is.readEndList("LPtrList<LListBase, T>");
93     }
94     else if (firstToken.isPunctuation())
95     {
96         if (firstToken.pToken() != token::BEGIN_LIST)
97         {
98             FatalIOErrorIn
99             (
100                 "LPtrList<LListBase, T>::read(Istream&, const INew&)",
101                 is
102             )   << "incorrect first token, '(', found " << firstToken.info()
103                 << exit(FatalIOError);
104         }
106         token lastToken(is);
107         is.fatalCheck("LPtrList<LListBase, T>::read(Istream&, const INew&)");
109         while
110         (
111            !(
112                 lastToken.isPunctuation()
113              && lastToken.pToken() == token::END_LIST
114             )
115         )
116         {
117             is.putBack(lastToken);
118             append(iNew(is).ptr());
120             is >> lastToken;
121             is.fatalCheck
122             (
123                 "LPtrList<LListBase, T>::read(Istream&, const INew&)"
124             );
125         }
126     }
127     else
128     {
129         FatalIOErrorIn
130         (
131             "LPtrList<LListBase, T>::read(Istream&, const INew&)",
132             is
133         )   << "incorrect first token, expected <int> or '(', found "
134             << firstToken.info()
135             << exit(FatalIOError);
136     }
138     is.fatalCheck("LPtrList<LListBase, T>::read(Istream&, const INew&)");
142 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
144 template<class LListBase, class T>
145 template<class INew>
146 Foam::LPtrList<LListBase, T>::LPtrList(Istream& is, const INew& iNew)
148     read(is, iNew);
152 template<class LListBase, class T>
153 Foam::LPtrList<LListBase, T>::LPtrList(Istream& is)
155     read(is, INew<T>());
159 // * * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * //
161 template<class LListBase, class T>
162 Foam::Istream& Foam::operator>>(Istream& is, LPtrList<LListBase, T>& L)
164     L.clear();
165     L.read(is, INew<T>());
167     return is;
171 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
173 template<class LListBase, class T>
174 Foam::Ostream& Foam::operator<<(Ostream& os, const LPtrList<LListBase, T>& lst)
176     // Write size
177     os << nl << lst.size();
179     // Write beginning of contents
180     os << nl << token::BEGIN_LIST << nl;
182     // Write contents
183     for
184     (
185         typename LPtrList<LListBase, T>::const_iterator iter = lst.begin();
186         iter != lst.end();
187         ++iter
188     )
189     {
190         os << iter() << nl;
191     }
193     // Write end of contents
194     os << token::END_LIST;
196     // Check state of IOstream
197     os.check("Ostream& operator<<(Ostream&, const LPtrList<LListBase, T>&)");
199     return os;
202 // ************************************************************************* //