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-2021 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 "SLList.H"
31#include "Istream.H"
32#include "INew.H"
33
34// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
35
36template<class T>
37template<class INew>
39{
40 clear(); // Delete old pointers and reset the list size
41
43
44 token tok(is);
45
46 is.fatalCheck("PtrList::readIstream : reading first token");
47
48 if (tok.isLabel())
49 {
50 // Label: could be int(..), int{...} or just a plain '0'
51
52 // Read size of list
53 const label len = tok.labelToken();
54
55 // Set list length to that read
56 resize(len);
57
58 // Read beginning of contents
59 const char delimiter = is.readBeginList("PtrList");
60
61 if (len)
62 {
63 if (delimiter == token::BEGIN_LIST)
64 {
65 for (label i=0; i<len; ++i)
66 {
67 T* p = inew(is).ptr();
68 set(i, p);
69
70 is.fatalCheck
71 (
72 "PtrList::readIstream : "
73 "reading entry"
74 );
75 }
76 }
77 else // Assumed to be BEGIN_BLOCK
78 {
79 T* p = inew(is).ptr();
80 set(0, p);
81
82 is.fatalCheck
83 (
84 "PtrList::readIstream : "
85 "reading the single entry"
86 );
87
88 for (label i=1; i<len; ++i)
89 {
90 set(i, p->clone());
91 }
92 }
93 }
94
95 // Read end of contents
96 is.readEndList("PtrList");
97 }
98 else if (tok.isPunctuation(token::BEGIN_LIST))
99 {
100 // "(...)" : read as SLList and transfer contents
101 // This would be more efficient (fewer allocations, lower overhead)
102 // using a DynamicList, but then we have circular dependencies
103
104 SLList<T*> slList;
105
106 is >> tok;
107 while (!tok.isPunctuation(token::END_LIST))
108 {
109 is.putBack(tok);
110
111 if (is.eof())
112 {
114 << "Premature EOF after reading " << tok.info() << nl
115 << exit(FatalIOError);
117
118 slList.append(inew(is).ptr());
119 is >> tok;
120 }
121
122 resize(slList.size());
123
124 // A list of pointers - can simply shallow copy them
125 label i = 0;
126 for (T* ptr : slList)
127 {
128 set(i++, ptr);
129 }
130 }
131 else
132 {
134 << "incorrect first token, expected <int> or '(', found "
135 << tok.info() << nl
136 << exit(FatalIOError);
137 }
138}
139
140
141// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
142
143template<class T>
144template<class INew>
146{
147 this->readIstream(is, inew);
148}
149
150
151template<class T>
153{
154 this->readIstream(is, INew<T>());
155}
156
157
158// * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
159
160template<class T>
162{
163 list.readIstream(is, INew<T>());
164 return is;
165}
166
167
168// ************************************************************************* //
Non-intrusive singly-linked list.
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:52
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
A token holds an item read from Istream.
Definition: token.H:69
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
label labelToken() const
Return label value.
Definition: tokenI.H:513
volScalarField & p
const volScalarField & T
patchWriters resize(patchIds.size())
patchWriters clear()
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FUNCTION_NAME
Istream & operator>>(Istream &, directionInfo &)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53