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 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 firstToken(is);
45 
46  is.fatalCheck
47  (
48  "PtrList::readIstream : "
49  "reading first token"
50  );
51 
52 
53  // Label: could be int(..), int{...} or just a plain '0'
54  if (firstToken.isLabel())
55  {
56  // Read size of list
57  const label len = firstToken.labelToken();
58 
59  // Set list length to that read
60  resize(len);
61 
62  // Read beginning of contents
63  const char delimiter = is.readBeginList("PtrList");
64 
65  if (len)
66  {
67  if (delimiter == token::BEGIN_LIST)
68  {
69  for (label i=0; i<len; ++i)
70  {
71  T* p = inew(is).ptr();
72  set(i, p);
73 
74  is.fatalCheck
75  (
76  "PtrList::readIstream : "
77  "reading entry"
78  );
79  }
80  }
81  else
82  {
83  T* p = inew(is).ptr();
84  set(0, p);
85 
86  is.fatalCheck
87  (
88  "PtrList::readIstream : "
89  "reading the single entry"
90  );
91 
92  for (label i=1; i<len; ++i)
93  {
94  set(i, p->clone());
95  }
96  }
97  }
98 
99  // Read end of contents
100  is.readEndList("PtrList");
101 
102  return;
103  }
104 
105 
106  // "(...)" : read as SLList and transfer contents
107  // This would be more efficient (fewer allocations, lower overhead)
108  // using a DynamicList, but then we have circular dependencies
109  if (firstToken.isPunctuation())
110  {
111  if (firstToken.pToken() != token::BEGIN_LIST)
112  {
114  << "incorrect first token, '(', found " << firstToken.info()
115  << exit(FatalIOError);
116  }
117 
118  SLList<T*> slList;
119 
120  token lastToken(is);
121  while
122  (
123  !(
124  lastToken.isPunctuation()
125  && lastToken.pToken() == token::END_LIST
126  )
127  )
128  {
129  is.putBack(lastToken);
130 
131  if (is.eof())
132  {
134  << "Premature EOF after reading " << lastToken.info()
135  << exit(FatalIOError);
136  }
137 
138  slList.append(inew(is).ptr());
139  is >> lastToken;
140  }
141 
142  resize(slList.size());
143 
144  // A list of pointers - can simply shallow copy them
145  label i = 0;
146  for (T* ptr : slList)
147  {
148  set(i++, ptr);
149  }
150 
151  return;
152  }
153 
155  << "incorrect first token, expected <int> or '(', found "
156  << firstToken.info()
157  << exit(FatalIOError);
158 }
159 
160 
161 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
162 
163 template<class T>
164 template<class INew>
166 {
167  this->readIstream(is, inew);
168 }
169 
170 
171 template<class T>
173 {
174  this->readIstream(is, INew<T>());
175 }
176 
177 
178 // * * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * //
179 
180 template<class T>
182 {
183  list.readIstream(is, INew<T>());
184  return is;
185 }
186 
187 
188 // ************************************************************************* //
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:487
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:481
Foam::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:230
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:57
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:167
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::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:459
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:551
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
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:453
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:62
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()
SLList.H
Non-intrusive singly-linked list.
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
PtrList.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401