From 1905039d7bbe66d6dc77cc71c9a66b874fa593f2 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 26 Nov 2018 12:35:01 +0100 Subject: [PATCH] ENH: add non-const field access without triggering update counter (#1081) - can now things like ref(), boundaryFieldRef(), primitiveFieldRef() with an optional argument that avoids triggering any update events Instead of Field& iF = const_cast&>(fld.primitiveField()); can now write Field& iF = fld.primitiveFieldRef(false); or simply auto& iF = fld.primitiveFieldRef(false); --- .../GeometricField/GeometricField.C | 44 +++++++++++++------ .../GeometricField/GeometricField.H | 33 +++++++++----- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C index df75d2ac64..f6ec16baf7 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -719,10 +719,16 @@ Foam::GeometricField::~GeometricField() template class PatchField, class GeoMesh> typename Foam::GeometricField::Internal& -Foam::GeometricField::ref() +Foam::GeometricField::ref +( + const bool updateAccessTime +) { - this->setUpToDate(); - storeOldTimes(); + if (updateAccessTime) + { + this->setUpToDate(); + storeOldTimes(); + } return *this; } @@ -730,10 +736,16 @@ Foam::GeometricField::ref() template class PatchField, class GeoMesh> typename Foam::GeometricField::Internal::FieldType& -Foam::GeometricField::primitiveFieldRef() +Foam::GeometricField::primitiveFieldRef +( + const bool updateAccessTime +) { - this->setUpToDate(); - storeOldTimes(); + if (updateAccessTime) + { + this->setUpToDate(); + storeOldTimes(); + } return *this; } @@ -741,10 +753,16 @@ Foam::GeometricField::primitiveFieldRef() template class PatchField, class GeoMesh> typename Foam::GeometricField::Boundary& -Foam::GeometricField::boundaryFieldRef() +Foam::GeometricField::boundaryFieldRef +( + const bool updateAccessTime +) { - this->setUpToDate(); - storeOldTimes(); + if (updateAccessTime) + { + this->setUpToDate(); + storeOldTimes(); + } return boundaryField_; } @@ -796,10 +814,8 @@ Foam::label Foam::GeometricField::nOldTimes() const { return field0Ptr_->nOldTimes() + 1; } - else - { - return 0; - } + + return 0; } diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H index d9ac7efa0e..8174985f75 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.H @@ -266,7 +266,7 @@ private: // Private Member Functions //- Read the field from the dictionary - void readFields(const dictionary&); + void readFields(const dictionary& dict); //- Read the field - create the field dictionary on-the-fly void readFields(); @@ -475,30 +475,39 @@ public: // Member Functions //- Return a reference to the dimensioned internal field - // Note: this increments the event counter and checks the - // old-time fields; avoid in loops. - Internal& ref(); + // \param updateAccessTime update event counter and check + // old-time fields + // + // \note Should avoid using updateAccessTime = true within loops. + Internal& ref(const bool updateAccessTime = true); //- Return a const-reference to the dimensioned internal field inline const Internal& internalField() const; //- Return a const-reference to the dimensioned internal field - // of a "vol" field. Useful in the formulation of source-terms - // for FV equations + //- of a "vol" field. + // Useful in the formulation of source-terms for FV equations inline const Internal& v() const; //- Return a reference to the internal field - // Note: this increments the event counter and checks the - // old-time fields; avoid in loops. - typename Internal::FieldType& primitiveFieldRef(); + // \param updateAccessTime update event counter and check + // old-time fields + // + // \note Should avoid using updateAccessTime = true within loops. + typename Internal::FieldType& primitiveFieldRef + ( + const bool updateAccessTime = true + ); //- Return a const-reference to the internal field inline const typename Internal::FieldType& primitiveField() const; //- Return a reference to the boundary field - // Note: this increments the event counter and checks the - // old-time fields; avoid in loops. - Boundary& boundaryFieldRef(); + // \param updateAccessTime update event counter and check + // old-time fields + // + // \note Should avoid using updateAccessTime = true within loops. + Boundary& boundaryFieldRef(const bool updateAccessTime = true); //- Return const-reference to the boundary field inline const Boundary& boundaryField() const;