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.
This commit is contained in:
Mark Olesen
2019-11-02 19:46:12 +01:00
committed by Andrew Heather
parent 5577606997
commit f084309b37
2 changed files with 26 additions and 1 deletions

View File

@ -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<class To, class From>
inline To& refCast(From& r, const label index)
{
try
{
return dynamic_cast<To&>(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<To&>(r);
}
}
//- Reference type cast template function.
// As per dynamicCast, but handles type names for the error messages
// via the virtual type() method.

View File

@ -188,7 +188,8 @@ Foam::nutWallFunctionFvPatchScalarField::nutw
return
refCast<const nutWallFunctionFvPatchScalarField>
(
turbModel.nut()().boundaryField()[patchi]
turbModel.nut()().boundaryField()[patchi],
patchi
);
}