ENH: treat self-assignment as no-op instead of a Fatal (#1473)

- this can help if using std algorithms that return a const reference
  such as std::min() does.
This commit is contained in:
Mark Olesen 2019-11-05 11:10:49 +01:00 committed by Andrew Heather
parent 883752cfb9
commit b0c88dff58
65 changed files with 391 additions and 210 deletions

View File

@ -89,19 +89,16 @@ Foam::CLASSNAME::~CLASSNAME()
void Foam::CLASSNAME::operator=(const CLASSNAME& rhs)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
}
// * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
// ************************************************************************* //

View File

@ -102,12 +102,9 @@ void Foam::CLASSNAME<TemplateArgument>::operator=
const CLASSNAME<TemplateArgument>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
}

View File

@ -126,7 +126,11 @@ inline bool Foam::regExpPosix::search(const std::string& text) const
inline void Foam::regExpPosix::swap(regExpPosix& rgx)
{
std::swap(preg_, rgx.preg_);
if (this != &rgx)
{
// Self-swap is a no-op
std::swap(preg_, rgx.preg_);
}
}

View File

@ -320,6 +320,7 @@ inline void Foam::PackedList<Width>::reference::operator=
const reference& other
)
{
// Accepts self-assignment
this->set(other.get());
}
@ -556,6 +557,11 @@ inline std::streamsize Foam::PackedList<Width>::byteSize() const
template<unsigned Width>
inline void Foam::PackedList<Width>::swap(PackedList<Width>& rhs)
{
if (this == &rhs)
{
return; // Self-swap is a no-op
}
blocks_.swap(rhs.blocks_);
Foam::Swap(size_, rhs.size_);
}
@ -564,8 +570,12 @@ inline void Foam::PackedList<Width>::swap(PackedList<Width>& rhs)
template<unsigned Width>
inline void Foam::PackedList<Width>::transfer(PackedList<Width>& rhs)
{
blocks_.transfer(rhs.blocks_);
if (this == &rhs)
{
return; // Self-assignment is a no-op
}
blocks_.transfer(rhs.blocks_);
size_ = rhs.size_;
rhs.size_ = 0;
}

View File

@ -197,6 +197,7 @@ inline void Foam::bitSet::reference::operator=
const reference& other
)
{
// Accepts self-assignment
set(other.get());
}

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2015 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -170,12 +171,9 @@ void Foam::Circulator<ContainerType>::operator=
const Circulator<ContainerType>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
begin_ = rhs.begin_;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2015 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -175,12 +176,9 @@ void Foam::ConstCirculator<ContainerType>::operator=
const ConstCirculator<ContainerType>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
begin_ = rhs.begin_;

View File

@ -232,6 +232,11 @@ void Foam::DictionaryBase<IDLListType, T>::transfer
DictionaryBase<IDLListType, T>& dict
)
{
if (this == &dict)
{
return; // Self-assignment is a no-op
}
IDLListType::transfer(dict);
hashedTs_.transfer(dict.hashedTs_);
}
@ -247,9 +252,7 @@ void Foam::DictionaryBase<IDLListType, T>::operator=
{
if (this == &dict)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
IDLListType::operator=(dict);

View File

@ -149,9 +149,7 @@ void Foam::HashPtrTable<T, Key, Hash>::operator=
{
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted copy assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
this->clear();
@ -179,9 +177,7 @@ void Foam::HashPtrTable<T, Key, Hash>::operator=
{
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted move assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
this->clear();

View File

@ -656,6 +656,11 @@ void Foam::HashTable<T, Key, Hash>::clearStorage()
template<class T, class Key, class Hash>
void Foam::HashTable<T, Key, Hash>::swap(HashTable<T, Key, Hash>& rhs)
{
if (this == &rhs)
{
return; // Self-swap is a no-op
}
Foam::Swap(size_, rhs.size_);
Foam::Swap(capacity_, rhs.capacity_);
Foam::Swap(table_, rhs.table_);
@ -665,6 +670,11 @@ void Foam::HashTable<T, Key, Hash>::swap(HashTable<T, Key, Hash>& rhs)
template<class T, class Key, class Hash>
void Foam::HashTable<T, Key, Hash>::transfer(HashTable<T, Key, Hash>& rhs)
{
if (this == &rhs)
{
return; // Self-assignment is a no-op
}
clear();
swap(rhs);
}
@ -759,12 +769,9 @@ void Foam::HashTable<T, Key, Hash>::operator=
const HashTable<T, Key, Hash>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (!capacity_)
@ -813,12 +820,9 @@ void Foam::HashTable<T, Key, Hash>::operator=
HashTable<T, Key, Hash>&& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
transfer(rhs);

View File

@ -185,6 +185,11 @@ inline void Foam::DLListBase::clear()
inline void Foam::DLListBase::swap(DLListBase& lst)
{
if (this == &lst)
{
return; // Self-swap is a no-op
}
std::swap(first_, lst.first_);
std::swap(last_, lst.last_);
std::swap(size_, lst.size_);
@ -193,6 +198,11 @@ inline void Foam::DLListBase::swap(DLListBase& lst)
inline void Foam::DLListBase::transfer(DLListBase& lst)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
first_ = lst.first_;
last_ = lst.last_;
size_ = lst.size_;

View File

@ -143,6 +143,11 @@ inline void Foam::SLListBase::clear()
inline void Foam::SLListBase::swap(SLListBase& lst)
{
if (this == &lst)
{
return; // Self-swap is a no-op
}
std::swap(last_, lst.last_);
std::swap(size_, lst.size_);
}
@ -150,6 +155,11 @@ inline void Foam::SLListBase::swap(SLListBase& lst)
inline void Foam::SLListBase::transfer(SLListBase& lst)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
last_ = lst.last_;
size_ = lst.size_;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -203,6 +204,11 @@ void Foam::CompactListList<T, Container>::swap
CompactListList<T, Container>& lst
)
{
if (this == &lst)
{
return; // Self-swap is a no-op
}
Foam::Swap(size_, lst.size_);
offsets_.swap(lst.offsets_);
m_.swap(lst.m_);
@ -215,6 +221,11 @@ void Foam::CompactListList<T, Container>::transfer
CompactListList<T, Container>& lst
)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
size_ = lst.size_;
offsets_.transfer(lst.offsets_);
m_.transfer(lst.m_);

View File

@ -303,6 +303,11 @@ inline void Foam::CompactListList<T, Container>::operator=
const CompactListList<T, Container>& lst
)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
size_ = lst.size_;
offsets_ = lst.offsets_,
m_ = lst.m_;
@ -315,6 +320,11 @@ inline void Foam::CompactListList<T, Container>::operator=
CompactListList<T, Container>&& lst
)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
transfer(lst);
}

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -557,12 +558,9 @@ void Foam::Distribution<Type>::operator=
const Distribution<Type>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
List<List<scalar>>::operator=(rhs);

View File

@ -397,6 +397,11 @@ inline void Foam::DynamicList<T, SizeMin>::swap
DynamicList<T, AnySizeMin>& lst
)
{
if (this == &lst)
{
return; // Self-swap is a no-op
}
DynamicList<T, SizeMin>& cur = *this;
// Make addressable size identical to the allocated capacity
@ -434,6 +439,11 @@ Foam::DynamicList<T, SizeMin>::transfer
DynamicList<T, AnySizeMin>& lst
)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
// Take over storage as-is (without shrink, without using SizeMin)
// clear addressing and storage for old lst.
capacity_ = lst.capacity();
@ -793,8 +803,7 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
{
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted assignment to self" << abort(FatalError);
return; // Self-assignment is a no-op
}
assignDynList(lst);
@ -808,6 +817,11 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
const DynamicList<T, SizeMin2>& lst
)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
assignDynList(lst);
}
@ -852,8 +866,7 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
{
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted assignment to self" << abort(FatalError);
return; // Self-assignment is a no-op
}
clear();
@ -868,6 +881,11 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
DynamicList<T, SizeMin2>&& lst
)
{
if (this == &lst)
{
return; // Self-assignment is a no-op
}
clear();
transfer(lst);
}
@ -887,7 +905,11 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T, int SizeMin1, int SizeMin2>
inline void Foam::Swap(DynamicList<T, SizeMin1>& a, DynamicList<T, SizeMin2>& b)
inline void Foam::Swap
(
DynamicList<T, SizeMin1>& a,
DynamicList<T, SizeMin2>& b
)
{
a.swap(b);
}

View File

@ -331,6 +331,11 @@ inline void Foam::FixedList<T, N>::setSize(const label n)
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& list)
{
if (this == &list)
{
return; // Self-swap is a no-op
}
for (unsigned i=0; i<N; ++i)
{
Foam::Swap(v_[i], list.v_[i]);
@ -341,6 +346,11 @@ inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& list)
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::transfer(FixedList<T, N>& list)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
for (unsigned i=0; i<N; ++i)
{
v_[i] = std::move(list[i]);
@ -428,6 +438,11 @@ inline void Foam::FixedList<T, N>::operator=(const T& val)
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
for (unsigned i=0; i<N; ++i)
{
v_[i] = list.v_[i];
@ -437,6 +452,11 @@ inline void Foam::FixedList<T, N>::operator=(const FixedList<T, N>& list)
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::operator=(FixedList<T, N>&& list)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
// No significant speedup observed for copy assignment on simple types,
// use move assignment for generality with more complex types
for (unsigned i=0; i<N; ++i)

View File

@ -435,6 +435,11 @@ void Foam::List<T>::resize(const label newSize, const T& val)
template<class T>
void Foam::List<T>::transfer(List<T>& list)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
// Clear and swap - could also check for self assignment
clear();
this->size_ = list.size_;
@ -472,6 +477,11 @@ void Foam::List<T>::transfer(SortableList<T>& list)
template<class T>
void Foam::List<T>::operator=(const UList<T>& a)
{
if (this == &a)
{
return; // Self-assignment is a no-op
}
reAlloc(a.size_);
const label len = this->size_;
@ -505,9 +515,7 @@ void Foam::List<T>::operator=(const List<T>& list)
{
if (this == &list)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
operator=(static_cast<const UList<T>&>(list));
@ -581,9 +589,7 @@ void Foam::List<T>::operator=(List<T>&& list)
{
if (this == &list)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
transfer(list);

View File

@ -193,7 +193,7 @@ inline void Foam::List<T>::append(const UList<T>& list)
if (this == &list)
{
FatalErrorInFunction
<< "attempted appending to self" << abort(FatalError);
<< "Attempted appending to self" << abort(FatalError);
}
label idx = this->size();

View File

@ -197,6 +197,11 @@ void Foam::SortableList<T>::partialReverseSort(label n, label start)
template<class T>
void Foam::SortableList<T>::swap(SortableList<T>& lst)
{
if (this == &lst)
{
return; // Self-swap is a no-op
}
List<T>::swap(lst);
indices_.swap(lst.indices_);
}
@ -223,6 +228,11 @@ inline void Foam::SortableList<T>::operator=(const UList<T>& lst)
template<class T>
inline void Foam::SortableList<T>::operator=(const SortableList<T>& lst)
{
if (this == &lst)
{
return; // Self-assigment is a no-op
}
List<T>::operator=(lst);
indices_ = lst.indices();
}
@ -239,6 +249,11 @@ inline void Foam::SortableList<T>::operator=(List<T>&& lst)
template<class T>
inline void Foam::SortableList<T>::operator=(SortableList<T>&& lst)
{
if (this == &lst)
{
return; // Self-assigment is a no-op
}
clear();
this->swap(lst);
}

View File

@ -380,6 +380,11 @@ inline bool Foam::UList<T>::empty() const noexcept
template<class T>
inline void Foam::UList<T>::swap(UList<T>& list)
{
if (&list == this)
{
return; // Self-swap is a no-op
}
Foam::Swap(size_, list.size_);
Foam::Swap(v_, list.v_);
}

View File

@ -369,6 +369,11 @@ inline void Foam::PtrDynList<T, SizeMin>::operator=
const PtrList<T>& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
PtrList<T>::operator=(list);
capacity_ = PtrList<T>::size();
}
@ -380,6 +385,11 @@ inline void Foam::PtrDynList<T, SizeMin>::operator=
const PtrDynList<T, SizeMin>& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
PtrList<T>::operator=(list);
capacity_ = PtrList<T>::size();
}
@ -392,6 +402,11 @@ inline void Foam::PtrDynList<T, SizeMin>::operator=
const PtrDynList<T, AnySizeMin>& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
PtrList<T>::operator=(list);
capacity_ = PtrList<T>::size();
}
@ -403,6 +418,11 @@ inline void Foam::PtrDynList<T, SizeMin>::operator=
PtrList<T>&& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
PtrList<T>::transfer(list);
capacity_ = PtrList<T>::size();
list.clearStorage();
@ -415,6 +435,11 @@ inline void Foam::PtrDynList<T, SizeMin>::operator=
PtrDynList<T, SizeMin>&& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
PtrList<T>::transfer(list);
capacity_ = list.capacity();
list.clearStorage();
@ -428,6 +453,11 @@ inline void Foam::PtrDynList<T, SizeMin>::operator=
PtrDynList<T, AnySizeMin>&& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
PtrList<T>::transfer(list);
capacity_ = list.capacity();
list.clearStorage();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -134,9 +134,7 @@ void Foam::PtrList<T>::operator=(const PtrList<T>& list)
{
if (this == &list)
{
FatalErrorInFunction
<< "attempted assignment to self for type " << typeid(T).name()
<< abort(FatalError);
return; // Self-assignment is a no-op
}
const label oldLen = this->size();

View File

@ -203,7 +203,7 @@ inline const T& Foam::UPtrList<T>::operator[](const label i) const
{
FatalErrorInFunction
<< "Cannot dereference nullptr at index " << i
<< " in range [0," << size() << ")"
<< " in range [0," << size() << ")\n"
<< abort(FatalError);
}
@ -220,7 +220,7 @@ inline T& Foam::UPtrList<T>::operator[](const label i)
{
FatalErrorInFunction
<< "Cannot dereference nullptr at index " << i
<< " in range [0," << size() << ")"
<< " in range [0," << size() << ")\n"
<< abort(FatalError);
}

View File

@ -215,6 +215,11 @@ void Foam::CompactIOField<T, BaseType>::operator=
const CompactIOField<T, BaseType>& rhs
)
{
if (this == &rhs)
{
return; // Self-assigment is a no-op
}
Field<T>::operator=(rhs);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2018 OpenCFD Ltd.
Copyright (C) 2015-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.

View File

@ -281,6 +281,11 @@ inline void Foam::token::reset()
inline void Foam::token::swap(token& tok)
{
if (this == &tok)
{
return; // Self-swap is a no-op
}
std::swap(data_, tok.data_);
std::swap(type_, tok.type_);
std::swap(lineNumber_, tok.lineNumber_);
@ -642,6 +647,11 @@ inline void Foam::token::setBad()
inline void Foam::token::operator=(const token& tok)
{
if (this == &tok)
{
return; // Self-assignment is a no-op
}
reset();
type_ = tok.type_;
@ -683,6 +693,11 @@ inline void Foam::token::operator=(const token& tok)
inline void Foam::token::operator=(token&& tok)
{
if (this == &tok)
{
return; // Self-assignment is a no-op
}
reset();
lineNumber_ = 0;
swap(tok);

View File

@ -876,7 +876,7 @@ bool Foam::dictionary::merge(const dictionary& dict)
if (this == &dict)
{
FatalIOErrorInFunction(*this)
<< "Attempted merge to self for dictionary "
<< "Attempted merge to self, for dictionary "
<< name() << nl
<< abort(FatalIOError);
}
@ -944,10 +944,7 @@ void Foam::dictionary::operator=(const dictionary& rhs)
{
if (this == &rhs)
{
FatalIOErrorInFunction(*this)
<< "Attempted assignment to self for dictionary "
<< name() << nl
<< abort(FatalIOError);
return; // Self-assignment is a no-op
}
name() = rhs.name();
@ -968,7 +965,7 @@ void Foam::dictionary::operator+=(const dictionary& rhs)
if (this == &rhs)
{
FatalIOErrorInFunction(*this)
<< "Attempted addition assignment to self for dictionary "
<< "Attempted addition to self, for dictionary "
<< name() << nl
<< abort(FatalIOError);
}
@ -985,7 +982,7 @@ void Foam::dictionary::operator|=(const dictionary& rhs)
if (this == &rhs)
{
FatalIOErrorInFunction(*this)
<< "Attempted assignment to self for dictionary "
<< "Attempted |= merging to self, for dictionary "
<< name() << nl
<< abort(FatalIOError);
}
@ -1005,7 +1002,7 @@ void Foam::dictionary::operator<<=(const dictionary& rhs)
if (this == &rhs)
{
FatalIOErrorInFunction(*this)
<< "Attempted assignment to self for dictionary "
<< "Attempted addition to self, for dictionary "
<< name() << nl
<< abort(FatalIOError);
}

View File

@ -179,12 +179,9 @@ void Foam::entry::checkITstream(const ITstream& is) const
void Foam::entry::operator=(const entry& e)
{
// check for assignment to self
if (this == &e)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
keyword_ = e.keyword_;
@ -193,20 +190,24 @@ void Foam::entry::operator=(const entry& e)
bool Foam::entry::operator==(const entry& e) const
{
if (this == &e)
{
return true;
}
if (keyword_ != e.keyword_)
{
return false;
}
else
{
OStringStream oss1;
oss1 << *this;
OStringStream oss2;
oss2 << e;
// Compare contents (as strings)
return oss1.str() == oss2.str();
}
OStringStream oss1;
oss1 << *this;
OStringStream oss2;
oss2 << e;
return oss1.str() == oss2.str();
}

View File

@ -259,7 +259,7 @@ bool Foam::objectRegistry::checkIn(regIOobject* io) const
if (!ok && objectRegistry::debug)
{
WarningInFunction
<< name() << " : attempted to checkIn object with name "
<< name() << " : Attempt to checkIn object with name "
<< io->name() << " which was already checked in"
<< endl;
}
@ -291,7 +291,7 @@ bool Foam::objectRegistry::checkOut(regIOobject* io) const
if (objectRegistry::debug)
{
WarningInFunction
<< name() << " : attempt to checkOut copy of "
<< name() << " : Attempt to checkOut copy of "
<< iter.key()
<< endl;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2017 OpenCFD Ltd.
Copyright (C) 2015-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -492,12 +492,9 @@ void Foam::DimensionedField<Type, GeoMesh>::operator=
const DimensionedField<Type, GeoMesh>& df
)
{
// Check for assignment to self
if (this == &df)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
checkField(*this, df, "=");
@ -516,12 +513,9 @@ void Foam::DimensionedField<Type, GeoMesh>::operator=
{
auto& df = tdf.constCast();
// Check for assignment to self
if (this == &df)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
checkField(*this, df, "=");

View File

@ -292,10 +292,9 @@ void FieldField<Field, Type>::operator=(const FieldField<Field, Type>& ff)
{
if (this == &ff)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
// No size checking done
forAll(*this, i)
@ -310,9 +309,7 @@ void FieldField<Field, Type>::operator=(FieldField<Field, Type>&& ff)
{
if (this == &ff)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
PtrList<Field<Type>>::transfer(ff);
@ -325,9 +322,7 @@ void FieldField<Field, Type>::operator=(const tmp<FieldField>& tf)
// The cref() method also checks that tmp is not nullptr.
if (this == &(tf.cref()))
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
PtrList<Field<Type>>::clear();
@ -414,6 +409,6 @@ Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type>>& tf)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "FieldFieldFunctions.C"
#include "FieldFieldFunctions.C"
// ************************************************************************* //

View File

@ -399,6 +399,11 @@ inline void Foam::DynamicField<T, SizeMin>::swap
DynamicField<T, AnySizeMin>& lst
)
{
if (this == &lst)
{
return; // Self-swap is a no-op
}
DynamicList<T, SizeMin>& cur = *this;
// Make addressable size identical to the allocated capacity
@ -450,6 +455,11 @@ inline void Foam::DynamicField<T, SizeMin>::transfer
DynamicField<T, AnySizeMin>& list
)
{
if (this == &list)
{
return; // Self-assignment is a no-op
}
// Take over storage as-is (without shrink, without using SizeMin)
// clear addressing and storage for old list.
capacity_ = list.capacity();
@ -484,7 +494,7 @@ Foam::DynamicField<T, SizeMin>::append
if (this == &list)
{
FatalErrorInFunction
<< "attempted appending to self" << abort(FatalError);
<< "Attempted appending to self" << abort(FatalError);
}
label idx = List<T>::size();
@ -563,8 +573,7 @@ inline void Foam::DynamicField<T, SizeMin>::operator=
{
if (this == &list)
{
FatalErrorInFunction
<< "Attempted assignment to self" << abort(FatalError);
return; // Self-assignment is a no-op
}
assignDynField(list);

View File

@ -656,9 +656,7 @@ void Foam::Field<Type>::operator=(const Field<Type>& rhs)
{
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
List<Type>::operator=(rhs);
@ -670,9 +668,7 @@ void Foam::Field<Type>::operator=(const tmp<Field>& rhs)
{
if (this == &(rhs()))
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
List<Type>::operator=(rhs());

View File

@ -1327,9 +1327,7 @@ void Foam::GeometricField<Type, PatchField, GeoMesh>::operator=
{
if (this == &gf)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
checkField(*this, gf, "=");
@ -1351,9 +1349,7 @@ void Foam::GeometricField<Type, PatchField, GeoMesh>::operator=
if (this == &gf)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
checkField(*this, gf, "=");

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -170,9 +171,7 @@ void Foam::LduMatrix<Type, DType, LUType>::operator=(const LduMatrix& A)
{
if (this == &A)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (A.diagPtr_)

View File

@ -289,6 +289,11 @@ Foam::List<Type> Foam::Matrix<Form, Type>::release()
template<class Form, class Type>
void Foam::Matrix<Form, Type>::swap(Matrix<Form, Type>& mat)
{
if (this == &mat)
{
return; // Self-swap is a no-op
}
Foam::Swap(mRows_, mat.mRows_);
Foam::Swap(nCols_, mat.nCols_);
Foam::Swap(v_, mat.v_);
@ -298,6 +303,11 @@ void Foam::Matrix<Form, Type>::swap(Matrix<Form, Type>& mat)
template<class Form, class Type>
void Foam::Matrix<Form, Type>::transfer(Matrix<Form, Type>& mat)
{
if (this == &mat)
{
return; // Self-assignment is a no-op
}
clear();
mRows_ = mat.mRows_;
@ -457,9 +467,7 @@ void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& mat)
{
if (this == &mat)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (mRows_ != mat.mRows_ || nCols_ != mat.nCols_)
@ -480,14 +488,11 @@ void Foam::Matrix<Form, Type>::operator=(const Matrix<Form, Type>& mat)
template<class Form, class Type>
void Foam::Matrix<Form, Type>::operator=(Matrix<Form, Type>&& mat)
{
if (this == &mat)
if (this != &mat)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
// Self-assignment is a no-op
this->transfer(mat);
}
this->transfer(mat);
}

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -91,10 +92,7 @@ void Foam::lduMatrix::operator=(const lduMatrix& A)
{
if (this == &A)
{
FatalError
<< "lduMatrix::operator=(const lduMatrix&) : "
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (A.lowerPtr_)

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -103,9 +104,7 @@ void Foam::simpleMatrix<Type>::operator=(const simpleMatrix<Type>& m)
{
if (this == &m)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (m() != m.m())

View File

@ -172,6 +172,7 @@ inline void Foam::autoPtr<T>::reset(autoPtr<T>&& ap) noexcept
template<class T>
inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
{
// Self-swap is effectively ignored
T* p = ptr_;
ptr_ = other.ptr_;
other.ptr_ = p;
@ -259,6 +260,7 @@ inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
{
if (this != &ap)
{
// Ignore self-assignment
reset(ap.release());
}
}
@ -270,6 +272,7 @@ inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
{
if (this != &ap)
{
// Ignore self-assignment
reset(ap.release());
}
}

View File

@ -369,6 +369,11 @@ inline void Foam::tmp<T>::cref(const T& obj) noexcept
template<class T>
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
{
if (&other == this)
{
return; // Self-swap is a no-op
}
// Copy/assign for pointer types
T* p = ptr_;
ptr_ = other.ptr_;
@ -460,6 +465,11 @@ inline void Foam::tmp<T>::operator=(T* p)
template<class T>
inline void Foam::tmp<T>::operator=(const tmp<T>& t)
{
if (&t == this)
{
return; // Self-assignment is a no-op
}
clear();
if (t.isTmp())

View File

@ -319,6 +319,11 @@ inline void Foam::tmpNrc<T>::cref(const T& obj) noexcept
template<class T>
inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept
{
if (&other == this)
{
return; // Self-swap is a no-op
}
// Copy/assign for pointer types
T* p = ptr_;
ptr_ = other.ptr_;
@ -403,6 +408,11 @@ inline void Foam::tmpNrc<T>::operator=(T* p)
template<class T>
inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>& t)
{
if (&t == this)
{
return; // Self-assignment is a no-op
}
clear();
if (t.isTmp())

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2018 OpenCFD Ltd.
Copyright (C) 2015-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -516,6 +516,11 @@ Foam::label Foam::mapDistribute::whichTransform(const label index) const
void Foam::mapDistribute::transfer(mapDistribute& rhs)
{
if (this == &rhs)
{
// Self-assignment is a no-op
}
mapDistributeBase::transfer(rhs);
transformElements_.transfer(rhs.transformElements_);
transformStart_.transfer(rhs.transformStart_);
@ -528,11 +533,9 @@ void Foam::mapDistribute::operator=(const mapDistribute& rhs)
{
if (this == &rhs)
{
// Avoid self-assignment
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
mapDistributeBase::operator=(rhs);
transformElements_ = rhs.transformElements_;
transformStart_ = rhs.transformStart_;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2015-2016, OpenCFD Ltd.
Copyright (C) 2015-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -839,6 +839,12 @@ Foam::mapDistributeBase::mapDistributeBase(Istream& is)
void Foam::mapDistributeBase::transfer(mapDistributeBase& rhs)
{
if (this == &rhs)
{
// Self-assignment is a no-op
return;
}
constructSize_ = rhs.constructSize_;
subMap_.transfer(rhs.subMap_);
constructMap_.transfer(rhs.constructMap_);
@ -1264,11 +1270,9 @@ void Foam::mapDistributeBase::operator=(const mapDistributeBase& rhs)
{
if (this == &rhs)
{
// Avoid self assignment
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
constructSize_ = rhs.constructSize_;
subMap_ = rhs.subMap_;
constructMap_ = rhs.constructMap_;

View File

@ -170,10 +170,9 @@ inline void Foam::keyType::clear()
inline void Foam::keyType::swap(keyType& s)
{
// Self-swapping is a no-op
if (this == &s)
{
return;
return; // Self-swap is a no-op
}
word::swap(static_cast<word&>(s));
@ -191,10 +190,9 @@ inline bool Foam::keyType::operator()(const std::string& text) const
inline void Foam::keyType::operator=(const keyType& s)
{
// Self-assignment is a no-op
if (this == &s)
{
return;
return; // Self-assignment is a no-op
}
assign(s); // Bypasses char checking
@ -204,10 +202,9 @@ inline void Foam::keyType::operator=(const keyType& s)
inline void Foam::keyType::operator=(keyType&& s)
{
// Self-assignment is a no-op
if (this == &s)
{
return;
return; // Self-assignment is a no-op
}
clear();

View File

@ -146,6 +146,11 @@ inline bool Foam::hashedWordList::found(const word& name) const
inline void Foam::hashedWordList::swap(hashedWordList& list)
{
if (this == &list)
{
return; // Self-swap is a no-op
}
wordList::swap(static_cast<wordList&>(list));
lookup_.swap(list.lookup_);
}

View File

@ -154,8 +154,12 @@ inline bool Foam::regExpCxx::clear()
inline void Foam::regExpCxx::swap(regExpCxx& rgx)
{
re_.swap(rgx.re_);
std::swap(ok_, rgx.ok_);
if (this != &rgx)
{
// Self-swap is a no-op
re_.swap(rgx.re_);
std::swap(ok_, rgx.ok_);
}
}

View File

@ -265,9 +265,9 @@ inline bool Foam::string::match(const std::string& text) const
inline void Foam::string::swap(std::string& str)
{
// Self-swapping is a no-op
if (this != &str)
{
// Self-swap is a no-op
std::string::swap(str);
}
}

View File

@ -259,10 +259,9 @@ inline void Foam::wordRe::set(const char* str, const compOption opt)
inline void Foam::wordRe::swap(wordRe& str)
{
// Self-swapping is a no-op
if (this == &str)
{
return;
return; // Self-swap is a no-op
}
word::swap(static_cast<word&>(str));
@ -280,10 +279,9 @@ inline bool Foam::wordRe::operator()(const std::string& text) const
inline void Foam::wordRe::operator=(const wordRe& str)
{
// Self-assignment is a no-op
if (this == &str)
{
return;
return; // Self-assignment is a no-op
}
assign(str);
@ -342,10 +340,9 @@ inline void Foam::wordRe::operator=(const char* str)
inline void Foam::wordRe::operator=(wordRe&& str)
{
// Self-assignment is a no-op
if (this == &str)
{
return;
return; // Self-assignment is a no-op
}
clear();

View File

@ -147,12 +147,9 @@ void Foam::refinementHistory::splitCell8::operator=(const splitCell8& s)
{
// Assignment operator since autoPtr otherwise 'steals' storage.
// Check for assignment to self
if (this == &s)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
parent_ = s.parent_;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -712,9 +713,7 @@ void Foam::faMatrix<Type>::operator=(const faMatrix<Type>& famv)
{
if (this == &famv)
{
FatalErrorInFunction
<< "attempted to assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (&psi_ != &(famv.psi_))

View File

@ -159,9 +159,7 @@ void convectionScheme<Type>::operator=(const convectionScheme<Type>& cs)
{
if (this == &cs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016 OpenCFD Ltd.
Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -1031,9 +1031,7 @@ void Foam::fvMatrix<Type>::operator=(const fvMatrix<Type>& fvmv)
{
if (this == &fvmv)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (&psi_ != &(fvmv.psi_))

View File

@ -228,12 +228,9 @@ void Foam::functionObjects::fieldAverageItem::operator=
const fieldAverageItem& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self" << nl
<< abort(FatalError);
return; // Self-assignment is a no-op
}
// Set updated values

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -393,12 +394,9 @@ void Foam::CollisionRecordList<PairType, WallType>::operator=
const CollisionRecordList<PairType, WallType>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
pairRecords_ = rhs.pairRecords_;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -87,12 +88,9 @@ void Foam::PairCollisionRecord<Type>::operator=
const PairCollisionRecord<Type>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
origProcOfOther_ = rhs.origProcOfOther_;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -85,12 +86,9 @@ void Foam::WallCollisionRecord<Type>::operator=
const WallCollisionRecord<Type>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
accessed_ = rhs.accessed_;

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -211,12 +212,9 @@ void Foam::bufferedAccumulator<Type>::operator=
const bufferedAccumulator<Type>& rhs
)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
List<Field<Type>>::operator=(rhs);
@ -229,6 +227,6 @@ void Foam::bufferedAccumulator<Type>::operator=
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "bufferedAccumulatorIO.C"
#include "bufferedAccumulatorIO.C"
// ************************************************************************* //

View File

@ -432,12 +432,9 @@ Foam::List<Foam::Pair<Foam::scalar>> Foam::distribution::raw()
void Foam::distribution::operator=(const distribution& rhs)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
Map<label>::operator=(rhs);

View File

@ -1805,12 +1805,9 @@ void Foam::surfaceFeatures::nearestFeatEdge
void Foam::surfaceFeatures::operator=(const surfaceFeatures& rhs)
{
// Check for assignment to self
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
if (&surf_ != &rhs.surface())

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -150,9 +150,7 @@ void Foam::cuttingPlane::operator=(const cuttingPlane& rhs)
{
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
static_cast<MeshStorage&>(*this) = rhs;

View File

@ -98,9 +98,7 @@ void Foam::cuttingSurfaceBase::operator=(const cuttingSurfaceBase& rhs)
{
if (this == &rhs)
{
FatalErrorInFunction
<< "Attempted assignment to self"
<< abort(FatalError);
return; // Self-assignment is a no-op
}
static_cast<MeshStorage&>(*this) = rhs;

View File

@ -1109,6 +1109,11 @@ void Foam::MeshedSurface<Face>::swap
MeshedSurface<Face>& surf
)
{
if (this == &surf)
{
return; // Self-swap is a no-op
}
ParentType::clearOut(); // Topology changes
surf.clearOut(); // Topology changes
@ -1139,6 +1144,11 @@ void Foam::MeshedSurface<Face>::transfer
MeshedSurface<Face>& surf
)
{
if (this == &surf)
{
return; // Self-assigment is a no-op
}
ParentType::clearOut(); // Topology changes
this->storedPoints().transfer(surf.storedPoints());

View File

@ -603,6 +603,11 @@ void Foam::UnsortedMeshedSurface<Face>::swap
UnsortedMeshedSurface<Face>& surf
)
{
if (this == &surf)
{
return; // Self-swap is a no-op
}
this->clearOut(); // Topology changes
surf.clearOut(); // Topology changes
@ -622,6 +627,11 @@ void Foam::UnsortedMeshedSurface<Face>::transfer
UnsortedMeshedSurface<Face>& surf
)
{
if (this == &surf)
{
return; // Self-assignment is a no-op
}
this->clear();
this->storedPoints().transfer(surf.storedPoints());

View File

@ -549,6 +549,11 @@ void Foam::triSurface::clearOut()
void Foam::triSurface::swap(triSurface& surf)
{
if (this == &surf)
{
return; // Self-swap is a no-op
}
clearOut();
surf.clearOut();