Loading...
Searching...
No Matches
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-2022 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()
49 );
50
51
52 // Check if header is ok for READ_IF_PRESENT
53 bool isHeaderOk = false;
54 if (isReadOptional())
55 {
56 if (masterOnly)
57 {
58 if (UPstream::master())
59 {
60 const bool oldParRun = UPstream::parRun(false);
61 isHeaderOk = headerOk();
62 UPstream::parRun(oldParRun);
63 }
64 Pstream::broadcast(isHeaderOk);
65 }
66 else
67 {
68 isHeaderOk = headerOk();
69 }
70 }
71
72 if (isReadRequired() || isHeaderOk)
73 {
74 return fileHandler().read(*this, masterOnly, fmt, typeName);
75 }
76
77 return false;
78}
79
80
81// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
82
83void Foam::regIOobject::readStream(const bool readOnProc)
84{
85 if (readOpt() == IOobject::NO_READ)
86 {
88 << "NO_READ specified for read-constructor of object " << name()
89 << " of class " << headerClassName()
90 << abort(FatalError);
91 }
92
93 // Construct object stream and read header if not already constructed
94 if (!isPtr_)
95 {
96 fileName objPath;
97 if (watchIndices_.size())
98 {
99 // File is being watched. Read exact file that is being watched.
100 objPath = fileHandler().getFile(watchIndices_.back());
101 }
102 else
103 {
104 // Search intelligently for file
105 objPath = filePath();
106
107 if (IFstream::debug)
108 {
109 Pout<< "regIOobject::readStream() : "
110 << "found object " << name()
111 << " (global " << global() << ")"
112 << " in file " << objPath
113 << endl;
114 }
116
117 isPtr_ = fileHandler().readStream(*this, objPath, type(), readOnProc);
118 }
119}
120
121
122Foam::Istream& Foam::regIOobject::readStream
123(
124 const word& expectName,
125 const bool readOnProc
126)
127{
128 if (IFstream::debug)
129 {
130 Pout<< "regIOobject::readStream(const word&) : "
131 << "reading object " << name()
132 << " of type " << type()
133 << " from file " << filePath()
134 << endl;
135 }
136
137 // Construct IFstream if not already constructed
138 if (!isPtr_)
139 {
140 readStream(readOnProc);
141
142 // Check the className of the regIOobject
143 // dictionary is an allowable name in case the actual class
144 // instantiated is a dictionary
145 if
146 (
147 readOnProc
148 && expectName.size()
149 && headerClassName() != expectName
150 && headerClassName() != "dictionary"
151 )
152 {
153 FatalIOErrorInFunction(isPtr_())
154 << "unexpected class name " << headerClassName()
155 << " expected " << expectName << endl
156 << " while reading object " << name()
157 << exit(FatalIOError);
159 }
160
161 return *isPtr_;
162}
163
164
166{
167 if (IFstream::debug)
168 {
169 Pout<< "regIOobject::close() : "
170 << "finished reading "
171 << (isPtr_ ? isPtr_->name() : "dummy")
173 }
174
175 isPtr_.reset(nullptr);
176}
177
180{
181 return false;
182}
183
184
186{
187 // Note: cannot do anything in readStream itself since this is used by
188 // e.g. GeometricField.
189
190 // Everyone or just master
191 const bool masterOnly
192 (
193 global()
195 );
196
197 // Remove old watches (indices) and clear:
198 // so the list of included files can change
199
200 const bool needWatch(!watchIndices_.empty());
201
202 if (!watchIndices_.empty())
203 {
204 forAllReverse(watchIndices_, i)
205 {
206 fileHandler().removeWatch(watchIndices_[i]);
207 }
208 watchIndices_.clear();
209 }
210
211 // Note: IOstream::binary flag is for all the processor comms. (Only for
212 // dictionaries should it be ascii)
213 bool ok =
214 fileHandler().read(*this, masterOnly, IOstreamOption::BINARY, type());
215
216 if (needWatch)
217 {
218 // Re-watch master file
220 }
221
222 return ok;
223}
224
225
227{
228 forAllReverse(watchIndices_, i)
229 {
230 if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
231 {
232 return true;
234 }
235
236 return false;
237}
238
239
241{
242 // Get index of modified file so we can give nice message. Could instead
243 // just call above modified()
244 label modified = -1;
245 forAllReverse(watchIndices_, i)
246 {
247 if (fileHandler().getState(watchIndices_[i]) != fileMonitor::UNMODIFIED)
248 {
249 modified = watchIndices_[i];
250 break;
251 }
252 }
253
254 if (modified != -1)
255 {
256 const fileName fName = fileHandler().getFile(watchIndices_.back());
257
258 if (modified == watchIndices_.back())
259 {
261 << " Re-reading object " << name()
262 << " from file " << fName << endl;
263 }
264 else
265 {
267 << " Re-reading object " << name()
268 << " from file " << fName
269 << " because of modified file "
270 << fileHandler().getFile(modified)
271 << endl;
272 }
273
274 return read();
275 }
276
277 return false;
278}
279
280
281// ************************************************************************* //
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
@ NO_READ
Nothing to be read.
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition IOobjectI.H:223
static bool fileModificationChecking_masterOnly() noexcept
Test fileModificationChecking for master-only.
Definition IOobjectI.H:23
streamFormat
Data format (ascii | binary | coherent).
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
@ broadcast
broadcast [MPI]
Definition UPstream.H:189
static bool & parRun() noexcept
Test if this a parallel run.
Definition UPstream.H:1681
A class for handling file names.
Definition fileName.H:75
virtual bool modified() const
Return true if the object's file (or files for objectRegistry) have been modified....
virtual bool global() const
Is object global.
void close()
Close Istream.
virtual fileName filePath() const
Return complete path + object name if the file exists.
bool headerOk()
Read and check header info. Does not check the headerClassName.
virtual bool readIfModified()
Read object if modified (as set by call to modified).
virtual bool readData(Istream &)
Virtual readData function.
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED).
bool readHeaderOk(const IOstreamOption::streamFormat fmt, const word &typeName)
Helper: check readOpt flags and read if necessary.
virtual bool read()
Read object.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
#define InfoInFunction
Report an information message using Foam::Info.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition stdFoam.H:315