PtrListIO.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-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2022 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 "PtrList.H"
30 #include "Istream.H"
31 #include "INew.H"
32 
33 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
34 
35 template<class T>
36 template<class INew>
37 void Foam::PtrList<T>::readIstream(Istream& is, const INew& inew)
38 {
39  clear(); // Delete old pointers and reset the list size
40 
42 
43  token tok(is);
44 
45  is.fatalCheck("PtrList::readIstream : reading first token");
46 
47  if (tok.isLabel())
48  {
49  // Label: could be int(..), int{...} or just a plain '0'
50 
51  // Read size of list
52  const label len = tok.labelToken();
53 
54  // Set list length to that read
55  resize(len);
56 
57  // Read beginning of contents
58  const char delimiter = is.readBeginList("PtrList");
59 
60  if (len)
61  {
62  if (delimiter == token::BEGIN_LIST)
63  {
64  for (label i=0; i<len; ++i)
65  {
66  T* p = inew(is).ptr();
67  set(i, p);
68 
69  is.fatalCheck
70  (
71  "PtrList::readIstream : "
72  "reading entry"
73  );
74  }
75  }
76  else // Assumed to be BEGIN_BLOCK
77  {
78  T* p = inew(is).ptr();
79  set(0, p);
80 
81  is.fatalCheck
82  (
83  "PtrList::readIstream : "
84  "reading the single entry"
85  );
86 
87  for (label i=1; i<len; ++i)
88  {
89  set(i, p->clone());
90  }
91  }
92  }
93 
94  // Read end of contents
95  is.readEndList("PtrList");
96  }
97  else if (tok.isPunctuation(token::BEGIN_LIST))
98  {
99  // "(...)" : read like a DynamicList
100 
101  // The length read
102  label len = 0;
103 
104  is >> tok;
105  while (!tok.isPunctuation(token::END_LIST))
106  {
107  is.putBack(tok);
108 
109  if (is.eof())
110  {
112  << "Premature EOF after reading " << tok.info() << nl
113  << exit(FatalIOError);
114  }
115 
116  if (!len)
117  {
118  // Initial reserved size (avoid unnecessary doubling)
119  resize(64);
120  }
121  else if (len == this->size())
122  {
123  resize(2*len);
124  }
125 
126  T* p = inew(is).ptr();
127  set(len, p);
128  ++len;
129 
130  is >> tok;
131  }
132 
133  // Set list length to that read
134  resize(len);
135  }
136  else
137  {
139  << "incorrect first token, expected <int> or '(', found "
140  << tok.info() << nl
141  << exit(FatalIOError);
142  }
143 }
144 
145 
146 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
147 
148 template<class T>
149 template<class INew>
151 {
152  this->readIstream(is, inew);
153 }
154 
155 
156 template<class T>
158 {
159  this->readIstream(is, INew<T>());
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
164 
165 template<class T>
166 Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& list)
167 {
168  list.readIstream(is, INew<T>());
169  return is;
170 }
171 
172 
173 // ************************************************************************* //
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:561
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
patchWriters resize(patchIds.size())
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:192
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A token holds an item read from Istream.
Definition: token.H:65
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:30
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:29
Istream & operator>>(Istream &, directionInfo &)
patchWriters clear()
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
#define FUNCTION_NAME
const volScalarField & T
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
label labelToken() const
Return label value.
Definition: tokenI.H:615
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1078
volScalarField & p
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:289
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...