Loading...
Searching...
No Matches
IOField.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-2017 OpenFOAM Foundation
9 Copyright (C) 2016-2025 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 "IOField.H"
30
31// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32
33template<class Type>
34bool Foam::IOField<Type>::readIOcontents(bool readOnProc)
35{
36 if (isReadRequired() || (isReadOptional() && headerOk()))
37 {
38 // Do reading
39 Istream& is = readStream(typeName, readOnProc);
40
41 if (readOnProc)
42 {
43 is >> *this;
44 }
45 close();
46 return true;
47 }
48
49 return false;
50}
51
52
53template<class T>
54Foam::label Foam::IOField<T>::readIOsize()
55{
56 label count(-1);
57
58 if (isReadRequired() || (isReadOptional() && headerOk()))
59 {
60 Istream& is = readStream(typeName);
61
62 token tok(is);
63
64 const bool quick = tok.isLabel();
65
66 if (quick)
67 {
68 // The majority of files will have lists with sizing info
69 count = tok.labelToken();
70 }
71 is.putBack(tok);
72
73 if (!quick)
74 {
75 List<T> list(is);
76 close();
77 count = list.size();
78 }
79 }
80
81 return count;
82}
83
84
85// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86
87template<class Type>
89:
91{
92 // Check for MUST_READ_IF_MODIFIED
94
95 readIOcontents();
96}
97
98
99template<class Type>
100Foam::IOField<Type>::IOField(const IOobject& io, const bool readOnProc)
101:
103{
104 // Check for MUST_READ_IF_MODIFIED
106
107 readIOcontents(readOnProc);
108}
109
110
111template<class Type>
113:
115{
116 // Check for MUST_READ_IF_MODIFIED
118
119 readIOcontents();
120}
121
122
123template<class Type>
124Foam::IOField<Type>::IOField(const IOobject& io, const label len)
125:
127{
128 // Check for MUST_READ_IF_MODIFIED
130
131 if (!readIOcontents())
134 }
135}
136
137
138template<class Type>
139Foam::IOField<Type>::IOField(const IOobject& io, const UList<Type>& content)
140:
141 regIOobject(io)
142{
143 // Check for MUST_READ_IF_MODIFIED
145
146 if (!readIOcontents())
148 Field<Type>::operator=(content);
149 }
150}
151
152
153template<class Type>
154Foam::IOField<Type>::IOField(const IOobject& io, Field<Type>&& content)
155:
156 regIOobject(io)
157{
158 // Check for MUST_READ_IF_MODIFIED
160
162
163 readIOcontents();
164}
165
166
167template<class Type>
169:
171{
172 const bool reuse = tfld.movable();
173
174 if (reuse)
175 {
176 Field<Type>::transfer(tfld.ref());
177 }
178
179 if (!readIOcontents() && !reuse)
180 {
183
184 tfld.clear();
185}
186
187
188template<class Type>
190(
191 const IOobject& io,
192 const Field<Type>& content
193)
194:
195 regIOobject(io),
196 contentRef_(content) // cref
197{}
198
199
200// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
201
202template<class Type>
204{
206 if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
207 {
208 rio.readOpt(IOobjectOption::MUST_READ);
209 }
210 rio.resetHeader();
211
212 // Construct NO_READ, changing after construction
213 const auto rOpt = rio.readOpt(IOobjectOption::NO_READ);
214
215 IOField<Type> reader(rio);
216 reader.readOpt(rOpt);
217
218 return reader.readIOsize();
219}
220
221
222template<class Type>
224{
227 {
229 }
230 rio.resetHeader();
231
232 IOField<Type> reader(rio);
233
234 return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
235}
236
237
238// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
239
240template<class Type>
244 return os.good();
245}
246
247
248template<class Type>
250{
251 os << contentRef_.cref();
252 return os.good();
253}
254
255
256// ************************************************************************* //
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
void operator=(const Field< Type > &)
Copy assignment.
Definition Field.C:781
constexpr Field() noexcept
Default construct.
Definition FieldI.H:24
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition IOField.C:242
IOFieldRef()=delete
No default construct.
IOField(const IOField &)=default
Default copy construct.
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition IOField.C:234
static Field< Type > readContents(const IOobject &io)
Read and return contents. The IOobject will not be registered.
Definition IOField.C:216
static label readContentsSize(const IOobject &io)
Read and return content size, -1 if not read.
Definition IOField.C:196
@ NO_REGISTER
Do not request registration (bool: false).
readOption readOpt() const noexcept
Get the read option.
@ NO_READ
Nothing to be read.
@ MUST_READ
Reading required.
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
void warnNoRereading() const
Helper: warn that type does not support re-reading.
void resetHeader(const word &newName=word::null)
Clear various bits (headerClassName, note, sizeof...) that would be obtained when reading from a file...
Definition IOobject.C:644
void transfer(List< Type > &list)
void resize(const label len)
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
A class for managing temporary objects.
Definition tmp.H:75
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
const auto & io
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73