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 -------------------------------------------------------------------------------
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 "SLList.H"
31 #include "Istream.H"
32 #include "INew.H"
33 
34 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
35 
36 template<class T>
37 template<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);
116  }
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 
143 template<class T>
144 template<class INew>
146 {
147  this->readIstream(is, inew);
148 }
149 
150 
151 template<class T>
153 {
154  this->readIstream(is, INew<T>());
155 }
156 
157 
158 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
159 
160 template<class T>
162 {
163  list.readIstream(is, INew<T>());
164  return is;
165 }
166 
167 
168 // ************************************************************************* //
Foam::LList::append
void append(const T &item)
Add copy at tail of list.
Definition: LList.H:237
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
INew.H
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:51
resize
patchWriters resize(patchIds.size())
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstream::eof
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
Foam::PtrList::PtrList
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
Foam::PtrList::readIstream
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Istream.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
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
SLList.H
Non-intrusive singly-linked list.
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
PtrList.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473