Commit d282d1a2 authored by Mark Olesen's avatar Mark Olesen

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
parent 12c91b94
......@@ -50,7 +50,7 @@ namespace Foam
class refCount
{
// Private data
// Private Data
int count_;
......@@ -62,7 +62,7 @@ public:
// Constructors
//- Construct null initializing count to 0
//- Default construct, initializing count to 0
constexpr refCount() noexcept
:
count_(0)
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -69,20 +69,21 @@ class tmp
enum refType
{
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_;
//- The type (managed pointer | const-reference object)
//- The type (managed pointer | object reference)
mutable refType type_;
// Private Member Operators
//- Increment the ref-count for a managed pointer
inline void operator++();
//- and check that it is not oversubscribed
inline void incrCount();
public:
......@@ -122,7 +123,7 @@ public:
// Constructors
//- Construct with no managed pointer.
//- Default construct, no managed pointer.
inline constexpr tmp() noexcept;
//- Construct with no managed pointer.
......@@ -159,15 +160,14 @@ public:
// Query
//- True if this is a managed pointer (not a const reference)
inline bool isTmp() const noexcept;
//- True if a null managed pointer
bool empty() const noexcept { return !ptr_ && type_ == PTR; }
//- True if this is a non-null managed pointer
inline bool empty() const noexcept;
//- True for non-null managed pointer or an object reference
bool valid() const noexcept { return ptr_ || type_ == CREF; }
//- 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 managed pointer (not a reference)
bool isTmp() const noexcept { return type_ == PTR; }
//- True if this is a non-null managed pointer with a unique ref-count
inline bool movable() const noexcept;
......@@ -179,10 +179,10 @@ public:
// Access
//- Return pointer without nullptr checking.
inline T* get() noexcept;
T* get() noexcept { return ptr_; }
//- 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
//- contents of a non-null managed pointer.
......@@ -211,12 +211,8 @@ public:
//- delete object and set pointer to nullptr
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
inline void reset(T* p) noexcept;
inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership.
inline void reset(tmp<T>&& other) noexcept;
......@@ -245,12 +241,8 @@ public:
// Fatal for a null managed pointer or if the object is const.
inline T* operator->();
//- Is non-null managed pointer or const object reference : valid()
explicit inline operator bool() const noexcept;
//- Take ownership of the pointer.
// Fatal for a null pointer, or when the pointer is non-unique.
inline void operator=(T* p);
//- Non-null managed pointer or an object reference : valid()
explicit operator bool() const noexcept { return ptr_ ||type_ == CREF; }
//- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const.
......@@ -259,8 +251,9 @@ public:
//- Clear existing and transfer ownership.
inline void operator=(tmp<T>&& other) noexcept;
// Housekeeping
//- Take ownership of the pointer.
// Fatal for a null pointer, or when the pointer is non-unique.
inline void operator=(T* p);
//- No assignment from literal nullptr.
// Consistent with run-time check for nullptr on assignment.
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -32,7 +32,7 @@ License
// * * * * * * * * * * * * * Private Member Operators * * * * * * * * * * * //
template<class T>
inline void Foam::tmp<T>::operator++()
inline void Foam::tmp<T>::incrCount()
{
ptr_->operator++();
......@@ -138,7 +138,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t)
{
if (ptr_)
{
operator++();
this->incrCount();
}
else
{
......@@ -166,7 +166,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t, bool reuse)
}
else
{
operator++();
this->incrCount();
}
}
else
......@@ -188,27 +188,6 @@ inline Foam::tmp<T>::~tmp()
// * * * * * * * * * * * * * * * 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>
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>
inline const T& Foam::tmp<T>::cref() const
{
......@@ -266,7 +231,7 @@ inline T& Foam::tmp<T>::ref() const
<< abort(FatalError);
}
}
else
else // if (type_ == CREF)
{
FatalErrorInFunction
<< "Attempted non-const reference to const object from a "
......@@ -311,10 +276,10 @@ inline T* Foam::tmp<T>::ptr() const
<< abort(FatalError);
}
T* ptr = ptr_;
T* p = ptr_;
ptr_ = nullptr;
return ptr;
return p;
}
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>
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>
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;
}
// ************************************************************************* //
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -62,13 +62,13 @@ class tmpNrc
enum refType
{
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_;
//- The type (managed pointer | const-reference object)
//- The type (managed pointer | object reference)
mutable refType type_;
......@@ -109,7 +109,7 @@ public:
// Constructors
//- Construct with no managed pointer.
//- Default construct, no managed pointer.
inline constexpr tmpNrc() noexcept;
//- Construct with no managed pointer.
......@@ -139,17 +139,16 @@ public:
// Query
//- True if this is a managed pointer (not a const reference)
inline bool isTmp() const noexcept;
//- True if a null managed pointer
bool empty() const noexcept { return !ptr_ && type_ == PTR; }
//- True if this is a non-null managed pointer
inline bool empty() const noexcept;
//- True for non-null managed pointer or an object reference
bool valid() const noexcept { return ptr_ || type_ == CREF; }
//- 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 managed pointer (not a reference)
bool isTmp() const noexcept { return type_ == PTR; }
//- True if this is a non-null managed pointer with a unique ref-count
//- True if this is a non-null managed pointer
inline bool movable() const noexcept;
//- Return type-name of the tmp, constructed from type-name of T
......@@ -159,10 +158,10 @@ public:
// Access
//- Return pointer without nullptr checking.
inline T* get() noexcept;
T* get() noexcept { return ptr_; }
//- 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
//- contents of a non-null managed pointer.
......@@ -191,12 +190,8 @@ public:
//- delete object and set pointer to nullptr
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
inline void reset(T* p) noexcept;
inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership.
inline void reset(tmpNrc<T>&& other) noexcept;
......@@ -225,12 +220,8 @@ public:
// Fatal for a null managed pointer or if the object is const.
inline T* operator->();
//- Is non-null managed pointer or const object reference : valid()
explicit inline operator bool() const noexcept;
//- Take ownership of the pointer.
// Fatal for a null pointer, or when the pointer is non-unique.
inline void operator=(T* p);
//- Non-null managed pointer or an object reference : valid()
explicit operator bool() const noexcept { return ptr_ ||type_ == CREF; }
//- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const.
......@@ -239,15 +230,16 @@ public:
//- Clear existing and transfer ownership.
inline void operator=(tmpNrc<T>&& other) noexcept;
//- Conversion to tmp
inline operator tmp<T>();
// Housekeeping
//- Take ownership of the pointer.
// Fatal for a null pointer
inline void operator=(T* p);
//- No assignment from literal nullptr.
// Consistent with run-time check for nullptr on assignment.
void operator=(std::nullptr_t) = delete;
//- Conversion to tmp - releases pointer or copies reference
inline operator tmp<T>();
};
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -152,27 +152,6 @@ inline Foam::tmpNrc<T>::~tmpNrc()
// * * * * * * * * * * * * * * * 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>
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>
inline const T& Foam::tmpNrc<T>::cref() const
{
......@@ -230,7 +195,7 @@ inline T& Foam::tmpNrc<T>::ref() const
<< abort(FatalError);
}
}
else
else // if (type_ == CREF)
{
FatalErrorInFunction
<< "Attempted non-const reference to const object from a "
......@@ -268,10 +233,10 @@ inline T* Foam::tmpNrc<T>::ptr() const
<< abort(FatalError);
}
T* ptr = ptr_;
T* p = ptr_;
ptr_ = nullptr;
return ptr;
return p;
}
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>
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>
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>
inline Foam::tmpNrc<T>::operator tmp<T>()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment