Commit 8aa7b135 authored by mattijs's avatar mattijs
Browse files

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

parent b2be9fa3
......@@ -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.
// As per dynamicCast, but handles type names via the virtual type() method.
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
template<class TestType, class Type>
inline bool isType(const Type& t)
......
......@@ -51,7 +51,7 @@ Foam::cyclicPointPatchField<Type>::cyclicPointPatchField
)
:
coupledPointPatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicPointPatch>(p))
cyclicPatch_(refCast<const cyclicPointPatch>(p, dict))
{
if (!isType<cyclicPointPatch>(p))
{
......
......@@ -49,7 +49,7 @@ Foam::processorPointPatchField<Type>::processorPointPatchField
)
:
coupledPointPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorPointPatch>(p))
procPatch_(refCast<const processorPointPatch>(p, dict))
{}
......
......@@ -52,7 +52,7 @@ Foam::processorCyclicPointPatchField<Type>::processorCyclicPointPatchField
)
:
coupledPointPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorCyclicPointPatch>(p)),
procPatch_(refCast<const processorCyclicPointPatch>(p, dict)),
receiveBuf_(0)
{}
......
......@@ -48,7 +48,7 @@ Foam::symmetryPlanePointPatchField<Type>::symmetryPlanePointPatchField
)
:
basicSymmetryPointPatchField<Type>(p, iF, dict),
symmetryPlanePatch_(refCast<const symmetryPlanePointPatch>(p))
symmetryPlanePatch_(refCast<const symmetryPlanePointPatch>(p, dict))
{
if (!isType<symmetryPlanePointPatch>(p))
{
......
......@@ -76,7 +76,7 @@ Foam::coupledFaPatchField<Type>::coupledFaPatchField
const dictionary& dict
)
:
lduInterfaceField(refCast<const lduInterface>(p)),
lduInterfaceField(refCast<const lduInterface>(p, dict)),
faPatchField<Type>(p, iF, dict)
{}
......
......@@ -75,7 +75,7 @@ Foam::cyclicFaPatchField<Type>::cyclicFaPatchField
)
:
coupledFaPatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicFaPatch>(p))
cyclicPatch_(refCast<const cyclicFaPatch>(p, dict))
{
if (!isA<cyclicFaPatch>(p))
{
......
......@@ -92,7 +92,7 @@ Foam::processorFaPatchField<Type>::processorFaPatchField
)
:
coupledFaPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorFaPatch>(p))
procPatch_(refCast<const processorFaPatch>(p, dict))
{
if (!isType<processorFaPatch>(p))
{
......
......@@ -74,7 +74,7 @@ Foam::cyclicFaePatchField<Type>::cyclicFaePatchField
)
:
coupledFaePatchField<Type>(p, iF, dict),
cyclicPatch_(refCast<const cyclicFaPatch>(p))
cyclicPatch_(refCast<const cyclicFaPatch>(p, dict))
{
if (!isType<cyclicFaPatch>(p))
{
......
......@@ -87,7 +87,7 @@ Foam::processorFaePatchField<Type>::processorFaePatchField
)
:
coupledFaePatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorFaPatch>(p))
procPatch_(refCast<const processorFaPatch>(p, dict))
{
if (!isType<processorFaPatch>(p))
{
......
......@@ -75,7 +75,7 @@ Foam::coupledFvPatchField<Type>::coupledFvPatchField
const bool valueRequired
)
:
LduInterfaceField<Type>(refCast<const lduInterface>(p)),
LduInterfaceField<Type>(refCast<const lduInterface>(p, dict)),
fvPatchField<Type>(p, iF, dict, valueRequired)
{}
......
......@@ -49,7 +49,7 @@ Foam::cyclicFvPatchField<Type>::cyclicFvPatchField
)
:
coupledFvPatchField<Type>(p, iF, dict, false),
cyclicPatch_(refCast<const cyclicFvPatch>(p))
cyclicPatch_(refCast<const cyclicFvPatch>(p, dict))
{
if (!isA<cyclicFvPatch>(p))
{
......
......@@ -51,7 +51,7 @@ Foam::cyclicACMIFvPatchField<Type>::cyclicACMIFvPatchField
:
cyclicACMILduInterfaceField(),
coupledFvPatchField<Type>(p, iF, dict, dict.found("value")),
cyclicACMIPatch_(refCast<const cyclicACMIFvPatch>(p))
cyclicACMIPatch_(refCast<const cyclicACMIFvPatch>(p, dict))
{
if (!isA<cyclicACMIFvPatch>(p))
{
......
......@@ -48,7 +48,7 @@ Foam::cyclicAMIFvPatchField<Type>::cyclicAMIFvPatchField
:
cyclicAMILduInterfaceField(),
coupledFvPatchField<Type>(p, iF, dict, dict.found("value")),
cyclicAMIPatch_(refCast<const cyclicAMIFvPatch>(p))
cyclicAMIPatch_(refCast<const cyclicAMIFvPatch>(p, dict))
{
if (!isA<cyclicAMIFvPatch>(p))
{
......
......@@ -76,7 +76,7 @@ Foam::processorFvPatchField<Type>::processorFvPatchField
)
:
coupledFvPatchField<Type>(p, iF, dict, dict.found("value")),
procPatch_(refCast<const processorFvPatch>(p)),
procPatch_(refCast<const processorFvPatch>(p, dict)),
sendBuf_(0),
receiveBuf_(0),
outstandingSendRequest_(-1),
......
......@@ -51,7 +51,7 @@ Foam::processorCyclicFvPatchField<Type>::processorCyclicFvPatchField
)
:
processorFvPatchField<Type>(p, iF, dict),
procPatch_(refCast<const processorCyclicFvPatch>(p))
procPatch_(refCast<const processorCyclicFvPatch>(p, dict))
{
if (!isType<processorCyclicFvPatch>(p))
{
......
......@@ -73,7 +73,7 @@ Foam::symmetryPlaneFvPatchField<Type>::symmetryPlaneFvPatchField
)
:
basicSymmetryFvPatchField<Type>(p, iF, dict),
symmetryPlanePatch_(refCast<const symmetryPlaneFvPatch>(p))
symmetryPlanePatch_(refCast<const symmetryPlaneFvPatch>(p, dict))
{
if (!isType<symmetryPlaneFvPatch>(p))
{
......
......@@ -96,7 +96,8 @@ activeBaffleVelocityFvPatchVectorField
(
refCast<const cyclicFvPatch>
(
p.boundaryMesh()[cyclicPatchLabel_]
p.boundaryMesh()[cyclicPatchLabel_],
dict
).neighbFvPatch().Sf()
),
openFraction_(readScalar(dict.lookup("openFraction"))),
......
......@@ -115,7 +115,8 @@ activePressureForceBaffleVelocityFvPatchVectorField
initCyclicSf_ = p.boundaryMesh()[cyclicPatchLabel_].Sf();
nbrCyclicSf_ = refCast<const cyclicFvPatch>
(
p.boundaryMesh()[cyclicPatchLabel_]
p.boundaryMesh()[cyclicPatchLabel_],
dict
).neighbFvPatch().Sf();
}
......
......@@ -94,7 +94,8 @@ mappedVelocityFluxFixedValueFvPatchField
const mappedPatchBase& mpp = refCast<const mappedPatchBase>
(
this->patch().patch()
this->patch().patch(),
dict
);
if (mpp.mode() == mappedPolyPatch::NEARESTCELL)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment