From f084309b377fc07fb56299db5b8c8d231fe487da Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sat, 2 Nov 2019 19:46:12 +0100 Subject: [PATCH] ENH: add refCast variant with an index to convey the context - can be helpful, for example, when the refCast has been used on a boundary patch. Knowing which patch index triggered the problem helps with isolating the issue. --- src/OpenFOAM/db/typeInfo/typeInfo.H | 24 +++++++++++++++++++ .../nutWallFunctionFvPatchScalarField.C | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/OpenFOAM/db/typeInfo/typeInfo.H b/src/OpenFOAM/db/typeInfo/typeInfo.H index 83f754f04d..3a93efbf6c 100644 --- a/src/OpenFOAM/db/typeInfo/typeInfo.H +++ b/src/OpenFOAM/db/typeInfo/typeInfo.H @@ -145,6 +145,30 @@ inline To& refCast(From& r) } +//- Reference type cast template function. +// As per dynamicCast, but handles type names for the error messages +// via the virtual type() method. +// Can use index to convey the context. +template +inline To& refCast(From& r, const label index) +{ + try + { + return dynamic_cast(r); + } + catch (const std::bad_cast&) + { + FatalErrorInFunction + << "Attempt to cast type " << r.type() + << " to type " << To::typeName + << " at index " << index + << abort(FatalError); + + return dynamic_cast(r); + } +} + + //- Reference type cast template function. // As per dynamicCast, but handles type names for the error messages // via the virtual type() method. diff --git a/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C b/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C index edbe662950..eb8d25d48f 100644 --- a/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C +++ b/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C @@ -188,7 +188,8 @@ Foam::nutWallFunctionFvPatchScalarField::nutw return refCast ( - turbModel.nut()().boundaryField()[patchi] + turbModel.nut()().boundaryField()[patchi], + patchi ); }