Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
35template<class T>
36template<class INew>
37void 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 }
144
145
146// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
147
148template<class T>
149template<class INew>
151{
152 this->readIstream(is, inew);
153}
154
155
156template<class T>
158{
159 this->readIstream(is, INew<T>());
160}
161
162
163// * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
164
165template<class T>
166Foam::Istream& Foam::operator>>(Istream& is, PtrList<T>& list)
167{
168 list.readIstream(is, INew<T>());
169 return is;
170}
171
172
173// ************************************************************************* //
A helper class when constructing from an Istream or dictionary.
Definition INew.H:47
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:51
bool eof() const noexcept
True if end of input seen.
Definition IOstream.H:289
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition Istream.C:192
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition Istream.C:171
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition Istream.C:71
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie,...
Definition PtrList.H:171
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition PtrListIO.C:30
constexpr PtrList() noexcept
Default construct.
Definition PtrListI.H:29
A token holds an item read from Istream.
Definition token.H:70
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition tokenI.H:650
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
bool isLabel() const noexcept
Integral token is convertible to Foam::label.
Definition tokenI.H:843
label labelToken() const
Return integer type as label value or Error.
Definition tokenI.H:869
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition token.H:1253
volScalarField & p
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
surface1 clear()
#define FUNCTION_NAME
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50