STYLE: minor code reduction/simplification for tmp (#1775)

- combine reset() methods by adding a default parameter

- improve top-level visibility of empty/valid/get methods for symmetry
  symmetry with autoPtr, future adjustment
This commit is contained in:
Mark Olesen 2020-07-16 09:10:12 +02:00
parent 12c91b9472
commit d282d1a285
5 changed files with 99 additions and 216 deletions

View File

@ -50,7 +50,7 @@ namespace Foam
class refCount class refCount
{ {
// Private data // Private Data
int count_; int count_;
@ -62,7 +62,7 @@ public:
// Constructors // Constructors
//- Construct null initializing count to 0 //- Default construct, initializing count to 0
constexpr refCount() noexcept constexpr refCount() noexcept
: :
count_(0) count_(0)

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -69,20 +69,21 @@ class tmp
enum refType enum refType
{ {
PTR, //!< Managing a pointer (ref-counted) PTR, //!< Managing a pointer (ref-counted)
CREF //!< Using a const-reference to an object CREF //!< Using (const) reference to an object
}; };
//- The managed pointer or the address of const-reference object //- The managed pointer or address of the object (reference)
mutable T* ptr_; mutable T* ptr_;
//- The type (managed pointer | const-reference object) //- The type (managed pointer | object reference)
mutable refType type_; mutable refType type_;
// Private Member Operators // Private Member Operators
//- Increment the ref-count for a managed pointer //- Increment the ref-count for a managed pointer
inline void operator++(); //- and check that it is not oversubscribed
inline void incrCount();
public: public:
@ -122,7 +123,7 @@ public:
// Constructors // Constructors
//- Construct with no managed pointer. //- Default construct, no managed pointer.
inline constexpr tmp() noexcept; inline constexpr tmp() noexcept;
//- Construct with no managed pointer. //- Construct with no managed pointer.
@ -159,15 +160,14 @@ public:
// Query // Query
//- True if this is a managed pointer (not a const reference) //- True if a null managed pointer
inline bool isTmp() const noexcept; bool empty() const noexcept { return !ptr_ && type_ == PTR; }
//- True if this is a non-null managed pointer //- True for non-null managed pointer or an object reference
inline bool empty() const noexcept; bool valid() const noexcept { return ptr_ || type_ == CREF; }
//- True if this is a non-null managed pointer, //- True if this is a managed pointer (not a reference)
//- or is a const object reference bool isTmp() const noexcept { return type_ == PTR; }
inline bool valid() const noexcept;
//- True if this is a non-null managed pointer with a unique ref-count //- True if this is a non-null managed pointer with a unique ref-count
inline bool movable() const noexcept; inline bool movable() const noexcept;
@ -179,10 +179,10 @@ public:
// Access // Access
//- Return pointer without nullptr checking. //- Return pointer without nullptr checking.
inline T* get() noexcept; T* get() noexcept { return ptr_; }
//- Return const pointer without nullptr checking. //- Return const pointer without nullptr checking.
inline const T* get() const noexcept; const T* get() const noexcept { return ptr_; }
//- Return the const object reference or a const reference to the //- Return the const object reference or a const reference to the
//- contents of a non-null managed pointer. //- contents of a non-null managed pointer.
@ -211,12 +211,8 @@ public:
//- delete object and set pointer to nullptr //- delete object and set pointer to nullptr
inline void clear() const noexcept; inline void clear() const noexcept;
//- Release ownership of managed temporary object.
// After this call no object is managed.
inline void reset() noexcept;
//- Delete managed temporary object and set to new given pointer //- Delete managed temporary object and set to new given pointer
inline void reset(T* p) noexcept; inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void reset(tmp<T>&& other) noexcept; inline void reset(tmp<T>&& other) noexcept;
@ -245,12 +241,8 @@ public:
// Fatal for a null managed pointer or if the object is const. // Fatal for a null managed pointer or if the object is const.
inline T* operator->(); inline T* operator->();
//- Is non-null managed pointer or const object reference : valid() //- Non-null managed pointer or an object reference : valid()
explicit inline operator bool() const noexcept; explicit operator bool() const noexcept { return ptr_ ||type_ == CREF; }
//- Take ownership of the pointer.
// Fatal for a null pointer, or when the pointer is non-unique.
inline void operator=(T* p);
//- Transfer ownership of the managed pointer. //- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const. // Fatal for a null managed pointer or if the object is const.
@ -259,8 +251,9 @@ public:
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void operator=(tmp<T>&& other) noexcept; inline void operator=(tmp<T>&& other) noexcept;
//- Take ownership of the pointer.
// Housekeeping // Fatal for a null pointer, or when the pointer is non-unique.
inline void operator=(T* p);
//- No assignment from literal nullptr. //- No assignment from literal nullptr.
// Consistent with run-time check for nullptr on assignment. // Consistent with run-time check for nullptr on assignment.

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -32,7 +32,7 @@ License
// * * * * * * * * * * * * * Private Member Operators * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Operators * * * * * * * * * * * //
template<class T> template<class T>
inline void Foam::tmp<T>::operator++() inline void Foam::tmp<T>::incrCount()
{ {
ptr_->operator++(); ptr_->operator++();
@ -138,7 +138,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t)
{ {
if (ptr_) if (ptr_)
{ {
operator++(); this->incrCount();
} }
else else
{ {
@ -166,7 +166,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t, bool reuse)
} }
else else
{ {
operator++(); this->incrCount();
} }
} }
else else
@ -188,27 +188,6 @@ inline Foam::tmp<T>::~tmp()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline bool Foam::tmp<T>::isTmp() const noexcept
{
return type_ == PTR;
}
template<class T>
inline bool Foam::tmp<T>::empty() const noexcept
{
return (!ptr_ && isTmp());
}
template<class T>
inline bool Foam::tmp<T>::valid() const noexcept
{
return (ptr_ || type_ == CREF);
}
template<class T> template<class T>
inline bool Foam::tmp<T>::movable() const noexcept inline bool Foam::tmp<T>::movable() const noexcept
{ {
@ -223,20 +202,6 @@ inline Foam::word Foam::tmp<T>::typeName() const
} }
template<class T>
inline T* Foam::tmp<T>::get() noexcept
{
return ptr_; // non-const pointer
}
template<class T>
inline const T* Foam::tmp<T>::get() const noexcept
{
return ptr_; // const pointer
}
template<class T> template<class T>
inline const T& Foam::tmp<T>::cref() const inline const T& Foam::tmp<T>::cref() const
{ {
@ -266,7 +231,7 @@ inline T& Foam::tmp<T>::ref() const
<< abort(FatalError); << abort(FatalError);
} }
} }
else else // if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempted non-const reference to const object from a " << "Attempted non-const reference to const object from a "
@ -311,10 +276,10 @@ inline T* Foam::tmp<T>::ptr() const
<< abort(FatalError); << abort(FatalError);
} }
T* ptr = ptr_; T* p = ptr_;
ptr_ = nullptr; ptr_ = nullptr;
return ptr; return p;
} }
return ptr_->clone().ptr(); return ptr_->clone().ptr();
@ -339,15 +304,6 @@ inline void Foam::tmp<T>::clear() const noexcept
} }
template<class T>
inline void Foam::tmp<T>::reset() noexcept
{
clear();
ptr_ = nullptr;
type_ = PTR;
}
template<class T> template<class T>
inline void Foam::tmp<T>::reset(T* p) noexcept inline void Foam::tmp<T>::reset(T* p) noexcept
{ {
@ -455,37 +411,6 @@ inline T* Foam::tmp<T>::operator->()
} }
template<class T>
inline Foam::tmp<T>::operator bool() const noexcept
{
return (ptr_ || type_ == CREF);
}
template<class T>
inline void Foam::tmp<T>::operator=(T* p)
{
clear();
if (!p)
{
FatalErrorInFunction
<< "Attempted copy of a deallocated " << typeName()
<< abort(FatalError);
}
else if (!p->unique())
{
FatalErrorInFunction
<< "Attempted assignment of a " << typeName()
<< " to non-unique pointer"
<< abort(FatalError);
}
ptr_ = p;
type_ = PTR;
}
template<class T> template<class T>
inline void Foam::tmp<T>::operator=(const tmp<T>& t) inline void Foam::tmp<T>::operator=(const tmp<T>& t)
{ {
@ -536,4 +461,28 @@ inline void Foam::tmp<T>::operator=(tmp<T>&& other) noexcept
} }
template<class T>
inline void Foam::tmp<T>::operator=(T* p)
{
clear();
if (!p)
{
FatalErrorInFunction
<< "Attempted copy of a deallocated " << typeName()
<< abort(FatalError);
}
else if (!p->unique())
{
FatalErrorInFunction
<< "Attempted assignment of a " << typeName()
<< " to non-unique pointer"
<< abort(FatalError);
}
ptr_ = p;
type_ = PTR;
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -62,13 +62,13 @@ class tmpNrc
enum refType enum refType
{ {
PTR, //!< Managing a pointer (not ref-counted) PTR, //!< Managing a pointer (not ref-counted)
CREF //!< Using a const-reference to an object CREF //!< Using (const) reference to an object
}; };
//- The managed pointer or the address of const-reference object //- The managed pointer or address of the object (reference)
mutable T* ptr_; mutable T* ptr_;
//- The type (managed pointer | const-reference object) //- The type (managed pointer | object reference)
mutable refType type_; mutable refType type_;
@ -109,7 +109,7 @@ public:
// Constructors // Constructors
//- Construct with no managed pointer. //- Default construct, no managed pointer.
inline constexpr tmpNrc() noexcept; inline constexpr tmpNrc() noexcept;
//- Construct with no managed pointer. //- Construct with no managed pointer.
@ -139,17 +139,16 @@ public:
// Query // Query
//- True if this is a managed pointer (not a const reference) //- True if a null managed pointer
inline bool isTmp() const noexcept; bool empty() const noexcept { return !ptr_ && type_ == PTR; }
//- True for non-null managed pointer or an object reference
bool valid() const noexcept { return ptr_ || type_ == CREF; }
//- True if this is a managed pointer (not a reference)
bool isTmp() const noexcept { return type_ == PTR; }
//- True if this is a non-null managed pointer //- True if this is a non-null managed pointer
inline bool empty() const noexcept;
//- True if this is a non-null managed pointer,
//- or is a const object reference
inline bool valid() const noexcept;
//- True if this is a non-null managed pointer with a unique ref-count
inline bool movable() const noexcept; inline bool movable() const noexcept;
//- Return type-name of the tmp, constructed from type-name of T //- Return type-name of the tmp, constructed from type-name of T
@ -159,10 +158,10 @@ public:
// Access // Access
//- Return pointer without nullptr checking. //- Return pointer without nullptr checking.
inline T* get() noexcept; T* get() noexcept { return ptr_; }
//- Return const pointer without nullptr checking. //- Return const pointer without nullptr checking.
inline const T* get() const noexcept; const T* get() const noexcept { return ptr_; }
//- Return the const object reference or a const reference to the //- Return the const object reference or a const reference to the
//- contents of a non-null managed pointer. //- contents of a non-null managed pointer.
@ -191,12 +190,8 @@ public:
//- delete object and set pointer to nullptr //- delete object and set pointer to nullptr
inline void clear() const noexcept; inline void clear() const noexcept;
//- Release ownership of managed temporary object.
// After this call no object is managed.
inline void reset() noexcept;
//- Delete managed temporary object and set to new given pointer //- Delete managed temporary object and set to new given pointer
inline void reset(T* p) noexcept; inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void reset(tmpNrc<T>&& other) noexcept; inline void reset(tmpNrc<T>&& other) noexcept;
@ -225,12 +220,8 @@ public:
// Fatal for a null managed pointer or if the object is const. // Fatal for a null managed pointer or if the object is const.
inline T* operator->(); inline T* operator->();
//- Is non-null managed pointer or const object reference : valid() //- Non-null managed pointer or an object reference : valid()
explicit inline operator bool() const noexcept; explicit operator bool() const noexcept { return ptr_ ||type_ == CREF; }
//- Take ownership of the pointer.
// Fatal for a null pointer, or when the pointer is non-unique.
inline void operator=(T* p);
//- Transfer ownership of the managed pointer. //- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const. // Fatal for a null managed pointer or if the object is const.
@ -239,15 +230,16 @@ public:
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void operator=(tmpNrc<T>&& other) noexcept; inline void operator=(tmpNrc<T>&& other) noexcept;
//- Conversion to tmp //- Take ownership of the pointer.
inline operator tmp<T>(); // Fatal for a null pointer
inline void operator=(T* p);
// Housekeeping
//- No assignment from literal nullptr. //- No assignment from literal nullptr.
// Consistent with run-time check for nullptr on assignment. // Consistent with run-time check for nullptr on assignment.
void operator=(std::nullptr_t) = delete; void operator=(std::nullptr_t) = delete;
//- Conversion to tmp - releases pointer or copies reference
inline operator tmp<T>();
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -152,27 +152,6 @@ inline Foam::tmpNrc<T>::~tmpNrc()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline bool Foam::tmpNrc<T>::isTmp() const noexcept
{
return type_ == PTR;
}
template<class T>
inline bool Foam::tmpNrc<T>::empty() const noexcept
{
return (!ptr_ && isTmp());
}
template<class T>
inline bool Foam::tmpNrc<T>::valid() const noexcept
{
return (ptr_ || type_ == CREF);
}
template<class T> template<class T>
inline bool Foam::tmpNrc<T>::movable() const noexcept inline bool Foam::tmpNrc<T>::movable() const noexcept
{ {
@ -187,20 +166,6 @@ inline Foam::word Foam::tmpNrc<T>::typeName() const
} }
template<class T>
inline T* Foam::tmpNrc<T>::get() noexcept
{
return ptr_; // non-const pointer
}
template<class T>
inline const T* Foam::tmpNrc<T>::get() const noexcept
{
return ptr_; // const pointer
}
template<class T> template<class T>
inline const T& Foam::tmpNrc<T>::cref() const inline const T& Foam::tmpNrc<T>::cref() const
{ {
@ -230,7 +195,7 @@ inline T& Foam::tmpNrc<T>::ref() const
<< abort(FatalError); << abort(FatalError);
} }
} }
else else // if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempted non-const reference to const object from a " << "Attempted non-const reference to const object from a "
@ -268,10 +233,10 @@ inline T* Foam::tmpNrc<T>::ptr() const
<< abort(FatalError); << abort(FatalError);
} }
T* ptr = ptr_; T* p = ptr_;
ptr_ = nullptr; ptr_ = nullptr;
return ptr; return p;
} }
return ptr_->clone().ptr(); return ptr_->clone().ptr();
@ -289,15 +254,6 @@ inline void Foam::tmpNrc<T>::clear() const noexcept
} }
template<class T>
inline void Foam::tmpNrc<T>::reset() noexcept
{
clear();
ptr_ = nullptr;
type_ = PTR;
}
template<class T> template<class T>
inline void Foam::tmpNrc<T>::reset(T* p) noexcept inline void Foam::tmpNrc<T>::reset(T* p) noexcept
{ {
@ -405,30 +361,6 @@ inline T* Foam::tmpNrc<T>::operator->()
} }
template<class T>
inline Foam::tmpNrc<T>::operator bool() const noexcept
{
return (ptr_ || type_ == CREF);
}
template<class T>
inline void Foam::tmpNrc<T>::operator=(T* p)
{
clear();
if (!p)
{
FatalErrorInFunction
<< "Attempted copy of a deallocated " << typeName()
<< abort(FatalError);
}
ptr_ = p;
type_ = PTR;
}
template<class T> template<class T>
inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>& t) inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>& t)
{ {
@ -479,6 +411,23 @@ inline void Foam::tmpNrc<T>::operator=(tmpNrc<T>&& other) noexcept
} }
template<class T>
inline void Foam::tmpNrc<T>::operator=(T* p)
{
clear();
if (!p)
{
FatalErrorInFunction
<< "Attempted copy of a deallocated " << typeName()
<< abort(FatalError);
}
ptr_ = p;
type_ = PTR;
}
template<class T> template<class T>
inline Foam::tmpNrc<T>::operator tmp<T>() inline Foam::tmpNrc<T>::operator tmp<T>()
{ {