diff --git a/applications/solvers/multiphase/compressibleInterFoam/twoPhaseMixtureThermo/twoPhaseMixtureThermo.H b/applications/solvers/multiphase/compressibleInterFoam/twoPhaseMixtureThermo/twoPhaseMixtureThermo.H index c712e5a47f..9209cc41d2 100644 --- a/applications/solvers/multiphase/compressibleInterFoam/twoPhaseMixtureThermo/twoPhaseMixtureThermo.H +++ b/applications/solvers/multiphase/compressibleInterFoam/twoPhaseMixtureThermo/twoPhaseMixtureThermo.H @@ -135,14 +135,14 @@ public: virtual volScalarField& he() { NotImplemented; - return thermo1_->he(); + return const_cast(volScalarField::null()); } //- Enthalpy/Internal energy [J/kg] virtual const volScalarField& he() const { NotImplemented; - return thermo1_->he(); + return volScalarField::null(); } //- Enthalpy/Internal energy @@ -213,7 +213,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Heat capacity at constant volume [J/kg/K] @@ -236,7 +236,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Gamma = Cp/Cv [] diff --git a/applications/solvers/multiphase/compressibleMultiphaseInterFoam/multiphaseMixtureThermo/multiphaseMixtureThermo.H b/applications/solvers/multiphase/compressibleMultiphaseInterFoam/multiphaseMixtureThermo/multiphaseMixtureThermo.H index 472bd285aa..c5e90dada5 100644 --- a/applications/solvers/multiphase/compressibleMultiphaseInterFoam/multiphaseMixtureThermo/multiphaseMixtureThermo.H +++ b/applications/solvers/multiphase/compressibleMultiphaseInterFoam/multiphaseMixtureThermo/multiphaseMixtureThermo.H @@ -243,14 +243,14 @@ public: virtual volScalarField& he() { NotImplemented; - return phases_[0].thermo().he(); + return const_cast(volScalarField::null()); } //- Enthalpy/Internal energy [J/kg] virtual const volScalarField& he() const { NotImplemented; - return phases_[0].thermo().he(); + return volScalarField::null(); } //- Enthalpy/Internal energy @@ -327,7 +327,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Heat capacity at constant volume [J/kg/K] @@ -350,7 +350,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Gamma = Cp/Cv [] diff --git a/applications/solvers/multiphase/interCondensatingEvaporatingFoam/temperaturePhaseChangeTwoPhaseMixtures/twoPhaseMixtureEThermo/twoPhaseMixtureEThermo.H b/applications/solvers/multiphase/interCondensatingEvaporatingFoam/temperaturePhaseChangeTwoPhaseMixtures/twoPhaseMixtureEThermo/twoPhaseMixtureEThermo.H index cedad45329..ab5b58febe 100644 --- a/applications/solvers/multiphase/interCondensatingEvaporatingFoam/temperaturePhaseChangeTwoPhaseMixtures/twoPhaseMixtureEThermo/twoPhaseMixtureEThermo.H +++ b/applications/solvers/multiphase/interCondensatingEvaporatingFoam/temperaturePhaseChangeTwoPhaseMixtures/twoPhaseMixtureEThermo/twoPhaseMixtureEThermo.H @@ -86,14 +86,14 @@ public: virtual volScalarField& he() { NotImplemented; - return p(); + return const_cast(volScalarField::null()); } //- Return access to the internal energy field [J/Kg] virtual const volScalarField& he() const { NotImplemented; - return p(); + return volScalarField::null(); } //- Enthalpy/Internal energy @@ -182,7 +182,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Return Cv of the mixture @@ -205,7 +205,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Gamma = Cp/Cv [] diff --git a/applications/test/nullObject/Test-nullObject.C b/applications/test/nullObject/Test-nullObject.C index 5c86e4e500..0f7cd10dad 100644 --- a/applications/test/nullObject/Test-nullObject.C +++ b/applications/test/nullObject/Test-nullObject.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2014 OpenFOAM Foundation - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -48,7 +48,7 @@ class SimpleClass public: //- Default construct - SimpleClass() {} + SimpleClass() = default; }; @@ -73,9 +73,8 @@ void printInfo(const UList& list) int main() { // Test pointer and reference to a class - - SimpleClass* ptrToClass = new SimpleClass; - SimpleClass& refToClass(*ptrToClass); + auto ptrToClass = autoPtr::New(); + auto& refToClass = ptrToClass.ref(); std::cout << "nullObject addr=" << name(&(nullObjectPtr)) << nl @@ -89,13 +88,13 @@ int main() << " pointer:" << name(nullObjectPtr->pointer()) << nl << " value:" << nullObjectPtr->value() << nl << nl; - if (notNull(ptrToClass)) + if (notNull(ptrToClass.get())) { Info<< "Pass: ptrToClass is not null" << nl; } else { - Info<< "FAIL: refToClass is null" << nl; + Info<< "FAIL: ptrToClass is null" << nl; } if (notNull(refToClass)) @@ -110,8 +109,8 @@ int main() // Test pointer and reference to the nullObject - const SimpleClass* ptrToNull(NullObjectPtr()); - const SimpleClass& refToNull(*ptrToNull); + const SimpleClass* ptrToNull = NullObjectPtr(); + const SimpleClass& refToNull = (*ptrToNull); if (isNull(ptrToNull)) { @@ -131,9 +130,6 @@ int main() Info<< "FAIL: refToNull is not null" << nl; } - // Clean-up - delete ptrToClass; - // Test List casting { @@ -152,7 +148,7 @@ int main() // Looks pretty ugly though! NullObject::nullObject = "hello world"; - NullObject::nullObject = labelList({1, 2, 3}); + NullObject::nullObject = Foam::identity(5); Info<< nl; diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H index 31d08d570f..b1e4c7fdd4 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H @@ -120,8 +120,11 @@ public: // Static Member Functions - //- Return a null bitSet reference - inline static const bitSet& null(); + //- Return a null bitSet (reference to a nullObject). + static const bitSet& null() noexcept + { + return NullObjectRef(); + } //- Declare type-name (with debug switch) diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H index 7e9eed8739..f035d0c99f 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetI.H @@ -405,12 +405,6 @@ inline Foam::label Foam::bitSet::find_next(label pos) const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline const Foam::bitSet& Foam::bitSet::null() -{ - return NullObjectRef(); -} - - inline bool Foam::bitSet::all() const { if (empty()) return true; // SIC. boost convention diff --git a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H index 3401179116..7935b876a2 100644 --- a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H +++ b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListList.H @@ -138,8 +138,11 @@ public: // Static Member Functions - //- Return a CompactListList reference to a nullObject - inline static const CompactListList& null(); + //- Return a null CompactListList (reference to a nullObject). + static const CompactListList& null() noexcept + { + return NullObjectRef>(); + } //- Construct by packing together the list of lists template> diff --git a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H index f5a17ba86d..5e5209fcb9 100644 --- a/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H +++ b/src/OpenFOAM/containers/CompactLists/CompactListList/CompactListListI.H @@ -29,15 +29,6 @@ License #include "ListOps.H" #include "SubList.H" -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -inline const Foam::CompactListList& Foam::CompactListList::null() -{ - return NullObjectRef>(); -} - - // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 59e1188bee..1c1f8c8476 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -46,9 +46,10 @@ SourceFiles #include "zero.H" #include "contiguous.H" #include "stdFoam.H" -#include "autoPtr.H" +#include "nullObject.H" #include "Hash.H" #include "ListPolicy.H" +#include "autoPtr.H" // already included by stdFoam.H #include @@ -139,8 +140,12 @@ public: // Static Functions - //- Return a null FixedList - inline static const FixedList& null(); + //- Return a null FixedList (reference to a nullObject). + //- Read/write access is questionable + static const FixedList& null() noexcept + { + return NullObjectRef>(); + } // Constructors diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 4bef6f07b3..7d590c3799 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -28,15 +28,6 @@ License #include "UList.H" -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -inline const Foam::FixedList& Foam::FixedList::null() -{ - return NullObjectRef>(); -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 65caab9fd8..87b9bf9019 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -129,8 +129,12 @@ public: // Static Member Functions - //- Return a null List - inline static const List& null(); + //- Return a null List (reference to a nullObject). + //- Behaves like an empty List. + static const List& null() noexcept + { + return NullObjectRef>(); + } // Constructors diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index 6bab28191b..5bac2f0ee4 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -133,13 +133,6 @@ inline Foam::autoPtr> Foam::List::clone() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline const Foam::List& Foam::List::null() -{ - return NullObjectRef>(); -} - - template inline void Foam::List::clear() { diff --git a/src/OpenFOAM/containers/Lists/List/SubList.H b/src/OpenFOAM/containers/Lists/List/SubList.H index e34f27fcd0..42f2a1dcd6 100644 --- a/src/OpenFOAM/containers/Lists/List/SubList.H +++ b/src/OpenFOAM/containers/Lists/List/SubList.H @@ -73,8 +73,12 @@ public: // Static Functions - //- Return a null SubList - inline static const SubList& null(); + //- Return a null SubList (reference to a nullObject). + //- Behaves like an empty SubList. + static const SubList& null() noexcept + { + return NullObjectRef>(); + } // Generated Methods diff --git a/src/OpenFOAM/containers/Lists/List/SubListI.H b/src/OpenFOAM/containers/Lists/List/SubListI.H index 3b15f4c9e1..f18c31646b 100644 --- a/src/OpenFOAM/containers/Lists/List/SubListI.H +++ b/src/OpenFOAM/containers/Lists/List/SubListI.H @@ -28,15 +28,6 @@ License #include "FixedList.H" -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -inline const Foam::SubList& Foam::SubList::null() -{ - return NullObjectRef>(); -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template @@ -124,7 +115,7 @@ inline Foam::SubList::SubList template inline Foam::UList& Foam::SubList::reset(std::nullptr_t) noexcept { - UList::shallowCopy(nullptr, 0); + UList::shallowCopy(nullptr); return *this; } diff --git a/src/OpenFOAM/containers/Lists/List/UList.H b/src/OpenFOAM/containers/Lists/List/UList.H index 252ec61cc3..ceb50199c5 100644 --- a/src/OpenFOAM/containers/Lists/List/UList.H +++ b/src/OpenFOAM/containers/Lists/List/UList.H @@ -183,8 +183,12 @@ public: // Static Functions - //- Return a UList reference to a nullObject - inline static const UList& null(); + //- Return a null UList (reference to a nullObject). + //- Behaves like an empty UList. + static const UList& null() noexcept + { + return NullObjectRef>(); + } // Public Classes @@ -367,6 +371,9 @@ public: //- Copy the pointer and size inline void shallowCopy(T* __restrict__ ptr, const label len) noexcept; + //- Copy nullptr and zero size + inline void shallowCopy(std::nullptr_t) noexcept; + //- Copy the pointer and size held by the given UList inline void shallowCopy(const UList& list) noexcept; diff --git a/src/OpenFOAM/containers/Lists/List/UListI.H b/src/OpenFOAM/containers/Lists/List/UListI.H index b143475599..74cd4c3bbe 100644 --- a/src/OpenFOAM/containers/Lists/List/UListI.H +++ b/src/OpenFOAM/containers/Lists/List/UListI.H @@ -93,13 +93,6 @@ inline void Foam::UList::fill_uniform(const Foam::zero) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline const Foam::UList& Foam::UList::null() -{ - return NullObjectRef>(); -} - - template inline Foam::label Foam::UList::fcIndex(const label i) const noexcept { @@ -330,6 +323,14 @@ inline void Foam::UList::shallowCopy } +template +inline void Foam::UList::shallowCopy(std::nullptr_t) noexcept +{ + size_ = 0; + v_ = nullptr; +} + + template inline void Foam::UList::shallowCopy(const UList& list) noexcept { diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.C b/src/OpenFOAM/containers/Lists/ListOps/ListOps.C index 9e4dbd83b7..dd73a69686 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.C @@ -116,7 +116,7 @@ Foam::labelListList Foam::invertOneToMany const labelUList& map ) { - labelList sizes(len, Zero); + labelList sizes(len, Foam::zero{}); for (const label newIdx : map) { diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C index 4c70358920..e1914040e3 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C @@ -707,7 +707,7 @@ void Foam::invertManyToMany ) { // The output list sizes - labelList sizes(len, Zero); + labelList sizes(len, Foam::zero{}); for (const InputIntListType& sublist : input) { diff --git a/src/OpenFOAM/expressions/Function1/Function1Expression.C b/src/OpenFOAM/expressions/Function1/Function1Expression.C index dd45c913e7..532344efa8 100644 --- a/src/OpenFOAM/expressions/Function1/Function1Expression.C +++ b/src/OpenFOAM/expressions/Function1/Function1Expression.C @@ -115,7 +115,7 @@ Type Foam::Function1Types::Function1Expression::integrate ) const { NotImplemented; - return Zero; + return Type(); } diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.H index c193f7e176..e7b4d21c6f 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.H @@ -137,8 +137,11 @@ public: // Static Member Functions - //- Return a NullObjectRef DimensionedField - inline static const DimensionedField& null(); + //- Return a null DimensionedField (reference to a nullObject). + static const DimensionedField& null() noexcept + { + return NullObjectRef>(); + } // Constructors diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldI.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldI.H index 5f898c1e6b..3391dc32f4 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldI.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldI.H @@ -28,14 +28,6 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline const Foam::DimensionedField& -Foam::DimensionedField::null() -{ - return NullObjectRef>(); -} - - template inline const typename GeoMesh::Mesh& Foam::DimensionedField::mesh() const noexcept diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SlicedDimensionedField.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SlicedDimensionedField.H index 9f6df361ac..de0eb8a6cc 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SlicedDimensionedField.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SlicedDimensionedField.H @@ -85,7 +85,7 @@ public: ~SlicedDimensionedField() { // Set internalField to nullptr to avoid deletion of underlying field - UList::shallowCopy(UList()); + UList::shallowCopy(nullptr); } }; diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H index 7debed435b..602e6f1cce 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H @@ -97,8 +97,8 @@ public: // Static Member Functions - //- Return a null field - inline static const DynamicField& null() + //- Return a null DynamicField (reference to a nullObject). + static const DynamicField& null() noexcept { return NullObjectRef>(); } diff --git a/src/OpenFOAM/fields/Fields/Field/Field.H b/src/OpenFOAM/fields/Fields/Field/Field.H index 9a2d17c7a4..fb7851edc6 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.H +++ b/src/OpenFOAM/fields/Fields/Field/Field.H @@ -122,8 +122,12 @@ public: // Static Member Functions - //- Return nullObject reference Field - inline static const Field& null(); + //- Return a null Field (reference to a nullObject). + //- Behaves like an empty Field. + static const Field& null() noexcept + { + return NullObjectRef>(); + } // Constructors diff --git a/src/OpenFOAM/fields/Fields/Field/FieldI.H b/src/OpenFOAM/fields/Fields/Field/FieldI.H index a5c37b841b..4c34df88b1 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldI.H +++ b/src/OpenFOAM/fields/Fields/Field/FieldI.H @@ -25,15 +25,6 @@ License \*---------------------------------------------------------------------------*/ -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -inline const Foam::Field& Foam::Field::null() -{ - return NullObjectRef>(); -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/fields/Fields/Field/SubField.H b/src/OpenFOAM/fields/Fields/Field/SubField.H index 48cc8530ff..15e09522a7 100644 --- a/src/OpenFOAM/fields/Fields/Field/SubField.H +++ b/src/OpenFOAM/fields/Fields/Field/SubField.H @@ -69,8 +69,12 @@ public: // Static Member Functions - //- Return nullObject reference SubField - inline static const SubField& null(); + //- Return a null SubField (reference to a nullObject). + //- Behaves like an empty SubField. + static const SubField& null() noexcept + { + return NullObjectRef>(); + } // Constructors diff --git a/src/OpenFOAM/fields/Fields/Field/SubFieldI.H b/src/OpenFOAM/fields/Fields/Field/SubFieldI.H index 738096e039..235f981da7 100644 --- a/src/OpenFOAM/fields/Fields/Field/SubFieldI.H +++ b/src/OpenFOAM/fields/Fields/Field/SubFieldI.H @@ -26,15 +26,6 @@ License \*---------------------------------------------------------------------------*/ -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -inline const Foam::SubField& Foam::SubField::null() -{ - return NullObjectRef>(); -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H index 8c66779bf2..427009b245 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H @@ -154,8 +154,11 @@ public: // Static Member Functions - //- Return a null geometric field - inline static const GeometricField& null(); + //- Return a null GeometricField (reference to a nullObject). + static const GeometricField& null() noexcept + { + return NullObjectRef>(); + } // Constructors diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldI.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldI.H index 90b57d17a4..0f3083ae48 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldI.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldI.H @@ -28,14 +28,6 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template class PatchField, class GeoMesh> -inline const Foam::GeometricField& -Foam::GeometricField::null() -{ - return NullObjectRef>(); -} - - template class PatchField, class GeoMesh> inline const typename Foam::GeometricField::Internal& diff --git a/src/OpenFOAM/fields/GeometricFields/SlicedGeometricField/SlicedGeometricField.C b/src/OpenFOAM/fields/GeometricFields/SlicedGeometricField/SlicedGeometricField.C index 2b378137fd..f77ccd0bfa 100644 --- a/src/OpenFOAM/fields/GeometricFields/SlicedGeometricField/SlicedGeometricField.C +++ b/src/OpenFOAM/fields/GeometricFields/SlicedGeometricField/SlicedGeometricField.C @@ -368,7 +368,7 @@ Foam::SlicedGeometricField:: ~SlicedGeometricField() { // Set internalField to nullptr to avoid deletion of underlying field - UList::shallowCopy(UList()); + UList::shallowCopy(nullptr); } diff --git a/src/OpenFOAM/matrices/Matrix/Matrix.H b/src/OpenFOAM/matrices/Matrix/Matrix.H index 7bff0b2a77..cdfa892253 100644 --- a/src/OpenFOAM/matrices/Matrix/Matrix.H +++ b/src/OpenFOAM/matrices/Matrix/Matrix.H @@ -114,8 +114,12 @@ public: // Static Member Functions - //- Return a null Matrix - inline static const Matrix& null(); + //- Return a null Matrix (reference to a nullObject). + //- Behaves like a empty Matrix. + static const Matrix& null() noexcept + { + return NullObjectRef>(); + } // Iterators diff --git a/src/OpenFOAM/matrices/Matrix/MatrixI.H b/src/OpenFOAM/matrices/Matrix/MatrixI.H index 17f35969cc..b1dc3b1267 100644 --- a/src/OpenFOAM/matrices/Matrix/MatrixI.H +++ b/src/OpenFOAM/matrices/Matrix/MatrixI.H @@ -85,13 +85,6 @@ Foam::Matrix::clone() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline const Foam::Matrix& Foam::Matrix::null() -{ - return NullObjectRef>(); -} - - template inline Foam::label Foam::Matrix::size() const { diff --git a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorCyclicGAMGInterfaceField/processorCyclicGAMGInterfaceField.H b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorCyclicGAMGInterfaceField/processorCyclicGAMGInterfaceField.H index 376631dd7d..5096b8cfbb 100644 --- a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorCyclicGAMGInterfaceField/processorCyclicGAMGInterfaceField.H +++ b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/interfaceFields/processorCyclicGAMGInterfaceField/processorCyclicGAMGInterfaceField.H @@ -102,7 +102,7 @@ public: ) const { NotImplemented; - return autoPtr(nullptr); + return nullptr; } diff --git a/src/OpenFOAM/meshes/lduMesh/lduMesh.C b/src/OpenFOAM/meshes/lduMesh/lduMesh.C index 16e73ddf7c..a6c62c3811 100644 --- a/src/OpenFOAM/meshes/lduMesh/lduMesh.C +++ b/src/OpenFOAM/meshes/lduMesh/lduMesh.C @@ -42,8 +42,7 @@ namespace Foam const Foam::objectRegistry& Foam::lduMesh::thisDb() const { NotImplemented; - const objectRegistry* orPtr_ = nullptr; - return *orPtr_; + return NullObjectRef(); } diff --git a/src/OpenFOAM/primitives/nullObject/nullObject.H b/src/OpenFOAM/primitives/nullObject/nullObject.H index 8d3793f50c..f8d1fc269e 100644 --- a/src/OpenFOAM/primitives/nullObject/nullObject.H +++ b/src/OpenFOAM/primitives/nullObject/nullObject.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2014 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -158,7 +158,7 @@ extern const NullObject* nullObjectPtr; // IOstream Operators -//- Read from Istream consumes no content +//- Read from Istream consumes no content, does not change NullObject inline Istream& operator>>(Istream& is, const NullObject&) noexcept { return is; @@ -173,31 +173,48 @@ inline Ostream& operator<<(Ostream& os, const NullObject&) noexcept // Global Functions -//- Pointer (of type T) to the nullObject +//- Const pointer (of type T) to the nullObject template -inline const T* NullObjectPtr() +inline const T* NullObjectPtr() noexcept { return reinterpret_cast(nullObjectPtr); } -//- Reference (of type T) to the nullObject +//- Non-const pointer (of type T) to the nullObject. +//- Only use when nothing will be written into it! template -inline const T& NullObjectRef() +inline T* NullObjectPtr_constCast() noexcept +{ + return reinterpret_cast(const_cast(nullObjectPtr)); +} + + +//- Const reference (of type T) to the nullObject +template +inline const T& NullObjectRef() noexcept { return *reinterpret_cast(nullObjectPtr); } +//- Non-const reference (of type T) to the nullObject +//- Only use when nothing will be written into it! +template +inline T& NullObjectRef_constCast() noexcept +{ + return *reinterpret_cast(const_cast(nullObjectPtr)); +} + //- True if ptr is a pointer (of type T) to the nullObject template -inline bool isNull(const T* ptr) +inline bool isNull(const T* ptr) noexcept { return ptr == NullObjectPtr(); } //- True if obj is a reference (of type T) to the nullObject template -inline bool isNull(const T& obj) +inline bool isNull(const T& obj) noexcept { return &obj == NullObjectPtr(); } @@ -205,14 +222,14 @@ inline bool isNull(const T& obj) //- True if ptr is not a pointer (of type T) to the nullObject template -inline bool notNull(const T* ptr) +inline bool notNull(const T* ptr) noexcept { return ptr != NullObjectPtr(); } //- True if obj is not a reference (of type T) to the nullObject template -inline bool notNull(const T& obj) +inline bool notNull(const T& obj) noexcept { return &obj != NullObjectPtr(); } diff --git a/src/OpenFOAM/primitives/strings/wordRes/wordRes.H b/src/OpenFOAM/primitives/strings/wordRes/wordRes.H index de7c5d56aa..e665f4d52d 100644 --- a/src/OpenFOAM/primitives/strings/wordRes/wordRes.H +++ b/src/OpenFOAM/primitives/strings/wordRes/wordRes.H @@ -86,8 +86,12 @@ public: // Static Data / Methods - //- Return a null wordRes - a reference to the NullObject - inline static const wordRes& null(); + //- Return a null wordRes (reference to a nullObject). + //- Behaves like a empty wordRes. + static const wordRes& null() noexcept + { + return NullObjectRef(); + } //- Return a wordRes with duplicate entries filtered out. // No distinction made between literals and regular expressions. diff --git a/src/OpenFOAM/primitives/strings/wordRes/wordResI.H b/src/OpenFOAM/primitives/strings/wordRes/wordResI.H index e82a49c89f..2b72076d96 100644 --- a/src/OpenFOAM/primitives/strings/wordRes/wordResI.H +++ b/src/OpenFOAM/primitives/strings/wordRes/wordResI.H @@ -27,12 +27,6 @@ License // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // -inline const Foam::wordRes& Foam::wordRes::null() -{ - return NullObjectRef(); -} - - inline Foam::label Foam::wordRes::first_match ( const UList& selectors, diff --git a/src/fileFormats/ensight/file/ensightFile.H b/src/fileFormats/ensight/file/ensightFile.H index e565f3eb2c..2e16f0e95d 100644 --- a/src/fileFormats/ensight/file/ensightFile.H +++ b/src/fileFormats/ensight/file/ensightFile.H @@ -92,7 +92,7 @@ public: // Static Functions //- Return a null ensightFile - static const ensightFile& null() + static const ensightFile& null() noexcept { return NullObjectRef(); } diff --git a/src/fileFormats/ensight/file/ensightGeoFile.H b/src/fileFormats/ensight/file/ensightGeoFile.H index 86d2d9beed..54c8078a6a 100644 --- a/src/fileFormats/ensight/file/ensightGeoFile.H +++ b/src/fileFormats/ensight/file/ensightGeoFile.H @@ -72,7 +72,7 @@ public: // Static Functions //- Return a null ensightGeoFile - static const ensightGeoFile& null() + static const ensightGeoFile& null() noexcept { return NullObjectRef(); } diff --git a/src/finiteArea/fields/faPatchFields/basic/sliced/slicedFaPatchField.C b/src/finiteArea/fields/faPatchFields/basic/sliced/slicedFaPatchField.C index fb9846b148..650beb7daa 100644 --- a/src/finiteArea/fields/faPatchFields/basic/sliced/slicedFaPatchField.C +++ b/src/finiteArea/fields/faPatchFields/basic/sliced/slicedFaPatchField.C @@ -127,7 +127,7 @@ template Foam::slicedFaPatchField::~slicedFaPatchField() { // Set to nullptr to avoid deletion of underlying field - UList::shallowCopy(UList()); + UList::shallowCopy(nullptr); } diff --git a/src/finiteArea/fields/faePatchFields/basic/sliced/slicedFaePatchField.C b/src/finiteArea/fields/faePatchFields/basic/sliced/slicedFaePatchField.C index fd746a305e..9f0e1818ab 100644 --- a/src/finiteArea/fields/faePatchFields/basic/sliced/slicedFaePatchField.C +++ b/src/finiteArea/fields/faePatchFields/basic/sliced/slicedFaePatchField.C @@ -127,7 +127,7 @@ template Foam::slicedFaePatchField::~slicedFaePatchField() { // Set to nullptr to avoid deletion of underlying field - UList::shallowCopy(UList()); + UList::shallowCopy(nullptr); } diff --git a/src/finiteVolume/fields/fvPatchFields/basic/sliced/slicedFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/basic/sliced/slicedFvPatchField.C index a15239c2bb..c053c9b934 100644 --- a/src/finiteVolume/fields/fvPatchFields/basic/sliced/slicedFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/basic/sliced/slicedFvPatchField.C @@ -127,7 +127,7 @@ template Foam::slicedFvPatchField::~slicedFvPatchField() { // Set to nullptr to avoid deletion of underlying field - UList::shallowCopy(UList()); + UList::shallowCopy(nullptr); } diff --git a/src/finiteVolume/fields/fvsPatchFields/basic/sliced/slicedFvsPatchField.C b/src/finiteVolume/fields/fvsPatchFields/basic/sliced/slicedFvsPatchField.C index 08179b72e8..717918883f 100644 --- a/src/finiteVolume/fields/fvsPatchFields/basic/sliced/slicedFvsPatchField.C +++ b/src/finiteVolume/fields/fvsPatchFields/basic/sliced/slicedFvsPatchField.C @@ -127,7 +127,7 @@ template Foam::slicedFvsPatchField::~slicedFvsPatchField() { // Set to nullptr to avoid deletion of underlying field - UList::shallowCopy(UList()); + UList::shallowCopy(nullptr); } diff --git a/src/finiteVolume/finiteVolume/ddtSchemes/ddtScheme/ddtScheme.C b/src/finiteVolume/finiteVolume/ddtSchemes/ddtScheme/ddtScheme.C index 94a00c2a68..06209c1334 100644 --- a/src/finiteVolume/finiteVolume/ddtSchemes/ddtScheme/ddtScheme.C +++ b/src/finiteVolume/finiteVolume/ddtSchemes/ddtScheme/ddtScheme.C @@ -96,11 +96,7 @@ tmp> ddtScheme::fvcDdt ) { NotImplemented; - - return tmp> - ( - GeometricField::null() - ); + return nullptr; } @@ -113,13 +109,7 @@ tmp> ddtScheme::fvmDdt ) { NotImplemented; - - return tmp>::New - ( - vf, - alpha.dimensions()*rho.dimensions() - *vf.dimensions()*dimVol/dimTime - ); + return nullptr; } @@ -130,15 +120,10 @@ tmp> ddtScheme::fvcDdt ) { NotImplemented; - - return tmp> - ( - GeometricField::null() - ); + return nullptr; } - template tmp ddtScheme::fvcDdtPhiCoeff ( diff --git a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C index 6b15f83645..716a2b1896 100644 --- a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C +++ b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2023 OpenCFD Ltd. + Copyright (C) 2016-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -894,13 +894,13 @@ void Foam::fvMatrix::transferFvMatrixCoeffs() template Foam::lduPrimitiveMeshAssembly* Foam::fvMatrix::lduMeshPtr() { - const lduPrimitiveMeshAssembly* lduAssemMeshPtr = - psi_.mesh().thisDb().objectRegistry::template findObject + return + ( + psi_.mesh().thisDb().objectRegistry::template getObjectPtr < lduPrimitiveMeshAssembly - > (lduAssemblyName_); - - return const_cast(lduAssemMeshPtr); + > (lduAssemblyName_) + ); } diff --git a/src/mesh/blockMesh/PDRblockMesh/PDRblock.C b/src/mesh/blockMesh/PDRblockMesh/PDRblock.C index 5e27e50075..10db4aeb8a 100644 --- a/src/mesh/blockMesh/PDRblockMesh/PDRblock.C +++ b/src/mesh/blockMesh/PDRblockMesh/PDRblock.C @@ -106,12 +106,6 @@ bool Foam::PDRblock::checkMonotonic } -const Foam::PDRblock& Foam::PDRblock::null() -{ - return NullObjectRef(); -} - - // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // void Foam::PDRblock::addDefaultPatches() diff --git a/src/mesh/blockMesh/PDRblockMesh/PDRblock.H b/src/mesh/blockMesh/PDRblockMesh/PDRblock.H index 8df077c4f4..d0302b075b 100644 --- a/src/mesh/blockMesh/PDRblockMesh/PDRblock.H +++ b/src/mesh/blockMesh/PDRblockMesh/PDRblock.H @@ -429,8 +429,11 @@ public: // Static Member Functions - //- Return a PDRblock reference to a nullObject - static const PDRblock& null(); + //- Return a null PDRblock (reference to a nullObject). + static const PDRblock& null() noexcept + { + return NullObjectRef(); + } // Constructors diff --git a/src/optimisation/adjointOptimisation/adjoint/boundaryAdjointContributions/boundaryAdjointContribution/boundaryAdjointContribution.C b/src/optimisation/adjointOptimisation/adjoint/boundaryAdjointContributions/boundaryAdjointContribution/boundaryAdjointContribution.C index a8220f2639..a36d646dff 100644 --- a/src/optimisation/adjointOptimisation/adjoint/boundaryAdjointContributions/boundaryAdjointContribution/boundaryAdjointContribution.C +++ b/src/optimisation/adjointOptimisation/adjoint/boundaryAdjointContributions/boundaryAdjointContribution/boundaryAdjointContribution.C @@ -144,7 +144,7 @@ tmp boundaryAdjointContribution::turbulentDiffusivity() const { NotImplemented; - return tmp(nullptr); + return nullptr; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.H b/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.H index d1e78eed35..f06a90edb8 100644 --- a/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.H +++ b/src/overset/lduPrimitiveProcessorInterface/GAMG/calculatedProcessorGAMGInterfaceField.H @@ -133,7 +133,7 @@ public: ) const { NotImplemented; - return autoPtr(nullptr); + return nullptr; } diff --git a/src/overset/oversetCoupledPolyPatch/oversetGAMGInterfaceField/oversetGAMGInterfaceField.H b/src/overset/oversetCoupledPolyPatch/oversetGAMGInterfaceField/oversetGAMGInterfaceField.H index 5edbd651f6..640a9940a2 100644 --- a/src/overset/oversetCoupledPolyPatch/oversetGAMGInterfaceField/oversetGAMGInterfaceField.H +++ b/src/overset/oversetCoupledPolyPatch/oversetGAMGInterfaceField/oversetGAMGInterfaceField.H @@ -105,7 +105,7 @@ public: ) const { NotImplemented; - return autoPtr(nullptr); + return nullptr; } diff --git a/src/phaseSystemModels/multiphaseInter/phasesSystem/multiphaseInterSystem/multiphaseInterSystem.H b/src/phaseSystemModels/multiphaseInter/phasesSystem/multiphaseInterSystem/multiphaseInterSystem.H index fbcacaefbc..b9dfffb4a4 100644 --- a/src/phaseSystemModels/multiphaseInter/phasesSystem/multiphaseInterSystem/multiphaseInterSystem.H +++ b/src/phaseSystemModels/multiphaseInter/phasesSystem/multiphaseInterSystem/multiphaseInterSystem.H @@ -367,7 +367,7 @@ public: ) const { NotImplemented; - return tmp::New(p); + return nullptr; } //- Return Cv of the mixture diff --git a/src/phaseSystemModels/reactingEuler/multiphaseSystem/phaseModel/PurePhaseModel/PurePhaseModel.C b/src/phaseSystemModels/reactingEuler/multiphaseSystem/phaseModel/PurePhaseModel/PurePhaseModel.C index 6279a080a4..de620d523a 100644 --- a/src/phaseSystemModels/reactingEuler/multiphaseSystem/phaseModel/PurePhaseModel/PurePhaseModel.C +++ b/src/phaseSystemModels/reactingEuler/multiphaseSystem/phaseModel/PurePhaseModel/PurePhaseModel.C @@ -88,7 +88,7 @@ Foam::PurePhaseModel::Y(const word& name) const << "Cannot get a species fraction by name from a pure phase" << exit(FatalError); - return NullObjectRef(); + return volScalarField::null(); } diff --git a/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C b/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C index 5076b34ca8..2653f238ce 100644 --- a/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C +++ b/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C @@ -59,7 +59,7 @@ const Foam::DimensionedField& Foam::basicSolidChemistryModel::RR(const label i) const { NotImplemented; - return (volScalarField::Internal::null()); + return volScalarField::Internal::null(); } @@ -67,14 +67,11 @@ Foam::DimensionedField& Foam::basicSolidChemistryModel::RR(const label i) { NotImplemented; - - return dynamic_cast - ( + return const_cast ( volScalarField::Internal::null() - ) - ); + ); } @@ -86,14 +83,7 @@ Foam::basicSolidChemistryModel::calculateRR ) const { NotImplemented; - - return dynamic_cast&> - ( - const_cast - ( - volScalarField::Internal::null() - ) - ); + return nullptr; } diff --git a/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C b/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C index 89b9fa51e2..ad096ee103 100644 --- a/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C +++ b/src/thermophysicalModels/specie/reaction/Reactions/Reaction/Reaction.C @@ -441,7 +441,7 @@ const Foam::List::specieCoeffs>& Foam::Reaction::glhs() const { NotImplemented; - return NullObjectRef>(); + return List::null(); } @@ -450,7 +450,7 @@ const Foam::List::specieCoeffs>& Foam::Reaction::grhs() const { NotImplemented; - return NullObjectRef>(); + return List::null(); }