ENH: cleanup autoPtr class (issue #639)

Improve alignment of its behaviour with std::unique_ptr

  - element_type typedef
  - release() method - identical to ptr() method
  - get() method to get the pointer without checking and without releasing it.
  - operator*() for dereferencing

Method name changes

  - renamed rawPtr() to get()
  - renamed rawRef() to ref(), removed unused const version.

Removed methods/operators

  - assignment from a raw pointer was deleted (was rarely used).
    Can be convenient, but uncontrolled and potentially unsafe.
    Do allow assignment from a literal nullptr though, since this
    can never leak (and also corresponds to the unique_ptr API).

Additional methods

  - clone() method: forwards to the clone() method of the underlying
    data object with argument forwarding.

  - reset(autoPtr&&) as an alternative to operator=(autoPtr&&)

STYLE: avoid implicit conversion from autoPtr to object type in many places

- existing implementation has the following:

     operator const T&() const { return operator*(); }

  which means that the following code works:

       autoPtr<mapPolyMesh> map = ...;
       updateMesh(*map);    // OK: explicit dereferencing
       updateMesh(map());   // OK: explicit dereferencing
       updateMesh(map);     // OK: implicit dereferencing

  for clarity it may preferable to avoid the implicit dereferencing

- prefer operator* to operator() when deferenced a return value
  so it is clearer that a pointer is involve and not a function call
  etc    Eg,   return *meshPtr_;  vs.  return meshPtr_();
This commit is contained in:
Mark Olesen
2018-02-26 12:00:00 +01:00
parent fc92d30e74
commit 660f3e5492
308 changed files with 1613 additions and 1388 deletions

View File

@ -89,22 +89,22 @@ public:
const rhoThermo& thermo1() const
{
return thermo1_();
return *thermo1_;
}
const rhoThermo& thermo2() const
{
return thermo2_();
return *thermo2_;
}
rhoThermo& thermo1()
{
return thermo1_();
return *thermo1_;
}
rhoThermo& thermo2()
{
return thermo2_();
return *thermo2_;
}
//- Correct the thermodynamics of each phase

View File

@ -81,7 +81,7 @@ Foam::phaseModel::phaseModel
Foam::autoPtr<Foam::phaseModel> Foam::phaseModel::clone() const
{
NotImplemented;
return autoPtr<phaseModel>(nullptr);
return autoPtr<phaseModel>();
}

View File

@ -120,13 +120,13 @@ public:
//- Return const-access to phase rhoThermo
const rhoThermo& thermo() const
{
return thermo_();
return *thermo_;
}
//- Return access to phase rhoThermo
rhoThermo& thermo()
{
return thermo_();
return *thermo_;
}
//- Return const-access to phase divergence

View File

@ -106,13 +106,13 @@ public:
//- Return const-access to the mixture viscosityModel
const mixtureViscosityModel& muModel() const
{
return muModel_();
return *muModel_;
}
//- Return const-access to the continuous-phase viscosityModel
const viscosityModel& nucModel() const
{
return nucModel_();
return *nucModel_;
}
//- Return const-access to the dispersed-phase density

View File

@ -181,19 +181,19 @@ public:
//- Return const-access to phase1 viscosityModel
const viscosityModel& nuModel1() const
{
return nuModel1_();
return *nuModel1_;
}
//- Return const-access to phase2 viscosityModel
const viscosityModel& nuModel2() const
{
return nuModel2_();
return *nuModel2_;
}
//- Return const-access to phase3 viscosityModel
const viscosityModel& nuModel3() const
{
return nuModel3_();
return *nuModel3_;
}
//- Return the dynamic laminar viscosity

View File

@ -205,7 +205,7 @@ Foam::phaseModel::~phaseModel()
Foam::autoPtr<Foam::phaseModel> Foam::phaseModel::clone() const
{
NotImplemented;
return autoPtr<phaseModel>(nullptr);
return autoPtr<phaseModel>();
}

View File

@ -190,12 +190,12 @@ public:
const surfaceScalarField& phi() const
{
return phiPtr_();
return *phiPtr_;
}
surfaceScalarField& phi()
{
return phiPtr_();
return *phiPtr_;
}
const surfaceScalarField& alphaPhi() const

View File

@ -68,7 +68,7 @@ Foam::phase::phase
Foam::autoPtr<Foam::phase> Foam::phase::clone() const
{
NotImplemented;
return autoPtr<phase>(nullptr);
return autoPtr<phase>();
}

View File

@ -120,7 +120,7 @@ public:
//- Return const-access to phase1 viscosityModel
const viscosityModel& nuModel() const
{
return nuModel_();
return *nuModel_;
}
//- Return the kinematic laminar viscosity

View File

@ -59,14 +59,9 @@ HeatAndMassTransferPhaseSystem
massTransferModels_
);
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -197,14 +192,9 @@ Foam::HeatAndMassTransferPhaseSystem<BasePhaseSystem>::dmdt
)
);
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -239,14 +229,9 @@ Foam::HeatAndMassTransferPhaseSystem<BasePhaseSystem>::momentumTransfer() const
phaseSystem::momentumTransferTable& eqns = eqnsPtr();
// Source term due to mass trasfer
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -291,17 +276,10 @@ Foam::HeatAndMassTransferPhaseSystem<BasePhaseSystem>::heatTransfer() const
}
// Heat transfer with the interface
forAllConstIter
(
heatTransferModelTable,
heatTransferModels_,
heatTransferModelIter
)
forAllConstIters(heatTransferModels_, heatTransferModelIter)
{
const phasePair& pair
(
this->phasePairs_[heatTransferModelIter.key()]
);
const phasePair& pair =
*(this->phasePairs_[heatTransferModelIter.key()]);
const phaseModel* phase = &pair.phase1();
const phaseModel* otherPhase = &pair.phase2();
@ -344,14 +322,9 @@ Foam::HeatAndMassTransferPhaseSystem<BasePhaseSystem>::heatTransfer() const
}
// Source term due to mass transfer
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{

View File

@ -128,16 +128,12 @@ Foam::HeatTransferPhaseSystem<BasePhaseSystem>::heatTransfer() const
);
}
forAllConstIter
(
heatTransferModelTable,
heatTransferModels_,
heatTransferModelIter
)
forAllConstIters(heatTransferModels_, heatTransferModelIter)
{
const volScalarField K(heatTransferModelIter()->K());
const phasePair& pair =
*(this->phasePairs_[heatTransferModelIter.key()]);
const phasePair& pair(this->phasePairs_[heatTransferModelIter.key()]);
const volScalarField K(heatTransferModelIter()->K());
const phaseModel* phase = &pair.phase1();
const phaseModel* otherPhase = &pair.phase2();

View File

@ -86,14 +86,9 @@ massTransfer() const
}
// Reset the interfacial mass flow rates
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -108,22 +103,18 @@ massTransfer() const
}
// Sum up the contribution from each interface composition model
forAllConstIter
forAllConstIters
(
interfaceCompositionModelTable,
interfaceCompositionModels_,
interfaceCompositionModelIter
)
{
const interfaceCompositionModel& compositionModel
(
interfaceCompositionModelIter()
);
const phasePair& pair =
*(this->phasePairs_[interfaceCompositionModelIter.key()]);
const interfaceCompositionModel& compositionModel =
*(interfaceCompositionModelIter.object());
const phasePair& pair
(
this->phasePairs_[interfaceCompositionModelIter.key()]
);
const phaseModel& phase = pair.phase1();
const phaseModel& otherPhase = pair.phase2();
const phasePairKey key(phase.name(), otherPhase.name());
@ -209,14 +200,9 @@ correctThermo()
// Yfi is likely to be a strong non-linear (typically exponential) function
// of Tf, so the solution for the temperature is newton-accelerated
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{

View File

@ -82,14 +82,10 @@ MomentumTransferPhaseSystem
turbulentDispersionModels_
);
forAllConstIter
(
dragModelTable,
dragModels_,
dragModelIter
)
forAllConstIters(dragModels_, dragModelIter)
{
const phasePair& pair(this->phasePairs_[dragModelIter.key()]);
const phasePair& pair =
*(this->phasePairs_[dragModelIter.key()]);
Kds_.insert
(
@ -102,14 +98,10 @@ MomentumTransferPhaseSystem
);
}
forAllConstIter
(
virtualMassModelTable,
virtualMassModels_,
virtualMassModelIter
)
forAllConstIters(virtualMassModels_, virtualMassModelIter)
{
const phasePair& pair(this->phasePairs_[virtualMassModelIter.key()]);
const phasePair& pair =
*(this->phasePairs_[virtualMassModelIter.key()]);
Vms_.insert
(
@ -183,16 +175,11 @@ Foam::MomentumTransferPhaseSystem<BasePhaseSystem>::Kd
)
);
forAllConstIter
(
phaseSystem::KdTable,
Kds_,
KdIter
)
forAllConstIters(Kds_, KdIter)
{
const volScalarField& K(*KdIter());
const phasePair& pair = *(this->phasePairs_[KdIter.key()]);
const phasePair& pair(this->phasePairs_[KdIter.key()]);
const volScalarField& K(*KdIter());
const phaseModel* phase1 = &pair.phase1();
const phaseModel* phase2 = &pair.phase2();
@ -430,27 +417,17 @@ Foam::MomentumTransferPhaseSystem<BasePhaseSystem>::momentumTransfer() const
}
// Update the drag coefficients
forAllConstIter
(
dragModelTable,
dragModels_,
dragModelIter
)
forAllConstIters(dragModels_, dragModelIter)
{
*Kds_[dragModelIter.key()] = dragModelIter()->K();
}
// Add the implicit part of the drag force
forAllConstIter
(
phaseSystem::KdTable,
Kds_,
KdIter
)
forAllConstIters(Kds_, KdIter)
{
const volScalarField& K(*KdIter());
const phasePair& pair = *(this->phasePairs_[KdIter.key()]);
const phasePair& pair(this->phasePairs_[KdIter.key()]);
const volScalarField& K(*KdIter());
const phaseModel* phase = &pair.phase1();
const phaseModel* otherPhase = &pair.phase2();
@ -466,27 +443,17 @@ Foam::MomentumTransferPhaseSystem<BasePhaseSystem>::momentumTransfer() const
}
// Update the virtual mass coefficients
forAllConstIter
(
virtualMassModelTable,
virtualMassModels_,
virtualMassModelIter
)
forAllConstIters(virtualMassModels_, virtualMassModelIter)
{
*Vms_[virtualMassModelIter.key()] = virtualMassModelIter()->K();
}
// Add the virtual mass force
forAllConstIter
(
phaseSystem::VmTable,
Vms_,
VmIter
)
forAllConstIters(Vms_, VmIter)
{
const volScalarField& Vm(*VmIter());
const phasePair& pair = *(this->phasePairs_[VmIter.key()]);
const phasePair& pair(this->phasePairs_[VmIter.key()]);
const volScalarField& Vm(*VmIter());
const phaseModel* phase = &pair.phase1();
const phaseModel* otherPhase = &pair.phase2();
@ -557,33 +524,22 @@ Foam::MomentumTransferPhaseSystem<BasePhaseSystem>::Fs() const
PtrList<volVectorField>& Fs = tFs();
// Add the lift force
forAllConstIter
(
liftModelTable,
liftModels_,
liftModelIter
)
forAllConstIters(liftModels_, modelIter)
{
const volVectorField F(liftModelIter()->F<vector>());
const phasePair& pair = *(this->phasePairs_[modelIter.key()]);
const phasePair& pair(this->phasePairs_[liftModelIter.key()]);
const volVectorField F(modelIter()->template F<vector>());
setF(Fs, pair.phase1().index()) += F;
setF(Fs, pair.phase2().index()) -= F;
}
// Add the wall lubrication force
forAllConstIter
(
wallLubricationModelTable,
wallLubricationModels_,
wallLubricationModelIter
)
forAllConstIters(wallLubricationModels_, modelIter)
{
const volVectorField F(wallLubricationModelIter()->F<vector>());
const phasePair& pair = *(this->phasePairs_[modelIter.key()]);
const phasePair&
pair(this->phasePairs_[wallLubricationModelIter.key()]);
const volVectorField F(modelIter()->template F<vector>());
setF(Fs, pair.phase1().index()) += F;
setF(Fs, pair.phase2().index()) -= F;
@ -647,15 +603,10 @@ Foam::MomentumTransferPhaseSystem<BasePhaseSystem>::phiDs
PtrList<surfaceScalarField>& phiDs = tphiDs();
// Add the turbulent dispersion force
forAllConstIter
(
turbulentDispersionModelTable,
turbulentDispersionModels_,
turbulentDispersionModelIter
)
forAllConstIters(turbulentDispersionModels_, turbulentDispersionModelIter)
{
const phasePair&
pair(this->phasePairs_[turbulentDispersionModelIter.key()]);
const phasePair& pair =
*(this->phasePairs_[turbulentDispersionModelIter.key()]);
const volScalarField D(turbulentDispersionModelIter()->D());
const surfaceScalarField snGradAlpha1

View File

@ -42,14 +42,9 @@ ThermalPhaseChangePhaseSystem
massTransfer_(this->lookup("massTransfer"))
{
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -92,7 +87,7 @@ template<class BasePhaseSystem>
const Foam::saturationModel&
Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::saturation() const
{
return saturationModel_();
return *saturationModel_;
}
@ -109,14 +104,9 @@ Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::heatTransfer() const
phaseSystem::heatTransferTable& eqns = eqnsPtr();
// Accumulate mDotL contributions from boundaries
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -219,19 +209,15 @@ Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::massTransfer() const
}
}
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
continue;
}
const phaseModel& phase = pair.phase1();
const phaseModel& otherPhase = pair.phase2();
@ -292,14 +278,9 @@ Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::iDmdt
)
);
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{
@ -332,14 +313,9 @@ void Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::correctThermo()
BasePhaseSystem::correctThermo();
forAllConstIter
(
phaseSystem::phasePairTable,
this->phasePairs_,
phasePairIter
)
forAllConstIters(this->phasePairs_, phasePairIter)
{
const phasePair& pair(phasePairIter());
const phasePair& pair = *(phasePairIter.object());
if (pair.ordered())
{

View File

@ -401,7 +401,7 @@ template<class BasePhaseModel>
const Foam::phaseCompressibleTurbulenceModel&
Foam::MovingPhaseModel<BasePhaseModel>::turbulence() const
{
return turbulence_;
return *turbulence_;
}

View File

@ -77,7 +77,7 @@ Foam::phaseModel::phaseModel
Foam::autoPtr<Foam::phaseModel> Foam::phaseModel::clone() const
{
NotImplemented;
return autoPtr<phaseModel>(nullptr);
return autoPtr<phaseModel>();
}

View File

@ -48,8 +48,8 @@ void Foam::phaseSystem::createSubModels
key,
modelType::New
(
*iter,
phasePairs_[key]
iter.object(),
phasePairs_[key]()
)
);
}
@ -98,11 +98,11 @@ void Foam::phaseSystem::generatePairsAndSubModels
const blendingMethod& blending
(
blendingMethods_.found(modelName)
? blendingMethods_[modelName]
: blendingMethods_["default"]
? *(blendingMethods_[modelName])
: *(blendingMethods_["default"])
);
autoPtr<modelType> noModel(nullptr);
autoPtr<modelType> noModel;
forAllConstIter(typename modelTypeTable, tempModels, iter)
{

View File

@ -217,16 +217,11 @@ while (pimple.correct())
)
);
forAllConstIter
(
phaseSystem::KdTable,
fluid.Kds(),
KdIter
)
forAllConstIters(fluid.Kds(), KdIter)
{
const volScalarField& K(*KdIter());
const phasePair& pair(fluid.phasePairs()[KdIter.key()]);
const phasePair& pair = *(fluid.phasePairs()[KdIter.key()]);
const phaseModel* phase1 = &pair.phase1();
const phaseModel* phase2 = &pair.phase2();

View File

@ -115,7 +115,7 @@ public:
autoPtr<IATEsource> clone() const
{
NotImplemented;
return autoPtr<IATEsource>(nullptr);
return autoPtr<IATEsource>();
}

View File

@ -451,9 +451,11 @@ bool Foam::BlendedInterfacialModel<modelType>::hasModel
) const
{
return
&phase == &(pair_.phase1())
(
&phase == &(pair_.phase1())
? model1In2_.valid()
: model2In1_.valid();
: model2In1_.valid()
);
}
@ -463,7 +465,7 @@ const modelType& Foam::BlendedInterfacialModel<modelType>::phaseModel
const class phaseModel& phase
) const
{
return &phase == &(pair_.phase1()) ? model1In2_ : model2In1_;
return &phase == &(pair_.phase1()) ? *model1In2_ : *model2In1_;
}

View File

@ -114,7 +114,7 @@ public:
autoPtr<IATEsource> clone() const
{
NotImplemented;
return autoPtr<IATEsource>(nullptr);
return autoPtr<IATEsource>();
}

View File

@ -227,14 +227,14 @@ Foam::tmp<Foam::volScalarField> Foam::phaseModel::d() const
Foam::PhaseCompressibleTurbulenceModel<Foam::phaseModel>&
Foam::phaseModel::turbulence()
{
return turbulence_();
return *turbulence_;
}
const Foam::PhaseCompressibleTurbulenceModel<Foam::phaseModel>&
Foam::phaseModel::turbulence() const
{
return turbulence_();
return *turbulence_;
}

View File

@ -162,14 +162,14 @@ public:
//- Return the thermophysical model
const rhoThermo& thermo() const
{
return thermo_();
return *thermo_;
}
//- Return non-const access to the thermophysical model
// for correction
rhoThermo& thermo()
{
return thermo_();
return *thermo_;
}
//- Return the laminar viscosity
@ -286,13 +286,13 @@ public:
//- Return the volumetric flux
const surfaceScalarField& phi() const
{
return phiPtr_();
return *phiPtr_;
}
//- Return non-const access to the volumetric flux
surfaceScalarField& phi()
{
return phiPtr_();
return *phiPtr_;
}
//- Return the volumetric flux of the phase

View File

@ -177,12 +177,12 @@ Foam::twoPhaseSystem::twoPhaseSystem
lookup("drag"),
(
blendingMethods_.found("drag")
? blendingMethods_["drag"]
: blendingMethods_["default"]
? *(blendingMethods_["drag"])
: *(blendingMethods_["default"])
),
pair_,
pair1In2_,
pair2In1_,
*pair_,
*pair1In2_,
*pair2In1_,
false // Do not zero drag coefficent at fixed-flux BCs
)
);
@ -194,12 +194,12 @@ Foam::twoPhaseSystem::twoPhaseSystem
lookup("virtualMass"),
(
blendingMethods_.found("virtualMass")
? blendingMethods_["virtualMass"]
: blendingMethods_["default"]
? *(blendingMethods_["virtualMass"])
: *(blendingMethods_["default"])
),
pair_,
pair1In2_,
pair2In1_
*pair_,
*pair1In2_,
*pair2In1_
)
);
@ -210,12 +210,12 @@ Foam::twoPhaseSystem::twoPhaseSystem
lookup("heatTransfer"),
(
blendingMethods_.found("heatTransfer")
? blendingMethods_["heatTransfer"]
: blendingMethods_["default"]
? *(blendingMethods_["heatTransfer"])
: *(blendingMethods_["default"])
),
pair_,
pair1In2_,
pair2In1_
*pair_,
*pair1In2_,
*pair2In1_
)
);
@ -226,12 +226,12 @@ Foam::twoPhaseSystem::twoPhaseSystem
lookup("lift"),
(
blendingMethods_.found("lift")
? blendingMethods_["lift"]
: blendingMethods_["default"]
? *(blendingMethods_["lift"])
: *(blendingMethods_["default"])
),
pair_,
pair1In2_,
pair2In1_
*pair_,
*pair1In2_,
*pair2In1_
)
);
@ -242,12 +242,12 @@ Foam::twoPhaseSystem::twoPhaseSystem
lookup("wallLubrication"),
(
blendingMethods_.found("wallLubrication")
? blendingMethods_["wallLubrication"]
: blendingMethods_["default"]
? *(blendingMethods_["wallLubrication"])
: *(blendingMethods_["default"])
),
pair_,
pair1In2_,
pair2In1_
*pair_,
*pair1In2_,
*pair2In1_
)
);
@ -258,12 +258,12 @@ Foam::twoPhaseSystem::twoPhaseSystem
lookup("turbulentDispersion"),
(
blendingMethods_.found("turbulentDispersion")
? blendingMethods_["turbulentDispersion"]
: blendingMethods_["default"]
? *(blendingMethods_["turbulentDispersion"])
: *(blendingMethods_["default"])
),
pair_,
pair1In2_,
pair2In1_
*pair_,
*pair1In2_,
*pair2In1_
)
);
}
@ -272,7 +272,7 @@ Foam::twoPhaseSystem::twoPhaseSystem
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::twoPhaseSystem::~twoPhaseSystem()
{}
{} // Define here (incomplete type in header)
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //