FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "objectRegistry.H"
00027 #include <OpenFOAM/Time.H>
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 defineTypeNameAndDebug(Foam::objectRegistry, 0);
00032 
00033 
00034 // * * * * * * * * * * * * * * * * Constructors *  * * * * * * * * * * * * * //
00035 
00036 Foam::objectRegistry::objectRegistry
00037 (
00038     const Time& t,
00039     const label nIoObjects
00040 )
00041 :
00042     regIOobject
00043     (
00044         IOobject
00045         (
00046             string::validate<word>(t.caseName()),
00047             "",
00048             t,
00049             IOobject::NO_READ,
00050             IOobject::AUTO_WRITE,
00051             false
00052         ),
00053         true    // to flag that this is the top-level regIOobject
00054     ),
00055     HashTable<regIOobject*>(nIoObjects),
00056     time_(t),
00057     parent_(t),
00058     dbDir_(name()),
00059     event_(1)
00060 {}
00061 
00062 
00063 Foam::objectRegistry::objectRegistry
00064 (
00065     const IOobject& io,
00066     const label nIoObjects
00067 )
00068 :
00069     regIOobject(io),
00070     HashTable<regIOobject*>(nIoObjects),
00071     time_(io.time()),
00072     parent_(io.db()),
00073     dbDir_(parent_.dbDir()/local()/name()),
00074     event_(1)
00075 {
00076     writeOpt() = IOobject::AUTO_WRITE;
00077 }
00078 
00079 
00080 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00081 
00082 Foam::objectRegistry::~objectRegistry()
00083 {
00084     List<regIOobject*> myObjects(size());
00085     label nMyObjects = 0;
00086 
00087     for (iterator iter = begin(); iter != end(); ++iter)
00088     {
00089         if (iter()->ownedByRegistry())
00090         {
00091             myObjects[nMyObjects++] = iter();
00092         }
00093     }
00094 
00095     for (label i=0; i<nMyObjects; i++)
00096     {
00097         checkOut(*myObjects[i]);
00098     }
00099 }
00100 
00101 
00102 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00103 
00104 Foam::wordList Foam::objectRegistry::names() const
00105 {
00106     wordList objectNames(size());
00107 
00108     label count=0;
00109     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00110     {
00111         objectNames[count++] = iter()->name();
00112     }
00113 
00114     return objectNames;
00115 }
00116 
00117 
00118 Foam::wordList Foam::objectRegistry::names(const word& ClassName) const
00119 {
00120     wordList objectNames(size());
00121 
00122     label count=0;
00123     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00124     {
00125         if (iter()->type() == ClassName)
00126         {
00127             objectNames[count++] = iter()->name();
00128         }
00129     }
00130 
00131     objectNames.setSize(count);
00132 
00133     return objectNames;
00134 }
00135 
00136 
00137 const Foam::objectRegistry& Foam::objectRegistry::subRegistry
00138 (
00139     const word& name
00140 ) const
00141 {
00142     return lookupObject<objectRegistry>(name);
00143 }
00144 
00145 
00146 Foam::label Foam::objectRegistry::getEvent() const
00147 {
00148     label curEvent = event_++;
00149 
00150     if (event_ == labelMax)
00151     {
00152         WarningIn("objectRegistry::getEvent() const")
00153             << "Event counter has overflowed. Resetting counter on all"
00154             << " dependent objects." << endl
00155             << "This might cause extra evaluations." << endl;
00156 
00157         // Reset event counter
00158         curEvent = 1;
00159         event_ = 2;
00160 
00161         for (const_iterator iter = begin(); iter != end(); ++iter)
00162         {
00163             const regIOobject& io = *iter();
00164 
00165             if (objectRegistry::debug)
00166             {
00167                 Pout<< "objectRegistry::getEvent() : "
00168                     << "resetting count on " << iter.key() << endl;
00169             }
00170 
00171             if (io.eventNo() != 0)
00172             {
00173                 const_cast<regIOobject&>(io).eventNo() = curEvent;
00174             }
00175         }
00176     }
00177 
00178     return curEvent;
00179 }
00180 
00181 
00182 bool Foam::objectRegistry::checkIn(regIOobject& io) const
00183 {
00184     if (objectRegistry::debug)
00185     {
00186         Pout<< "objectRegistry::checkIn(regIOobject&) : "
00187             << name() << " : checking in " << io.name()
00188             << endl;
00189     }
00190 
00191     return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
00192 }
00193 
00194 
00195 bool Foam::objectRegistry::checkOut(regIOobject& io) const
00196 {
00197     iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
00198 
00199     if (iter != end())
00200     {
00201         if (objectRegistry::debug)
00202         {
00203             Pout<< "objectRegistry::checkOut(regIOobject&) : "
00204                 << name() << " : checking out " << io.name()
00205                 << endl;
00206         }
00207 
00208         if (iter() != &io)
00209         {
00210             if (objectRegistry::debug)
00211             {
00212                 WarningIn("objectRegistry::checkOut(regIOobject&)")
00213                     << name() << " : attempt to checkOut copy of " << io.name()
00214                     << endl;
00215             }
00216 
00217             return false;
00218         }
00219         else
00220         {
00221             regIOobject* object = iter();
00222 
00223             bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
00224 
00225             if (io.ownedByRegistry())
00226             {
00227                 delete object;
00228             }
00229 
00230             return hasErased;
00231         }
00232     }
00233     else
00234     {
00235         if (objectRegistry::debug)
00236         {
00237             Pout<< "objectRegistry::checkOut(regIOobject&) : "
00238                 << name() << " : could not find " << io.name()
00239                 << " in registry " << name()
00240                 << endl;
00241         }
00242     }
00243 
00244     return false;
00245 }
00246 
00247 
00248 void Foam::objectRegistry::rename(const word& newName)
00249 {
00250     regIOobject::rename(newName);
00251 
00252     // adjust dbDir_ as well
00253     string::size_type i = dbDir_.rfind('/');
00254 
00255     if (i == string::npos)
00256     {
00257         dbDir_ = newName;
00258     }
00259     else
00260     {
00261         dbDir_.replace(i+1, string::npos, newName);
00262     }
00263 }
00264 
00265 
00266 bool Foam::objectRegistry::modified() const
00267 {
00268     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00269     {
00270         if (iter()->modified())
00271         {
00272             return true;
00273         }
00274     }
00275 
00276     return false;
00277 }
00278 
00279 
00280 void Foam::objectRegistry::readModifiedObjects()
00281 {
00282     for (iterator iter = begin(); iter != end(); ++iter)
00283     {
00284         if (objectRegistry::debug)
00285         {
00286             Pout<< "objectRegistry::readModifiedObjects() : "
00287                 << name() << " : Considering reading object "
00288                 << iter()->name()
00289                 << endl;
00290         }
00291 
00292         iter()->readIfModified();
00293     }
00294 }
00295 
00296 
00297 bool Foam::objectRegistry::readIfModified()
00298 {
00299     readModifiedObjects();
00300     return true;
00301 }
00302 
00303 
00304 bool Foam::objectRegistry::writeObject
00305 (
00306     IOstream::streamFormat fmt,
00307     IOstream::versionNumber ver,
00308     IOstream::compressionType cmp
00309 ) const
00310 {
00311     bool ok = true;
00312 
00313     for (const_iterator iter = cbegin(); iter != cend(); ++iter)
00314     {
00315         if (objectRegistry::debug)
00316         {
00317             Pout<< "objectRegistry::write() : "
00318                 << name() << " : Considering writing object "
00319                 << iter()->name()
00320                 << " with writeOpt " << iter()->writeOpt()
00321                 << " to file " << iter()->objectPath()
00322                 << endl;
00323         }
00324 
00325         if (iter()->writeOpt() != NO_WRITE)
00326         {
00327             ok = iter()->writeObject(fmt, ver, cmp) && ok;
00328         }
00329     }
00330 
00331     return ok;
00332 }
00333 
00334 
00335 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines