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-2024 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 "IOField.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
34 bool 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 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
55 template<class Type>
57 :
59 {
60  // Check for MUST_READ_IF_MODIFIED
61  warnNoRereading<IOField<Type>>();
62 
63  readIOcontents();
64 }
65 
66 
67 template<class Type>
68 Foam::IOField<Type>::IOField(const IOobject& io, const bool readOnProc)
69 :
71 {
72  // Check for MUST_READ_IF_MODIFIED
73  warnNoRereading<IOField<Type>>();
74 
75  readIOcontents(readOnProc);
76 }
77 
78 
79 template<class Type>
81 :
83 {
84  // Check for MUST_READ_IF_MODIFIED
85  warnNoRereading<IOField<Type>>();
86 
87  readIOcontents();
88 }
89 
90 
91 template<class Type>
92 Foam::IOField<Type>::IOField(const IOobject& io, const label len)
93 :
95 {
96  // Check for MUST_READ_IF_MODIFIED
97  warnNoRereading<IOField<Type>>();
98 
99  if (!readIOcontents())
100  {
101  Field<Type>::resize(len);
102  }
103 }
104 
105 
106 template<class Type>
107 Foam::IOField<Type>::IOField(const IOobject& io, const UList<Type>& content)
108 :
109  regIOobject(io)
110 {
111  // Check for MUST_READ_IF_MODIFIED
112  warnNoRereading<IOField<Type>>();
113 
114  if (!readIOcontents())
115  {
116  Field<Type>::operator=(content);
117  }
118 }
119 
120 
121 template<class Type>
122 Foam::IOField<Type>::IOField(const IOobject& io, Field<Type>&& content)
123 :
124  regIOobject(io)
125 {
126  // Check for MUST_READ_IF_MODIFIED
127  warnNoRereading<IOField<Type>>();
128 
130 
131  readIOcontents();
132 }
133 
134 
135 template<class Type>
137 :
138  regIOobject(io)
139 {
140  const bool reuse = tfld.movable();
141 
142  if (reuse)
143  {
144  Field<Type>::transfer(tfld.ref());
145  }
146 
147  if (!readIOcontents() && !reuse)
148  {
149  Field<Type>::operator=(tfld());
150  }
152  tfld.clear();
153 }
154 
155 
156 template<class Type>
158 (
159  const IOobject& io,
160  const Field<Type>& content
161 )
162 :
163  regIOobject(io),
164  contentRef_(content) // cref
165 {}
166 
167 
168 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
169 
170 template<class Type>
172 {
174  if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
175  {
176  rio.readOpt(IOobjectOption::MUST_READ);
177  }
178 
179  IOField<Type> reader(rio);
180 
181  return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
182 }
183 
184 
185 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
186 
187 template<class Type>
189 {
190  os << static_cast<const Field<Type>&>(*this);
191  return os.good();
192 }
193 
194 
195 template<class Type>
197 {
198  os << contentRef_.cref();
199  return os.good();
200 }
201 
202 
203 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
204 
205 template<class Type>
207 {
209 }
210 
211 
212 // ************************************************************************* //
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:153
void transfer(List< Type > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
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:181
Generic templated field type.
Definition: Field.H:62
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void operator=(const IOField< Type > &rhs)
Copy assignment of entries.
Definition: IOField.C:199
OBJstream os(runTime.globalPath()/outputName)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:747
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: IOField.C:189
static Field< Type > readContents(const IOobject &io)
Read and return contents. The IOobject will not be registered.
Definition: IOField.C:164
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
A primitive field of type <T> with automated input and output.
Do not request registration (bool: false)