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-2016 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 
67 (
68  fileCheckTypesNames.read
69  (
71  (
72  "fileModificationChecking"
73  )
74  )
75 );
76 
77 namespace Foam
78 {
79  // Register re-reader
81  :
83  {
84  public:
85 
87  :
89  {}
90 
92  {}
93 
94  virtual void readData(Foam::Istream& is)
95  {
98  }
99 
100  virtual void writeData(Foam::Ostream& os) const
101  {
104  }
105  };
106 
108  (
109  "fileModificationChecking"
110  );
111 }
112 
113 
115 
116 
117 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118 
119 Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
120 :
121  IOobject(io),
122  registered_(false),
123  ownedByRegistry_(false),
124  watchIndex_(-1),
125  eventNo_ // Do not get event for top level Time database
126  (
127  isTime
128  ? 0
129  : db().getEvent()
130  ),
131  isPtr_(NULL)
132 {
133  // Register with objectRegistry if requested
134  if (registerObject())
135  {
136  checkIn();
137  }
138 }
139 
140 
142 :
143  IOobject(rio),
144  registered_(false),
145  ownedByRegistry_(false),
146  watchIndex_(rio.watchIndex_),
147  eventNo_(db().getEvent()),
148  isPtr_(NULL)
149 {
150  // Do not register copy with objectRegistry
151 }
152 
153 
154 Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
155 :
156  IOobject(rio),
157  registered_(false),
158  ownedByRegistry_(false),
159  watchIndex_(-1),
160  eventNo_(db().getEvent()),
161  isPtr_(NULL)
162 {
163  if (registerCopy && rio.registered_)
164  {
165  const_cast<regIOobject&>(rio).checkOut();
166  checkIn();
167  }
168 }
169 
170 
172 (
173  const word& newName,
174  const regIOobject& rio,
175  bool registerCopy
176 )
177 :
178  IOobject(newName, rio.instance(), rio.local(), rio.db()),
179  registered_(false),
180  ownedByRegistry_(false),
181  watchIndex_(-1),
182  eventNo_(db().getEvent()),
183  isPtr_(NULL)
184 {
185  if (registerCopy)
186  {
187  checkIn();
188  }
189 }
190 
191 
193 (
194  const IOobject& io,
195  const regIOobject& rio
196 )
197 :
198  IOobject(io),
199  registered_(false),
200  ownedByRegistry_(false),
201  watchIndex_(-1),
202  eventNo_(db().getEvent()),
203  isPtr_(NULL)
204 {
205  if (registerObject())
206  {
207  checkIn();
208  }
209 }
210 
211 
212 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
213 
215 {
216  if (objectRegistry::debug)
217  {
218  Info<< "Destroying regIOobject called " << name()
219  << " of type " << type()
220  << " in directory " << path()
221  << endl;
222  }
223 
224  if (isPtr_)
225  {
226  delete isPtr_;
227  isPtr_ = NULL;
228  }
229 
230  // Check out of objectRegistry if not owned by the registry
231 
232  if (!ownedByRegistry_)
233  {
234  checkOut();
235  }
236 }
237 
238 
239 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
240 
242 {
243  if (!registered_)
244  {
245  // multiple checkin of same object is disallowed - this would mess up
246  // any mapping
247  registered_ = db().checkIn(*this);
248 
249  if
250  (
251  registered_
253  && time().runTimeModifiable()
254  )
255  {
256  if (watchIndex_ != -1)
257  {
259  << "Object " << objectPath()
260  << " already watched with index " << watchIndex_
261  << abort(FatalError);
262  }
263 
264  fileName f = filePath();
265  if (!f.size())
266  {
267  // We don't have this file but would like to re-read it.
268  // Possibly if master-only reading mode.
269  f = objectPath();
270  }
271  watchIndex_ = time().addWatch(f);
272  }
273 
274  // check-in on defaultRegion is allowed to fail, since subsetted meshes
275  // are created with the same name as their originating mesh
276  if (!registered_ && debug && name() != polyMesh::defaultRegion)
277  {
278  if (debug == 2)
279  {
280  // for ease of finding where attempted duplicate check-in
281  // originated
283  << "failed to register object " << objectPath()
284  << " the name already exists in the objectRegistry" << endl
285  << "Contents:" << db().sortedToc()
286  << abort(FatalError);
287  }
288  else
289  {
291  << "failed to register object " << objectPath()
292  << " the name already exists in the objectRegistry"
293  << endl;
294  }
295  }
296  }
297 
298  return registered_;
299 }
300 
301 
303 {
304  if (registered_)
305  {
306  registered_ = false;
307 
308  if (watchIndex_ != -1)
309  {
310  time().removeWatch(watchIndex_);
311  watchIndex_ = -1;
312  }
313  return db().checkOut(*this);
314  }
315 
316  return false;
317 }
318 
319 
321 {
322  if (a.eventNo() >= eventNo_)
323  {
324  return false;
325  }
326  else
327  {
328  return true;
329  }
330 }
331 
332 
334 (
335  const regIOobject& a,
336  const regIOobject& b
337 ) const
338 {
339  if
340  (
341  a.eventNo() >= eventNo_
342  || b.eventNo() >= eventNo_
343  )
344  {
345  return false;
346  }
347  else
348  {
349  return true;
350  }
351 }
352 
353 
355 (
356  const regIOobject& a,
357  const regIOobject& b,
358  const regIOobject& c
359 ) const
360 {
361  if
362  (
363  a.eventNo() >= eventNo_
364  || b.eventNo() >= eventNo_
365  || c.eventNo() >= eventNo_
366  )
367  {
368  return false;
369  }
370  else
371  {
372  return true;
373  }
374 }
375 
376 
378 (
379  const regIOobject& a,
380  const regIOobject& b,
381  const regIOobject& c,
382  const regIOobject& d
383 ) const
384 {
385  if
386  (
387  a.eventNo() >= eventNo_
388  || b.eventNo() >= eventNo_
389  || c.eventNo() >= eventNo_
390  || d.eventNo() >= eventNo_
391  )
392  {
393  return false;
394  }
395  else
396  {
397  return true;
398  }
399 }
400 
401 
403 {
404  eventNo_ = db().getEvent();
405 }
406 
407 
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 void Foam::regIOobject::operator=(const IOobject& io)
424 {
425  if (isPtr_)
426  {
427  delete isPtr_;
428  isPtr_ = NULL;
429  }
430 
431  // Check out of objectRegistry
432  checkOut();
433 
435 
436  if (registerObject())
437  {
438  // Re-register object with objectRegistry
439  checkIn();
440  }
441 }
442 
443 
444 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
A class for handling file names.
Definition: fileName.H:69
addfileModificationCheckingToOpt(const char *name)
Definition: regIOobject.C:86
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:221
void operator=(const IOobject &)
Definition: IOobject.C:459
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual void readData(Foam::Istream &is)
Read.
Definition: regIOobject.C:94
void setUpToDate()
Set up to date (obviously)
Definition: regIOobject.C:402
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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:114
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:284
fileCheckTypes
Types of communications.
Definition: regIOobject.H:68
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:184
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:80
bool upToDate(const regIOobject &) const
Return true if up-to-date with respect to given object.
Definition: regIOobject.C:320
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
bool removeWatch(const label) const
Remove watch on a file (using handle)
Definition: Time.C:689
registerOptSwitch("fileModificationSkew", int, Foam::regIOobject::fileModificationSkew)
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:119
fileName path() const
Return complete path.
Definition: IOobject.C:275
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:299
A class for handling words, derived from string.
Definition: word.H:59
const Time & time() const
Return time.
Definition: IOobject.C:227
const fileName & local() const
Definition: IOobject.H:347
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool checkIn(regIOobject &) const
Add an regIOobject to registry.
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:302
label getEvent() const
Return new event number.
addfileModificationCheckingToOpt addfileModificationCheckingToOpt_("fileModificationChecking")
defineTypeNameAndDebug(combustionModel, 0)
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:408
labelList f(nPoints)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
#define WarningInFunction
Report a warning using Foam::Warning.
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:213
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: regIOobject.C:100
readOption readOpt() const
Definition: IOobject.H:304
static int fileModificationSkew
Definition: regIOobject.H:126
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
messageStream Info
static const NamedEnum< fileCheckTypes, 4 > fileCheckTypesNames
Definition: regIOobject.H:76
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:160
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:236
bool & registerObject()
Register object created from this IOobject with registry if true.
Definition: IOobject.H:290
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
const fileName & instance() const
Definition: IOobject.H:337
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
bool checkIn()
Add object to registry.
Definition: regIOobject.C:241
label addWatch(const fileName &) const
Add watching of a file. Returns handle.
Definition: Time.C:683
const word & name() const
Return name.
Definition: IOobject.H:260
Namespace for OpenFOAM.
virtual ~regIOobject()
Destructor.
Definition: regIOobject.C:214
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451