FreeFOAM The Cross-Platform CFD Toolkit
objectRegistry.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 "objectRegistry.H"
27 #include <OpenFOAM/Time.H>
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
32 
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 Foam::objectRegistry::objectRegistry
37 (
38  const Time& t,
39  const label nIoObjects
40 )
41 :
43  (
44  IOobject
45  (
46  string::validate<word>(t.caseName()),
47  "",
48  t,
49  IOobject::NO_READ,
50  IOobject::AUTO_WRITE,
51  false
52  ),
53  true // to flag that this is the top-level regIOobject
54  ),
55  HashTable<regIOobject*>(nIoObjects),
56  time_(t),
57  parent_(t),
58  dbDir_(name()),
59  event_(1)
60 {}
61 
62 
63 Foam::objectRegistry::objectRegistry
64 (
65  const IOobject& io,
66  const label nIoObjects
67 )
68 :
69  regIOobject(io),
70  HashTable<regIOobject*>(nIoObjects),
71  time_(io.time()),
72  parent_(io.db()),
73  dbDir_(parent_.dbDir()/local()/name()),
74  event_(1)
75 {
76  writeOpt() = IOobject::AUTO_WRITE;
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
81 
83 {
84  List<regIOobject*> myObjects(size());
85  label nMyObjects = 0;
86 
87  for (iterator iter = begin(); iter != end(); ++iter)
88  {
89  if (iter()->ownedByRegistry())
90  {
91  myObjects[nMyObjects++] = iter();
92  }
93  }
94 
95  for (label i=0; i<nMyObjects; i++)
96  {
97  checkOut(*myObjects[i]);
98  }
99 }
100 
101 
102 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
103 
105 {
106  wordList objectNames(size());
107 
108  label count=0;
109  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
110  {
111  objectNames[count++] = iter()->name();
112  }
113 
114  return objectNames;
115 }
116 
117 
119 {
120  wordList objectNames(size());
121 
122  label count=0;
123  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
124  {
125  if (iter()->type() == ClassName)
126  {
127  objectNames[count++] = iter()->name();
128  }
129  }
130 
131  objectNames.setSize(count);
132 
133  return objectNames;
134 }
135 
136 
138 (
139  const word& name
140 ) const
141 {
142  return lookupObject<objectRegistry>(name);
143 }
144 
145 
147 {
148  label curEvent = event_++;
149 
150  if (event_ == labelMax)
151  {
152  WarningIn("objectRegistry::getEvent() const")
153  << "Event counter has overflowed. Resetting counter on all"
154  << " dependent objects." << endl
155  << "This might cause extra evaluations." << endl;
156 
157  // Reset event counter
158  curEvent = 1;
159  event_ = 2;
160 
161  for (const_iterator iter = begin(); iter != end(); ++iter)
162  {
163  const regIOobject& io = *iter();
164 
165  if (objectRegistry::debug)
166  {
167  Pout<< "objectRegistry::getEvent() : "
168  << "resetting count on " << iter.key() << endl;
169  }
170 
171  if (io.eventNo() != 0)
172  {
173  const_cast<regIOobject&>(io).eventNo() = curEvent;
174  }
175  }
176  }
177 
178  return curEvent;
179 }
180 
181 
183 {
184  if (objectRegistry::debug)
185  {
186  Pout<< "objectRegistry::checkIn(regIOobject&) : "
187  << name() << " : checking in " << io.name()
188  << endl;
189  }
190 
191  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
192 }
193 
194 
196 {
197  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
198 
199  if (iter != end())
200  {
201  if (objectRegistry::debug)
202  {
203  Pout<< "objectRegistry::checkOut(regIOobject&) : "
204  << name() << " : checking out " << io.name()
205  << endl;
206  }
207 
208  if (iter() != &io)
209  {
210  if (objectRegistry::debug)
211  {
212  WarningIn("objectRegistry::checkOut(regIOobject&)")
213  << name() << " : attempt to checkOut copy of " << io.name()
214  << endl;
215  }
216 
217  return false;
218  }
219  else
220  {
221  regIOobject* object = iter();
222 
223  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
224 
225  if (io.ownedByRegistry())
226  {
227  delete object;
228  }
229 
230  return hasErased;
231  }
232  }
233  else
234  {
235  if (objectRegistry::debug)
236  {
237  Pout<< "objectRegistry::checkOut(regIOobject&) : "
238  << name() << " : could not find " << io.name()
239  << " in registry " << name()
240  << endl;
241  }
242  }
243 
244  return false;
245 }
246 
247 
248 void Foam::objectRegistry::rename(const word& newName)
249 {
250  regIOobject::rename(newName);
251 
252  // adjust dbDir_ as well
253  string::size_type i = dbDir_.rfind('/');
254 
255  if (i == string::npos)
256  {
257  dbDir_ = newName;
258  }
259  else
260  {
261  dbDir_.replace(i+1, string::npos, newName);
262  }
263 }
264 
265 
267 {
268  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
269  {
270  if (iter()->modified())
271  {
272  return true;
273  }
274  }
275 
276  return false;
277 }
278 
279 
281 {
282  for (iterator iter = begin(); iter != end(); ++iter)
283  {
284  if (objectRegistry::debug)
285  {
286  Pout<< "objectRegistry::readModifiedObjects() : "
287  << name() << " : Considering reading object "
288  << iter()->name()
289  << endl;
290  }
291 
292  iter()->readIfModified();
293  }
294 }
295 
296 
298 {
299  readModifiedObjects();
300  return true;
301 }
302 
303 
305 (
309 ) const
310 {
311  bool ok = true;
312 
313  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
314  {
315  if (objectRegistry::debug)
316  {
317  Pout<< "objectRegistry::write() : "
318  << name() << " : Considering writing object "
319  << iter()->name()
320  << " with writeOpt " << iter()->writeOpt()
321  << " to file " << iter()->objectPath()
322  << endl;
323  }
324 
325  if (iter()->writeOpt() != NO_WRITE)
326  {
327  ok = iter()->writeObject(fmt, ver, cmp) && ok;
328  }
329  }
330 
331  return ok;
332 }
333 
334 
335 // ************************ vim: set sw=4 sts=4 et: ************************ //