diff --git a/src/OpenFOAM/db/typeInfo/typeInfo.H b/src/OpenFOAM/db/typeInfo/typeInfo.H index b9a2257c0d..2a985c5dd3 100644 --- a/src/OpenFOAM/db/typeInfo/typeInfo.H +++ b/src/OpenFOAM/db/typeInfo/typeInfo.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -55,6 +55,7 @@ Description #include "error.H" #include "className.H" +#include "nullObject.H" #include #include @@ -105,6 +106,23 @@ inline To& dynamicCast(From& r) } +//- Reference type cast template function, +// wraps dynamic_cast to handle bad_cast exception and generate a null +// reference. +template +inline To& dynamicCastNull(From& r) +{ + try + { + return dynamic_cast(r); + } + catch (const std::bad_cast&) + { + return NullObjectNonConstRef(); + } +} + + //- Reference type cast template function. // As per dynamicCast, but handles type names via the virtual type() method. template @@ -126,6 +144,15 @@ inline To& refCast(From& r) } +//- Reference type cast template function, +// As dynamicCastNull. +template +inline To& refCastNull(From& r) +{ + return dynamicCastNull(r); +} + + //- Check the typeid template inline bool isType(const Type& t) diff --git a/src/OpenFOAM/primitives/nullObject/nullObject.H b/src/OpenFOAM/primitives/nullObject/nullObject.H index ea959a5425..bd9c31b4e1 100644 --- a/src/OpenFOAM/primitives/nullObject/nullObject.H +++ b/src/OpenFOAM/primitives/nullObject/nullObject.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2014-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2014-2024 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -72,6 +72,10 @@ extern const NullObject* nullObjectPtr; template inline const T& NullObjectRef(); +//- Return non-const reference to the nullObject of type T +template +inline T& NullObjectNonConstRef(); + //- Return const pointer to the nullObject of type T template inline const T* NullObjectConstPtr(); diff --git a/src/OpenFOAM/primitives/nullObject/nullObjectI.H b/src/OpenFOAM/primitives/nullObject/nullObjectI.H index 919ab6793f..f88f3c52cb 100644 --- a/src/OpenFOAM/primitives/nullObject/nullObjectI.H +++ b/src/OpenFOAM/primitives/nullObject/nullObjectI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2014-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2014-2024 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -30,9 +30,9 @@ inline const T& Foam::NullObjectRef() } template -inline T&& Foam::NullObjectMove() +inline T& Foam::NullObjectNonConstRef() { - return move(const_cast(*reinterpret_cast(nullObjectPtr))); + return const_cast(*reinterpret_cast(nullObjectPtr)); } template @@ -47,6 +47,12 @@ inline T* Foam::NullObjectPtr() return const_cast(reinterpret_cast(nullObjectPtr)); } +template +inline T&& Foam::NullObjectMove() +{ + return move(const_cast(*reinterpret_cast(nullObjectPtr))); +} + template inline bool Foam::isNull(const T& t)