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,
59  Foam::regIOobject::fileModificationSkew
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 
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_
259  && time().runTimeModifiable()
260  )
261  {
262  if (watchIndex_ != -1)
263  {
264  FatalErrorIn("regIOobject::checkIn()")
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
288  FatalErrorIn("regIOobject::checkIn()")
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  {
296  WarningIn("regIOobject::checkIn()")
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  if (a.eventNo() >= eventNo_)
329  {
330  return false;
331  }
332  else
333  {
334  return true;
335  }
336 }
337 
338 
340 (
341  const regIOobject& a,
342  const regIOobject& b
343 ) const
344 {
345  if
346  (
347  a.eventNo() >= eventNo_
348  || b.eventNo() >= eventNo_
349  )
350  {
351  return false;
352  }
353  else
354  {
355  return true;
356  }
357 }
358 
359 
361 (
362  const regIOobject& a,
363  const regIOobject& b,
364  const regIOobject& c
365 ) const
366 {
367  if
368  (
369  a.eventNo() >= eventNo_
370  || b.eventNo() >= eventNo_
371  || c.eventNo() >= eventNo_
372  )
373  {
374  return false;
375  }
376  else
377  {
378  return true;
379  }
380 }
381 
382 
384 (
385  const regIOobject& a,
386  const regIOobject& b,
387  const regIOobject& c,
388  const regIOobject& d
389 ) const
390 {
391  if
392  (
393  a.eventNo() >= eventNo_
394  || b.eventNo() >= eventNo_
395  || c.eventNo() >= eventNo_
396  || d.eventNo() >= eventNo_
397  )
398  {
399  return false;
400  }
401  else
402  {
403  return true;
404  }
405 }
406 
407 
408 //- Flag me as up to date
410 {
411  eventNo_ = db().getEvent();
412 }
413 
414 
415 // Rename object and re-register with objectRegistry under new name
416 void Foam::regIOobject::rename(const word& newName)
417 {
418  // Check out of objectRegistry
419  checkOut();
420 
421  IOobject::rename(newName);
422 
423  if (registerObject())
424  {
425  // Re-register object with objectRegistry
426  checkIn();
427  }
428 }
429 
430 
431 // Assign to IOobject
432 void Foam::regIOobject::operator=(const IOobject& io)
433 {
434  if (isPtr_)
435  {
436  delete isPtr_;
437  isPtr_ = NULL;
438  }
439 
440  // Check out of objectRegistry
441  checkOut();
442 
444 
445  if (registerObject())
446  {
447  // Re-register object with objectRegistry
448  checkIn();
449  }
450 }
451 
452 
453 // ************************************************************************* //
void setUpToDate()
Set up to date (obviously)
Definition: regIOobject.C:409
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:82
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: regIOobject.H:76
readOption readOpt() const
Definition: IOobject.H:304
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:323
label getEvent() const
Return new event number.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
labelList f(nPoints)
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:284
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:216
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: regIOobject.C:101
const fileName & local() const
Definition: IOobject.H:347
A class for handling words, derived from string.
Definition: word.H:59
const fileName & instance() const
Definition: IOobject.H:337
messageStream Info
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:308
bool checkIn(regIOobject &) const
Add an regIOobject to registry.
void operator=(const IOobject &)
Definition: IOobject.C:484
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:290
Namespace for OpenFOAM.
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:326
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:416
fileName path() const
Return complete path.
Definition: IOobject.C:299
label addWatch(const fileName &) const
Add watching of a file. Returns handle.
Definition: Time.C:709
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:121
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
#define WarningIn(functionName)
Report a warning using Foam::Warning.
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
static int fileModificationSkew
Definition: regIOobject.H:126
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:159
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:235
addfileModificationCheckingToOpt(const char *name)
Definition: regIOobject.C:87
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word & name() const
Return name.
Definition: IOobject.H:260
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:589
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
Abstract base class for registered object with I/O. Used in debug symbol registration.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
registerOptSwitch("fileModificationSkew", int, Foam::regIOobject::fileModificationSkew)
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
bool checkIn()
Add object to registry.
Definition: regIOobject.C:247
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
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:131
virtual void readData(Foam::Istream &is)
Read.
Definition: regIOobject.C:95
A class for handling file names.
Definition: fileName.H:69
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const Time & time() const
Return time.
Definition: IOobject.C:251
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
virtual ~regIOobject()
Destructor.
Definition: regIOobject.C:220
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:183
addfileModificationCheckingToOpt addfileModificationCheckingToOpt_("fileModificationChecking")
fileCheckTypes
Types of communications.
Definition: regIOobject.H:68
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:245
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
defineTypeNameAndDebug(combustionModel, 0)
bool removeWatch(const label) const
Remove watch on a file (using handle)
Definition: Time.C:715