regIOobject.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "regIOobject.H"
27 #include "Time.H"
28 #include "polyMesh.H"
29 #include "fileOperation.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(regIOobject, 0);
36 }
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
41 Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
42 :
43  IOobject(io),
44  registered_(false),
45  ownedByRegistry_(false),
46  watchIndices_(),
47  eventNo_ // Do not get event for top level Time database
48  (
49  isTime
50  ? 0
51  : db().getEvent()
52  )
53 {
54  // Register with objectRegistry if requested
55  if (registerObject())
56  {
57  checkIn();
58  }
59 }
60 
61 
63 :
64  IOobject(rio),
65  registered_(false),
66  ownedByRegistry_(false),
67  watchIndices_(rio.watchIndices_),
68  eventNo_(db().getEvent())
69 {
70  // Do not register copy with objectRegistry
71 }
72 
73 
74 Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
75 :
76  IOobject(rio),
77  registered_(false),
78  ownedByRegistry_(false),
79  watchIndices_(),
80  eventNo_(db().getEvent())
81 {
82  if (registerCopy)
83  {
84  if (rio.registered_)
85  {
86  const_cast<regIOobject&>(rio).checkOut();
87  }
88  checkIn();
89  }
90 }
91 
92 
94 (
95  const word& newName,
96  const regIOobject& rio,
97  bool registerCopy
98 )
99 :
100  IOobject(newName, rio.instance(), rio.local(), rio.db()),
101  registered_(false),
102  ownedByRegistry_(false),
103  watchIndices_(),
104  eventNo_(db().getEvent())
105 {
106  if (registerCopy)
107  {
108  checkIn();
109  }
110 }
111 
112 
114 (
115  const IOobject& io,
116  const regIOobject& rio
117 )
118 :
119  IOobject(io),
120  registered_(false),
121  ownedByRegistry_(false),
122  watchIndices_(),
123  eventNo_(db().getEvent())
124 {
125  if (registerObject())
126  {
127  checkIn();
128  }
129 }
130 
131 
132 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
133 
135 {
136  if (objectRegistry::debug)
137  {
138  if (this == &db())
139  {
140  Pout<< "Destroying objectRegistry " << name()
141  << " in directory " << rootPath()/caseName()/instance()
142  << endl;
143  }
144  else
145  {
146  Pout<< "Destroying regIOobject " << name()
147  << " in directory " << path()
148  << endl;
149  }
150  }
151 
152  db().resetCacheTemporaryObject(*this);
153 
154  // Check out of objectRegistry if not owned by the registry
155  if (!ownedByRegistry_)
156  {
157  checkOut();
158  }
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
163 
165 {
166  if (!registered_)
167  {
168  // multiple checkin of same object is disallowed - this would mess up
169  // any mapping
170  registered_ = db().checkIn(*this);
171 
172  // check-in on defaultRegion is allowed to fail, since subsetted meshes
173  // are created with the same name as their originating mesh
174  if (!registered_ && debug && name() != polyMesh::defaultRegion)
175  {
176  if (debug == 2)
177  {
178  // for ease of finding where attempted duplicate check-in
179  // originated
181  << "failed to register object " << objectPath()
182  << " the name already exists in the objectRegistry" << endl
183  << "Contents:" << db().sortedToc()
184  << abort(FatalError);
185  }
186  else
187  {
189  << "failed to register object " << objectPath()
190  << " the name already exists in the objectRegistry"
191  << endl;
192  }
193  }
194  }
195 
196  return registered_;
197 }
198 
199 
201 {
202  if (registered_)
203  {
204  registered_ = false;
205 
206  forAllReverse(watchIndices_, i)
207  {
208  fileHandler().removeWatch(watchIndices_[i]);
209  }
210  watchIndices_.clear();
211  return db().checkOut(*this);
212  }
213 
214  return false;
215 }
216 
217 
219 {
220  if
221  (
222  registered_
224  && time().runTimeModifiable()
225  )
226  {
227  fileName f = filePath();
228  if (!f.size())
229  {
230  // We don't have this file but would like to re-read it.
231  // Possibly if master-only reading mode.
232  f = objectPath();
233  }
234 
235  label index = fileHandler().findWatch(watchIndices_, f);
236  if (index != -1)
237  {
238  FatalErrorIn("regIOobject::addWatch()")
239  << "Object " << objectPath() << " of type " << type()
240  << " already watched with index " << watchIndices_[index]
241  << abort(FatalError);
242  }
243 
244  // If master-only reading only the master will have all dependencies
245  // so scatter these to slaves
246  bool masterOnly =
247  global()
248  && (
251  );
252 
253  if (masterOnly && Pstream::parRun())
254  {
255  // Get master watched files
256  fileNameList watchFiles;
257  if (Pstream::master())
258  {
259  watchFiles.setSize(watchIndices_.size());
260  forAll(watchIndices_, i)
261  {
262  watchFiles[i] = fileHandler().getFile(watchIndices_[i]);
263  }
264  }
265  Pstream::scatter(watchFiles);
266 
267  if (!Pstream::master())
268  {
269  // unregister current ones
270  forAllReverse(watchIndices_, i)
271  {
272  fileHandler().removeWatch(watchIndices_[i]);
273  }
274 
275  watchIndices_.clear();
276  forAll(watchFiles, i)
277  {
278  watchIndices_.append(fileHandler().addWatch(watchFiles[i]));
279  }
280  }
281  }
282 
283  watchIndices_.append(fileHandler().addWatch(f));
284  }
285 }
286 
287 
289 {
290  if (a.eventNo() >= eventNo_)
291  {
292  return false;
293  }
294  else
295  {
296  return true;
297  }
298 }
299 
300 
302 (
303  const regIOobject& a,
304  const regIOobject& b
305 ) const
306 {
307  if
308  (
309  a.eventNo() >= eventNo_
310  || b.eventNo() >= eventNo_
311  )
312  {
313  return false;
314  }
315  else
316  {
317  return true;
318  }
319 }
320 
321 
323 (
324  const regIOobject& a,
325  const regIOobject& b,
326  const regIOobject& c
327 ) const
328 {
329  if
330  (
331  a.eventNo() >= eventNo_
332  || b.eventNo() >= eventNo_
333  || c.eventNo() >= eventNo_
334  )
335  {
336  return false;
337  }
338  else
339  {
340  return true;
341  }
342 }
343 
344 
346 (
347  const regIOobject& a,
348  const regIOobject& b,
349  const regIOobject& c,
350  const regIOobject& d
351 ) const
352 {
353  if
354  (
355  a.eventNo() >= eventNo_
356  || b.eventNo() >= eventNo_
357  || c.eventNo() >= eventNo_
358  || d.eventNo() >= eventNo_
359  )
360  {
361  return false;
362  }
363  else
364  {
365  return true;
366  }
367 }
368 
369 
371 {
372  eventNo_ = db().getEvent();
373 }
374 
375 
376 void Foam::regIOobject::rename(const word& newName)
377 {
378  // Check out of objectRegistry
379  checkOut();
380 
381  IOobject::rename(newName);
382 
383  if (registerObject())
384  {
385  // Re-register object with objectRegistry
386  checkIn();
387  }
388 }
389 
390 
392 {
393  return localFilePath(type());
394 }
395 
396 
398 {
399  // Note: Should be consistent with IOobject::typeHeaderOk(false)
400 
401  bool ok = true;
402 
403  fileName fName(filePath());
404 
405  ok = Foam::fileHandler().readHeader(*this, fName, type());
406 
407  if (!ok && IOobject::debug)
408  {
409  IOWarningInFunction(fName)
410  << "failed to read header of file " << objectPath()
411  << endl;
412  }
413 
414  return ok;
415 }
416 
417 
419 {
420  return false;
421 }
422 
423 
424 void Foam::regIOobject::operator=(const IOobject& io)
425 {
426  // Close any file
427  isPtr_.clear();
428 
429  // Check out of objectRegistry
430  checkOut();
431 
433 
434  if (registerObject())
435  {
436  // Re-register object with objectRegistry
437  checkIn();
438  }
439 }
440 
441 
442 // ************************************************************************* //
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:288
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const word & name() const
Return name.
Definition: IOobject.H:303
A class for handling file names.
Definition: fileName.H:79
void operator=(const IOobject &)
Definition: IOobject.C:418
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
void resetCacheTemporaryObject(const regIOobject &ob) const
Reset the cache state of the given object.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
void setUpToDate()
Set up to date (obviously)
Definition: regIOobject.C:370
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
IOobject(const word &name, const fileName &instance, const objectRegistry &registry, readOption r=NO_READ, writeOption w=NO_WRITE, bool registerObject=true)
Construct from name, instance, registry, io options.
Definition: IOobject.C:167
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:333
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:309
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
bool headerOk()
Read and check header info.
Definition: regIOobject.C:397
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:446
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:391
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:41
fileName path() const
Return complete path.
Definition: IOobject.C:348
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:385
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
A class for handling words, derived from string.
Definition: word.H:59
virtual fileName getFile(const label) const
Get name of file being watched (using handle)
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
label getEvent() const
Return new event number.
const fileName & local() const
Definition: IOobject.H:400
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:83
const fileOperation & fileHandler()
Get current file handler.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:200
defineTypeNameAndDebug(combustionModel, 0)
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:219
void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:218
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:376
labelList f(nPoints)
const fileName & rootPath() const
Definition: IOobject.C:342
void setSize(const label)
Reset size of List.
Definition: List.C:281
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
const fileName & instance() const
Definition: IOobject.H:390
#define WarningInFunction
Report a warning using Foam::Warning.
const Time & time() const
Return time.
Definition: IOobject.C:318
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:217
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
virtual bool global() const
Is object same for all processors.
Definition: regIOobject.C:418
bool checkIn(regIOobject &) const
Add an regIOobject to registry.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
virtual label findWatch(const labelList &watchIndices, const fileName &) const
Find index (or -1) of file in list of handles.
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:339
readOption readOpt() const
Definition: IOobject.H:353
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
const fileName & caseName() const
Definition: IOobject.C:324
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:419
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:318
bool checkIn()
Add object to registry.
Definition: regIOobject.C:164
virtual bool removeWatch(const label) const
Remove watch on a file (using handle)
virtual bool readHeader(IOobject &, const fileName &, const word &typeName) const =0
Read object header from supplied file.
Namespace for OpenFOAM.
virtual ~regIOobject()
Destructor.
Definition: regIOobject.C:134