objectRegistry.C 10 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2004-2010, 2015-2019 OpenCFD Ltd.
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7 8
     \\/     M anipulation  |
-------------------------------------------------------------------------------
                            | Copyright (C) 2011-2017 OpenFOAM Foundation
Henry's avatar
Henry committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "objectRegistry.H"
#include "Time.H"
30
#include "predicates.H"
Henry's avatar
Henry committed
31 32 33 34 35

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
36
    defineTypeNameAndDebug(objectRegistry, 0);
Henry's avatar
Henry committed
37 38 39
}


40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //

namespace Foam
{

// Templated implementation for erase() with iterator range.
// Prefer not to expose directly.
template<class InputIter>
static label eraseImpl(objectRegistry& obr, InputIter first, InputIter last)
{
    label changed = 0;

    for
    (
        const label nTotal = obr.size();
        changed < nTotal && first != last; // Terminate early
        ++first
    )
    {
        if (obr.erase(*first))
        {
            ++changed;
        }
    }

    return changed;
}

} // End namespace Foam


Henry's avatar
Henry committed
71 72 73 74 75 76 77 78 79 80
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

bool Foam::objectRegistry::parentNotTime() const
{
    return (&parent_ != dynamic_cast<const objectRegistry*>(&time_));
}


// * * * * * * * * * * * * * * * * Constructors *  * * * * * * * * * * * * * //

81
Foam::objectRegistry::objectRegistry(const Time& t, const label nObjects)
Henry's avatar
Henry committed
82 83 84 85 86
:
    regIOobject
    (
        IOobject
        (
87
            word::validate(t.caseName()),
88
            t.path(),
Henry's avatar
Henry committed
89 90 91 92 93 94 95
            t,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE,
            false
        ),
        true    // to flag that this is the top-level regIOobject
    ),
96
    HashTable<regIOobject*>(nObjects),
Henry's avatar
Henry committed
97 98 99 100 101 102 103
    time_(t),
    parent_(t),
    dbDir_(name()),
    event_(1)
{}


104
Foam::objectRegistry::objectRegistry(const IOobject& io, const label nObjects)
Henry's avatar
Henry committed
105 106
:
    regIOobject(io),
107
    HashTable<regIOobject*>(nObjects),
Henry's avatar
Henry committed
108 109 110 111 112 113 114 115 116 117 118 119 120
    time_(io.time()),
    parent_(io.db()),
    dbDir_(parent_.dbDir()/local()/name()),
    event_(1)
{
    writeOpt() = IOobject::AUTO_WRITE;
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::objectRegistry::~objectRegistry()
{
121
    objectRegistry::clear();
Henry's avatar
Henry committed
122 123 124 125 126
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

127 128 129 130 131 132
Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classes() const
{
    return classesImpl(*this, predicates::always());
}


133 134 135 136 137 138 139
Foam::label Foam::objectRegistry::count(const char* clsName) const
{
    // No nullptr check - only called with string literals
    return count(static_cast<word>(clsName));
}


Henry's avatar
Henry committed
140 141 142 143 144 145 146 147 148 149 150 151
Foam::wordList Foam::objectRegistry::names() const
{
    return HashTable<regIOobject*>::toc();
}


Foam::wordList Foam::objectRegistry::sortedNames() const
{
    return HashTable<regIOobject*>::sortedToc();
}


152
Foam::wordList Foam::objectRegistry::names(const char* clsName) const
Henry's avatar
Henry committed
153
{
154 155
    // No nullptr check - only called with string literals
    return names(static_cast<word>(clsName));
Henry's avatar
Henry committed
156 157 158
}


159
Foam::wordList Foam::objectRegistry::sortedNames(const char* clsName) const
Henry's avatar
Henry committed
160
{
161 162
    // No nullptr check - only called with string literals
    return sortedNames(static_cast<word>(clsName));
Henry's avatar
Henry committed
163 164 165 166 167 168
}


const Foam::objectRegistry& Foam::objectRegistry::subRegistry
(
    const word& name,
169 170
    const bool forceCreate,
    const bool recursive
Henry's avatar
Henry committed
171 172
) const
{
173
    if (forceCreate && !foundObject<objectRegistry>(name, recursive))
Henry's avatar
Henry committed
174
    {
175
        objectRegistry* subObr = new objectRegistry
Henry's avatar
Henry committed
176 177 178 179 180 181 182 183 184 185
        (
            IOobject
            (
                name,
                time().constant(),
                *this,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            )
        );
186
        subObr->store();
Henry's avatar
Henry committed
187
    }
188 189

    return lookupObject<objectRegistry>(name, recursive);
Henry's avatar
Henry committed
190 191 192 193 194 195 196 197 198 199 200
}


Foam::label Foam::objectRegistry::getEvent() const
{
    label curEvent = event_++;

    if (event_ == labelMax)
    {
        if (objectRegistry::debug)
        {
201
            WarningInFunction
Henry's avatar
Henry committed
202 203 204 205 206 207 208 209 210
                << "Event counter has overflowed. "
                << "Resetting counter on all dependent objects." << nl
                << "This might cause extra evaluations." << endl;
        }

        // Reset event counter
        curEvent = 1;
        event_ = 2;

211 212
        // No need to reset dependent objects; overflow is now handled
        // in regIOobject::upToDate
Henry's avatar
Henry committed
213 214 215 216 217 218 219 220 221 222 223 224
    }

    return curEvent;
}


bool Foam::objectRegistry::checkIn(regIOobject& io) const
{
    if (objectRegistry::debug)
    {
        Pout<< "objectRegistry::checkIn(regIOobject&) : "
            << name() << " : checking in " << io.name()
225
            << " of type " << io.type()
Henry's avatar
Henry committed
226 227 228
            << endl;
    }

229 230 231 232 233 234 235 236 237 238 239 240 241
    objectRegistry& obr = const_cast<objectRegistry&>(*this);

    bool ok = obr.insert(io.name(), &io);

    if (!ok && objectRegistry::debug)
    {
        WarningInFunction
            << name() << " : attempted to checkIn object with name "
            << io.name() << " which was already checked in"
            << endl;
    }

    return ok;
Henry's avatar
Henry committed
242 243 244 245 246
}


bool Foam::objectRegistry::checkOut(regIOobject& io) const
{
247 248 249
    objectRegistry& obr = const_cast<objectRegistry&>(*this);

    iterator iter = obr.find(io.name());
Henry's avatar
Henry committed
250

251
    if (iter.found())
Henry's avatar
Henry committed
252 253 254 255
    {
        if (objectRegistry::debug)
        {
            Pout<< "objectRegistry::checkOut(regIOobject&) : "
256
                << name() << " : checking out " << io.name()
Henry's avatar
Henry committed
257 258 259
                << endl;
        }

260
        if (iter.val() != &io)
Henry's avatar
Henry committed
261 262 263
        {
            if (objectRegistry::debug)
            {
264
                WarningInFunction
Henry's avatar
Henry committed
265 266 267 268 269 270 271 272
                    << name() << " : attempt to checkOut copy of "
                    << iter.key()
                    << endl;
            }

            return false;
        }

273 274
        return obr.erase(iter);
    }
Henry's avatar
Henry committed
275 276


277
    if (objectRegistry::debug)
Henry's avatar
Henry committed
278
    {
279 280 281
        Pout<< "objectRegistry::checkOut(regIOobject&) : "
            << name() << " : could not find " << io.name() << " in registry"
            << endl;
Henry's avatar
Henry committed
282 283 284 285 286 287
    }

    return false;
}


288 289 290 291 292 293
bool Foam::objectRegistry::checkOut(const word& key) const
{
    return const_cast<objectRegistry&>(*this).erase(key);
}


294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
void Foam::objectRegistry::clear()
{
    // Free anything owned by the registry
    for (iterator iter = begin(); iter != end(); ++iter)
    {
        regIOobject* ptr = iter.val();

        if (ptr && ptr->ownedByRegistry())
        {
            delete ptr;
        }
    }

    HashTable<regIOobject*>::clear();
}


void Foam::objectRegistry::clearStorage()
{
    objectRegistry::clear();
    HashTable<regIOobject*>::clearStorage();
}


318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
bool Foam::objectRegistry::erase(const iterator& iter)
{
    // Free anything owned by the registry

    if (iter.found())
    {
        regIOobject* ptr = iter.val();

        bool ok = HashTable<regIOobject*>::erase(iter);

        if (ptr && ptr->ownedByRegistry())
        {
            delete ptr;
        }

        return ok;
    }

    return false;
}


bool Foam::objectRegistry::erase(const word& key)
{
    return erase(find(key));
}


Foam::label Foam::objectRegistry::erase(std::initializer_list<word> keys)
{
     return eraseImpl(*this, keys.begin(), keys.end());
}


Foam::label Foam::objectRegistry::erase(const UList<word>& keys)
{
    return eraseImpl(*this, keys.begin(), keys.end());
}


Henry's avatar
Henry committed
358 359 360 361
void Foam::objectRegistry::rename(const word& newName)
{
    regIOobject::rename(newName);

362 363
    // Adjust dbDir_ as well
    const auto i = dbDir_.rfind('/');
Henry's avatar
Henry committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377

    if (i == string::npos)
    {
        dbDir_ = newName;
    }
    else
    {
        dbDir_.replace(i+1, string::npos, newName);
    }
}


bool Foam::objectRegistry::modified() const
{
378
    for (const_iterator iter = cbegin(); iter != cend(); ++iter)
Henry's avatar
Henry committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    {
        if (iter()->modified())
        {
            return true;
        }
    }

    return false;
}


void Foam::objectRegistry::readModifiedObjects()
{
    for (iterator iter = begin(); iter != end(); ++iter)
    {
        if (objectRegistry::debug)
        {
            Pout<< "objectRegistry::readModifiedObjects() : "
                << name() << " : Considering reading object "
                << iter.key() << endl;
        }

        iter()->readIfModified();
    }
}


bool Foam::objectRegistry::readIfModified()
{
    readModifiedObjects();
    return true;
}


bool Foam::objectRegistry::writeObject
(
    IOstream::streamFormat fmt,
    IOstream::versionNumber ver,
417 418
    IOstream::compressionType cmp,
    const bool valid
Henry's avatar
Henry committed
419 420 421 422
) const
{
    bool ok = true;

423
    for (const_iterator iter = cbegin(); iter != cend(); ++iter)
Henry's avatar
Henry committed
424 425 426 427 428 429
    {
        if (objectRegistry::debug)
        {
            Pout<< "objectRegistry::write() : "
                << name() << " : Considering writing object "
                << iter.key()
430
                << " of type " << iter()->type()
Henry's avatar
Henry committed
431 432 433 434 435 436 437
                << " with writeOpt " << iter()->writeOpt()
                << " to file " << iter()->objectPath()
                << endl;
        }

        if (iter()->writeOpt() != NO_WRITE)
        {
438
            ok = iter()->writeObject(fmt, ver, cmp, valid) && ok;
Henry's avatar
Henry committed
439 440 441 442 443 444 445 446
        }
    }

    return ok;
}


// ************************************************************************* //