mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add Function1 caching selector
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2017 OpenFOAM Foundation
|
||||
Copyright (C) 2018-2020 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2021 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -76,6 +76,7 @@ SourceFiles
|
||||
|
||||
#include "function1Base.H"
|
||||
#include "Field.H"
|
||||
#include "HashPtrTable.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -191,6 +192,32 @@ public:
|
||||
);
|
||||
|
||||
|
||||
// Caching Selectors - accept wildcards in dictionary
|
||||
|
||||
//- Selector with external storage of Function1.
|
||||
//- This also allows wildcard matches in a dictionary
|
||||
static refPtr<Function1<Type>> New
|
||||
(
|
||||
HashPtrTable<Function1<Type>>& cache,
|
||||
const word& entryName,
|
||||
const dictionary& dict,
|
||||
enum keyType::option matchOpt = keyType::LITERAL,
|
||||
const bool mandatory = true
|
||||
);
|
||||
|
||||
/// //- Selector with external storage of Function1.
|
||||
/// //- This also allows wildcard matches in a dictionary.
|
||||
/// // If the default value is used, stores an entry as "default"
|
||||
/// static refPtr<Function1<Type>> NewOrDefault
|
||||
/// (
|
||||
/// HashPtrTable<Function1<Type>>& cache,
|
||||
/// const word& entryName,
|
||||
/// const dictionary& dict,
|
||||
/// const Type& deflt,
|
||||
/// enum keyType::option matchOpt = keyType::LITERAL
|
||||
/// );
|
||||
|
||||
|
||||
//- Destructor
|
||||
virtual ~Function1() = default;
|
||||
|
||||
|
||||
@ -209,4 +209,104 @@ Foam::Function1<Type>::NewIfPresent
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
Foam::refPtr<Foam::Function1<Type>>
|
||||
Foam::Function1<Type>::New
|
||||
(
|
||||
HashPtrTable<Function1<Type>>& cache,
|
||||
|
||||
const word& entryName,
|
||||
const dictionary& dict,
|
||||
enum keyType::option matchOpt,
|
||||
const bool mandatory
|
||||
)
|
||||
{
|
||||
// Use the dictionary to find the keyword (allowing wildcards).
|
||||
// Alternative would be to have
|
||||
// a HashTable where the key type uses a wildcard match
|
||||
|
||||
|
||||
refPtr<Function1<Type>> fref; // return value
|
||||
|
||||
// Try for direct cache hit
|
||||
fref.cref(cache.get(entryName));
|
||||
|
||||
if (fref)
|
||||
{
|
||||
return fref;
|
||||
}
|
||||
|
||||
|
||||
// Lookup from dictionary
|
||||
const entry* eptr = dict.findEntry(entryName, matchOpt);
|
||||
|
||||
if (eptr)
|
||||
{
|
||||
// Use keyword (potentially a wildcard) instead of entry name
|
||||
const auto& kw = eptr->keyword();
|
||||
|
||||
// Try for a cache hit
|
||||
fref.cref(cache.get(kw));
|
||||
|
||||
if (!fref)
|
||||
{
|
||||
// Create new entry
|
||||
auto fauto
|
||||
(
|
||||
Function1<Type>::New
|
||||
(
|
||||
kw,
|
||||
eptr, // Already resolved
|
||||
dict,
|
||||
word::null,
|
||||
mandatory
|
||||
)
|
||||
);
|
||||
|
||||
if (fauto)
|
||||
{
|
||||
// Cache the newly created function
|
||||
fref.cref(fauto.get());
|
||||
cache.set(kw, fauto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mandatory && !fref)
|
||||
{
|
||||
FatalIOErrorInFunction(dict)
|
||||
<< "No match for " << entryName << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return fref;
|
||||
}
|
||||
|
||||
|
||||
/// template<class Type>
|
||||
/// Foam::refPtr<Foam::Function1<Type>>
|
||||
/// Foam::Function1<Type>::NewOrDefault
|
||||
/// (
|
||||
/// HashPtrTable<Function1<Type>>& cache,
|
||||
///
|
||||
/// const word& entryName,
|
||||
/// const dictionary& dict,
|
||||
/// const Type& deflt,
|
||||
/// enum keyType::option matchOpt
|
||||
/// )
|
||||
/// {
|
||||
/// auto fref
|
||||
/// (
|
||||
/// Function1<Type>::New(entryName, dict, cache, matchOpt, false)
|
||||
/// );
|
||||
///
|
||||
/// if (!fref)
|
||||
/// {
|
||||
/// fref.reset(new Function1Types::Constant<Type>("default", deflt));
|
||||
/// }
|
||||
///
|
||||
/// return fref;
|
||||
/// }
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
Reference in New Issue
Block a user