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())
37  {
38  // Reading
39  }
40  else if (isReadOptional())
41  {
42  if (!headerOk())
43  {
44  readOnProc = false;
45  }
46  }
47  else
48  {
49  return false;
50  }
51 
52 
53  // Do reading
54  Istream& is = readStream(typeName, readOnProc);
55 
56  if (readOnProc)
57  {
58  is >> *this;
59  }
60  close();
61  return true;
62 }
63 
64 
65 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
66 
67 template<class Type>
69 :
71 {
72  // Check for MUST_READ_IF_MODIFIED
73  warnNoRereading<IOField<Type>>();
74 
75  readIOcontents();
76 }
77 
78 
79 template<class Type>
80 Foam::IOField<Type>::IOField(const IOobject& io, const bool readOnProc)
81 :
83 {
84  // Check for MUST_READ_IF_MODIFIED
85  warnNoRereading<IOField<Type>>();
86 
87  readIOcontents(readOnProc);
88 }
89 
90 
91 template<class Type>
93 :
95 {
96  // Check for MUST_READ_IF_MODIFIED
97  warnNoRereading<IOField<Type>>();
98 
99  readIOcontents();
100 }
101 
102 
103 template<class Type>
104 Foam::IOField<Type>::IOField(const IOobject& io, const label len)
105 :
106  regIOobject(io)
107 {
108  // Check for MUST_READ_IF_MODIFIED
109  warnNoRereading<IOField<Type>>();
110 
111  if (!readIOcontents())
112  {
113  Field<Type>::resize(len);
114  }
115 }
116 
117 
118 template<class Type>
119 Foam::IOField<Type>::IOField(const IOobject& io, const UList<Type>& content)
120 :
121  regIOobject(io)
122 {
123  // Check for MUST_READ_IF_MODIFIED
124  warnNoRereading<IOField<Type>>();
125 
126  if (!readIOcontents())
127  {
128  Field<Type>::operator=(content);
129  }
130 }
131 
132 
133 template<class Type>
134 Foam::IOField<Type>::IOField(const IOobject& io, Field<Type>&& content)
135 :
136  regIOobject(io)
137 {
138  // Check for MUST_READ_IF_MODIFIED
139  warnNoRereading<IOField<Type>>();
140 
142 
143  readIOcontents();
144 }
145 
146 
147 template<class Type>
149 :
150  regIOobject(io)
151 {
152  const bool reuse = tfld.movable();
153 
154  if (reuse)
155  {
156  Field<Type>::transfer(tfld.ref());
157  }
158 
159  if (!readIOcontents() && !reuse)
160  {
161  Field<Type>::operator=(tfld());
162  }
164  tfld.clear();
165 }
166 
167 
168 template<class Type>
170 (
171  const IOobject& io,
172  const Field<Type>& content
173 )
174 :
175  regIOobject(io),
176  contentRef_(content) // cref
177 {}
178 
179 
180 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
181 
182 template<class Type>
184 {
186  if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
187  {
188  rio.readOpt(IOobjectOption::MUST_READ);
189  }
190 
191  IOField<Type> reader(rio);
192 
193  return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
194 }
195 
196 
197 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
198 
199 template<class Type>
201 {
202  os << static_cast<const Field<Type>&>(*this);
203  return os.good();
204 }
205 
206 
207 template<class Type>
209 {
210  os << contentRef_.cref();
211  return os.good();
212 }
213 
214 
215 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
216 
217 template<class Type>
219 {
221 }
222 
223 
224 // ************************************************************************* //
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:193
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:211
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:201
static Field< Type > readContents(const IOobject &io)
Read and return contents. The IOobject will not be registered.
Definition: IOField.C:176
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)