regIOobjectRead.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-2018 OpenFOAM Foundation
9 Copyright (C) 2015-2021 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 "regIOobject.H"
30#include "IFstream.H"
31#include "Time.H"
32#include "Pstream.H"
33#include "HashSet.H"
34#include "fileOperation.H"
35
36// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
37
39(
41 const word& typeName
42)
43{
44 // Everyone check or just master
45 const bool masterOnly
46 (
47 global()
48 && (
51 )
52 );
53
54
55 // Check if header is ok for READ_IF_PRESENT
56 bool isHeaderOk = false;
58 {
59 if (masterOnly)
60 {
61 if (Pstream::master())
62 {
63 isHeaderOk = headerOk();
64 }
65 Pstream::broadcast(isHeaderOk);
66 }
67 else
68 {
69 isHeaderOk = headerOk();
70 }
71 }
72
73 if
74 (
75 (
78 )
79 || isHeaderOk
80 )
81 {
82 return fileHandler().read(*this, masterOnly, fmt, typeName);
83 }
84
85 return false;
86}
87
88
89// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90
91void Foam::regIOobject::readStream(const bool valid)
92{
93 if (readOpt() == NO_READ)
94 {
96 << "NO_READ specified for read-constructor of object " << name()
97 << " of class " << headerClassName()
98 << abort(FatalError);
99 }
100
101 // Construct object stream and read header if not already constructed
102 if (!isPtr_)
103 {
104 fileName objPath;
105 if (watchIndices_.size())
106 {
107 // File is being watched. Read exact file that is being watched.
108 objPath = fileHandler().getFile(watchIndices_.last());
109 }
110 else
111 {
112 // Search intelligently for file
113 objPath = filePath();
114
115 if (IFstream::debug)
116 {
117 Pout<< "regIOobject::readStream() : "
118 << "found object " << name()
119 << " (global " << global() << ")"
120 << " in file " << objPath
121 << endl;
122 }
123 }
124
125 isPtr_ = fileHandler().readStream(*this, objPath, type(), valid);
126 }
127}
128
129
130Foam::Istream& Foam::regIOobject::readStream
131(
132 const word& expectName,
133 const bool valid
134)
135{
136 if (IFstream::debug)
137 {
138 Pout<< "regIOobject::readStream(const word&) : "
139 << "reading object " << name()
140 << " of type " << type()
141 << " from file " << filePath()
142 << endl;
143 }
144
145 // Construct IFstream if not already constructed
146 if (!isPtr_)
147 {
148 readStream(valid);
149
150 // Check the className of the regIOobject
151 // dictionary is an allowable name in case the actual class
152 // instantiated is a dictionary
153 if
154 (
155 valid
156 && expectName.size()
157 && headerClassName() != expectName
158 && headerClassName() != "dictionary"
159 )
160 {
161 FatalIOErrorInFunction(isPtr_())
162 << "unexpected class name " << headerClassName()
163 << " expected " << expectName << endl
164 << " while reading object " << name()
165 << exit(FatalIOError);
166 }
167 }
168
169 return *isPtr_;
170}
171
172
174{
175 if (IFstream::debug)
176 {
177 Pout<< "regIOobject::close() : "
178 << "finished reading "
179 << (isPtr_ ? isPtr_->name() : "dummy")
180 << endl;
181 }
182
183 isPtr_.reset(nullptr);
184}
185
186
188{
189 return false;
190}
191
192
194{
195 // Note: cannot do anything in readStream itself since this is used by
196 // e.g. GeometricField.
197
198
199 // Save old watchIndices and clear (so the list of included files can
200 // change)
201 fileNameList oldWatchFiles;
202 if (watchIndices_.size())
203 {
204 oldWatchFiles.setSize(watchIndices_.size());
205 forAll(watchIndices_, i)
206 {
207 oldWatchFiles[i] = fileHandler().getFile(watchIndices_[i]);
208 }
209 forAllReverse(watchIndices_, i)
210 {
211 fileHandler().removeWatch(watchIndices_[i]);
212 }
213 watchIndices_.clear();
214 }
215
216
217 // Read
218 const bool masterOnly
219 (
220 global()
221 && (
224 )
225 );
226
227 // Note: IOstream::binary flag is for all the processor comms. (Only for
228 // dictionaries should it be ascii)
229 bool ok = fileHandler().read(*this, masterOnly, IOstream::BINARY, type());
230
231 if (oldWatchFiles.size())
232 {
233 // Re-watch master file
234 addWatch();
235 }
236
237 return ok;
238}
239
240
242{
243 forAllReverse(watchIndices_, i)
244 {
245 if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
246 {
247 return true;
248 }
249 }
250
251 return false;
252}
253
254
256{
257 // Get index of modified file so we can give nice message. Could instead
258 // just call above modified()
259 label modified = -1;
260 forAllReverse(watchIndices_, i)
261 {
262 if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
263 {
264 modified = watchIndices_[i];
265 break;
266 }
267 }
268
269 if (modified != -1)
270 {
271 const fileName fName = fileHandler().getFile(watchIndices_.last());
272
273 if (modified == watchIndices_.last())
274 {
276 << " Re-reading object " << name()
277 << " from file " << fName << endl;
278 }
279 else
280 {
282 << " Re-reading object " << name()
283 << " from file " << fName
284 << " because of modified file "
285 << fileHandler().getFile(modified)
286 << endl;
287 }
288
289 return read();
290 }
291
292 return false;
293}
294
295
296// ************************************************************************* //
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:303
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:180
streamFormat
Data format (ascii | binary)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void setSize(const label n)
Alias for resize()
Definition: List.H:218
static void broadcast(Type &value, const label comm=UPstream::worldComm)
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A class for handling file names.
Definition: fileName.H:76
virtual bool removeWatch(const label) const
Remove watch on a file (using handle)
virtual bool read(regIOobject &, const bool masterOnly, const IOstreamOption::streamFormat format, const word &typeName) const =0
Top-level read.
virtual autoPtr< ISstream > readStream(regIOobject &, const fileName &, const word &typeName, const bool valid=true) const =0
Reads header for regIOobject and returns an ISstream.
virtual fileName getFile(const label) const
Get name of file being watched (using handle)
virtual bool modified() const
virtual bool global() const
Is object global.
Definition: regIOobject.H:359
void close()
Close Istream.
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:438
virtual bool readIfModified()
Read object if modified (as set by call to modified)
virtual bool readData(Istream &)
Virtual readData function.
bool readHeaderOk(const IOstreamOption::streamFormat fmt, const word &typeName)
Helper: check readOpt flags and read if necessary.
virtual bool read()
Read object.
splitCell * master() const
Definition: splitCell.H:113
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define InfoInFunction
Report an information message using Foam::Info.
const fileOperation & fileHandler()
Get current file handler.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
IOerror FatalIOError
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:346