LPtrListIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2017-2018 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "LPtrList.H"
30 #include "Istream.H"
31 #include "Ostream.H"
32 #include "INew.H"
33 
34 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
35 
36 template<class LListBase, class T>
37 template<class INew>
38 void Foam::LPtrList<LListBase, T>::readIstream(Istream& is, const INew& inew)
39 {
40  is.fatalCheck(FUNCTION_NAME);
41 
42  token firstToken(is);
43 
44  is.fatalCheck
45  (
46  "LPtrList::readIstream : "
47  "reading first token"
48  );
49 
50  if (firstToken.isLabel())
51  {
52  const label len = firstToken.labelToken();
53 
54  // Read beginning of contents
55  const char delimiter = is.readBeginList("LPtrList");
56 
57  if (len)
58  {
59  if (delimiter == token::BEGIN_LIST)
60  {
61  for (label i=0; i<len; ++i)
62  {
63  T* p = inew(is).ptr();
64  this->append(p);
65 
66  is.fatalCheck
67  (
68  "LPtrList::readIstream : "
69  "reading entry"
70  );
71  }
72  }
73  else
74  {
75  T* p = inew(is).ptr();
76  this->append(p);
77 
78  is.fatalCheck
79  (
80  "LPtrList::readIstream : "
81  "reading the single entry"
82  );
83 
84  for (label i=1; i<len; ++i)
85  {
86  this->append(p->clone().ptr());
87  }
88  }
89  }
90 
91  // Read end of contents
92  is.readEndList("LPtrList");
93  }
94  else if (firstToken.isPunctuation())
95  {
96  if (firstToken.pToken() != token::BEGIN_LIST)
97  {
99  << "incorrect first token, '(', found " << firstToken.info()
100  << exit(FatalIOError);
101  }
102 
103  token lastToken(is);
104  is.fatalCheck(FUNCTION_NAME);
105 
106  while
107  (
108  !(
109  lastToken.isPunctuation()
110  && lastToken.pToken() == token::END_LIST
111  )
112  )
113  {
114  is.putBack(lastToken);
115  this->append(inew(is).ptr());
116 
117  is >> lastToken;
118  is.fatalCheck(FUNCTION_NAME);
119  }
120  }
121  else
122  {
124  << "incorrect first token, expected <int> or '(', found "
125  << firstToken.info()
126  << exit(FatalIOError);
127  }
128 
129  is.fatalCheck(FUNCTION_NAME);
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
134 
135 template<class LListBase, class T>
136 template<class INew>
138 {
139  this->readIstream(is, inew);
140 }
141 
142 
143 template<class LListBase, class T>
145 {
146  this->readIstream(is, INew<T>());
147 }
148 
149 
150 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
151 
152 template<class LListBase, class T>
154 {
155  list.clear();
156  list.readIstream(is, INew<T>());
157 
158  return is;
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
163 
164 template<class LListBase, class T>
165 Foam::Ostream& Foam::operator<<
166 (
167  Ostream& os,
168  const LPtrList<LListBase, T>& list
169 )
170 {
171  // Size and start delimiter
172  os << nl << list.size() << nl << token::BEGIN_LIST << nl;
173 
174  // Contents
175  for (auto iter = list.cbegin(); iter != list.cend(); ++iter)
176  {
177  os << *iter << nl;
178  }
179 
180  // End delimiter
181  os << token::END_LIST;
182 
183  os.check(FUNCTION_NAME);
184  return os;
185 }
186 
187 // ************************************************************************* //
Foam::LPtrList::cbegin
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LPtrList.H:352
p
volScalarField & p
Definition: createFieldRefs.H:8
INew.H
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::LPtrList
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:50
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:51
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Istream.H
LPtrList.H
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:265
Foam::LPtrList::cend
const const_iterator & cend() const
End of list for forward iterators.
Definition: LPtrList.H:389
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:392
Foam::LPtrList::LPtrList
LPtrList()=default
Null construct.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::LPtrList::clear
void clear()
Clear the contents of the list.
Definition: LPtrList.C:80