objectRegistryTemplates.C 10.4 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8 9
    Copyright (C) 2011-2015 OpenFOAM Foundation
    Copyright (C) 2016-2019 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    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.
18 19 20 21 22 23 24

    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
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29

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

#include "objectRegistry.H"
30
#include "predicates.H"
31
#include <type_traits>
32

33
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
34

35
// Templated implementation for classes()
36
template<class MatchPredicate>
37 38 39
Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
(
    const objectRegistry& list,
40
    const MatchPredicate& matchName
41
)
42
{
43
    HashTable<wordHashSet> summary(2*list.size());
44

45 46
    // Summary (key,val) = (class-name, object-names)
    forAllConstIters(list, iter)
47
    {
48
        const regIOobject* obj = iter.val();
49 50

        if (matchName(obj->name()))
51
        {
52
            // Create entry (if needed) and insert
53
            summary(obj->type()).insert(obj->name());
54 55 56
        }
    }

57
    return summary;
58 59 60
}


61 62 63 64 65 66 67 68 69 70 71 72 73
// Templated implementation for count()
template<class MatchPredicate1, class MatchPredicate2>
Foam::label Foam::objectRegistry::countImpl
(
    const objectRegistry& list,
    const MatchPredicate1& matchClass,
    const MatchPredicate2& matchName
)
{
    label count = 0;

    forAllConstIters(list, iter)
    {
74
        const regIOobject* obj = iter.val();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

        if (matchClass(obj->type()) && matchName(obj->name()))
        {
            ++count;
        }
    }

    return count;
}


// Templated implementation for count()
template<class Type, class MatchPredicate>
Foam::label Foam::objectRegistry::countTypeImpl
(
    const objectRegistry& list,
    const MatchPredicate& matchName
)
{
    label count = 0;

    forAllConstIters(list, iter)
    {
98
        const regIOobject* obj = iter.val();
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

        if
        (
            (std::is_void<Type>::value || isA<Type>(*obj))
         && matchName(obj->name())
        )
        {
            ++count;
        }
    }

    return count;
}


114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
// Templated implementation for names(), sortedNames()
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::objectRegistry::namesImpl
(
    const objectRegistry& list,
    const MatchPredicate1& matchClass,
    const MatchPredicate2& matchName,
    const bool doSort
)
{
    wordList objNames(list.size());

    label count=0;
    forAllConstIters(list, iter)
    {
129
        const regIOobject* obj = iter.val();
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

        if (matchClass(obj->type()) && matchName(obj->name()))
        {
            objNames[count] = obj->name();
            ++count;
        }
    }

    objNames.resize(count);

    if (doSort)
    {
        Foam::sort(objNames);
    }

    return objNames;
}


149 150 151
// Templated implementation for names(), sortedNames()
template<class Type, class MatchPredicate>
Foam::wordList Foam::objectRegistry::namesTypeImpl
152 153
(
    const objectRegistry& list,
154
    const MatchPredicate& matchName,
155 156
    const bool doSort
)
157
{
158
    wordList objNames(list.size());
159 160

    label count = 0;
161
    forAllConstIters(list, iter)
162
    {
163
        const regIOobject* obj = iter.val();
164 165 166 167 168 169

        if
        (
            (std::is_void<Type>::value || isA<Type>(*obj))
         && matchName(obj->name())
        )
170
        {
171
            objNames[count] = obj->name();
172
            ++count;
173 174 175
        }
    }

176
    objNames.resize(count);
177 178 179 180 181

    if (doSort)
    {
        Foam::sort(objNames);
    }
182

183
    return objNames;
184 185 186
}


187 188
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

189 190 191 192 193 194
template<class MatchPredicate>
Foam::HashTable<Foam::wordHashSet>
Foam::objectRegistry::classes
(
    const MatchPredicate& matchName
) const
195
{
196
    return classesImpl(*this, matchName);
197 198
}

199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
template<class MatchPredicate>
Foam::label Foam::objectRegistry::count
(
    const MatchPredicate& matchClass
) const
{
    return countImpl(*this, matchClass, predicates::always());
}


template<class MatchPredicate1, class MatchPredicate2>
Foam::label Foam::objectRegistry::count
(
    const MatchPredicate1& matchClass,
    const MatchPredicate2& matchName
) const
{
    return countImpl(*this, matchClass, matchName);
}


template<class Type, class MatchPredicate>
Foam::label Foam::objectRegistry::count
(
    const MatchPredicate& matchName
) const
{
    return countTypeImpl<Type>(*this, matchName);
}


template<class Type>
Foam::label Foam::objectRegistry::count
(
    const bool strict
) const
{
    label nObjects = 0;

    forAllConstIters(*this, iter)
    {
241
        const regIOobject* obj = iter.val();
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

        if
        (
            std::is_void<Type>::value
         || (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
        )
        {
            ++nObjects;
        }
    }

    return nObjects;
}


257 258 259 260 261
template<class MatchPredicate>
Foam::wordList Foam::objectRegistry::names
(
    const MatchPredicate& matchClass
) const
262
{
263
    return namesImpl(*this, matchClass, predicates::always(), false);
264 265 266
}


267 268 269 270 271 272
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::objectRegistry::names
(
    const MatchPredicate1& matchClass,
    const MatchPredicate2& matchName
) const
273
{
274
    return namesImpl(*this, matchClass, matchName, false);
275 276 277 278
}


template<class Type>
279 280 281 282 283 284 285 286 287 288 289
Foam::wordList Foam::objectRegistry::names() const
{
    return namesTypeImpl<Type>(*this, predicates::always(), false);
}


template<class Type, class MatchPredicate>
Foam::wordList Foam::objectRegistry::names
(
    const MatchPredicate& matchName
) const
290
{
291
    return namesTypeImpl<Type>(*this, matchName, false);
292 293
}

294

295 296 297 298 299
template<class MatchPredicate>
Foam::wordList Foam::objectRegistry::sortedNames
(
    const MatchPredicate& matchClass
) const
300
{
301
    return namesImpl(*this, matchClass, predicates::always(), true);
302 303
}

304

305 306 307 308 309 310
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::objectRegistry::sortedNames
(
    const MatchPredicate1& matchClass,
    const MatchPredicate2& matchName
) const
311
{
312
    return namesImpl(*this, matchClass, matchName, true);
313 314 315 316
}


template<class Type>
317
Foam::wordList Foam::objectRegistry::sortedNames() const
318
{
319
    return namesTypeImpl<Type>(*this, predicates::always(), true);
320 321 322
}


323
template<class Type, class MatchPredicate>
324 325
Foam::wordList Foam::objectRegistry::sortedNames
(
326
    const MatchPredicate& matchName
327 328
) const
{
329
    return namesTypeImpl<Type>(*this, matchName, true);
330 331 332
}


333
template<class Type>
334 335 336 337
Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass
(
    const bool strict
) const
338 339 340
{
    HashTable<const Type*> objectsOfClass(size());

341
    forAllConstIters(*this, iter)
342
    {
343
        const regIOobject* obj = iter.val();
344 345

        if (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
346
        {
347
            objectsOfClass.insert(obj->name(), dynamic_cast<const Type*>(obj));
348 349 350 351 352 353 354
        }
    }

    return objectsOfClass;
}


355 356 357 358 359 360 361 362
template<class Type>
Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
(
    const bool strict
)
{
    HashTable<Type*> objectsOfClass(size());

363
    forAllIters(*this, iter)
364
    {
365
        regIOobject* obj = iter.val();
366 367

        if (strict ? isType<Type>(*obj) : bool(isA<Type>(*obj)))
368
        {
369
            objectsOfClass.insert(obj->name(), dynamic_cast<Type*>(obj));
370 371 372 373 374 375 376
        }
    }

    return objectsOfClass;
}


377
template<class Type>
378 379 380 381 382
bool Foam::objectRegistry::foundObject
(
    const word& name,
    const bool recursive
) const
383
{
384
    return this->cfindObject<Type>(name, recursive);
385 386 387 388
}


template<class Type>
389
const Type* Foam::objectRegistry::cfindObject
390 391 392 393
(
    const word& name,
    const bool recursive
) const
394
{
395
    return dynamic_cast<const Type*>(this->cfindIOobject(name, recursive));
396
}
397

398 399 400 401 402 403 404 405 406

template<class Type>
const Type* Foam::objectRegistry::findObject
(
    const word& name,
    const bool recursive
) const
{
    return this->cfindObject<Type>(name, recursive);
407 408 409 410
}


template<class Type>
411 412 413 414 415 416
Type* Foam::objectRegistry::findObject
(
    const word& name,
    const bool recursive
)
{
417
    return const_cast<Type*>(this->cfindObject<Type>(name, recursive));
418 419 420 421 422
}


template<class Type>
Type* Foam::objectRegistry::getObjectPtr
423 424 425 426 427
(
    const word& name,
    const bool recursive
) const
{
428
    return const_cast<Type*>(this->cfindObject<Type>(name, recursive));
429 430 431 432
}


template<class Type>
433
const Type& Foam::objectRegistry::lookupObject
434 435 436 437 438
(
    const word& name,
    const bool recursive
) const
{
439
    const_iterator iter = cfind(name);
440

441
    if (iter.found())
442
    {
443 444 445
        const Type* ptr = dynamic_cast<const Type*>(iter());

        if (ptr)
446
        {
447
            return *ptr;
448
        }
449 450 451 452 453 454

        FatalErrorInFunction
            << nl
            << "    lookup of " << name << " from objectRegistry "
            << this->name()
            << " successful\n    but it is not a " << Type::typeName
455
            << ", it is a " << (*iter)->type()
456
            << abort(FatalError);
457 458 459
    }
    else if (recursive && this->parentNotTime())
    {
460
        return parent_.lookupObject<Type>(name, recursive);
461 462
    }

463 464 465 466 467 468 469 470 471 472
    FatalErrorInFunction
        << nl
        << "    request for " << Type::typeName
        << " " << name << " from objectRegistry " << this->name()
        << " failed\n    available objects of type " << Type::typeName
        << " are" << nl
        << names<Type>()
        << abort(FatalError);

    return NullObjectRef<Type>();
473 474 475 476
}


template<class Type>
477
Type& Foam::objectRegistry::lookupObjectRef
478 479 480 481 482
(
    const word& name,
    const bool recursive
) const
{
483 484
    const Type& ref = this->lookupObject<Type>(name, recursive);
    // The above will already fail if things didn't work
485

486
    return const_cast<Type&>(ref);
487 488 489 490
}


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