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-2015 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 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(regIOobject, 0);
36 
37  template<>
38  const char* NamedEnum
39  <
41  4
42  >::names[] =
43  {
44  "timeStamp",
45  "timeStampMaster",
46  "inotify",
47  "inotifyMaster"
48  };
49 }
50 
52 (
53  Foam::debug::optimisationSwitch("fileModificationSkew", 30)
54 );
56 (
57  "fileModificationSkew",
58  int,
60 );
61 
62 
65 
66 // Default fileCheck type
68 (
69  fileCheckTypesNames.read
70  (
72  (
73  "fileModificationChecking"
74  )
75  )
76 );
77 
78 namespace Foam
79 {
80  // Register re-reader
82  :
84  {
85  public:
86 
88  :
90  {}
91 
93  {}
94 
95  virtual void readData(Foam::Istream& is)
96  {
99  }
100 
101  virtual void writeData(Foam::Ostream& os) const
102  {
105  }
106  };
107 
108  addfileModificationCheckingToOpt addfileModificationCheckingToOpt_
109  (
110  "fileModificationChecking"
111  );
112 }
113 
114 
116 
117 
118 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
119 
120 // Construct from IOobject
121 Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
122 :
123  IOobject(io),
124  registered_(false),
125  ownedByRegistry_(false),
126  watchIndex_(-1),
127  eventNo_ // Do not get event for top level Time database
128  (
129  isTime
130  ? 0
131  : db().getEvent()
132  ),
133  isPtr_(NULL)
134 {
135  // Register with objectRegistry if requested
136  if (registerObject())
137  {
138  checkIn();
139  }
140 }
141 
142 
143 // Construct as copy
145 :
146  IOobject(rio),
147  registered_(false),
148  ownedByRegistry_(false),
149  watchIndex_(rio.watchIndex_),
150  eventNo_(db().getEvent()),
151  isPtr_(NULL)
152 {
153  // Do not register copy with objectRegistry
154 }
155 
156 
157 // Construct as copy, and transfering objectRegistry registration to copy
158 // if registerCopy is true
159 Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
160 :
161  IOobject(rio),
162  registered_(false),
163  ownedByRegistry_(false),
164  watchIndex_(-1),
165  eventNo_(db().getEvent()),
166  isPtr_(NULL)
167 {
168  if (registerCopy && rio.registered_)
169  {
170  const_cast<regIOobject&>(rio).checkOut();
171  checkIn();
172  }
173 }
174 
175 
177 (
178  const word& newName,
179  const regIOobject& rio,
180  bool registerCopy
181 )
182 :
183  IOobject(newName, rio.instance(), rio.local(), rio.db()),
184  registered_(false),
185  ownedByRegistry_(false),
186  watchIndex_(-1),
187  eventNo_(db().getEvent()),
188  isPtr_(NULL)
189 {
190  if (registerCopy)
191  {
192  checkIn();
193  }
194 }
195 
196 
198 (
199  const IOobject& io,
200  const regIOobject& rio
201 )
202 :
203  IOobject(io),
204  registered_(false),
205  ownedByRegistry_(false),
206  watchIndex_(-1),
207  eventNo_(db().getEvent()),
208  isPtr_(NULL)
209 {
210  if (registerObject())
211  {
212  checkIn();
213  }
214 }
215 
216 
217 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
218 
219 // Delete read stream, checkout from objectRegistry and destroy
221 {
222  if (objectRegistry::debug)
223  {
224  Info<< "Destroying regIOobject called " << name()
225  << " of type " << type()
226  << " in directory " << path()
227  << endl;
228  }
229 
230  if (isPtr_)
231  {
232  delete isPtr_;
233  isPtr_ = NULL;
234  }
235 
236  // Check out of objectRegistry if not owned by the registry
237 
238  if (!ownedByRegistry_)
239  {
240  checkOut();
241  }
242 }
243 
244 
245 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
246 
248 {
249  if (!registered_)
250  {
251  // multiple checkin of same object is disallowed - this would mess up
252  // any mapping
253  registered_ = db().checkIn(*this);
254 
255  if
256  (
257  registered_
258  && readOpt() == MUST_READ_IF_MODIFIED
259  && time().runTimeModifiable()
260  )
261  {
262  if (watchIndex_ != -1)
263  {
265  << "Object " << objectPath()
266  << " already watched with index " << watchIndex_
267  << abort(FatalError);
268  }
269 
270  fileName f = filePath();
271  if (!f.size())
272  {
273  // We don't have this file but would like to re-read it.
274  // Possibly if master-only reading mode.
275  f = objectPath();
276  }
277  watchIndex_ = time().addWatch(f);
278  }
279 
280  // check-in on defaultRegion is allowed to fail, since subsetted meshes
281  // are created with the same name as their originating mesh
282  if (!registered_ && debug && name() != polyMesh::defaultRegion)
283  {
284  if (debug == 2)
285  {
286  // for ease of finding where attempted duplicate check-in
287  // originated
289  << "failed to register object " << objectPath()
290  << " the name already exists in the objectRegistry" << endl
291  << "Contents:" << db().sortedToc()
292  << abort(FatalError);
293  }
294  else
295  {
297  << "failed to register object " << objectPath()
298  << " the name already exists in the objectRegistry"
299  << endl;
300  }
301  }
302  }
303 
304  return registered_;
305 }
306 
307 
309 {
310  if (registered_)
311  {
312  registered_ = false;
313 
314  if (watchIndex_ != -1)
315  {
316  time().removeWatch(watchIndex_);
317  watchIndex_ = -1;
318  }
319  return db().checkOut(*this);
320  }
321 
322  return false;
323 }
324 
325 
327 {
328  label da = a.eventNo()-eventNo_;
329 
330  // In case of overflow *this.event() might be 2G but a.event() might
331  // have overflowed to 0.
332  // Detect this by detecting a massive difference (labelMax/2) between
333  // the two events.
334  //
335  // a *this return
336  // - ----- ------
337  // normal operation:
338  // 11 10 false
339  // 11 11 false
340  // 10 11 true
341  // overflow situation:
342  // 0 big false
343  // big 0 true
344 
345  if (da > labelMax/2)
346  {
347  // *this.event overflowed but a.event not yet
348  return true;
349  }
350  else if (da < -labelMax/2)
351  {
352  // a.event overflowed but *this not yet
353  return false;
354  }
355  else if (da < 0)
356  {
357  // My event number higher than a
358  return true;
359  }
360  else
361  {
362  return false;
363  }
364 }
365 
366 
368 (
369  const regIOobject& a,
370  const regIOobject& b
371 ) const
372 {
373  return upToDate(a) && upToDate(b);
374 }
375 
376 
378 (
379  const regIOobject& a,
380  const regIOobject& b,
381  const regIOobject& c
382 ) const
383 {
384  return upToDate(a) && upToDate(b) && upToDate(c);
385 }
386 
387 
389 (
390  const regIOobject& a,
391  const regIOobject& b,
392  const regIOobject& c,
393  const regIOobject& d
394 ) const
395 {
396  return upToDate(a) && upToDate(b) && upToDate(c) && upToDate(d);
397 }
398 
399 
400 //- Flag me as up to date
402 {
403  eventNo_ = db().getEvent();
404 }
405 
406 
407 // Rename object and re-register with objectRegistry under new name
408 void Foam::regIOobject::rename(const word& newName)
409 {
410  // Check out of objectRegistry
411  checkOut();
412 
413  IOobject::rename(newName);
414 
415  if (registerObject())
416  {
417  // Re-register object with objectRegistry
418  checkIn();
419  }
420 }
421 
422 
423 // Assign to IOobject
425 {
426  if (isPtr_)
427  {
428  delete isPtr_;
429  isPtr_ = NULL;
430  }
431 
432  // Check out of objectRegistry
433  checkOut();
434 
436 
437  if (registerObject())
438  {
439  // Re-register object with objectRegistry
440  checkIn();
441  }
442 }
443 
444 
445 // ************************************************************************* //
Foam::regIOobject::operator=
void operator=(const regIOobject &)
Dissallow assignment.
regIOobject.H
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::addfileModificationCheckingToOpt_
addfileModificationCheckingToOpt addfileModificationCheckingToOpt_("fileModificationChecking")
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
Foam::regIOobject::fileCheckTypesNames
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: regIOobject.H:76
Foam::regIOobject::upToDate
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:326
Foam::labelMax
static const label labelMax
Definition: label.H:62
Foam::addfileModificationCheckingToOpt::addfileModificationCheckingToOpt
addfileModificationCheckingToOpt(const char *name)
Definition: regIOobject.C:87
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::regIOobject::checkIn
bool checkIn()
Add object to registry.
Definition: regIOobject.C:247
Foam::regIOobject::regIOobject
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:121
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobject.H:350
polyMesh.H
Foam::IOobject::db
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:239
Foam::regIOobject::registered_
bool registered_
Is this object registered with the registry.
Definition: regIOobject.H:90
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:182
Foam::label
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
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
Foam::addfileModificationCheckingToOpt::readData
virtual void readData(Foam::Istream &is)
Read.
Definition: regIOobject.C:95
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Info
messageStream Info
Foam::IOobject::registerObject
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:303
Foam::regIOobject::eventNo
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:80
Foam::addfileModificationCheckingToOpt::~addfileModificationCheckingToOpt
virtual ~addfileModificationCheckingToOpt()
Definition: regIOobject.C:92
Foam::regIOobject::fileCheckTypes
fileCheckTypes
Types of communications.
Definition: regIOobject.H:68
Foam::IOobject::operator=
void operator=(const IOobject &)
Definition: IOobject.C:499
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::regIOobject::masterOnlyReading
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::IOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:297
Foam::addfileModificationCheckingToOpt::writeData
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: regIOobject.C:101
Foam::regIOobject::setUpToDate
void setUpToDate()
Set up to date (obviously)
Definition: regIOobject.C:401
Foam::regIOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
Foam::NamedEnum::read
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::regIOobject::checkOut
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:308
f
labelList f(nPoints)
Foam::regIOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:408
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
registerSwitch.H
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::regIOobject::~regIOobject
virtual ~regIOobject()
Destructor.
Definition: regIOobject.C:220
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::debug::optimisationSwitches
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:158
Foam::regIOobject::fileModificationSkew
static int fileModificationSkew
Definition: regIOobject.H:126
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::IOobject::local
const fileName & local() const
Definition: IOobject.H:360
Foam::debug::addOptimisationObject
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:234
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:50
registerOptSwitch
registerOptSwitch("fileModificationSkew", int, Foam::regIOobject::fileModificationSkew)
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::NamedEnum
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
Foam::addfileModificationCheckingToOpt
Definition: regIOobject.C:81