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-2022 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>
35 {
36  if (isReadRequired() || (isReadOptional() && headerOk()))
37  {
38  readStream(typeName) >> *this;
39  close();
40  return true;
41  }
42 
43  return false;
44 }
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
49 template<class Type>
51 :
53 {
54  // Check for MUST_READ_IF_MODIFIED
55  warnNoRereading<IOField<Type>>();
56 
57  readContents();
58 }
59 
60 
61 template<class Type>
62 Foam::IOField<Type>::IOField(const IOobject& io, const bool valid)
63 :
65 {
66  // Check for MUST_READ_IF_MODIFIED
67  warnNoRereading<IOField<Type>>();
68 
69  if (isReadRequired())
70  {
71  Istream& is = readStream(typeName, valid);
72 
73  if (valid)
74  {
75  is >> *this;
76  }
77  close();
78  }
79  else if (isReadOptional())
80  {
81  bool haveFile = headerOk();
82 
83  Istream& is = readStream(typeName, haveFile && valid);
84 
85  if (valid && haveFile)
86  {
87  is >> *this;
88  }
89  close();
90  }
91 }
92 
93 
94 template<class Type>
96 :
97  regIOobject(io)
98 {
99  // Check for MUST_READ_IF_MODIFIED
100  warnNoRereading<IOField<Type>>();
101 
102  readContents();
103 }
104 
105 
106 template<class Type>
107 Foam::IOField<Type>::IOField(const IOobject& io, const label len)
108 :
109  regIOobject(io)
110 {
111  // Check for MUST_READ_IF_MODIFIED
112  warnNoRereading<IOField<Type>>();
113 
114  if (!readContents())
115  {
116  Field<Type>::resize(len);
117  }
118 }
119 
120 
121 template<class Type>
122 Foam::IOField<Type>::IOField(const IOobject& io, const UList<Type>& content)
123 :
124  regIOobject(io)
125 {
126  // Check for MUST_READ_IF_MODIFIED
127  warnNoRereading<IOField<Type>>();
128 
129  if (!readContents())
130  {
131  Field<Type>::operator=(content);
132  }
133 }
134 
135 
136 template<class Type>
137 Foam::IOField<Type>::IOField(const IOobject& io, Field<Type>&& content)
138 :
139  regIOobject(io)
140 {
141  // Check for MUST_READ_IF_MODIFIED
142  warnNoRereading<IOField<Type>>();
143 
145 
146  readContents();
147 }
148 
149 
150 template<class Type>
152 :
153  regIOobject(io)
154 {
155  const bool reuse = tfld.movable();
156 
157  if (reuse)
158  {
159  Field<Type>::transfer(tfld.ref());
160  }
161 
162  if (!readContents() && !reuse)
163  {
164  Field<Type>::operator=(tfld());
165  }
167  tfld.clear();
168 }
169 
170 
171 template<class Type>
173 (
174  const IOobject& io,
175  const Field<Type>& content
176 )
177 :
178  regIOobject(io),
179  contentRef_(content) // cref
180 {}
181 
182 
183 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
184 
185 template<class Type>
187 {
188  os << static_cast<const Field<Type>&>(*this);
189  return os.good();
190 }
191 
192 
193 template<class Type>
195 {
196  os << contentRef_.cref();
197  return os.good();
198 }
199 
200 
201 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
202 
203 template<class Type>
205 {
207 }
208 
209 
210 // ************************************************************************* //
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:132
void transfer(List< Type > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:439
IOFieldRef()=delete
No default construct.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:434
IOField(const IOField &)=default
Default copy construct.
bool isReadOptional() const noexcept
True if (READ_IF_PRESENT) bits are set.
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: IOField.C:179
static const char *const typeName
Typename for Field.
Definition: Field.H:86
Istream & readStream(const word &, const bool valid=true)
Return Istream and check object type against that given.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
Generic templated field type.
Definition: Field.H:61
void close()
Close Istream.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
void operator=(const IOField< Type > &rhs)
Copy assignment of entries.
Definition: IOField.C:197
OBJstream os(runTime.globalPath()/outputName)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:671
bool isReadRequired() const noexcept
True if (MUST_READ | MUST_READ_IF_MODIFIED) bits are set.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:274
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:69
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:58
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: IOField.C:187
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:166
A primitive field of type <T> with automated input and output.