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