ENH: refCast: give better error messages. Fixes #953.

This commit is contained in:
mattijs
2018-07-26 16:55:49 +01:00
parent b2be9fa3ff
commit 8aa7b1353f
29 changed files with 73 additions and 28 deletions

View File

@ -100,6 +100,27 @@ inline To& dynamicCast(From& r)
} }
//- Reference type cast template function,
// wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
template<class To, class From>
inline To& dynamicCast(From& r, const dictionary& d)
{
try
{
return dynamic_cast<To&>(r);
}
catch (std::bad_cast&)
{
FatalIOErrorInFunction(d)
<< "Attempt to cast type " << typeid(r).name()
<< " to type " << typeid(To).name()
<< abort(FatalIOError);
return dynamic_cast<To&>(r);
}
}
//- Reference type cast template function. //- Reference type cast template function.
// As per dynamicCast, but handles type names via the virtual type() method. // As per dynamicCast, but handles type names via the virtual type() method.
template<class To, class From> template<class To, class From>
@ -121,6 +142,27 @@ inline To& refCast(From& r)
} }
//- Reference type cast template function.
// As per dynamicCast, but handles type names via the virtual type() method.
template<class To, class From>
inline To& refCast(From& r, const dictionary& d)
{
try
{
return dynamic_cast<To&>(r);
}
catch (std::bad_cast&)
{
FatalIOErrorInFunction(d)
<< "Attempt to cast type " << r.type()
<< " to type " << To::typeName
<< abort(FatalIOError);
return dynamic_cast<To&>(r);
}
}
//- Check the typeid //- Check the typeid
template<class TestType, class Type> template<class TestType, class Type>
inline bool isType(const Type& t) inline bool isType(const Type& t)

View File

@ -51,7 +51,7 @@ Foam::cyclicPointPatchField<Type>::cyclicPointPatchField
) )
: :
coupledPointPatchField<Type>(p, iF, dict), coupledPointPatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicPointPatch>(p)) cyclicPatch_(refCast<const cyclicPointPatch>(p, dict))
{ {
if (!isType<cyclicPointPatch>(p)) if (!isType<cyclicPointPatch>(p))
{ {

View File

@ -49,7 +49,7 @@ Foam::processorPointPatchField<Type>::processorPointPatchField
) )
: :
coupledPointPatchField<Type>(p, iF, dict), coupledPointPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorPointPatch>(p)) procPatch_(refCast<const processorPointPatch>(p, dict))
{} {}

View File

@ -52,7 +52,7 @@ Foam::processorCyclicPointPatchField<Type>::processorCyclicPointPatchField
) )
: :
coupledPointPatchField<Type>(p, iF, dict), coupledPointPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorCyclicPointPatch>(p)), procPatch_(refCast<const processorCyclicPointPatch>(p, dict)),
receiveBuf_(0) receiveBuf_(0)
{} {}

View File

@ -48,7 +48,7 @@ Foam::symmetryPlanePointPatchField<Type>::symmetryPlanePointPatchField
) )
: :
basicSymmetryPointPatchField<Type>(p, iF, dict), basicSymmetryPointPatchField<Type>(p, iF, dict),
symmetryPlanePatch_(refCast<const symmetryPlanePointPatch>(p)) symmetryPlanePatch_(refCast<const symmetryPlanePointPatch>(p, dict))
{ {
if (!isType<symmetryPlanePointPatch>(p)) if (!isType<symmetryPlanePointPatch>(p))
{ {

View File

@ -76,7 +76,7 @@ Foam::coupledFaPatchField<Type>::coupledFaPatchField
const dictionary& dict const dictionary& dict
) )
: :
lduInterfaceField(refCast<const lduInterface>(p)), lduInterfaceField(refCast<const lduInterface>(p, dict)),
faPatchField<Type>(p, iF, dict) faPatchField<Type>(p, iF, dict)
{} {}

View File

@ -75,7 +75,7 @@ Foam::cyclicFaPatchField<Type>::cyclicFaPatchField
) )
: :
coupledFaPatchField<Type>(p, iF, dict), coupledFaPatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicFaPatch>(p)) cyclicPatch_(refCast<const cyclicFaPatch>(p, dict))
{ {
if (!isA<cyclicFaPatch>(p)) if (!isA<cyclicFaPatch>(p))
{ {

View File

@ -92,7 +92,7 @@ Foam::processorFaPatchField<Type>::processorFaPatchField
) )
: :
coupledFaPatchField<Type>(p, iF, dict), coupledFaPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorFaPatch>(p)) procPatch_(refCast<const processorFaPatch>(p, dict))
{ {
if (!isType<processorFaPatch>(p)) if (!isType<processorFaPatch>(p))
{ {

View File

@ -74,7 +74,7 @@ Foam::cyclicFaePatchField<Type>::cyclicFaePatchField
) )
: :
coupledFaePatchField<Type>(p, iF, dict), coupledFaePatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicFaPatch>(p)) cyclicPatch_(refCast<const cyclicFaPatch>(p, dict))
{ {
if (!isType<cyclicFaPatch>(p)) if (!isType<cyclicFaPatch>(p))
{ {

View File

@ -87,7 +87,7 @@ Foam::processorFaePatchField<Type>::processorFaePatchField
) )
: :
coupledFaePatchField<Type>(p, iF, dict), coupledFaePatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorFaPatch>(p)) procPatch_(refCast<const processorFaPatch>(p, dict))
{ {
if (!isType<processorFaPatch>(p)) if (!isType<processorFaPatch>(p))
{ {

View File

@ -75,7 +75,7 @@ Foam::coupledFvPatchField<Type>::coupledFvPatchField
const bool valueRequired const bool valueRequired
) )
: :
LduInterfaceField<Type>(refCast<const lduInterface>(p)), LduInterfaceField<Type>(refCast<const lduInterface>(p, dict)),
fvPatchField<Type>(p, iF, dict, valueRequired) fvPatchField<Type>(p, iF, dict, valueRequired)
{} {}

View File

@ -49,7 +49,7 @@ Foam::cyclicFvPatchField<Type>::cyclicFvPatchField
) )
: :
coupledFvPatchField<Type>(p, iF, dict, false), coupledFvPatchField<Type>(p, iF, dict, false),
cyclicPatch_(refCast<const cyclicFvPatch>(p)) cyclicPatch_(refCast<const cyclicFvPatch>(p, dict))
{ {
if (!isA<cyclicFvPatch>(p)) if (!isA<cyclicFvPatch>(p))
{ {

View File

@ -51,7 +51,7 @@ Foam::cyclicACMIFvPatchField<Type>::cyclicACMIFvPatchField
: :
cyclicACMILduInterfaceField(), cyclicACMILduInterfaceField(),
coupledFvPatchField<Type>(p, iF, dict, dict.found("value")), coupledFvPatchField<Type>(p, iF, dict, dict.found("value")),
cyclicACMIPatch_(refCast<const cyclicACMIFvPatch>(p)) cyclicACMIPatch_(refCast<const cyclicACMIFvPatch>(p, dict))
{ {
if (!isA<cyclicACMIFvPatch>(p)) if (!isA<cyclicACMIFvPatch>(p))
{ {

View File

@ -48,7 +48,7 @@ Foam::cyclicAMIFvPatchField<Type>::cyclicAMIFvPatchField
: :
cyclicAMILduInterfaceField(), cyclicAMILduInterfaceField(),
coupledFvPatchField<Type>(p, iF, dict, dict.found("value")), coupledFvPatchField<Type>(p, iF, dict, dict.found("value")),
cyclicAMIPatch_(refCast<const cyclicAMIFvPatch>(p)) cyclicAMIPatch_(refCast<const cyclicAMIFvPatch>(p, dict))
{ {
if (!isA<cyclicAMIFvPatch>(p)) if (!isA<cyclicAMIFvPatch>(p))
{ {

View File

@ -76,7 +76,7 @@ Foam::processorFvPatchField<Type>::processorFvPatchField
) )
: :
coupledFvPatchField<Type>(p, iF, dict, dict.found("value")), coupledFvPatchField<Type>(p, iF, dict, dict.found("value")),
procPatch_(refCast<const processorFvPatch>(p)), procPatch_(refCast<const processorFvPatch>(p, dict)),
sendBuf_(0), sendBuf_(0),
receiveBuf_(0), receiveBuf_(0),
outstandingSendRequest_(-1), outstandingSendRequest_(-1),

View File

@ -51,7 +51,7 @@ Foam::processorCyclicFvPatchField<Type>::processorCyclicFvPatchField
) )
: :
processorFvPatchField<Type>(p, iF, dict), processorFvPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorCyclicFvPatch>(p)) procPatch_(refCast<const processorCyclicFvPatch>(p, dict))
{ {
if (!isType<processorCyclicFvPatch>(p)) if (!isType<processorCyclicFvPatch>(p))
{ {

View File

@ -73,7 +73,7 @@ Foam::symmetryPlaneFvPatchField<Type>::symmetryPlaneFvPatchField
) )
: :
basicSymmetryFvPatchField<Type>(p, iF, dict), basicSymmetryFvPatchField<Type>(p, iF, dict),
symmetryPlanePatch_(refCast<const symmetryPlaneFvPatch>(p)) symmetryPlanePatch_(refCast<const symmetryPlaneFvPatch>(p, dict))
{ {
if (!isType<symmetryPlaneFvPatch>(p)) if (!isType<symmetryPlaneFvPatch>(p))
{ {

View File

@ -96,7 +96,8 @@ activeBaffleVelocityFvPatchVectorField
( (
refCast<const cyclicFvPatch> refCast<const cyclicFvPatch>
( (
p.boundaryMesh()[cyclicPatchLabel_] p.boundaryMesh()[cyclicPatchLabel_],
dict
).neighbFvPatch().Sf() ).neighbFvPatch().Sf()
), ),
openFraction_(readScalar(dict.lookup("openFraction"))), openFraction_(readScalar(dict.lookup("openFraction"))),

View File

@ -115,7 +115,8 @@ activePressureForceBaffleVelocityFvPatchVectorField
initCyclicSf_ = p.boundaryMesh()[cyclicPatchLabel_].Sf(); initCyclicSf_ = p.boundaryMesh()[cyclicPatchLabel_].Sf();
nbrCyclicSf_ = refCast<const cyclicFvPatch> nbrCyclicSf_ = refCast<const cyclicFvPatch>
( (
p.boundaryMesh()[cyclicPatchLabel_] p.boundaryMesh()[cyclicPatchLabel_],
dict
).neighbFvPatch().Sf(); ).neighbFvPatch().Sf();
} }

View File

@ -94,7 +94,8 @@ mappedVelocityFluxFixedValueFvPatchField
const mappedPatchBase& mpp = refCast<const mappedPatchBase> const mappedPatchBase& mpp = refCast<const mappedPatchBase>
( (
this->patch().patch() this->patch().patch(),
dict
); );
if (mpp.mode() == mappedPolyPatch::NEARESTCELL) if (mpp.mode() == mappedPolyPatch::NEARESTCELL)
{ {

View File

@ -48,7 +48,7 @@ Foam::cyclicFvsPatchField<Type>::cyclicFvsPatchField
) )
: :
coupledFvsPatchField<Type>(p, iF, dict), coupledFvsPatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicFvPatch>(p)) cyclicPatch_(refCast<const cyclicFvPatch>(p, dict))
{ {
if (!isA<cyclicFvPatch>(p)) if (!isA<cyclicFvPatch>(p))
{ {

View File

@ -72,7 +72,7 @@ Foam::cyclicACMIFvsPatchField<Type>::cyclicACMIFvsPatchField
) )
: :
coupledFvsPatchField<Type>(p, iF, dict), coupledFvsPatchField<Type>(p, iF, dict),
cyclicACMIPatch_(refCast<const cyclicACMIFvPatch>(p)) cyclicACMIPatch_(refCast<const cyclicACMIFvPatch>(p, dict))
{ {
if (!isA<cyclicACMIFvPatch>(p)) if (!isA<cyclicACMIFvPatch>(p))
{ {

View File

@ -72,7 +72,7 @@ Foam::cyclicAMIFvsPatchField<Type>::cyclicAMIFvsPatchField
) )
: :
coupledFvsPatchField<Type>(p, iF, dict), coupledFvsPatchField<Type>(p, iF, dict),
cyclicAMIPatch_(refCast<const cyclicAMIFvPatch>(p)) cyclicAMIPatch_(refCast<const cyclicAMIFvPatch>(p, dict))
{ {
if (!isA<cyclicAMIFvPatch>(p)) if (!isA<cyclicAMIFvPatch>(p))
{ {

View File

@ -61,7 +61,7 @@ Foam::processorFvsPatchField<Type>::processorFvsPatchField
) )
: :
coupledFvsPatchField<Type>(p, iF, dict), coupledFvsPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorFvPatch>(p)) procPatch_(refCast<const processorFvPatch>(p, dict))
{ {
if (!isType<processorFvPatch>(p)) if (!isType<processorFvPatch>(p))
{ {

View File

@ -61,7 +61,7 @@ Foam::processorCyclicFvsPatchField<Type>::processorCyclicFvsPatchField
) )
: :
coupledFvsPatchField<Type>(p, iF, dict), coupledFvsPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorCyclicFvPatch>(p)) procPatch_(refCast<const processorCyclicFvPatch>(p, dict))
{ {
if (!isType<processorCyclicFvPatch>(p)) if (!isType<processorCyclicFvPatch>(p))
{ {

View File

@ -53,7 +53,7 @@ Foam::cyclicACMIPointPatchField<Type>::cyclicACMIPointPatchField
) )
: :
coupledPointPatchField<Type>(p, iF, dict), coupledPointPatchField<Type>(p, iF, dict),
cyclicACMIPatch_(refCast<const cyclicACMIPointPatch>(p)), cyclicACMIPatch_(refCast<const cyclicACMIPointPatch>(p, dict)),
ppiPtr_(nullptr), ppiPtr_(nullptr),
nbrPpiPtr_(nullptr) nbrPpiPtr_(nullptr)
{ {

View File

@ -53,7 +53,7 @@ Foam::cyclicAMIPointPatchField<Type>::cyclicAMIPointPatchField
) )
: :
coupledPointPatchField<Type>(p, iF, dict), coupledPointPatchField<Type>(p, iF, dict),
cyclicAMIPatch_(refCast<const cyclicAMIPointPatch>(p)), cyclicAMIPatch_(refCast<const cyclicAMIPointPatch>(p, dict)),
ppiPtr_(nullptr), ppiPtr_(nullptr),
nbrPpiPtr_(nullptr) nbrPpiPtr_(nullptr)
{ {

View File

@ -236,7 +236,7 @@ energyRegionCoupledFvPatchScalarField
) )
: :
coupledFvPatchField<scalar>(p, iF, dict), coupledFvPatchField<scalar>(p, iF, dict),
regionCoupledPatch_(refCast<const regionCoupledBaseFvPatch>(p)), regionCoupledPatch_(refCast<const regionCoupledBaseFvPatch>(p, dict)),
method_(UNDEFINED), method_(UNDEFINED),
nbrThermoPtr_(nullptr), nbrThermoPtr_(nullptr),
thermoPtr_(nullptr) thermoPtr_(nullptr)

View File

@ -180,7 +180,7 @@ void thermalBaffleFvPatchScalarField::createPatchMesh()
} }
const mappedPatchBase& mpp = const mappedPatchBase& mpp =
refCast<const mappedPatchBase>(patch().patch()); refCast<const mappedPatchBase>(patch().patch(), dict_);
const word coupleGroup(mpp.coupleGroup()); const word coupleGroup(mpp.coupleGroup());