FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/Time.H>
28 #include <OpenFOAM/polyMesh.H>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
35 (
36  Foam::debug::optimisationSwitch("fileModificationSkew", 30)
37 );
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 // Construct from IOobject
43 Foam::regIOobject::regIOobject(const IOobject& io, const bool isTime)
44 :
45  IOobject(io),
46  registered_(false),
47  ownedByRegistry_(false),
48  lastModified_(0),
49  eventNo_ // Do not get event for top level Time database
50  (
51  isTime
52  ? 0
53  : db().getEvent()
54  ),
55  isPtr_(NULL)
56 {
57  // Register with objectRegistry if requested
58  if (registerObject())
59  {
60  checkIn();
61  }
62 }
63 
64 
65 // Construct as copy
67 :
68  IOobject(rio),
69  registered_(false),
70  ownedByRegistry_(false),
71  lastModified_(rio.lastModified_),
72  eventNo_(db().getEvent()),
73  isPtr_(NULL)
74 {
75  // Do not register copy with objectRegistry
76 }
77 
78 
79 // Construct as copy, and transfering objectRegistry registration to copy
80 // if registerCopy is true
81 Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
82 :
83  IOobject(rio),
84  registered_(false),
85  ownedByRegistry_(false),
86  lastModified_(rio.lastModified_),
87  eventNo_(db().getEvent()),
88  isPtr_(NULL)
89 {
90  if (registerCopy && rio.registered_)
91  {
92  const_cast<regIOobject&>(rio).checkOut();
93  checkIn();
94  }
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
99 
100 // Delete read stream, checkout from objectRegistry and destroy
102 {
103  if (objectRegistry::debug)
104  {
105  Info<< "Destroying regIOobject called " << name()
106  << " of type " << type()
107  << " in directory " << path()
108  << endl;
109  }
110 
111  if (isPtr_)
112  {
113  delete isPtr_;
114  isPtr_ = NULL;
115  }
116 
117  // Check out of objectRegistry if not owned by the registry
118 
119  if (!ownedByRegistry_)
120  {
121  checkOut();
122  }
123 }
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
129 {
130  if (!registered_)
131  {
132  // multiple checkin of same object is disallowed - this would mess up
133  // any mapping
134  registered_ = db().checkIn(*this);
135 
136  // check-in on defaultRegion is allowed to fail, since subsetted meshes
137  // are created with the same name as their originating mesh
138  if (!registered_ && debug && name() != polyMesh::defaultRegion)
139  {
140  if (debug == 2)
141  {
142  // for ease of finding where attempted duplicate check-in
143  // originated
144  FatalErrorIn("regIOobject::checkIn()")
145  << "failed to register object " << objectPath()
146  << " the name already exists in the objectRegistry"
147  << abort(FatalError);
148  }
149  else
150  {
151  WarningIn("regIOobject::checkIn()")
152  << "failed to register object " << objectPath()
153  << " the name already exists in the objectRegistry"
154  << endl;
155  }
156  }
157  }
158 
159  return registered_;
160 }
161 
162 
164 {
165  if (registered_)
166  {
167  registered_ = false;
168  return db().checkOut(*this);
169  }
170 
171  return false;
172 }
173 
174 
175 bool Foam::regIOobject::upToDate(const word& a) const
176 {
177  if (db().lookupObject<regIOobject>(a).eventNo() >= eventNo_)
178  {
179  return false;
180  }
181  else
182  {
183  return true;
184  }
185 }
186 
187 
188 bool Foam::regIOobject::upToDate(const word& a, const word& b) const
189 {
190  if
191  (
192  db().lookupObject<regIOobject>(a).eventNo() >= eventNo_
193  || db().lookupObject<regIOobject>(b).eventNo() >= eventNo_
194  )
195  {
196  return false;
197  }
198  else
199  {
200  return true;
201  }
202 }
203 
204 
206 (
207  const word& a,
208  const word& b,
209  const word& c
210 ) const
211 {
212  if
213  (
214  db().lookupObject<regIOobject>(a).eventNo() >= eventNo_
215  || db().lookupObject<regIOobject>(b).eventNo() >= eventNo_
216  || db().lookupObject<regIOobject>(c).eventNo() >= eventNo_
217  )
218  {
219  return false;
220  }
221  else
222  {
223  return true;
224  }
225 }
226 
227 
229 (
230  const word& a,
231  const word& b,
232  const word& c,
233  const word& d
234 ) const
235 {
236  if
237  (
238  db().lookupObject<regIOobject>(a).eventNo() >= eventNo_
239  || db().lookupObject<regIOobject>(b).eventNo() >= eventNo_
240  || db().lookupObject<regIOobject>(c).eventNo() >= eventNo_
241  || db().lookupObject<regIOobject>(d).eventNo() >= eventNo_
242  )
243  {
244  return false;
245  }
246  else
247  {
248  return true;
249  }
250 }
251 
252 
253 //- Flag me as up to date
255 {
256  eventNo_ = db().getEvent();
257 }
258 
259 
260 // Rename object and re-register with objectRegistry under new name
261 void Foam::regIOobject::rename(const word& newName)
262 {
263  // Check out of objectRegistry
264  checkOut();
265 
266  IOobject::rename(newName);
267 
268  if (registerObject())
269  {
270  // Re-register object with objectRegistry
271  checkIn();
272  }
273 }
274 
275 
276 // Assign to IOobject
277 void Foam::regIOobject::operator=(const IOobject& io)
278 {
279  if (isPtr_)
280  {
281  delete isPtr_;
282  isPtr_ = NULL;
283  }
284 
285  // Check out of objectRegistry
286  checkOut();
287 
289 
290  if (registerObject())
291  {
292  // Re-register object with objectRegistry
293  checkIn();
294  }
295 }
296 
297 
298 // ************************ vim: set sw=4 sts=4 et: ************************ //