From f1098673c02f4fbd0057eec7915245a76a355305 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 17 May 2022 11:17:14 +0200 Subject: [PATCH] STYLE: compare Dynamic{Field,List} by casting address to common class - previously used cdata() comparison but that tends to obscure what is going on --- .../Lists/DynamicList/DynamicListI.H | 28 ++++++++++----- .../Fields/DynamicField/DynamicFieldI.H | 35 ++++++++++++------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index ee52e49b84..610f7d69d8 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -445,8 +445,11 @@ inline void Foam::DynamicList::swap DynamicList& other ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == other.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&other) + ) { return; // Self-swap is a no-op } @@ -477,8 +480,11 @@ Foam::DynamicList::transfer DynamicList& list ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == list.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&list) + ) { return; // Self-assignment is a no-op } @@ -808,8 +814,11 @@ inline void Foam::DynamicList::operator= const DynamicList& list ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == list.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&list) + ) { return; // Self-assignment is a no-op } @@ -873,8 +882,11 @@ inline void Foam::DynamicList::operator= DynamicList&& list ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == list.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&list) + ) { return; // Self-assignment is a no-op } diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H index bfca3759ca..24fc97d075 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H @@ -486,8 +486,11 @@ inline void Foam::DynamicField::swap DynamicField& other ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == other.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&other) + ) { return; // Self-swap is a no-op } @@ -507,11 +510,11 @@ inline void Foam::DynamicField::swap DynamicList& other ) { - auto& self = (*this); - - // ... not yet needed: - // Cannot compare 'this' for different types, so use cdata() - if (self.cdata() == other.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&other) + ) { return; // Self-swap is a no-op } @@ -520,10 +523,10 @@ inline void Foam::DynamicField::swap UList::swap(other); // Swap capacity - const label oldCap = self.capacity(); + const label oldCap = this->capacity(); const label newCap = other.capacity(); - self.setCapacity_unsafe(newCap); + this->setCapacity_unsafe(newCap); other.setCapacity_unsafe(oldCap); } @@ -544,8 +547,11 @@ inline void Foam::DynamicField::transfer DynamicList& list ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == list.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&list) + ) { return; // Self-assignment is a no-op } @@ -566,8 +572,11 @@ inline void Foam::DynamicField::transfer DynamicField& list ) { - // Cannot compare 'this' for different types, so use cdata() - if (this->cdata() == list.cdata()) + if + ( + static_cast*>(this) + == static_cast*>(&list) + ) { return; // Self-assignment is a no-op }