|
|
|
<!-- --- title: OpenFOAM C++ Coding Patterns (registry) -->
|
|
|
|
|
|
|
|
[![home](/icons/home.svg "wiki home")](/home)
|
|
|
|
[![code](/icons/code.svg "coding patterns")][code-patterns]
|
|
|
|
|
|
|
|
***We are happy to incorporate content from volunteers!!***
|
|
|
|
|
|
|
|
## Coding Patterns - objectRegistry
|
|
|
|
|
|
|
|
The `objectRegistry` in OpenFOAM behaves much like a global collection
|
|
|
|
of `regIOobject` items (and things derived from `regIOobject` such as
|
|
|
|
IODictionary, IOField, volScalarField, etc). The items within the
|
|
|
|
collection are the so-called *registered* entries.
|
|
|
|
|
|
|
|
The items known by the `objectRegistry` can either have their storage
|
|
|
|
within the registry or outside of the registry.
|
|
|
|
|
|
|
|
|
|
|
|
[[_TOC_]]
|
|
|
|
|
|
|
|
### Maintaining a cached value
|
|
|
|
|
|
|
|
This is a common use. A derived quantity is calculated within a
|
|
|
|
function object or within a local scope with the following requirements:
|
|
|
|
|
|
|
|
1. item should be persistent (available) for other functions
|
|
|
|
2. reuse or create the item as required.
|
|
|
|
|
|
|
|
```
|
|
|
|
// Retrieve existing (if any)
|
|
|
|
auto* fieldPtr = mesh.getObjectPtr<volScalarField>(resultName_);
|
|
|
|
|
|
|
|
if (!fieldPtr)
|
|
|
|
{
|
|
|
|
// It does not exist, create a new one. Note use of raw pointer.
|
|
|
|
fieldPtr = new volScalarField
|
|
|
|
(
|
|
|
|
IOobject
|
|
|
|
(
|
|
|
|
resultName_,
|
|
|
|
mesh.time().timeName(),
|
|
|
|
mesh.thisDb(),
|
|
|
|
IOobject::NO_READ,
|
|
|
|
IOobject::NO_WRITE
|
|
|
|
// implicit: IOobject::REGISTER
|
|
|
|
),
|
|
|
|
mesh,
|
|
|
|
dimensionedScalar(dimless, Zero)
|
|
|
|
// implicit: calculatedType()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Pass pointer ownership to the registry
|
|
|
|
mesh.objectRegistry::store(fieldPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The field
|
|
|
|
auto* result = *fieldPtr;
|
|
|
|
|
|
|
|
// Perform some calculations...
|
|
|
|
```
|
|
|
|
|
|
|
|
----
|
|
|
|
|
|
|
|
Copyright (C) 2023 OpenCFD Ltd.
|
|
|
|
|
|
|
|
[code-patterns]: /coding/patterns/patterns |