- makes it easier to use for local or alternative storage.
Eg,
```
tmp<volScalarField> tfld;
tfld.cref(obj.cfindObject<volScalarField>("name"));
if (!tfld)
{
tfld = volScalarField::New("name", ...);
}
```
- old code just checked for pointer vs non-pointer.
Should actually treat CREF and REF types differently
Overseen in commit be058bec7d. Only affects develop branch
ENH: improved naming consistency in tmp, refPtr
- also use long-form to check for pointer type instead of the isTmp()
method. Makes differences between PTR, CREF, REF easier to spot.
STYLE: typeName() for tmp, refPtr is static
- improves flexibility. Can tag a tmp as allowing non-const access to
the reference and skip additional const_cast in following code. For
example,
tmp<volScalarField> tfld(nullptr);
auto* ptr = getObjectPtr<volScalarField>("field");
if (ptr)
{
tfld.ref(*ptr);
}
else
{
tfld.reset(volScalarField::New(...));
}
auto& fld = tfld.ref();
ENH: renamed tmpNrc to refPtr
- the name 'refPtr' (reference|pointer) should be easier to remember
than tmpNrc (tmp, but non-ref-counted).
- provide tmpNrc typedef and header for code compatibility
NOTE
- in some places refPtr and tmp can be used instead of a
std::reference_wrapper for handling external references.
Unlike std::reference_wrapper, it can be default constructed
(holding nothing), whereas reference_wrapper may need a dummy
reference. However, the lifetime extension of references _may_ be
better with reference_wrapper.
- previously this was marked as '= delete' for consistency with
assignment from an empty pointer being a runtime error.
However, these can be considered semantically different and it makes
sense to permit this as equivalent to reset(nullptr).
This change does not break existing code since the operator was
previously unavailable (deleted).
STYLE: refactor tmp operator=(T*)
- delegate to reset() after initial checks
- combine reset() methods by adding a default parameter
- improve top-level visibility of empty/valid/get methods for symmetry
symmetry with autoPtr, future adjustment
- improves similarity to autoPtr. Simplifies coding.
Example,
tmp<volScalarField> tfield;
// sometime later...
tfield.reset
(
volScalarField::New("myfield", mesh, dimensionedScalar(Zero))
);
- as per tmp, disallow tmpNrc assignment from literal nullptr
- as per autoPtr, allow explicit test as bool (same as valid).
- similar to autoPtr and unique_ptr. Returns the pointer value without
any checks. This provides a simple way for use to use either
an autoPtr or a tmp for local memory management without accidentally
stealing the pointer.
Eg,
volVectorField* ptr;
tmp<volVectorField> tempField;
if (someField.valid())
{
ptr = someField.get();
}
else
{
tempField.reset(new volVectorField(....));
ptr = tmpField.get();
}
const volVectorField& withField = *ptr;
STYLE: make more tmp methods noexcept
Improve alignment of its behaviour with std::shared_ptr
- element_type typedef
- swap, reset methods
* additional reference access methods:
cref()
returns a const reference, synonymous with operator().
This provides a more verbose alternative to using the '()' operator
when that is desired.
Mnemonic: a const form of 'ref()'
constCast()
returns a non-const reference, regardless if the underlying object
itself is a managed pointer or a const object.
This is similar to ref(), but more permissive.
Mnemonic: const_cast<>
Using the constCast() method greatly reduces the amount of typing
and reading. And since the data type is already defined via the tmp
template parameter, the type deduction is automatically known.
Previously,
const tmp<volScalarField>& tfld;
const_cast<volScalarField&>(tfld()).rename("name");
volScalarField& fld = const_cast<volScalarField&>(tfld());
Now,
tfld.constCast().rename("name");
auto& fld = tfld.constCast();
--
BUG: attempts to move tmp value that may still be shared.
- old code simply checked isTmp() to decide if the contents could be
transfered. However, this means that the content of a shared tmp
would be removed, leaving other instances without content.
* movable() method checks that for a non-null temporary that is
unique (not shared).
- simplify structure, removed unused constuctors.
- transfer from base objects via '=' assignment removed as being too
non-transparent
- add New factory method with perfect forwarding.
which reduces the number of potential problems with the reuse of
temporary objects.
In order to avoid unnecessary creation of tmp's referring to temporary
objects the assignment operator now transfers ownership of the object
and resets the argument.
The deprecated non-const tmp functionality is now on the compiler switch
NON_CONST_TMP which can be enabled by adding -DNON_CONST_TMP to EXE_INC
in the Make/options file. However, it is recommended to upgrade all
code to the new safer tmp by using the '.ref()' member function rather
than the non-const '()' dereference operator when non-const access to
the temporary object is required.
Please report any problems on Mantis.
Henry G. Weller
CFD Direct.
in case of tmp misuse.
Simplified tmp reuse pattern in field algebra to use tmp copy and
assignment rather than the complex delayed call to 'ptr()'.
Removed support for unused non-const 'REF' storage of non-tmp objects due to C++
limitation in constructor overloading: if both tmp(T&) and tmp(const T&)
constructors are provided resolution is ambiguous.
The turbulence libraries have been upgraded and '-DCONST_TMP' option
specified in the 'options' file to switch to the new 'tmp' behavior.
This change requires that the de-reference operator '()' returns a
const-reference to the object stored irrespective of the const-ness of
object stored and the new member function 'ref()' is provided to return
an non-const reference to stored object which throws a fatal error if the
stored object is const.
In order to smooth the transition to this new safer 'tmp' the now
deprecated and unsafe non-const de-reference operator '()' is still
provided by default but may be switched-off with the compilation switch
'CONST_TMP'.
The main OpenFOAM library has already been upgraded and '-DCONST_TMP'
option specified in the 'options' file to switch to the new 'tmp'
behavior. The rest of OpenFOAM-dev will be upgraded over the following
few weeks.
Henry G. Weller
CFD Direct
To be used instead of zeroGradientFvPatchField for temporary fields for
which zero-gradient extrapolation is use to evaluate the boundary field
but avoiding fields derived from temporary field using field algebra
inheriting the zeroGradient boundary condition by the reuse of the
temporary field storage.
zeroGradientFvPatchField should not be used as the default patch field
for any temporary fields and should be avoided for non-temporary fields
except where it is clearly appropriate;
extrapolatedCalculatedFvPatchField and calculatedFvPatchField are
generally more suitable defaults depending on the manner in which the
boundary values are specified or evaluated.
The entire OpenFOAM-dev code-base has been updated following the above
recommendations.
Henry G. Weller
CFD Direct