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-2019 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 "registerSwitch.H"
30 #include "fileOperation.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(regIOobject, 0);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
43 :
44  IOobject(io),
45  registered_(false),
46  ownedByRegistry_(false),
47  watchIndices_(),
48  eventNo_ // Do not get event for top level Time database
49  (
50  isTime
51  ? 0
52  : db().getEvent()
53  )
54 {
55  // Register with objectRegistry if requested
56  if (registerObject())
57  {
58  checkIn();
59  }
60 }
61 
62 
64 :
65  IOobject(rio),
66  registered_(false),
67  ownedByRegistry_(false),
68  watchIndices_(rio.watchIndices_),
69  eventNo_(db().getEvent())
70 {
71  // Do not register copy with objectRegistry
72 }
73 
74 
75 Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
76 :
77  IOobject(rio),
78  registered_(false),
79  ownedByRegistry_(false),
80  watchIndices_(),
81  eventNo_(db().getEvent())
82 {
83  if (registerCopy)
84  {
85  if (rio.registered_)
86  {
87  const_cast<regIOobject&>(rio).checkOut();
88  }
89  checkIn();
90  }
91 }
92 
93 
95 (
96  const word& newName,
97  const regIOobject& rio,
98  bool registerCopy
99 )
100 :
101  IOobject(newName, rio.instance(), rio.local(), rio.db()),
102  registered_(false),
103  ownedByRegistry_(false),
104  watchIndices_(),
105  eventNo_(db().getEvent())
106 {
107  if (registerCopy)
108  {
109  checkIn();
110  }
111 }
112 
113 
115 (
116  const IOobject& io,
117  const regIOobject& rio
118 )
119 :
120  IOobject(io),
121  registered_(false),
122  ownedByRegistry_(false),
123  watchIndices_(),
124  eventNo_(db().getEvent())
125 {
126  if (registerObject())
127  {
128  checkIn();
129  }
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
134 
136 {
137  if (objectRegistry::debug)
138  {
139  if (this == &db())
140  {
141  Pout<< "Destroying objectRegistry " << name()
142  << " in directory " << rootPath()/caseName()/instance()
143  << endl;
144  }
145  else
146  {
147  Pout<< "Destroying regIOobject " << name()
148  << " in directory " << path()
149  << endl;
150  }
151  }
152 
153  db().resetCacheTemporaryObject(*this);
154 
155  // Check out of objectRegistry if not owned by the registry
156  if (!ownedByRegistry_)
157  {
158  checkOut();
159  }
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
166 {
167  if (!registered_)
168  {
169  // multiple checkin of same object is disallowed - this would mess up
170  // any mapping
171  registered_ = db().checkIn(*this);
172 
173  // check-in on defaultRegion is allowed to fail, since subsetted meshes
174  // are created with the same name as their originating mesh
175  if (!registered_ && debug && name() != polyMesh::defaultRegion)
176  {
177  if (debug == 2)
178  {
179  // for ease of finding where attempted duplicate check-in
180  // originated
182  << "failed to register object " << objectPath()
183  << " the name already exists in the objectRegistry" << endl
184  << "Contents:" << db().sortedToc()
185  << abort(FatalError);
186  }
187  else
188  {
190  << "failed to register object " << objectPath()
191  << " the name already exists in the objectRegistry"
192  << endl;
193  }
194  }
195  }
196 
197  return registered_;
198 }
199 
200 
202 {
203  if (registered_)
204  {
205  registered_ = false;
206 
207  forAllReverse(watchIndices_, i)
208  {
209  fileHandler().removeWatch(watchIndices_[i]);
210  }
211  watchIndices_.clear();
212  return db().checkOut(*this);
213  }
214 
215  return false;
216 }
217 
218 
220 {
221  label index = -1;
222 
223  if
224  (
225  registered_
227  && time().runTimeModifiable()
228  )
229  {
230  index = fileHandler().findWatch(watchIndices_, f);
231 
232  if (index == -1)
233  {
234  index = watchIndices_.size();
235  watchIndices_.append(fileHandler().addWatch(f));
236  }
237  }
238  return index;
239 }
240 
241 
243 {
244  if
245  (
246  registered_
248  && time().runTimeModifiable()
249  )
250  {
251  fileName f = filePath();
252  if (!f.size())
253  {
254  // We don't have this file but would like to re-read it.
255  // Possibly if master-only reading mode.
256  f = objectPath();
257  }
258 
259  label index = fileHandler().findWatch(watchIndices_, f);
260  if (index != -1)
261  {
262  FatalErrorIn("regIOobject::addWatch()")
263  << "Object " << objectPath() << " of type " << type()
264  << " already watched with index " << watchIndices_[index]
265  << abort(FatalError);
266  }
267 
268  // If master-only reading only the master will have all dependencies
269  // so scatter these to slaves
270  bool masterOnly =
271  global()
272  && (
275  );
276 
277  if (masterOnly && Pstream::parRun())
278  {
279  // Get master watched files
280  fileNameList watchFiles;
281  if (Pstream::master())
282  {
283  watchFiles.setSize(watchIndices_.size());
284  forAll(watchIndices_, i)
285  {
286  watchFiles[i] = fileHandler().getFile(watchIndices_[i]);
287  }
288  }
289  Pstream::scatter(watchFiles);
290 
291  if (!Pstream::master())
292  {
293  // unregister current ones
294  forAllReverse(watchIndices_, i)
295  {
296  fileHandler().removeWatch(watchIndices_[i]);
297  }
298 
299  watchIndices_.clear();
300  forAll(watchFiles, i)
301  {
302  watchIndices_.append(fileHandler().addWatch(watchFiles[i]));
303  }
304  }
305  }
306 
307  watchIndices_.append(fileHandler().addWatch(f));
308  }
309 }
310 
311 
313 {
314  if (a.eventNo() >= eventNo_)
315  {
316  return false;
317  }
318  else
319  {
320  return true;
321  }
322 }
323 
324 
326 (
327  const regIOobject& a,
328  const regIOobject& b
329 ) const
330 {
331  if
332  (
333  a.eventNo() >= eventNo_
334  || b.eventNo() >= eventNo_
335  )
336  {
337  return false;
338  }
339  else
340  {
341  return true;
342  }
343 }
344 
345 
347 (
348  const regIOobject& a,
349  const regIOobject& b,
350  const regIOobject& c
351 ) const
352 {
353  if
354  (
355  a.eventNo() >= eventNo_
356  || b.eventNo() >= eventNo_
357  || c.eventNo() >= eventNo_
358  )
359  {
360  return false;
361  }
362  else
363  {
364  return true;
365  }
366 }
367 
368 
370 (
371  const regIOobject& a,
372  const regIOobject& b,
373  const regIOobject& c,
374  const regIOobject& d
375 ) const
376 {
377  if
378  (
379  a.eventNo() >= eventNo_
380  || b.eventNo() >= eventNo_
381  || c.eventNo() >= eventNo_
382  || d.eventNo() >= eventNo_
383  )
384  {
385  return false;
386  }
387  else
388  {
389  return true;
390  }
391 }
392 
393 
395 {
396  eventNo_ = db().getEvent();
397 }
398 
399 
400 void Foam::regIOobject::rename(const word& newName)
401 {
402  // Check out of objectRegistry
403  checkOut();
404 
405  IOobject::rename(newName);
406 
407  if (registerObject())
408  {
409  // Re-register object with objectRegistry
410  checkIn();
411  }
412 }
413 
414 
416 {
417  return localFilePath(type());
418 }
419 
420 
422 {
423  // Note: Should be consistent with IOobject::typeHeaderOk(false)
424 
425  bool ok = true;
426 
427  fileName fName(filePath());
428 
429  ok = Foam::fileHandler().readHeader(*this, fName, type());
430 
431  if (!ok && IOobject::debug)
432  {
433  IOWarningInFunction(fName)
434  << "failed to read header of file " << objectPath()
435  << endl;
436  }
437 
438  return ok;
439 }
440 
441 
443 {
444  return false;
445 }
446 
447 
448 void Foam::regIOobject::operator=(const IOobject& io)
449 {
450  // Close any file
451  isPtr_.clear();
452 
453  // Check out of objectRegistry
454  checkOut();
455 
457 
458  if (registerObject())
459  {
460  // Re-register object with objectRegistry
461  checkIn();
462  }
463 }
464 
465 
466 // ************************************************************************* //
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:312
#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:428
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
void resetCacheTemporaryObject(const regIOobject &ob) const
Reset the cache state of the given object.
void setUpToDate()
Set up to date (obviously)
Definition: regIOobject.C:394
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:177
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:421
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:415
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:42
fileName path() const
Return complete path.
Definition: IOobject.C:358
fileName localFilePath(const word &typeName) const
Helper for filePath that searches locally.
Definition: IOobject.C:395
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:201
defineTypeNameAndDebug(combustionModel, 0)
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:219
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:242
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:400
labelList f(nPoints)
const fileName & rootPath() const
Definition: IOobject.C:352
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:328
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:442
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:55
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:322
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:334
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:314
bool checkIn()
Add object to registry.
Definition: regIOobject.C:165
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:135