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-2023 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 void Foam::IOField<Type>::readFromStream(const bool readOnProc)
35 {
36  Istream& is = readStream(typeName, readOnProc);
37 
38  if (readOnProc)
39  {
40  is >> *this;
41  }
42  close();
43 }
44 
45 
46 template<class Type>
48 {
49  if (isReadRequired() || (isReadOptional() && headerOk()))
50  {
51  readFromStream();
52  return true;
53  }
54 
55  return false;
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
61 template<class Type>
63 :
65 {
66  // Check for MUST_READ_IF_MODIFIED
67  warnNoRereading<IOField<Type>>();
68 
69  readContents();
70 }
71 
72 
73 template<class Type>
74 Foam::IOField<Type>::IOField(const IOobject& io, const bool readOnProc)
75 :
77 {
78  // Check for MUST_READ_IF_MODIFIED
79  warnNoRereading<IOField<Type>>();
80 
81  if (isReadRequired())
82  {
83  readFromStream(readOnProc);
84  }
85  else if (isReadOptional())
86  {
87  const bool haveFile = headerOk();
88  readFromStream(readOnProc && haveFile);
89  }
90 }
91 
92 
93 template<class Type>
95 :
96  regIOobject(io)
97 {
98  // Check for MUST_READ_IF_MODIFIED
99  warnNoRereading<IOField<Type>>();
100 
101  readContents();
102 }
103 
104 
105 template<class Type>
106 Foam::IOField<Type>::IOField(const IOobject& io, const label len)
107 :
108  regIOobject(io)
109 {
110  // Check for MUST_READ_IF_MODIFIED
111  warnNoRereading<IOField<Type>>();
112 
113  if (!readContents())
114  {
115  Field<Type>::resize(len);
116  }
117 }
118 
119 
120 template<class Type>
121 Foam::IOField<Type>::IOField(const IOobject& io, const UList<Type>& content)
122 :
123  regIOobject(io)
124 {
125  // Check for MUST_READ_IF_MODIFIED
126  warnNoRereading<IOField<Type>>();
127 
128  if (!readContents())
129  {
130  Field<Type>::operator=(content);
131  }
132 }
133 
134 
135 template<class Type>
136 Foam::IOField<Type>::IOField(const IOobject& io, Field<Type>&& content)
137 :
138  regIOobject(io)
139 {
140  // Check for MUST_READ_IF_MODIFIED
141  warnNoRereading<IOField<Type>>();
142 
144 
145  readContents();
146 }
147 
148 
149 template<class Type>
151 :
152  regIOobject(io)
153 {
154  const bool reuse = tfld.movable();
155 
156  if (reuse)
157  {
158  Field<Type>::transfer(tfld.ref());
159  }
160 
161  if (!readContents() && !reuse)
162  {
163  Field<Type>::operator=(tfld());
164  }
166  tfld.clear();
167 }
168 
169 
170 template<class Type>
172 (
173  const IOobject& io,
174  const Field<Type>& content
175 )
176 :
177  regIOobject(io),
178  contentRef_(content) // cref
179 {}
180 
181 
182 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
183 
184 template<class Type>
186 {
188  if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
189  {
190  rio.readOpt(IOobjectOption::MUST_READ);
191  }
192 
193  IOField<Type> reader(rio);
194 
195  return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
196 }
197 
198 
199 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
200 
201 template<class Type>
203 {
204  os << static_cast<const Field<Type>&>(*this);
205  return os.good();
206 }
207 
208 
209 template<class Type>
211 {
212  os << contentRef_.cref();
213  return os.good();
214 }
215 
216 
217 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
218 
219 template<class Type>
221 {
223 }
224 
225 
226 // ************************************************************************* //
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
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.
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:505
IOField(const IOField &)=default
Default copy construct.
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: IOField.C:195
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:213
OBJstream os(runTime.globalPath()/outputName)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:747
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
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:66
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:203
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:172
A primitive field of type <T> with automated input and output.
Do not request registration (bool: false)