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<Type>& iF = const_cast<Field<Type>&>(fld.primitiveField());

  can now write

      Field<Type>& iF = fld.primitiveFieldRef(false);

  or simply

      auto& iF = fld.primitiveFieldRef(false);
This commit is contained in:
Mark Olesen
2018-11-26 12:35:01 +01:00
parent 0498d4e743
commit 1905039d7b
2 changed files with 51 additions and 26 deletions

View File

@ -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<Type, PatchField, GeoMesh>::~GeometricField()
template<class Type, template<class> class PatchField, class GeoMesh>
typename
Foam::GeometricField<Type, PatchField, GeoMesh>::Internal&
Foam::GeometricField<Type, PatchField, GeoMesh>::ref()
Foam::GeometricField<Type, PatchField, GeoMesh>::ref
(
const bool updateAccessTime
)
{
this->setUpToDate();
storeOldTimes();
if (updateAccessTime)
{
this->setUpToDate();
storeOldTimes();
}
return *this;
}
@ -730,10 +736,16 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::ref()
template<class Type, template<class> class PatchField, class GeoMesh>
typename
Foam::GeometricField<Type, PatchField, GeoMesh>::Internal::FieldType&
Foam::GeometricField<Type, PatchField, GeoMesh>::primitiveFieldRef()
Foam::GeometricField<Type, PatchField, GeoMesh>::primitiveFieldRef
(
const bool updateAccessTime
)
{
this->setUpToDate();
storeOldTimes();
if (updateAccessTime)
{
this->setUpToDate();
storeOldTimes();
}
return *this;
}
@ -741,10 +753,16 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::primitiveFieldRef()
template<class Type, template<class> class PatchField, class GeoMesh>
typename
Foam::GeometricField<Type, PatchField, GeoMesh>::Boundary&
Foam::GeometricField<Type, PatchField, GeoMesh>::boundaryFieldRef()
Foam::GeometricField<Type, PatchField, GeoMesh>::boundaryFieldRef
(
const bool updateAccessTime
)
{
this->setUpToDate();
storeOldTimes();
if (updateAccessTime)
{
this->setUpToDate();
storeOldTimes();
}
return boundaryField_;
}
@ -796,10 +814,8 @@ Foam::label Foam::GeometricField<Type, PatchField, GeoMesh>::nOldTimes() const
{
return field0Ptr_->nOldTimes() + 1;
}
else
{
return 0;
}
return 0;
}

View File

@ -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;