OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

objectRegistry.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "objectRegistry.H"
00028 #include "Time.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::objectRegistry, 0);
00033 
00034 
00035 // * * * * * * * * * * * * * * * * Constructors *  * * * * * * * * * * * * * //
00036 
00037 Foam::objectRegistry::objectRegistry
00038 (
00039     const Time& t,
00040     const label nIoObjects
00041 )
00042 :
00043     regIOobject
00044     (
00045         IOobject
00046         (
00047             string::validate<word>(t.caseName()),
00048             "",
00049             t,
00050             IOobject::NO_READ,
00051             IOobject::AUTO_WRITE,
00052             false
00053         ),
00054         true    // to flag that this is the top-level regIOobject
00055     ),
00056     HashTable<regIOobject*>(nIoObjects),
00057     time_(t),
00058     parent_(t),
00059     dbDir_(name()),
00060     event_(1)
00061 {}
00062 
00063 
00064 Foam::objectRegistry::objectRegistry
00065 (
00066     const IOobject& io,
00067     const label nIoObjects
00068 )
00069 :
00070     regIOobject(io),
00071     HashTable<regIOobject*>(nIoObjects),
00072     time_(io.time()),
00073     parent_(io.db()),
00074     dbDir_(parent_.dbDir()/local()/name()),
00075     event_(1)
00076 {
00077     writeOpt() = IOobject::AUTO_WRITE;
00078 }
00079 
00080 
00081 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00082 
00083 Foam::objectRegistry::~objectRegistry()
00084 {
00085     List<regIOobject*> myObjects(size());
00086     label nMyObjects = 0;
00087 
00088     for (iterator iter = begin(); iter != end(); ++iter)
00089     {
00090         if (iter()->ownedByRegistry())
00091         {
00092             myObjects[nMyObjects++] = iter();
00093         }
00094     }
00095 
00096     for (label i=0; i<nMyObjects; i++)
00097     {
00098         checkOut(*myObjects[i]);
00099     }
00100 }
00101 
00102 
00103 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00104 
00105 Foam::wordList Foam::objectRegistry::names() const
00106 {
00107     wordList objectNames(size());
00108 
00109     label count=0;
00110     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00111     {
00112         objectNames[count++] = iter()->name();
00113     }
00114 
00115     return objectNames;
00116 }
00117 
00118 
00119 Foam::wordList Foam::objectRegistry::names(const word& ClassName) const
00120 {
00121     wordList objectNames(size());
00122 
00123     label count=0;
00124     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00125     {
00126         if (iter()->type() == ClassName)
00127         {
00128             objectNames[count++] = iter()->name();
00129         }
00130     }
00131 
00132     objectNames.setSize(count);
00133 
00134     return objectNames;
00135 }
00136 
00137 
00138 const Foam::objectRegistry& Foam::objectRegistry::subRegistry
00139 (
00140     const word& name
00141 ) const
00142 {
00143     return lookupObject<objectRegistry>(name);
00144 }
00145 
00146 
00147 Foam::label Foam::objectRegistry::getEvent() const
00148 {
00149     label curEvent = event_++;
00150 
00151     if (event_ == labelMax)
00152     {
00153         WarningIn("objectRegistry::getEvent() const")
00154             << "Event counter has overflowed. Resetting counter on all"
00155             << " dependent objects." << endl
00156             << "This might cause extra evaluations." << endl;
00157 
00158         // Reset event counter
00159         curEvent = 1;
00160         event_ = 2;
00161 
00162         for (const_iterator iter = begin(); iter != end(); ++iter)
00163         {
00164             const regIOobject& io = *iter();
00165 
00166             if (objectRegistry::debug)
00167             {
00168                 Pout<< "objectRegistry::getEvent() : "
00169                     << "resetting count on " << iter.key() << endl;
00170             }
00171 
00172             if (io.eventNo() != 0)
00173             {
00174                 const_cast<regIOobject&>(io).eventNo() = curEvent;
00175             }
00176         }
00177     }
00178 
00179     return curEvent;
00180 }
00181 
00182 
00183 bool Foam::objectRegistry::checkIn(regIOobject& io) const
00184 {
00185     if (objectRegistry::debug)
00186     {
00187         Pout<< "objectRegistry::checkIn(regIOobject&) : "
00188             << name() << " : checking in " << io.name()
00189             << endl;
00190     }
00191 
00192     return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
00193 }
00194 
00195 
00196 bool Foam::objectRegistry::checkOut(regIOobject& io) const
00197 {
00198     iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
00199 
00200     if (iter != end())
00201     {
00202         if (objectRegistry::debug)
00203         {
00204             Pout<< "objectRegistry::checkOut(regIOobject&) : "
00205                 << name() << " : checking out " << io.name()
00206                 << endl;
00207         }
00208 
00209         if (iter() != &io)
00210         {
00211             if (objectRegistry::debug)
00212             {
00213                 WarningIn("objectRegistry::checkOut(regIOobject&)")
00214                     << name() << " : attempt to checkOut copy of " << io.name()
00215                     << endl;
00216             }
00217 
00218             return false;
00219         }
00220         else
00221         {
00222             regIOobject* object = iter();
00223 
00224             bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
00225 
00226             if (io.ownedByRegistry())
00227             {
00228                 delete object;
00229             }
00230 
00231             return hasErased;
00232         }
00233     }
00234     else
00235     {
00236         if (objectRegistry::debug)
00237         {
00238             Pout<< "objectRegistry::checkOut(regIOobject&) : "
00239                 << name() << " : could not find " << io.name()
00240                 << " in registry " << name()
00241                 << endl;
00242         }
00243     }
00244 
00245     return false;
00246 }
00247 
00248 
00249 void Foam::objectRegistry::rename(const word& newName)
00250 {
00251     regIOobject::rename(newName);
00252 
00253     // adjust dbDir_ as well
00254     string::size_type i = dbDir_.rfind('/');
00255 
00256     if (i == string::npos)
00257     {
00258         dbDir_ = newName;
00259     }
00260     else
00261     {
00262         dbDir_.replace(i+1, string::npos, newName);
00263     }
00264 }
00265 
00266 
00267 bool Foam::objectRegistry::modified() const
00268 {
00269     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00270     {
00271         if (iter()->modified())
00272         {
00273             return true;
00274         }
00275     }
00276 
00277     return false;
00278 }
00279 
00280 
00281 void Foam::objectRegistry::readModifiedObjects()
00282 {
00283     for (iterator iter = begin(); iter != end(); ++iter)
00284     {
00285         if (objectRegistry::debug)
00286         {
00287             Pout<< "objectRegistry::readModifiedObjects() : "
00288                 << name() << " : Considering reading object "
00289                 << iter()->name()
00290                 << endl;
00291         }
00292 
00293         iter()->readIfModified();
00294     }
00295 }
00296 
00297 
00298 bool Foam::objectRegistry::readIfModified()
00299 {
00300     readModifiedObjects();
00301     return true;
00302 }
00303 
00304 
00305 bool Foam::objectRegistry::writeObject
00306 (
00307     IOstream::streamFormat fmt,
00308     IOstream::versionNumber ver,
00309     IOstream::compressionType cmp
00310 ) const
00311 {
00312     bool ok = true;
00313 
00314     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00315     {
00316         if (objectRegistry::debug)
00317         {
00318             Pout<< "objectRegistry::write() : "
00319                 << name() << " : Considering writing object "
00320                 << iter()->name()
00321                 << " with writeOpt " << iter()->writeOpt()
00322                 << " to file " << iter()->objectPath()
00323                 << endl;
00324         }
00325 
00326         if (iter()->writeOpt() != NO_WRITE)
00327         {
00328             ok = iter()->writeObject(fmt, ver, cmp) && ok;
00329         }
00330     }
00331 
00332     return ok;
00333 }
00334 
00335 
00336 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd