From 52b36f84b53d0a028a719e00db5aca2a8fff4886 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 26 Feb 2018 12:05:00 +0100 Subject: [PATCH] ENH: cleanup tmp class (issue #639) Improve alignment of its behaviour with std::shared_ptr - element_type typedef - swap, reset methods * additional reference access methods: cref() returns a const reference, synonymous with operator(). This provides a more verbose alternative to using the '()' operator when that is desired. Mnemonic: a const form of 'ref()' constCast() returns a non-const reference, regardless if the underlying object itself is a managed pointer or a const object. This is similar to ref(), but more permissive. Mnemonic: const_cast<> Using the constCast() method greatly reduces the amount of typing and reading. And since the data type is already defined via the tmp template parameter, the type deduction is automatically known. Previously, const tmp& tfld; const_cast(tfld()).rename("name"); volScalarField& fld = const_cast(tfld()); Now, tfld.constCast().rename("name"); auto& fld = tfld.constCast(); -- BUG: attempts to move tmp value that may still be shared. - old code simply checked isTmp() to decide if the contents could be transfered. However, this means that the content of a shared tmp would be removed, leaving other instances without content. * movable() method checks that for a non-null temporary that is unique (not shared). --- applications/test/tmp/Make/files | 3 + .../test/{tmpField => tmp}/Make/options | 0 .../Test-tmpField.C => tmp/Test-tmp.C} | 47 +++- applications/test/tmpField/Make/files | 3 - .../DimensionedField/DimensionedField.C | 18 +- .../DimensionedFieldReuseFunctions.H | 186 +++++++------ .../FieldFields/FieldField/FieldField.C | 6 +- .../FieldField/FieldFieldReuseFunctions.H | 72 +++-- src/OpenFOAM/fields/Fields/Field/Field.C | 2 +- .../fields/Fields/Field/FieldReuseFunctions.H | 40 ++- .../GeometricField/GeometricField.C | 29 +- .../GeometricField/GeometricField.H | 26 +- .../GeometricFieldReuseFunctions.H | 207 +++++++------- .../lduMatrix/lduMatrix/lduMatrixTemplates.C | 14 +- src/OpenFOAM/memory/refCount/refCount.H | 4 + src/OpenFOAM/memory/tmp/tmp.H | 200 ++++++++++---- src/OpenFOAM/memory/tmp/tmpI.H | 252 ++++++++++++------ src/OpenFOAM/memory/tmp/tmpNrc.H | 214 +++++++++++++++ src/OpenFOAM/memory/{tmpNrc => tmp}/tmpNrcI.H | 180 ++++++++----- src/OpenFOAM/memory/tmpNrc/tmpNrc.H | 171 ------------ .../functions/Function1/Function1/Function1.C | 6 +- .../schemes/DEShybrid/DEShybrid.H | 23 +- .../LES/LESfilters/LESfilter/LESfilter.H | 2 +- .../lnGradSchemes/lnGradScheme/lnGradScheme.H | 2 +- .../edgeInterpolationScheme.H | 2 +- .../CoEulerDdtScheme/CoEulerDdtScheme.C | 12 +- .../regionCoupled/regionCoupledFvPatch.C | 7 +- .../regionCoupled/regionCoupledWallFvPatch.C | 6 +- .../ReynoldsAnalogy/ReynoldsAnalogy.C | 24 +- .../solidificationMeltingSource.C | 2 +- .../MPPIC/ParticleStressModels/Lun/Lun.C | 3 +- .../DispersionRASModel/DispersionRASModel.C | 28 +- .../BrownianMotion/BrownianMotionForce.C | 14 +- .../EulerCoordinateRotation.C | 8 +- .../STARCDCoordinateRotation.C | 8 +- .../coordinateRotation/axesRotation.C | 4 +- .../sampledSurface/sampledSurface.C | 10 +- .../submodels/sootModel/noSoot/noSoot.C | 2 +- .../basicSolidChemistryModel.C | 4 +- .../waveAlpha/waveAlphaFvPatchScalarField.C | 4 +- .../waveVelocityFvPatchVectorField.C | 4 +- 41 files changed, 1034 insertions(+), 815 deletions(-) create mode 100644 applications/test/tmp/Make/files rename applications/test/{tmpField => tmp}/Make/options (100%) rename applications/test/{tmpField/Test-tmpField.C => tmp/Test-tmp.C} (59%) delete mode 100644 applications/test/tmpField/Make/files create mode 100644 src/OpenFOAM/memory/tmp/tmpNrc.H rename src/OpenFOAM/memory/{tmpNrc => tmp}/tmpNrcI.H (72%) delete mode 100644 src/OpenFOAM/memory/tmpNrc/tmpNrc.H diff --git a/applications/test/tmp/Make/files b/applications/test/tmp/Make/files new file mode 100644 index 0000000000..5b4ecf10aa --- /dev/null +++ b/applications/test/tmp/Make/files @@ -0,0 +1,3 @@ +Test-tmp.C + +EXE = $(FOAM_USER_APPBIN)/Test-tmp diff --git a/applications/test/tmpField/Make/options b/applications/test/tmp/Make/options similarity index 100% rename from applications/test/tmpField/Make/options rename to applications/test/tmp/Make/options diff --git a/applications/test/tmpField/Test-tmpField.C b/applications/test/tmp/Test-tmp.C similarity index 59% rename from applications/test/tmpField/Test-tmpField.C rename to applications/test/tmp/Test-tmp.C index 6896344ec5..73ba236063 100644 --- a/applications/test/tmpField/Test-tmpField.C +++ b/applications/test/tmp/Test-tmp.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -22,10 +22,10 @@ License along with OpenFOAM. If not, see . Application - tmpFieldTest + Test-tmp Description - Tests for possible memory leaks in the tmp algebra. + Tests for possible memory leaks in the tmp (and tmp algebra). \*---------------------------------------------------------------------------*/ @@ -33,19 +33,50 @@ Description using namespace Foam; +struct myScalarField : public scalarField +{ + using scalarField::scalarField; +}; + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: int main() { - scalarField f1(1000000, 1.0), f2(1000000, 2.0), f3(1000000, 3.0); - - for (;;) { - f1 = f2 + f3 + f2 + f3; + scalarField f1(1000000, 1.0), f2(1000000, 2.0), f3(1000000, 3.0); + + for (int iter=0; iter < 50; ++iter) + { + f1 = f2 + f3 + f2 + f3; + } + + Info<<"f1 = " << f1 << nl; } - Info<< "end" << endl; + { + tmp tfld1 = tmp::New(20, Zero); + + Info<< "tmp refCount = " << tfld1->count() << nl; + if (tfld1.valid()) + { + Info<<"tmp: " << tfld1() << nl; + } + } + + { + tmp tfld2 = + tmp::NewFrom(20, Zero); + + Info<< "tmp refCount = " << tfld2->count() << nl; + if (tfld2.valid()) + { + Info<<"tmp: " << tfld2() << nl; + } + } + + Info<< "\nEnd" << endl; } diff --git a/applications/test/tmpField/Make/files b/applications/test/tmpField/Make/files deleted file mode 100644 index c3dd60a71d..0000000000 --- a/applications/test/tmpField/Make/files +++ /dev/null @@ -1,3 +0,0 @@ -Test-tmpField.C - -EXE = $(FOAM_USER_APPBIN)/Test-tmpField diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C index e4717cd7fe..f2f54f3270 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedField.C @@ -217,12 +217,8 @@ DimensionedField::DimensionedField const tmp>& tdf ) : - regIOobject(tdf(), tdf.isTmp()), - Field - ( - const_cast&>(tdf()), - tdf.isTmp() - ), + regIOobject(tdf.constCast(), tdf.movable()), + Field(tdf.constCast(), tdf.movable()), mesh_(tdf().mesh_), dimensions_(tdf().dimensions_), oriented_(tdf().oriented_) @@ -318,11 +314,7 @@ DimensionedField::DimensionedField ) : regIOobject(newName, tdf(), true), - Field - ( - const_cast&>(tdf()), - tdf.isTmp() - ), + Field(tdf.constCast(), tdf.movable()), mesh_(tdf().mesh_), dimensions_(tdf().dimensions_), oriented_(tdf().oriented_) @@ -511,7 +503,7 @@ void DimensionedField::operator= const tmp>& tdf ) { - const DimensionedField& df = tdf(); + auto& df = tdf.constCast(); // Check for assignment to self if (this == &df) @@ -525,7 +517,7 @@ void DimensionedField::operator= dimensions_ = df.dimensions(); oriented_ = df.oriented(); - this->transfer(const_cast&>(df)); + this->transfer(df); tdf.clear(); } diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldReuseFunctions.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldReuseFunctions.H index 3cf47f0cd5..967eace9ad 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldReuseFunctions.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldReuseFunctions.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -41,32 +41,31 @@ tmp> New const dimensionSet& dimensions ) { - DimensionedField& df1 = - const_cast&>(tdf1()); - if (tdf1.isTmp()) { + auto& df1 = tdf1.constCast(); + df1.rename(name); df1.dimensions().reset(dimensions); return tdf1; } - else - { - return tmp> + + const auto& df1 = tdf1(); + + return tmp> + ( + new DimensionedField ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) - ); - } + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions + ) + ); } @@ -82,7 +81,7 @@ public: const dimensionSet& dimensions ) { - const DimensionedField& df1 = tdf1(); + const auto& df1 = tdf1(); return tmp> ( @@ -114,32 +113,31 @@ public: const dimensionSet& dimensions ) { - DimensionedField& df1 = - const_cast&>(tdf1()); - if (tdf1.isTmp()) { + auto& df1 = tdf1.constCast(); + df1.rename(name); df1.dimensions().reset(dimensions); return tdf1; } - else - { - return tmp> + + const auto& df1 = tdf1(); + + return tmp> + ( + new DimensionedField ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) - ); - } + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions + ) + ); } }; @@ -157,7 +155,7 @@ public: const dimensionSet& dimensions ) { - const DimensionedField& df1 = tdf1(); + const auto& df1 = tdf1(); return tmp> ( @@ -190,33 +188,31 @@ public: const dimensionSet& dimensions ) { - const DimensionedField& df1 = tdf1(); - DimensionedField& df2 = - const_cast&>(tdf2()); - if (tdf2.isTmp()) { + auto& df2 = tdf2.constCast(); + df2.rename(name); df2.dimensions().reset(dimensions); return tdf2; } - else - { - return tmp> + + const auto& df1 = tdf1(); + + return tmp> + ( + new DimensionedField ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) - ); - } + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions + ) + ); } }; @@ -234,32 +230,31 @@ public: const dimensionSet& dimensions ) { - DimensionedField& df1 = - const_cast&>(tdf1()); - if (tdf1.isTmp()) { + auto& df1 = tdf1.constCast(); + df1.rename(name); df1.dimensions().reset(dimensions); return tdf1; } - else - { - return tmp> + + const auto& df1 = tdf1(); + + return tmp> + ( + new DimensionedField ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) - ); - } + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions + ) + ); } }; @@ -277,40 +272,39 @@ public: const dimensionSet& dimensions ) { - DimensionedField& df1 = - const_cast&>(tdf1()); - DimensionedField& df2 = - const_cast&>(tdf2()); - if (tdf1.isTmp()) { + auto& df1 = tdf1.constCast(); + df1.rename(name); df1.dimensions().reset(dimensions); return tdf1; } else if (tdf2.isTmp()) { + auto& df2 = tdf2.constCast(); + df2.rename(name); df2.dimensions().reset(dimensions); return tdf2; } - else - { - return tmp> + + const auto& df1 = tdf1(); + + return tmp> + ( + new DimensionedField ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) - ); - } + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions + ) + ); } }; diff --git a/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C b/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C index 940afea59b..48610c8f97 100644 --- a/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C +++ b/src/OpenFOAM/fields/FieldFields/FieldField/FieldField.C @@ -160,11 +160,7 @@ FieldField::FieldField(const PtrList>& tl) template class Field, class Type> FieldField::FieldField(const tmp>& tf) : - PtrList> - ( - const_cast&>(tf()), - tf.isTmp() - ) + PtrList>(tf.constCast(), tf.movable()) { tf.clear(); } diff --git a/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldReuseFunctions.H b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldReuseFunctions.H index 0006d9aa01..43383f1eb5 100644 --- a/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldReuseFunctions.H +++ b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldReuseFunctions.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -44,20 +44,18 @@ tmp> New { return tf1; } - else + + tmp> rtf + ( + FieldField::NewCalculatedType(tf1()) + ); + + if (initRet) { - tmp> rtf - ( - FieldField::NewCalculatedType(tf1()) - ); - - if (initRet) - { - rtf.ref() = tf1(); - } - - return rtf; + rtf.ref() = tf1(); } + + return rtf; } @@ -93,13 +91,11 @@ public: { return tf1; } - else - { - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); - } + + return tmp> + ( + FieldField::NewCalculatedType(tf1()) + ); } }; @@ -145,13 +141,11 @@ public: { return tf2; } - else - { - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); - } + + return tmp> + ( + FieldField::NewCalculatedType(tf1()) + ); } }; @@ -171,13 +165,11 @@ public: { return tf1; } - else - { - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); - } + + return tmp> + ( + FieldField::NewCalculatedType(tf1()) + ); } }; @@ -201,13 +193,11 @@ public: { return tf2; } - else - { - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); - } + + return tmp> + ( + FieldField::NewCalculatedType(tf1()) + ); } }; diff --git a/src/OpenFOAM/fields/Fields/Field/Field.C b/src/OpenFOAM/fields/Fields/Field/Field.C index eaaf483064..321fdb1029 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.C +++ b/src/OpenFOAM/fields/Fields/Field/Field.C @@ -256,7 +256,7 @@ Foam::Field::Field(const UIndirectList& list) template Foam::Field::Field(const tmp>& tf) : - List(const_cast&>(tf()), tf.isTmp()) + List(tf.constCast(), tf.movable()) { tf.clear(); } diff --git a/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H b/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H index fac9e9a51c..3818628c3e 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H +++ b/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H @@ -44,17 +44,15 @@ tmp> New { return tf1; } - else + + tmp> rtf(new Field(tf1().size())); + + if (initRet) { - tmp> rtf(new Field(tf1().size())); - - if (initRet) - { - rtf.ref() = tf1(); - } - - return rtf; + rtf.ref() = tf1(); } + + return rtf; } @@ -81,10 +79,8 @@ public: { return tf1; } - else - { - return tmp>(new Field(tf1().size())); - } + + return tmp>(new Field(tf1().size())); } }; @@ -120,10 +116,8 @@ public: { return tf2; } - else - { - return tmp>(new Field(tf1().size())); - } + + return tmp>(new Field(tf1().size())); } }; @@ -143,10 +137,8 @@ public: { return tf1; } - else - { - return tmp>(new Field(tf1().size())); - } + + return tmp>(new Field(tf1().size())); } }; @@ -170,10 +162,8 @@ public: { return tf2; } - else - { - return tmp>(new Field(tf1().size())); - } + + return tmp>(new Field(tf1().size())); } }; diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C index 6f2c45eb6f..6fd5a97ce5 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C @@ -455,11 +455,7 @@ Foam::GeometricField::GeometricField const tmp>& tgf ) : - Internal - ( - const_cast&>(tgf()), - tgf.isTmp() - ), + Internal(tgf.constCast(), tgf.movable()), timeIndex_(tgf().timeIndex()), field0Ptr_(nullptr), fieldPrevIterPtr_(nullptr), @@ -517,12 +513,7 @@ Foam::GeometricField::GeometricField const tmp>& tgf ) : - Internal - ( - io, - const_cast&>(tgf()), - tgf.isTmp() - ), + Internal(io, tgf.constCast(), tgf.movable()), timeIndex_(tgf().timeIndex()), field0Ptr_(nullptr), fieldPrevIterPtr_(nullptr), @@ -581,12 +572,7 @@ Foam::GeometricField::GeometricField const tmp>& tgf ) : - Internal - ( - newName, - const_cast&>(tgf()), - tgf.isTmp() - ), + Internal(newName, tgf.constCast(), tgf.movable()), timeIndex_(tgf().timeIndex()), field0Ptr_(nullptr), fieldPrevIterPtr_(nullptr), @@ -690,12 +676,7 @@ Foam::GeometricField::GeometricField const wordList& actualPatchTypes ) : - Internal - ( - io, - const_cast&>(tgf()), - tgf.isTmp() - ), + Internal(io, tgf.constCast(), tgf.movable()), timeIndex_(tgf().timeIndex()), field0Ptr_(nullptr), fieldPrevIterPtr_(nullptr), @@ -1221,7 +1202,7 @@ void Foam::GeometricField::operator= this->dimensions() = gf.dimensions(); this->oriented() = gf.oriented(); - if (tgf.isTmp()) + if (tgf.movable()) { // Transfer the storage from the tmp primitiveFieldRef().transfer diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H index 3162084d4d..0b13a145b2 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H @@ -125,16 +125,16 @@ public: // Constructors //- Construct from a BoundaryMesh - Boundary(const BoundaryMesh&); + Boundary(const BoundaryMesh& bmesh); //- Construct from a BoundaryMesh, // reference to the internal field // and a patch type Boundary ( - const BoundaryMesh&, - const Internal&, - const word& + const BoundaryMesh& bmesh, + const Internal& field, + const word& patchFieldType ); //- Construct from a BoundaryMesh, @@ -143,8 +143,8 @@ public: // types (to override constraint patches) Boundary ( - const BoundaryMesh&, - const Internal&, + const BoundaryMesh& bmesh, + const Internal& field, const wordList& wantedPatchTypes, const wordList& actualPatchTypes = wordList() ); @@ -154,16 +154,16 @@ public: // and a PtrList> Boundary ( - const BoundaryMesh&, - const Internal&, + const BoundaryMesh& bmesh, + const Internal& field, const PtrList>& ); //- Construct as copy setting the reference to the internal field Boundary ( - const Internal&, - const Boundary& + const Internal& field, + const Boundary& btf ); //- Construct as copy @@ -173,14 +173,14 @@ public: // BoundaryField for which such operations are unavailable. Boundary ( - const Boundary& + const Boundary& btf ); //- Construct from dictionary Boundary ( - const BoundaryMesh&, - const Internal&, + const BoundaryMesh& bmesh, + const Internal& field, const dictionary& ); diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldReuseFunctions.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldReuseFunctions.H index c6fb56706d..df17b18468 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldReuseFunctions.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldReuseFunctions.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -45,9 +45,8 @@ bool reusable(const tmp>& tgf) { if (GeometricField::debug) { - const GeometricField& gf = tgf(); - const typename GeometricField:: - Boundary& gbf = gf.boundaryField(); + const auto& gf = tgf(); + const auto& gbf = gf.boundaryField(); forAll(gbf, patchi) { @@ -68,10 +67,8 @@ bool reusable(const tmp>& tgf) return true; } - else - { - return false; - } + + return false; } @@ -84,39 +81,38 @@ tmp> New const bool initRet = false ) { - GeometricField& gf1 = - const_cast&>(tgf1()); - if (reusable(tgf1)) { + auto& gf1 = tgf1.constCast(); + gf1.rename(name); gf1.dimensions().reset(dimensions); return tgf1; } - else - { - tmp> rtgf + + const auto& gf1 = tgf1(); + + tmp> rtgf + ( + new GeometricField ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) - ); + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions + ) + ); - if (initRet) - { - rtgf.ref() == tgf1(); - } - - return rtgf; + if (initRet) + { + rtgf.ref() == gf1; } + + return rtgf; } @@ -138,7 +134,7 @@ public: const dimensionSet& dimensions ) { - const GeometricField& gf1 = tgf1(); + const auto& gf1 = tgf1(); return tmp> ( @@ -170,32 +166,31 @@ public: const dimensionSet& dimensions ) { - GeometricField& gf1 = - const_cast&>(tgf1()); - if (reusable(tgf1)) { + auto& gf1 = tgf1.constCast(); + gf1.rename(name); gf1.dimensions().reset(dimensions); return tgf1; } - else - { - return tmp> + + const auto& gf1 = tgf1(); + + return tmp> + ( + new GeometricField ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) - ); - } + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions + ) + ); } }; @@ -221,7 +216,7 @@ public: const dimensionSet& dimensions ) { - const GeometricField& gf1 = tgf1(); + const auto& gf1 = tgf1(); return tmp> ( @@ -262,33 +257,31 @@ public: const dimensionSet& dimensions ) { - const GeometricField& gf1 = tgf1(); - GeometricField& gf2 = - const_cast&>(tgf2()); - if (reusable(tgf2)) { + auto& gf2 = tgf2.constCast(); + gf2.rename(name); gf2.dimensions().reset(dimensions); return tgf2; } - else - { - return tmp> + + const auto& gf1 = tgf1(); + + return tmp> + ( + new GeometricField ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) - ); - } + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions + ) + ); } }; @@ -312,32 +305,31 @@ public: const dimensionSet& dimensions ) { - GeometricField& gf1 = - const_cast&>(tgf1()); - if (reusable(tgf1)) { + auto& gf1 = tgf1.constCast(); + gf1.rename(name); gf1.dimensions().reset(dimensions); return tgf1; } - else - { - return tmp> + + const auto& gf1 = tgf1(); + + return tmp> + ( + new GeometricField ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) - ); - } + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions + ) + ); } }; @@ -355,40 +347,39 @@ public: const dimensionSet& dimensions ) { - GeometricField& gf1 = - const_cast&>(tgf1()); - GeometricField& gf2 = - const_cast&>(tgf2()); - if (reusable(tgf1)) { + auto& gf1 = tgf1.constCast(); + gf1.rename(name); gf1.dimensions().reset(dimensions); return tgf1; } else if (reusable(tgf2)) { + auto& gf2 = tgf2.constCast(); + gf2.rename(name); gf2.dimensions().reset(dimensions); return tgf2; } - else - { - return tmp> + + const auto& gf1 = tgf1(); + + return tmp> + ( + new GeometricField ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) - ); - } + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions + ) + ); } }; diff --git a/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixTemplates.C b/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixTemplates.C index 0ac399fb59..9f28ad17cd 100644 --- a/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixTemplates.C +++ b/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrixTemplates.C @@ -98,15 +98,13 @@ Foam::lduMatrix::faceH(const Field& psi) const return tfaceHpsi; } - else - { - FatalErrorInFunction - << "Cannot calculate faceH" - " the matrix does not have any off-diagonal coefficients." - << exit(FatalError); - return tmp>(nullptr); - } + FatalErrorInFunction + << "Cannot calculate faceH" + " the matrix does not have any off-diagonal coefficients." + << exit(FatalError); + + return tmp>(); } diff --git a/src/OpenFOAM/memory/refCount/refCount.H b/src/OpenFOAM/memory/refCount/refCount.H index 1f90ba73a4..5b6e22c681 100644 --- a/src/OpenFOAM/memory/refCount/refCount.H +++ b/src/OpenFOAM/memory/refCount/refCount.H @@ -53,6 +53,10 @@ class refCount public: + //- A non-counting (dummy) refCount + struct zero {}; + + // Constructors //- Construct null initializing count to 0 diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 52dc896a83..736a7c4cca 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,14 +25,19 @@ Class Foam::tmp Description - A class for managing temporary objects + A class for managing temporary objects. + + This is a combination of std::shared_ptr (with intrusive ref-counting) + and a shared_ptr without ref-counting and null deleter. + This allows the tmp to double as a pointer management and an indirect + pointer to externally allocated objects. SourceFiles tmpI.H See also - Foam::refCount Foam::autoPtr + Foam::refCount \*---------------------------------------------------------------------------*/ @@ -41,6 +46,7 @@ See also #include "refCount.H" #include "word.H" +#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -57,111 +63,201 @@ class tmp // Private data //- Object types - enum type + enum refType { - TMP, - CONST_REF + PTR, //!< Managing a (ref-counted) pointer + CREF //!< Using a const-reference to an object }; - //- Pointer to object + //- The managed pointer or the address of const-reference object mutable T* ptr_; - //- Type of object - type type_; + //- The type (managed pointer | const-reference object) + mutable refType type_; - // Private member operators + // Private Member Operators + //- Increment the ref-count for a managed pointer inline void operator++(); public: - typedef T Type; + // STL type definitions + + //- Type of object being managed or referenced + typedef T element_type; + + //- Pointer to type of object being managed or referenced + typedef T* pointer; + + + //- Reference counter class typedef Foam::refCount refCount; + // Factory Methods + + //- Construct tmp of T with forwarding arguments + // \param args list of arguments with which an instance of T + // will be constructed. + // + // \note Similar to std::make_shared, but the overload for + // array types is not disabled. + template + inline static tmp New(Args&&... args); + + //- Construct tmp from derived type with forwarding arguments + // \param args list of arguments with which an instance of U + // will be constructed. + // + // \note Similar to New but for derived types + template + inline static tmp NewFrom(Args&&... args); + + // Constructors - //- Null pointer construct - inline tmp(); + //- Construct with no managed pointer. + inline constexpr tmp() noexcept; - //- Store object pointer + //- Construct with no managed pointer. + inline constexpr tmp(std::nullptr_t) noexcept; + + //- Construct, taking ownership of the pointer. inline explicit tmp(T* p); - //- Store object const reference - inline tmp(const T& t); + //- Construct for a const reference to an object. + inline tmp(const T& obj) noexcept; - //- Construct copy and increment reference count + //- Move construct, transferring ownership. + // Does not affect ref-count + inline tmp(tmp&& t) noexcept; + + //- Move construct, transferring ownership. + // Does not affect ref-count + // \note Non-standard definition - should be non-const + inline tmp(const tmp&& t) noexcept; + + //- Copy construct, incrementing ref-count of managed pointer. + // \note Non-standard definition - should be non-const inline tmp(const tmp& t); - //- Construct copy moving content, does not increment reference count - inline tmp(const tmp&& t); - - //- Construct copy transferring content of temporary if required - inline tmp(const tmp& t, bool allowTransfer); + //- Copy construct. Optionally reusing ref-counted pointer. + inline tmp(const tmp& t, bool reuse); - //- Destructor: deletes temporary object when the reference count is 0 + //- Destructor: deletes managed pointer when the ref-count is 0 inline ~tmp(); // Member Functions - // Access + // Query - //- Return true if this is really a temporary object - inline bool isTmp() const; + //- True if this is a managed pointer (not a const reference) + inline bool isTmp() const; - //- Return true if this temporary object empty, - // ie, a temporary without allocation - inline bool empty() const; + //- True if this is a non-null managed pointer + inline bool empty() const; - //- Is this temporary object valid, - // ie, it is a reference or a temporary that has been allocated - inline bool valid() const; + //- True if this is a non-null managed pointer, + //- or is a const object reference + inline bool valid() const; - //- Return the type name of the tmp - // constructed from the type name of T - inline word typeName() const; + //- True if this is a non-null managed pointer with a unique ref-count + inline bool movable() const; + + //- Return type-name of the tmp, constructed from type-name of T + inline word typeName() const; - // Edit + // Access - //- Return tmp pointer for reuse. - // Returns a clone if the object is not a temporary - inline T* ptr() const; + //- Return the const object reference or a const reference to the + //- contents of a non-null managed pointer. + // Fatal for a null managed pointer + inline const T& cref() const; - //- Return non-const reference. Fatal if the object is const. - inline T& ref() const; + //- Return non-const reference to the contents of a non-null + //- managed pointer. + // Fatal for a null managed pointer or if the object is const. + inline T& ref() const; - //- If object pointer points to valid object: - //- delete object and set pointer to nullptr - inline void clear() const; + //- Non-const dereference, even if the object is const. + // This is similar to ref(), but applies a const_cast to access + // const objects. + // Fatal for a null managed pointer. + inline T& constCast() const; + + + // Edit + + //- Return managed pointer for reuse, or clone() the const object + //- reference. + inline T* ptr() const; + + //- If object pointer points to valid object: + //- delete object and set pointer to nullptr + inline void clear() const; + + //- Release ownership of managed temporary object. + // After this call no object is managed. + inline void reset(); + + //- Delete managed temporary object and set to new given pointer + inline void reset(T* p); + + //- Swaps the managed object with other tmp. + inline void swap(tmp& other) noexcept; // Member operators - //- Const dereference operator + //- Return const reference to the object. + // Identical to cref() method. inline const T& operator()() const; - //- Const cast to the underlying type reference + //- Cast to underlying data type, using the cref() method. inline operator const T&() const; - //- Return object pointer - inline T* operator->(); - - //- Return const object pointer + //- Dereferences (const) pointer to the managed object. + // Fatal for a null managed pointer. inline const T* operator->() const; - //- Assignment to pointer changing this tmp to a temporary T + //- Dereferences (non-const) pointer to the managed object. + // Fatal for a null managed pointer or if the object is const. + inline T* operator->(); + + //- Take ownership of the pointer. + // Fatal for a null pointer, or when the pointer is non-unique. inline void operator=(T* p); - //- Assignment transferring the temporary T to this tmp + //- Transfer ownership of the managed pointer. + // Fatal for a null managed pointer or if the object is const. inline void operator=(const tmp& t); + + + // Housekeeping + + //- Disallow assignment from literal nullptr. + // Consistent with run-time check for nullptr on assignment. + void operator=(std::nullptr_t) = delete; }; +// Global Functions + +//- Specializes the Swap algorithm for tmp. +// Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs) +template +void Swap(tmp& lhs, tmp& rhs) +{ + lhs.swap(rhs); +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index c789bc0dbf..c2729bc1c7 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,13 +43,39 @@ inline void Foam::tmp::operator++() } +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +template +template +inline Foam::tmp Foam::tmp::New(Args&&... args) +{ + return tmp(new T(std::forward(args)...)); +} + + +template +template +inline Foam::tmp Foam::tmp::NewFrom(Args&&... args) +{ + return tmp(new U(std::forward(args)...)); +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -inline Foam::tmp::tmp() +inline constexpr Foam::tmp::tmp() noexcept : ptr_(nullptr), - type_(TMP) + type_(PTR) +{} + + +template +inline constexpr Foam::tmp::tmp(std::nullptr_t) noexcept +: + ptr_(nullptr), + type_(PTR) {} @@ -57,7 +83,7 @@ template inline Foam::tmp::tmp(T* p) : ptr_(p), - type_(TMP) + type_(PTR) { if (p && !p->unique()) { @@ -70,13 +96,35 @@ inline Foam::tmp::tmp(T* p) template -inline Foam::tmp::tmp(const T& t) +inline Foam::tmp::tmp(const T& obj) noexcept : - ptr_(const_cast(&t)), - type_(CONST_REF) + ptr_(const_cast(&obj)), + type_(CREF) {} +template +inline Foam::tmp::tmp(tmp&& t) noexcept +: + ptr_(t.ptr_), + type_(t.type_) +{ + t.ptr_ = nullptr; + t.type_ = PTR; +} + + +template +inline Foam::tmp::tmp(const tmp&& t) noexcept +: + ptr_(t.ptr_), + type_(t.type_) +{ + t.ptr_ = nullptr; + t.type_ = PTR; +} + + template inline Foam::tmp::tmp(const tmp& t) : @@ -100,20 +148,7 @@ inline Foam::tmp::tmp(const tmp& t) template -inline Foam::tmp::tmp(const tmp&& t) -: - ptr_(t.ptr_), - type_(t.type_) -{ - if (isTmp()) - { - t.ptr_ = nullptr; - } -} - - -template -inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) +inline Foam::tmp::tmp(const tmp& t, bool reuse) : ptr_(t.ptr_), type_(t.type_) @@ -122,9 +157,9 @@ inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) { if (ptr_) { - if (allowTransfer) + if (reuse) { - t.ptr_ = nullptr; + t.ptr_ = nullptr; // t.type_ already set as PTR } else { @@ -153,21 +188,28 @@ inline Foam::tmp::~tmp() template inline bool Foam::tmp::isTmp() const { - return type_ == TMP; + return type_ == PTR; } template inline bool Foam::tmp::empty() const { - return (isTmp() && !ptr_); + return (!ptr_ && isTmp()); } template inline bool Foam::tmp::valid() const { - return (!isTmp() || (isTmp() && ptr_)); + return (ptr_ || type_ == CREF); +} + + +template +inline bool Foam::tmp::movable() const +{ + return (type_ == PTR && ptr_ && ptr_->unique()); } @@ -179,7 +221,7 @@ inline Foam::word Foam::tmp::typeName() const template -inline T* Foam::tmp::ptr() const +inline const T& Foam::tmp::cref() const { if (isTmp()) { @@ -189,22 +231,9 @@ inline T* Foam::tmp::ptr() const << typeName() << " deallocated" << abort(FatalError); } - - if (!ptr_->unique()) - { - FatalErrorInFunction - << "Attempt to acquire pointer to object referred to" - << " by multiple temporaries of type " << typeName() - << abort(FatalError); - } - - T* ptr = ptr_; - ptr_ = nullptr; - - return ptr; } - return ptr_->clone().ptr(); + return *ptr_; // const reference } @@ -223,12 +252,55 @@ inline T& Foam::tmp::ref() const else { FatalErrorInFunction - << "Attempt to acquire non-const reference to const object" - << " from a " << typeName() + << "Attempted non-const reference to const object from a " + << typeName() << abort(FatalError); } - return *ptr_; + return *ptr_; // non-const reference +} + + +template +inline T& Foam::tmp::constCast() const +{ + if (isTmp() && !ptr_) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } + + return const_cast(*ptr_); +} + + +template +inline T* Foam::tmp::ptr() const +{ + if (isTmp()) + { + if (!ptr_) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } + else if (!ptr_->unique()) + { + FatalErrorInFunction + << "Attempt to acquire pointer to object referred to" + << " by multiple temporaries of type " << typeName() + << abort(FatalError); + } + + T* ptr = ptr_; + ptr_ = nullptr; + + return ptr; + } + + return ptr_->clone().ptr(); } @@ -240,41 +312,75 @@ inline void Foam::tmp::clear() const if (ptr_->unique()) { delete ptr_; - ptr_ = nullptr; } else { ptr_->operator--(); - ptr_ = nullptr; } + ptr_ = nullptr; } } +template +inline void Foam::tmp::reset() +{ + clear(); + ptr_ = nullptr; + type_ = PTR; +} + + +template +inline void Foam::tmp::reset(T* p) +{ + clear(); + ptr_ = p; + type_ = PTR; +} + + +template +inline void Foam::tmp::swap(tmp& other) noexcept +{ + // Copy/assign for pointer types + T* p = ptr_; + ptr_ = other.ptr_; + other.ptr_ = p; + + refType t = type_; + type_ = other.type_; + other.type_ = t; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template inline const T& Foam::tmp::operator()() const { - if (isTmp()) - { - if (!ptr_) - { - FatalErrorInFunction - << typeName() << " deallocated" - << abort(FatalError); - } - } - - // Return const reference - return *ptr_; + return cref(); } template inline Foam::tmp::operator const T&() const { - return operator()(); + return cref(); +} + + +template +inline const T* Foam::tmp::operator->() const +{ + if (!ptr_ && isTmp()) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } + + return ptr_; } @@ -301,20 +407,6 @@ inline T* Foam::tmp::operator->() } -template -inline const T* Foam::tmp::operator->() const -{ - if (isTmp() && !ptr_) - { - FatalErrorInFunction - << typeName() << " deallocated" - << abort(FatalError); - } - - return ptr_; -} - - template inline void Foam::tmp::operator=(T* p) { @@ -326,8 +418,7 @@ inline void Foam::tmp::operator=(T* p) << "Attempted copy of a deallocated " << typeName() << abort(FatalError); } - - if (p && !p->unique()) + else if (!p->unique()) { FatalErrorInFunction << "Attempted assignment of a " << typeName() @@ -336,7 +427,7 @@ inline void Foam::tmp::operator=(T* p) } ptr_ = p; - type_ = TMP; + type_ = PTR; } @@ -347,17 +438,16 @@ inline void Foam::tmp::operator=(const tmp& t) if (t.isTmp()) { - type_ = TMP; + ptr_ = t.ptr_; + type_ = PTR; + t.ptr_ = nullptr; - if (!t.ptr_) + if (!ptr_) { FatalErrorInFunction << "Attempted assignment to a deallocated " << typeName() << abort(FatalError); } - - ptr_ = t.ptr_; - t.ptr_ = nullptr; } else { diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H new file mode 100644 index 0000000000..30eabf397a --- /dev/null +++ b/src/OpenFOAM/memory/tmp/tmpNrc.H @@ -0,0 +1,214 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::tmpNrc + +Description + A class for managing temporary objects without reference counting. + +SourceFiles + tmpNrcI.H + +See also + Foam::autoPtr + +\*---------------------------------------------------------------------------*/ + +#ifndef tmpNrc_H +#define tmpNrc_H + +#include "refCount.H" +#include "word.H" +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class tmpNrc Declaration +\*---------------------------------------------------------------------------*/ + +template +class tmpNrc +{ + // Private data + + //- Object types + enum refType + { + PTR, //!< Managing a pointer (not ref-counted) + CREF //!< Using a const-reference to an object + }; + + //- The managed pointer or the address of const-reference object + mutable T* ptr_; + + //- The type (managed pointer | const-reference object) + refType type_; + + +public: + + // STL type definitions + + //- Type of object being managed + typedef T element_type; + + //- Pointer to type of object being managed + typedef T* pointer; + + + //- Null reference counter class + typedef Foam::refCount::zero refCount; + + + // Constructors + + //- Construct with no managed pointer. + inline constexpr tmpNrc() noexcept; + + //- Construct with no managed pointer. + inline constexpr tmpNrc(std::nullptr_t) noexcept; + + //- Construct, taking ownership of the pointer. + inline explicit tmpNrc(T* p) noexcept; + + //- Construct for a const reference to an object. + inline tmpNrc(const T& obj) noexcept; + + //- Copy construct + inline tmpNrc(const tmpNrc& t); + + //- Construct copy, transferring content of temporary if required + inline tmpNrc(const tmpNrc& t, bool reuse); + + + //- Destructor: deletes managed pointer + inline ~tmpNrc(); + + + // Member Functions + + // Query + + //- True if this is a managed pointer (not a const reference) + inline bool isTmp() const; + + //- True if this is a non-null managed pointer + inline bool empty() const; + + //- True if this is a non-null managed pointer, + //- or is a const object reference + inline bool valid() const; + + //- True if this is a non-null managed pointer with a unique ref-count + inline bool movable() const; + + //- Return type-name of the tmp, constructed from type-name of T + inline word typeName() const; + + + // Access + + //- Return the const object reference or a const reference to the + //- contents of a non-null managed pointer. + // Fatal for a null managed pointer + inline const T& cref() const; + + //- Return non-const reference to the contents of a non-null + //- managed pointer. + // Fatal for a null managed pointer or if the object is const. + inline T& ref() const; + + //- Non-const dereference, even if the object is const. + // This is similar to ref(), but applies a const_cast to access + // const objects. + // Fatal for a null managed pointer. + inline T& constCast() const; + + + // Edit + + //- Return managed pointer for reuse, or clone() the const object + //- reference. + inline T* ptr() const; + + //- If object pointer points to valid object: + //- delete object and set pointer to nullptr + inline void clear() const; + + + // Member operators + + //- Return const reference to the object. + // Identical to cref() method. + inline const T& operator()() const; + + //- Cast to underlying data type, using the cref() method. + inline operator const T&() const; + + //- Dereferences (const) pointer to the managed object. + // Fatal for a null managed pointer. + inline const T* operator->() const; + + //- Dereferences (non-const) pointer to the managed object. + // Fatal for a null managed pointer or if the object is const. + inline T* operator->(); + + //- Take ownership of the pointer. + // Fatal for a null pointer, or when the pointer is non-unique. + inline void operator=(T* p); + + //- Transfer ownership of the managed pointer. + // Fatal for a null managed pointer or if the object is const. + inline void operator=(const tmpNrc& t); +}; + + +// Global Functions + +//- Specializes the Swap algorithm for tmp. +// Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs) +template +void Swap(tmpNrc& lhs, tmpNrc& rhs) +{ + lhs.swap(rhs); +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "tmpNrcI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H similarity index 72% rename from src/OpenFOAM/memory/tmpNrc/tmpNrcI.H rename to src/OpenFOAM/memory/tmp/tmpNrcI.H index 7f26ab095f..85e2b3a25c 100644 --- a/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H +++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2016-2017 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,32 +29,48 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -inline Foam::tmpNrc::tmpNrc(T* tPtr) +inline constexpr Foam::tmpNrc::tmpNrc() noexcept : - type_(TMP), - ptr_(tPtr) + ptr_(nullptr), + type_(PTR) {} template -inline Foam::tmpNrc::tmpNrc(const T& tRef) +inline constexpr Foam::tmpNrc::tmpNrc(std::nullptr_t) noexcept : - type_(CONST_REF), - ptr_(const_cast(&tRef)) + ptr_(nullptr), + type_(PTR) +{} + + +template +inline Foam::tmpNrc::tmpNrc(T* p) noexcept +: + ptr_(p), + type_(PTR) +{} + + +template +inline Foam::tmpNrc::tmpNrc(const T& obj) noexcept +: + ptr_(const_cast(&obj)), + type_(CREF) {} template inline Foam::tmpNrc::tmpNrc(const tmpNrc& t) : - type_(t.type_), - ptr_(t.ptr_) + ptr_(t.ptr_), + type_(t.type_) { if (isTmp()) { if (ptr_) { - t.type_ = CONST_REF; + t.type_ = CREF; } else { @@ -67,22 +83,22 @@ inline Foam::tmpNrc::tmpNrc(const tmpNrc& t) template -inline Foam::tmpNrc::tmpNrc(const tmpNrc& t, bool allowTransfer) +inline Foam::tmpNrc::tmpNrc(const tmpNrc& t, bool reuse) : - type_(t.type_), - ptr_(t.ptr_) + ptr_(t.ptr_), + type_(t.type_) { if (isTmp()) { if (ptr_) { - if (allowTransfer) + if (reuse) { - t.ptr_ = nullptr; + t.ptr_ = nullptr; // t.type_ already set as PTR } else { - t.type_ = CONST_REF; + t.type_ = CREF; } } else @@ -107,21 +123,28 @@ inline Foam::tmpNrc::~tmpNrc() template inline bool Foam::tmpNrc::isTmp() const { - return type_ == TMP; + return type_ == PTR; } template inline bool Foam::tmpNrc::empty() const { - return (isTmp() && !ptr_); + return (!ptr_ && isTmp()); } template inline bool Foam::tmpNrc::valid() const { - return (!isTmp() || (isTmp() && ptr_)); + return (ptr_ || type_ == CREF); +} + + +template +inline bool Foam::tmpNrc::movable() const +{ + return (type_ == PTR && ptr_); } @@ -133,7 +156,24 @@ inline Foam::word Foam::tmpNrc::typeName() const template -inline T& Foam::tmpNrc::ref() +inline const T& Foam::tmpNrc::cref() const +{ + if (isTmp()) + { + if (!ptr_) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } + } + + return *ptr_; // const reference +} + + +template +inline T& Foam::tmpNrc::ref() const { if (isTmp()) { @@ -147,12 +187,26 @@ inline T& Foam::tmpNrc::ref() else { FatalErrorInFunction - << "Attempt to acquire non-const reference to const object" - << " from a " << typeName() + << "Attempted non-const reference to const object from a " + << typeName() << abort(FatalError); } - return *ptr_; + return *ptr_; // non-const reference +} + + +template +inline T& Foam::tmpNrc::constCast() const +{ + if (isTmp() && !ptr_) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } + + return const_cast(*ptr_); } @@ -173,10 +227,8 @@ inline T* Foam::tmpNrc::ptr() const return ptr; } - else - { - return ptr_->clone().ptr(); - } + + return ptr_->clone().ptr(); } @@ -191,30 +243,47 @@ inline void Foam::tmpNrc::clear() const } +template +inline void Foam::tmpNrc::swap(tmpNrc& other) noexcept +{ + // Copy/assign for pointer types + T* p = ptr_; + ptr_ = other.ptr_; + other.ptr_ = p; + + refType t = type_; + type_ = other.type_; + other.type_ = t; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template inline const T& Foam::tmpNrc::operator()() const { - if (isTmp()) - { - if (!ptr_) - { - FatalErrorInFunction - << typeName() << " deallocated" - << abort(FatalError); - } - } - - // Return const reference - return *ptr_; + return cref(); } template inline Foam::tmpNrc::operator const T&() const { - return operator()(); + return cref(); +} + + +template +inline const T* Foam::tmpNrc::operator->() const +{ + if (!ptr_ && isTmp()) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } + + return ptr_; } @@ -242,33 +311,19 @@ inline T* Foam::tmpNrc::operator->() template -inline const T* Foam::tmpNrc::operator->() const -{ - if (isTmp() && !ptr_) - { - FatalErrorInFunction - << typeName() << " deallocated" - << abort(FatalError); - } - - return ptr_; -} - - -template -inline void Foam::tmpNrc::operator=(T* tPtr) +inline void Foam::tmpNrc::operator=(T* p) { clear(); - if (!tPtr) + if (!p) { FatalErrorInFunction << "Attempted copy of a deallocated " << typeName() << abort(FatalError); } - type_ = TMP; - ptr_ = tPtr; + ptr_ = p; + type_ = PTR; } @@ -279,17 +334,16 @@ inline void Foam::tmpNrc::operator=(const tmpNrc& t) if (t.isTmp()) { - type_ = TMP; + ptr_ = t.ptr_; + type_ = PTR; + t.ptr_ = nullptr; - if (!t.ptr_) + if (!ptr_) { FatalErrorInFunction << "Attempted assignment to a deallocated " << typeName() << abort(FatalError); } - - ptr_ = t.ptr_; - t.ptr_ = nullptr; } else { diff --git a/src/OpenFOAM/memory/tmpNrc/tmpNrc.H b/src/OpenFOAM/memory/tmpNrc/tmpNrc.H deleted file mode 100644 index 6aa3f91156..0000000000 --- a/src/OpenFOAM/memory/tmpNrc/tmpNrc.H +++ /dev/null @@ -1,171 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation - \\/ M anipulation | -------------------------------------------------------------------------------- -License - This file is part of OpenFOAM. - - OpenFOAM is free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - OpenFOAM is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License - along with OpenFOAM. If not, see . - -Class - Foam::tmpNrc - -Description - A class for managing temporary objects without reference counting. - -SourceFiles - tmpNrcI.H - -See also - Foam::autoPtr - -\*---------------------------------------------------------------------------*/ - -#ifndef tmpNrc_H -#define tmpNrc_H - -#include "word.H" - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - -/*---------------------------------------------------------------------------*\ - Class tmpNrc Declaration -\*---------------------------------------------------------------------------*/ - -template -class tmpNrc -{ - // Private data - - //- Object types - enum type - { - TMP, - CONST_REF - }; - - //- Type of object - mutable type type_; - - //- Pointer to object - mutable T* ptr_; - - -public: - - // Null reference counter class - class refCount - { - public: - - refCount() - {} - }; - - - // Constructors - - //- Store object pointer - inline explicit tmpNrc(T* tPtr = nullptr); - - //- Store object const reference - inline tmpNrc(const T& tRef); - - //- Construct copy and increment reference count - inline tmpNrc(const tmpNrc& t); - - //- Construct copy transferring content of temporary if required - inline tmpNrc(const tmpNrc& t, bool allowTransfer); - - - //- Destructor: deletes temporary object when the reference count is 0 - inline ~tmpNrc(); - - - // Member Functions - - // Access - - //- Return true if this is really a temporary object - inline bool isTmp() const; - - //- Return true if this temporary object empty, - // ie, a temporary without allocation - inline bool empty() const; - - //- Is this temporary object valid, - // ie, it is a reference or a temporary that has been allocated - inline bool valid() const; - - //- Return the type name of the tmpNrc - // constructed from the type name of T - inline word typeName() const; - - - // Edit - - //- Return non-const reference or generate a fatal error - // if the object is const. - inline T& ref(); - - //- Return tmpNrc pointer for reuse. - // Returns a clone if the object is not a temporary - inline T* ptr() const; - - //- If object pointer points to valid object: - // delete object and set pointer to nullptr - inline void clear() const; - - - // Member operators - - //- Const dereference operator - inline const T& operator()() const; - - //- Const cast to the underlying type reference - inline operator const T&() const; - - //- Return object pointer - inline T* operator->(); - - //- Return const object pointer - inline const T* operator->() const; - - //- Assignment to pointer changing this tmpNrc to a temporary T - inline void operator=(T* tPtr); - - //- Assignment transferring the temporary T to this tmpNrc - inline void operator=(const tmpNrc& t); -}; - - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -#include "tmpNrcI.H" - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -#endif - -// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C index e5aedbd501..6d46ce921d 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C +++ b/src/OpenFOAM/primitives/functions/Function1/Function1/Function1.C @@ -79,8 +79,7 @@ Foam::tmp> Foam::Function1::value ) const { NotImplemented; - - return tmp>(nullptr); + return tmp>(); } @@ -101,8 +100,7 @@ Foam::tmp> Foam::Function1::integrate ) const { NotImplemented; - - return tmp>(nullptr); + return tmp>(); } diff --git a/src/TurbulenceModels/schemes/DEShybrid/DEShybrid.H b/src/TurbulenceModels/schemes/DEShybrid/DEShybrid.H index da7dfa8bdf..f1c8dfd599 100644 --- a/src/TurbulenceModels/schemes/DEShybrid/DEShybrid.H +++ b/src/TurbulenceModels/schemes/DEShybrid/DEShybrid.H @@ -417,15 +417,13 @@ public: vf, model.muEff()/model.rho(), model.U(), delta ); } - else - { - FatalErrorInFunction - << "Scheme requires a turbulence model to be present. " - << "Unable to retrieve turbulence model from the mesh " - << "database" << exit(FatalError); - return tmp(nullptr); - } + FatalErrorInFunction + << "Scheme requires a turbulence model to be present. " + << "Unable to retrieve turbulence model from the mesh " + << "database" << exit(FatalError); + + return tmp(); } @@ -501,13 +499,8 @@ public: { return (bf*tScheme2_().correction(vf)); } - else - { - return tmp> - ( - nullptr - ); - } + + return tmp>(); } }; diff --git a/src/TurbulenceModels/turbulenceModels/LES/LESfilters/LESfilter/LESfilter.H b/src/TurbulenceModels/turbulenceModels/LES/LESfilters/LESfilter/LESfilter.H index aa653e524c..11c366356d 100644 --- a/src/TurbulenceModels/turbulenceModels/LES/LESfilters/LESfilter/LESfilter.H +++ b/src/TurbulenceModels/turbulenceModels/LES/LESfilters/LESfilter/LESfilter.H @@ -79,7 +79,7 @@ protected: template void correctBoundaryConditions(const tmp& tgf) const { - const_cast(tgf()).correctBoundaryConditions(); + tgf.constCast().correctBoundaryConditions(); } diff --git a/src/finiteArea/finiteArea/lnGradSchemes/lnGradScheme/lnGradScheme.H b/src/finiteArea/finiteArea/lnGradSchemes/lnGradScheme/lnGradScheme.H index 3429ea0d06..be8e5b7e19 100644 --- a/src/finiteArea/finiteArea/lnGradSchemes/lnGradScheme/lnGradScheme.H +++ b/src/finiteArea/finiteArea/lnGradSchemes/lnGradScheme/lnGradScheme.H @@ -148,7 +148,7 @@ public: virtual tmp> correction(const GeometricField&) const { - return tmp>(nullptr); + return tmp>(); } //- Return the lnGrad of the given cell field diff --git a/src/finiteArea/interpolation/edgeInterpolation/edgeInterpolationScheme/edgeInterpolationScheme.H b/src/finiteArea/interpolation/edgeInterpolation/edgeInterpolationScheme/edgeInterpolationScheme.H index a26dbbf747..fd2e8b8ff7 100644 --- a/src/finiteArea/interpolation/edgeInterpolation/edgeInterpolationScheme/edgeInterpolationScheme.H +++ b/src/finiteArea/interpolation/edgeInterpolation/edgeInterpolationScheme/edgeInterpolationScheme.H @@ -189,7 +189,7 @@ public: virtual tmp> correction(const GeometricField&) const { - return tmp>(nullptr); + return tmp>(); } //- Return the face-interpolate of the given cell field diff --git a/src/finiteVolume/finiteVolume/ddtSchemes/CoEulerDdtScheme/CoEulerDdtScheme.C b/src/finiteVolume/finiteVolume/ddtSchemes/CoEulerDdtScheme/CoEulerDdtScheme.C index 5d896b07a9..04600de1be 100644 --- a/src/finiteVolume/finiteVolume/ddtSchemes/CoEulerDdtScheme/CoEulerDdtScheme.C +++ b/src/finiteVolume/finiteVolume/ddtSchemes/CoEulerDdtScheme/CoEulerDdtScheme.C @@ -135,14 +135,12 @@ tmp CoEulerDdtScheme::CofrDeltaT() const return max(Co/maxCo_, scalar(1))/deltaT; } - else - { - FatalErrorInFunction - << "Incorrect dimensions of phi: " << phi.dimensions() - << abort(FatalError); - return tmp(nullptr); - } + FatalErrorInFunction + << "Incorrect dimensions of phi: " << phi.dimensions() + << abort(FatalError); + + return tmp(); } diff --git a/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledFvPatch.C b/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledFvPatch.C index d263b854d7..dce9cc4759 100644 --- a/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledFvPatch.C +++ b/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledFvPatch.C @@ -56,13 +56,8 @@ Foam::tmp Foam::regionCoupledFvPatch::internalFieldTransfer { return neighbFvPatch().patchInternalField(iF); } - else - { - return tmp(new labelField(iF.size(), 0)); - } - - return tmp(nullptr); + return tmp(new labelField(iF.size(), 0)); } diff --git a/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledWallFvPatch.C b/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledWallFvPatch.C index 2aad929858..5662a8103e 100644 --- a/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledWallFvPatch.C +++ b/src/finiteVolume/fvMesh/fvPatches/derived/regionCoupled/regionCoupledWallFvPatch.C @@ -58,12 +58,8 @@ internalFieldTransfer { return neighbFvPatch().patchInternalField(iF); } - else - { - return tmp(new labelField(iF.size(), 0)); - } - return tmp(nullptr); + return tmp(new labelField(iF.size(), 0)); } // ************************************************************************* // diff --git a/src/functionObjects/field/heatTransferCoeff/heatTransferCoeffModels/ReynoldsAnalogy/ReynoldsAnalogy.C b/src/functionObjects/field/heatTransferCoeff/heatTransferCoeffModels/ReynoldsAnalogy/ReynoldsAnalogy.C index 8a37466955..9dc7ba2ea8 100644 --- a/src/functionObjects/field/heatTransferCoeff/heatTransferCoeffModels/ReynoldsAnalogy/ReynoldsAnalogy.C +++ b/src/functionObjects/field/heatTransferCoeff/heatTransferCoeffModels/ReynoldsAnalogy/ReynoldsAnalogy.C @@ -62,14 +62,12 @@ Foam::heatTransferCoeffModels::ReynoldsAnalogy::rho(const label patchi) const mesh_.lookupObject(rhoName_); return rho.boundaryField()[patchi]; } - else - { - FatalErrorInFunction - << "Unable to set rho for patch " << patchi - << exit(FatalError); - } - return tmp>(nullptr); + FatalErrorInFunction + << "Unable to set rho for patch " << patchi + << exit(FatalError); + + return tmp>(); } @@ -91,14 +89,12 @@ Foam::heatTransferCoeffModels::ReynoldsAnalogy::Cp(const label patchi) const return thermo.Cp(pp, Tp, patchi); } - else - { - FatalErrorInFunction - << "Unable to set Cp for patch " << patchi - << exit(FatalError); - } - return tmp>(nullptr); + FatalErrorInFunction + << "Unable to set Cp for patch " << patchi + << exit(FatalError); + + return tmp>(); } diff --git a/src/fvOptions/sources/derived/solidificationMeltingSource/solidificationMeltingSource.C b/src/fvOptions/sources/derived/solidificationMeltingSource/solidificationMeltingSource.C index 057614e094..79eaa7d6e8 100644 --- a/src/fvOptions/sources/derived/solidificationMeltingSource/solidificationMeltingSource.C +++ b/src/fvOptions/sources/derived/solidificationMeltingSource/solidificationMeltingSource.C @@ -119,7 +119,7 @@ Foam::fv::solidificationMeltingSource::Cp() const } } - return tmp(nullptr); + return tmp(); } diff --git a/src/lagrangian/intermediate/submodels/MPPIC/ParticleStressModels/Lun/Lun.C b/src/lagrangian/intermediate/submodels/MPPIC/ParticleStressModels/Lun/Lun.C index 2c1c04c456..47ba719288 100644 --- a/src/lagrangian/intermediate/submodels/MPPIC/ParticleStressModels/Lun/Lun.C +++ b/src/lagrangian/intermediate/submodels/MPPIC/ParticleStressModels/Lun/Lun.C @@ -109,8 +109,7 @@ Foam::ParticleStressModels::Lun::dTaudTheta ) const { NotImplemented; - - return tmp>(nullptr); + return tmp>(); } diff --git a/src/lagrangian/turbulence/submodels/Kinematic/DispersionModel/DispersionRASModel/DispersionRASModel.C b/src/lagrangian/turbulence/submodels/Kinematic/DispersionModel/DispersionRASModel/DispersionRASModel.C index 87bf0d84ac..35f869c586 100644 --- a/src/lagrangian/turbulence/submodels/Kinematic/DispersionModel/DispersionRASModel/DispersionRASModel.C +++ b/src/lagrangian/turbulence/submodels/Kinematic/DispersionModel/DispersionRASModel/DispersionRASModel.C @@ -47,15 +47,13 @@ Foam::DispersionRASModel::kModel() const obr.lookupObject(turbName); return model.k(); } - else - { - FatalErrorInFunction - << "Turbulence model not found in mesh database" << nl - << "Database objects include: " << obr.sortedToc() - << abort(FatalError); - return tmp(nullptr); - } + FatalErrorInFunction + << "Turbulence model not found in mesh database" << nl + << "Database objects include: " << obr.sortedToc() + << abort(FatalError); + + return tmp(); } @@ -77,15 +75,13 @@ Foam::DispersionRASModel::epsilonModel() const obr.lookupObject(turbName); return model.epsilon(); } - else - { - FatalErrorInFunction - << "Turbulence model not found in mesh database" << nl - << "Database objects include: " << obr.sortedToc() - << abort(FatalError); - return tmp(nullptr); - } + FatalErrorInFunction + << "Turbulence model not found in mesh database" << nl + << "Database objects include: " << obr.sortedToc() + << abort(FatalError); + + return tmp(); } diff --git a/src/lagrangian/turbulence/submodels/Thermodynamic/ParticleForces/BrownianMotion/BrownianMotionForce.C b/src/lagrangian/turbulence/submodels/Thermodynamic/ParticleForces/BrownianMotion/BrownianMotionForce.C index a771a0837d..4b0be43e65 100644 --- a/src/lagrangian/turbulence/submodels/Thermodynamic/ParticleForces/BrownianMotion/BrownianMotionForce.C +++ b/src/lagrangian/turbulence/submodels/Thermodynamic/ParticleForces/BrownianMotion/BrownianMotionForce.C @@ -70,15 +70,13 @@ Foam::BrownianMotionForce::kModel() const obr.lookupObject(turbName); return model.k(); } - else - { - FatalErrorInFunction - << "Turbulence model not found in mesh database" << nl - << "Database objects include: " << obr.sortedToc() - << abort(FatalError); - return tmp(nullptr); - } + FatalErrorInFunction + << "Turbulence model not found in mesh database" << nl + << "Database objects include: " << obr.sortedToc() + << abort(FatalError); + + return tmp(); } diff --git a/src/meshTools/coordinateSystems/coordinateRotation/EulerCoordinateRotation.C b/src/meshTools/coordinateSystems/coordinateRotation/EulerCoordinateRotation.C index 179836560a..cbc6124624 100644 --- a/src/meshTools/coordinateSystems/coordinateRotation/EulerCoordinateRotation.C +++ b/src/meshTools/coordinateSystems/coordinateRotation/EulerCoordinateRotation.C @@ -70,7 +70,7 @@ Foam::tmp Foam::EulerCoordinateRotation::transform ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -80,7 +80,7 @@ Foam::tmp Foam::EulerCoordinateRotation::invTransform ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -97,7 +97,7 @@ Foam::tmp Foam::EulerCoordinateRotation::transformTensor ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -117,7 +117,7 @@ Foam::tmp Foam::EulerCoordinateRotation::transformTensor ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } diff --git a/src/meshTools/coordinateSystems/coordinateRotation/STARCDCoordinateRotation.C b/src/meshTools/coordinateSystems/coordinateRotation/STARCDCoordinateRotation.C index e6a50a25d6..fcaa3ed099 100644 --- a/src/meshTools/coordinateSystems/coordinateRotation/STARCDCoordinateRotation.C +++ b/src/meshTools/coordinateSystems/coordinateRotation/STARCDCoordinateRotation.C @@ -71,7 +71,7 @@ Foam::tmp Foam::STARCDCoordinateRotation::transform ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -81,7 +81,7 @@ Foam::tmp Foam::STARCDCoordinateRotation::invTransform ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -98,7 +98,7 @@ Foam::tmp Foam::STARCDCoordinateRotation::transformTensor ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -118,7 +118,7 @@ Foam::tmp Foam::STARCDCoordinateRotation::transformTensor ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } diff --git a/src/meshTools/coordinateSystems/coordinateRotation/axesRotation.C b/src/meshTools/coordinateSystems/coordinateRotation/axesRotation.C index b22c509c4d..162bab5cb8 100644 --- a/src/meshTools/coordinateSystems/coordinateRotation/axesRotation.C +++ b/src/meshTools/coordinateSystems/coordinateRotation/axesRotation.C @@ -245,7 +245,7 @@ Foam::tmp Foam::axesRotation::transformTensor ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -265,7 +265,7 @@ Foam::tmp Foam::axesRotation::transformTensor ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } diff --git a/src/sampling/sampledSurface/sampledSurface/sampledSurface.C b/src/sampling/sampledSurface/sampledSurface/sampledSurface.C index 8082337550..8f3ea1ad50 100644 --- a/src/sampling/sampledSurface/sampledSurface/sampledSurface.C +++ b/src/sampling/sampledSurface/sampledSurface/sampledSurface.C @@ -137,7 +137,7 @@ Foam::tmp Foam::sampledSurface::sample ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -147,7 +147,7 @@ Foam::tmp Foam::sampledSurface::sample ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -157,7 +157,7 @@ Foam::tmp Foam::sampledSurface::sample ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -167,7 +167,7 @@ Foam::tmp Foam::sampledSurface::sample ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } @@ -177,7 +177,7 @@ Foam::tmp Foam::sampledSurface::sample ) const { NotImplemented; - return tmp(nullptr); + return tmp(); } diff --git a/src/thermophysicalModels/radiation/submodels/sootModel/noSoot/noSoot.C b/src/thermophysicalModels/radiation/submodels/sootModel/noSoot/noSoot.C index dd7ee27b91..fa9f6d8b9f 100644 --- a/src/thermophysicalModels/radiation/submodels/sootModel/noSoot/noSoot.C +++ b/src/thermophysicalModels/radiation/submodels/sootModel/noSoot/noSoot.C @@ -66,7 +66,7 @@ void Foam::radiation::noSoot::correct() const Foam::volScalarField& Foam::radiation::noSoot::soot() const { NotImplemented; - return tmp(nullptr); + return tmp(); } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C b/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C index 1e0d946903..2c8429ab0b 100644 --- a/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C +++ b/src/thermophysicalModels/solidChemistryModel/basicSolidChemistryModel/basicSolidChemistryModel.C @@ -69,7 +69,7 @@ Foam::basicSolidChemistryModel::RR(const label i) return dynamic_cast ( - const_cast + const_cast ( volScalarField::Internal::null() ) @@ -88,7 +88,7 @@ Foam::basicSolidChemistryModel::calculateRR return dynamic_cast&> ( - const_cast + const_cast ( volScalarField::Internal::null() ) diff --git a/src/waveModels/derivedFvPatchFields/waveAlpha/waveAlphaFvPatchScalarField.C b/src/waveModels/derivedFvPatchFields/waveAlpha/waveAlphaFvPatchScalarField.C index 4e57fcba84..5017506fba 100644 --- a/src/waveModels/derivedFvPatchFields/waveAlpha/waveAlphaFvPatchScalarField.C +++ b/src/waveModels/derivedFvPatchFields/waveAlpha/waveAlphaFvPatchScalarField.C @@ -106,11 +106,11 @@ void Foam::waveAlphaFvPatchScalarField::updateCoeffs() ) ); - waveModel& model = const_cast(tmodel()); + waveModel& model = tmodel.constCast(); model.correct(db().time().value()); - operator == (model.alpha()); + operator==(model.alpha()); fixedValueFvPatchField::updateCoeffs(); } diff --git a/src/waveModels/derivedFvPatchFields/waveVelocity/waveVelocityFvPatchVectorField.C b/src/waveModels/derivedFvPatchFields/waveVelocity/waveVelocityFvPatchVectorField.C index 83fa6c64ea..9755b2bd8c 100644 --- a/src/waveModels/derivedFvPatchFields/waveVelocity/waveVelocityFvPatchVectorField.C +++ b/src/waveModels/derivedFvPatchFields/waveVelocity/waveVelocityFvPatchVectorField.C @@ -106,11 +106,11 @@ void Foam::waveVelocityFvPatchVectorField::updateCoeffs() ) ); - waveModel& model = const_cast(tmodel()); + waveModel& model = tmodel.constCast(); model.correct(db().time().value()); - operator == (model.U()); + operator==(model.U()); fixedValueFvPatchField::updateCoeffs(); }