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-2021 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 tok(is);
43 
44  is.fatalCheck
45  (
46  "LPtrList::readIstream : "
47  "reading first token"
48  );
49 
50  if (tok.isLabel())
51  {
52  const label len = tok.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 // Assumed to be token::BEGIN_BLOCK
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 (tok.isPunctuation(token::BEGIN_LIST))
95  {
96  is >> tok;
97  is.fatalCheck(FUNCTION_NAME);
98 
99  while (!tok.isPunctuation(token::END_LIST))
100  {
101  is.putBack(tok);
102  this->append(inew(is).ptr());
103 
104  is >> tok;
105  is.fatalCheck(FUNCTION_NAME);
106  }
107  }
108  else
109  {
111  << "incorrect first token, expected <int> or '(', found "
112  << tok.info() << nl
113  << exit(FatalIOError);
114  }
115 
116  is.fatalCheck(FUNCTION_NAME);
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
121 
122 template<class LListBase, class T>
123 template<class INew>
125 {
126  this->readIstream(is, inew);
127 }
128 
129 
130 template<class LListBase, class T>
132 {
133  this->readIstream(is, INew<T>());
134 }
135 
136 
137 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
138 
139 template<class LListBase, class T>
141 {
142  list.clear();
143  list.readIstream(is, INew<T>());
144 
145  return is;
146 }
147 
148 
149 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
150 
151 template<class LListBase, class T>
152 Foam::Ostream& Foam::operator<<
153 (
154  Ostream& os,
155  const LPtrList<LListBase, T>& list
156 )
157 {
158  // Size and start delimiter
159  os << nl << list.size() << nl << token::BEGIN_LIST << nl;
160 
161  // Contents
162  for (auto iter = list.cbegin(); iter != list.cend(); ++iter)
163  {
164  os << *iter << nl;
165  }
166 
167  // End delimiter
168  os << token::END_LIST;
169 
170  os.check(FUNCTION_NAME);
171  return os;
172 }
173 
174 // ************************************************************************* //
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
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
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
os
OBJstream os(runTime.globalPath()/outputName)
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:404
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::LPtrList::cend
const const_iterator & cend() const
End of list for forward iterators.
Definition: LPtrList.H:389
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
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