Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / db / objectRegistry / objectRegistry.C
blob706624fc13da42e8ac8c23345c79cb0c42acb5ac
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "objectRegistry.H"
27 #include "foamTime.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 defineTypeNameAndDebug(Foam::objectRegistry, 0);
34 // * * * * * * * * * * * * * * * * Constructors *  * * * * * * * * * * * * * //
36 Foam::objectRegistry::objectRegistry
38     const Time& t,
39     const label nIoObjects
42     regIOobject
43     (
44         IOobject
45         (
46             string::validate<word>(t.caseName()),
47             t,
48             IOobject::NO_READ,
49             IOobject::AUTO_WRITE
50         ),
51         true    // to flag that this is the top-level regIOobject
52     ),
53     HashTable<regIOobject*>(nIoObjects),
54     time_(t),
55     parent_(t),
56     dbDir_(name()),
57     event_(1)
61 Foam::objectRegistry::objectRegistry
63     const IOobject& io,
64     const label nIoObjects
67     regIOobject(io),
68     HashTable<regIOobject*>(nIoObjects),
69     time_(io.time()),
70     parent_(io.db()),
71     dbDir_(parent_.dbDir()/local()/name()),
72     event_(1)
74     writeOpt() = IOobject::AUTO_WRITE;
78 Foam::objectRegistry::objectRegistry
80     const IOobject& io,
81     const fileName& dbDir,
82     const label nIoObjects
85     regIOobject(io),
86     HashTable<regIOobject*>(nIoObjects),
87     time_(io.time()),
88     parent_(io.db()),
89     dbDir_(dbDir),
90     event_(1)
92     writeOpt() = IOobject::AUTO_WRITE;
96 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
98 Foam::objectRegistry::~objectRegistry()
100     List<regIOobject*> myObjects(size());
101     label nMyObjects = 0;
103     for (iterator iter = begin(); iter != end(); ++iter)
104     {
105         if (iter()->ownedByRegistry())
106         {
107             myObjects[nMyObjects++] = iter();
108         }
109     }
111     for (label i=0; i<nMyObjects; i++)
112     {
113         checkOut(*myObjects[i]);
114     }
118 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
120 const Foam::Time& Foam::objectRegistry::time() const
122     return time_;
126 Foam::wordList Foam::objectRegistry::names() const
128     wordList objectNames(size());
130     label count=0;
131     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
132     {
133         objectNames[count++] = iter()->name();
134     }
136     return objectNames;
140 Foam::wordList Foam::objectRegistry::names(const word& ClassName) const
142     wordList objectNames(size());
144     label count=0;
145     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
146     {
147         if (iter()->type() == ClassName)
148         {
149             objectNames[count++] = iter()->name();
150         }
151     }
153     objectNames.setSize(count);
155     return objectNames;
159 Foam::fileName Foam::objectRegistry::mangleFileName
161     const fileName& fName
162 ) const
164     return fName;
168 const Foam::objectRegistry& Foam::objectRegistry::subRegistry
170     const word& name
171 ) const
173     return lookupObject<objectRegistry>(name);
177 Foam::label Foam::objectRegistry::getEvent() const
179     label curEvent = event_++;
181     if (event_ == labelMax)
182     {
183         WarningIn("objectRegistry::getEvent() const")
184             << "Event counter has overflowed. Resetting counter on all"
185             << " dependent objects." << endl
186             << "This might cause extra evaluations." << endl;
188         // Reset event counter
189         curEvent = 1;
190         event_ = 2;
192         for (const_iterator iter = begin(); iter != end(); ++iter)
193         {
194             const regIOobject& io = *iter();
196             if (objectRegistry::debug)
197             {
198                 Pout<< "objectRegistry::getEvent() : "
199                     << "resetting count on " << iter.key() << endl;
200             }
202             if (io.eventNo() != 0)
203             {
204                 const_cast<regIOobject&>(io).eventNo() = curEvent;
205             }
206         }
207     }
209     return curEvent;
213 bool Foam::objectRegistry::checkIn(regIOobject& io) const
215     if (objectRegistry::debug)
216     {
217         Pout<< "objectRegistry::checkIn(regIOobject&) : "
218             << name() << " : checking in " << io.name()
219             << endl;
220     }
222     return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
226 bool Foam::objectRegistry::checkOut(regIOobject& io) const
228     iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
230     if (iter != end())
231     {
232         if (objectRegistry::debug)
233         {
234             Pout<< "objectRegistry::checkOut(regIOobject&) : "
235                 << name() << " : checking out " << io.name()
236                 << endl;
237         }
239         if (iter() != &io)
240         {
241             if (objectRegistry::debug)
242             {
243                 WarningIn("objectRegistry::checkOut(regIOobject&)")
244                     << name() << " : attempt to checkOut copy of " << io.name()
245                     << endl;
246             }
248             return false;
249         }
250         else
251         {
252             regIOobject* object = iter();
254             bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
256             if (io.ownedByRegistry())
257             {
258                 delete object;
259             }
261             return hasErased;
262         }
263     }
264     else
265     {
266         if (objectRegistry::debug)
267         {
268             Pout<< "objectRegistry::checkOut(regIOobject&) : "
269                 << name() << " : could not find " << io.name()
270                 << " in registry " << name()
271                 << endl;
272         }
273     }
275     return false;
279 void Foam::objectRegistry::rename(const word& newName)
281     regIOobject::rename(newName);
283     // adjust dbDir_ as well
284     string::size_type i = dbDir_.rfind('/');
286     if (i == string::npos)
287     {
288         dbDir_ = newName;
289     }
290     else
291     {
292         dbDir_.replace(i+1, string::npos, newName);
293     }
297 bool Foam::objectRegistry::modified() const
299     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
300     {
301         if (iter()->modified())
302         {
303             return true;
304         }
305     }
307     return false;
311 void Foam::objectRegistry::readModifiedObjects()
313     for (iterator iter = begin(); iter != end(); ++iter)
314     {
315         if (objectRegistry::debug)
316         {
317             Pout<< "objectRegistry::readModifiedObjects() : "
318                 << name() << " : Considering reading object "
319                 << iter()->name()
320                 << endl;
321         }
323         iter()->readIfModified();
324     }
328 bool Foam::objectRegistry::readIfModified()
330     readModifiedObjects();
331     return true;
335 bool Foam::objectRegistry::writeObject
337     IOstream::streamFormat fmt,
338     IOstream::versionNumber ver,
339     IOstream::compressionType cmp
340 ) const
342     bool ok = true;
344     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
345     {
346         if (objectRegistry::debug)
347         {
348             Pout<< "objectRegistry::write() : "
349                 << name() << " : Considering writing object "
350                 << iter()->name()
351                 << " with writeOpt " << iter()->writeOpt()
352                 << " to file " << iter()->objectPath()
353                 << endl;
354         }
356         if (iter()->writeOpt() != NO_WRITE)
357         {
358             ok = iter()->writeObject(fmt, ver, cmp) && ok;
359         }
360     }
362     return ok;
366 // ************************************************************************* //