From 35fc2d1be2f75c01c5d4dacbf06ed2c479f264b7 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 10 Jan 2019 17:06:04 +0100 Subject: [PATCH] STYLE: modernize code for tmp reuse functions (#1160) - use forwarding tmp factory methods --- .../DimensionedFieldReuseFunctions.H | 194 ++++++-------- .../FieldField/FieldFieldReuseFunctions.H | 104 +++----- .../fields/Fields/Field/FieldReuseFunctions.H | 85 +++--- .../GeometricFieldReuseFunctions.H | 246 ++++++++---------- 4 files changed, 251 insertions(+), 378 deletions(-) diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldReuseFunctions.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldReuseFunctions.H index 967eace9ad..9357d12007 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 | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -33,47 +33,9 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -template -tmp> New -( - const tmp>& tdf1, - const word& name, - const dimensionSet& dimensions -) -{ - if (tdf1.isTmp()) - { - auto& df1 = tdf1.constCast(); - - df1.rename(name); - df1.dimensions().reset(dimensions); - return tdf1; - } - - const auto& df1 = tdf1(); - - return tmp> - ( - new DimensionedField - ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) - ); -} - - template -class reuseTmpDimensionedField +struct reuseTmpDimensionedField { -public: - static tmp> New ( const tmp>& tdf1, @@ -83,29 +45,24 @@ public: { const auto& df1 = tdf1(); - return tmp> + return tmp>::New ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions ); } }; template -class reuseTmpDimensionedField +struct reuseTmpDimensionedField { -public: - static tmp> New ( const tmp>& tdf1, @@ -124,29 +81,42 @@ public: const auto& df1 = tdf1(); - return tmp> + return tmp>::New ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions ); } }; -template -class reuseTmpTmpDimensionedField +//- Global function forwards to reuseTmpDimensionedField::New +template +tmp> New +( + const tmp>& tdf1, + const word& name, + const dimensionSet& dimensions +) { -public: + return reuseTmpDimensionedField::New + ( + tdf1, + name, + dimensions + ); +} + +template +struct reuseTmpTmpDimensionedField +{ static tmp> New ( const tmp>& tdf1, @@ -157,29 +127,24 @@ public: { const auto& df1 = tdf1(); - return tmp> + return tmp>::New ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions ); } }; template -class reuseTmpTmpDimensionedField +struct reuseTmpTmpDimensionedField { -public: - static tmp> New ( const tmp>& tdf1, @@ -199,29 +164,24 @@ public: const auto& df1 = tdf1(); - return tmp> + return tmp>::New ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions ); } }; template -class reuseTmpTmpDimensionedField +struct reuseTmpTmpDimensionedField { -public: - static tmp> New ( const tmp>& tdf1, @@ -241,29 +201,24 @@ public: const auto& df1 = tdf1(); - return tmp> + return tmp>::New ( - new DimensionedField + IOobject ( - IOobject - ( - name, - df1.instance(), - df1.db() - ), - df1.mesh(), - dimensions - ) + name, + df1.instance(), + df1.db() + ), + df1.mesh(), + dimensions ); } }; template -class reuseTmpTmpDimensionedField +struct reuseTmpTmpDimensionedField { -public: - static tmp> New ( const tmp>& tdf1, @@ -291,19 +246,16 @@ public: const auto& df1 = tdf1(); - return tmp> + return tmp>::New ( - 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/FieldFieldReuseFunctions.H b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldReuseFunctions.H index 43383f1eb5..6a0db8c6c1 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 | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -33,58 +33,26 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -template class Field, class TypeR> -tmp> New -( - const tmp>& tf1, - const bool initRet = false -) -{ - if (tf1.isTmp()) - { - return tf1; - } - - tmp> rtf - ( - FieldField::NewCalculatedType(tf1()) - ); - - if (initRet) - { - rtf.ref() = tf1(); - } - - return rtf; -} - - template class Field, class TypeR, class Type1> -class reuseTmpFieldField +struct reuseTmpFieldField { -public: - static tmp> New ( const tmp>& tf1 ) { - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); + return FieldField::NewCalculatedType(tf1()); } }; template class Field, class TypeR> -class reuseTmpFieldField +struct reuseTmpFieldField { -public: - static tmp> New ( - const tmp>& tf1 + const tmp>& tf1, + const bool initCopy = false ) { if (tf1.isTmp()) @@ -92,14 +60,30 @@ public: return tf1; } - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); + auto rtf = FieldField::NewCalculatedType(tf1()); + + if (initCopy) + { + rtf.ref() = tf1(); + } + + return rtf; } }; +//- Global function forwards to reuseTmpFieldField::New +template class Field, class TypeR> +tmp> New +( + const tmp>& tf1, + const bool initCopy = false +) +{ + return reuseTmpFieldField::New(tf1, initCopy); +} + + template < template class Field, @@ -108,29 +92,22 @@ template class Type12, class Type2 > -class reuseTmpTmpFieldField +struct reuseTmpTmpFieldField { -public: - static tmp> New ( const tmp>& tf1, const tmp>& tf2 ) { - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); + return FieldField::NewCalculatedType(tf1()); } }; template class Field, class TypeR, class Type1, class Type12> -class reuseTmpTmpFieldField +struct reuseTmpTmpFieldField { -public: - static tmp> New ( const tmp>& tf1, @@ -142,19 +119,14 @@ public: return tf2; } - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); + return FieldField::NewCalculatedType(tf1()); } }; template class Field, class TypeR, class Type2> -class reuseTmpTmpFieldField +struct reuseTmpTmpFieldField { -public: - static tmp> New ( const tmp>& tf1, @@ -166,19 +138,14 @@ public: return tf1; } - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); + return FieldField::NewCalculatedType(tf1()); } }; template class Field, class TypeR> -class reuseTmpTmpFieldField +struct reuseTmpTmpFieldField { -public: - static tmp> New ( const tmp>& tf1, @@ -194,10 +161,7 @@ public: return tf2; } - return tmp> - ( - FieldField::NewCalculatedType(tf1()) - ); + return FieldField::NewCalculatedType(tf1()); } }; diff --git a/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H b/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H index 3818628c3e..686acf59cf 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.H +++ b/src/OpenFOAM/fields/Fields/Field/FieldReuseFunctions.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) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -33,79 +33,70 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -template -tmp> New -( - const tmp>& tf1, - const bool initRet = false -) -{ - if (tf1.isTmp()) - { - return tf1; - } - - tmp> rtf(new Field(tf1().size())); - - if (initRet) - { - rtf.ref() = tf1(); - } - - return rtf; -} - - template -class reuseTmp +struct reuseTmp { -public: - static tmp> New(const tmp>& tf1) { - return tmp>(new Field(tf1().size())); + return tmp>::New(tf1().size()); } }; template -class reuseTmp +struct reuseTmp { -public: - - static tmp> New(const tmp>& tf1) + static tmp> New + ( + const tmp>& tf1, + const bool initCopy = false + ) { if (tf1.isTmp()) { return tf1; } - return tmp>(new Field(tf1().size())); + auto rtf = tmp>::New(tf1().size()); + + if (initCopy) + { + rtf.ref() = tf1(); + } + + return rtf; } }; -template -class reuseTmpTmp +//- This global function forwards to reuseTmp::New +template tmp> New +( + const tmp>& tf1, + const bool initCopy = false +) { -public: + return reuseTmp::New(tf1, initCopy); +} + +template +struct reuseTmpTmp +{ static tmp> New ( const tmp>& tf1, const tmp>& tf2 ) { - return tmp>(new Field(tf1().size())); + return tmp>::New(tf1().size()); } }; template -class reuseTmpTmp +struct reuseTmpTmp { -public: - static tmp> New ( const tmp>& tf1, @@ -117,16 +108,14 @@ public: return tf2; } - return tmp>(new Field(tf1().size())); + return tmp>::New(tf1().size()); } }; template -class reuseTmpTmp +struct reuseTmpTmp { -public: - static tmp> New ( const tmp>& tf1, @@ -138,16 +127,14 @@ public: return tf1; } - return tmp>(new Field(tf1().size())); + return tmp>::New(tf1().size()); } }; template -class reuseTmpTmp +struct reuseTmpTmp { -public: - static tmp> New ( const tmp>& tf1, @@ -163,7 +150,7 @@ public: return tf2; } - return tmp>(new Field(tf1().size())); + return tmp>::New(tf1().size()); } }; diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldReuseFunctions.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldReuseFunctions.H index df17b18468..0ee99690bd 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 | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -33,6 +33,11 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +//- Test if a tmp GeometricField is reusable, which means that it has been +//- allocated (not a const-reference). +// +// When GeometricField debug is on, also checks that the patches have +// reusable types. template < class Type, @@ -45,20 +50,17 @@ bool reusable(const tmp>& tgf) { if (GeometricField::debug) { - const auto& gf = tgf(); - const auto& gbf = gf.boundaryField(); - - forAll(gbf, patchi) + for (const auto& p : tgf().boundaryField()) { if ( - !polyPatch::constraintType(gbf[patchi].patch().type()) - && !isA::Calculated>(gbf[patchi]) + !polyPatch::constraintType(p.patch().type()) + && !isA::Calculated>(p) ) { WarningInFunction << "Attempt to reuse temporary with non-reusable BC " - << gbf[patchi].type() << endl; + << p.type() << endl; return false; } @@ -72,50 +74,6 @@ bool reusable(const tmp>& tgf) } -template class PatchField, class GeoMesh> -tmp> New -( - const tmp>& tgf1, - const word& name, - const dimensionSet& dimensions, - const bool initRet = false -) -{ - if (reusable(tgf1)) - { - auto& gf1 = tgf1.constCast(); - - gf1.rename(name); - gf1.dimensions().reset(dimensions); - return tgf1; - } - - const auto& gf1 = tgf1(); - - tmp> rtgf - ( - new GeometricField - ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) - ); - - if (initRet) - { - rtgf.ref() == gf1; - } - - return rtgf; -} - - template < class TypeR, @@ -123,10 +81,8 @@ template template class PatchField, class GeoMesh > -class reuseTmpGeometricField +struct reuseTmpGeometricField { -public: - static tmp> New ( const tmp>& tgf1, @@ -136,34 +92,32 @@ public: { const auto& gf1 = tgf1(); - return tmp> + return tmp>::New ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions ); } }; template class PatchField, class GeoMesh> -class reuseTmpGeometricField +struct reuseTmpGeometricField { -public: - + //- Since the input and output types are identical, also allow + //- optional copy assignment of the initial content. static tmp> New ( const tmp>& tgf1, const word& name, - const dimensionSet& dimensions + const dimensionSet& dimensions, + const bool initCopy = false ) { if (reusable(tgf1)) @@ -177,24 +131,51 @@ public: const auto& gf1 = tgf1(); - return tmp> + auto rtgf = tmp>::New ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions ); + + if (initCopy) + { + rtgf.ref() == gf1; + } + + return rtgf; } }; +//- This global function forwards to reuseTmpGeometricField::New +template class PatchField, class GeoMesh> +tmp +< + GeometricField +> New +( + const tmp>& tgf1, + const word& name, + const dimensionSet& dimensions, + const bool initCopy = false +) +{ + return reuseTmpGeometricField::New + ( + tgf1, + name, + dimensions, + initCopy + ); +} + + template < class TypeR, @@ -204,10 +185,8 @@ template template class PatchField, class GeoMesh > -class reuseTmpTmpGeometricField +struct reuseTmpTmpGeometricField { -public: - static tmp> New ( const tmp>& tgf1, @@ -218,19 +197,16 @@ public: { const auto& gf1 = tgf1(); - return tmp> + return tmp>::New ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions ); } }; @@ -244,11 +220,11 @@ template template class PatchField, class GeoMesh > -class reuseTmpTmpGeometricField - +struct reuseTmpTmpGeometricField +< + TypeR, Type1, Type12, TypeR, PatchField, GeoMesh +> { -public: - static tmp> New ( const tmp>& tgf1, @@ -268,19 +244,16 @@ public: const auto& gf1 = tgf1(); - return tmp> + return tmp>::New ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions ); } }; @@ -293,10 +266,11 @@ template template class PatchField, class GeoMesh > -class reuseTmpTmpGeometricField +struct reuseTmpTmpGeometricField +< + TypeR, TypeR, TypeR, Type2, PatchField, GeoMesh +> { -public: - static tmp> New ( const tmp>& tgf1, @@ -316,29 +290,27 @@ public: const auto& gf1 = tgf1(); - return tmp> + return tmp>::New ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions ); } }; template class PatchField, class GeoMesh> -class reuseTmpTmpGeometricField +struct reuseTmpTmpGeometricField +< + TypeR, TypeR, TypeR, TypeR, PatchField, GeoMesh +> { -public: - static tmp> New ( const tmp>& tgf1, @@ -366,24 +338,22 @@ public: const auto& gf1 = tgf1(); - return tmp> + return tmp>::New ( - new GeometricField + IOobject ( - IOobject - ( - name, - gf1.instance(), - gf1.db() - ), - gf1.mesh(), - dimensions - ) + name, + gf1.instance(), + gf1.db() + ), + gf1.mesh(), + dimensions ); } }; + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam