From d282d1a2855ab8132e0f55bd7714a7e3c705aedf Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 16 Jul 2020 09:10:12 +0200 Subject: [PATCH] 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 --- src/OpenFOAM/memory/refCount/refCount.H | 4 +- src/OpenFOAM/memory/tmp/tmp.H | 49 +++++----- src/OpenFOAM/memory/tmp/tmpI.H | 113 +++++++----------------- src/OpenFOAM/memory/tmp/tmpNrc.H | 56 +++++------- src/OpenFOAM/memory/tmp/tmpNrcI.H | 93 +++++-------------- 5 files changed, 99 insertions(+), 216 deletions(-) diff --git a/src/OpenFOAM/memory/refCount/refCount.H b/src/OpenFOAM/memory/refCount/refCount.H index 303188bda5..281d653590 100644 --- a/src/OpenFOAM/memory/refCount/refCount.H +++ b/src/OpenFOAM/memory/refCount/refCount.H @@ -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) diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index dacb813c5f..ebd8f676c9 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -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&& 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&& 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. diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index a035432d81..10f370ae51 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -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 -inline void Foam::tmp::operator++() +inline void Foam::tmp::incrCount() { ptr_->operator++(); @@ -138,7 +138,7 @@ inline Foam::tmp::tmp(const tmp& t) { if (ptr_) { - operator++(); + this->incrCount(); } else { @@ -166,7 +166,7 @@ inline Foam::tmp::tmp(const tmp& t, bool reuse) } else { - operator++(); + this->incrCount(); } } else @@ -188,27 +188,6 @@ inline Foam::tmp::~tmp() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline bool Foam::tmp::isTmp() const noexcept -{ - return type_ == PTR; -} - - -template -inline bool Foam::tmp::empty() const noexcept -{ - return (!ptr_ && isTmp()); -} - - -template -inline bool Foam::tmp::valid() const noexcept -{ - return (ptr_ || type_ == CREF); -} - - template inline bool Foam::tmp::movable() const noexcept { @@ -223,20 +202,6 @@ inline Foam::word Foam::tmp::typeName() const } -template -inline T* Foam::tmp::get() noexcept -{ - return ptr_; // non-const pointer -} - - -template -inline const T* Foam::tmp::get() const noexcept -{ - return ptr_; // const pointer -} - - template inline const T& Foam::tmp::cref() const { @@ -266,7 +231,7 @@ inline T& Foam::tmp::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::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::clear() const noexcept } -template -inline void Foam::tmp::reset() noexcept -{ - clear(); - ptr_ = nullptr; - type_ = PTR; -} - - template inline void Foam::tmp::reset(T* p) noexcept { @@ -455,37 +411,6 @@ inline T* Foam::tmp::operator->() } -template -inline Foam::tmp::operator bool() const noexcept -{ - return (ptr_ || type_ == CREF); -} - - -template -inline void Foam::tmp::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 inline void Foam::tmp::operator=(const tmp& t) { @@ -536,4 +461,28 @@ inline void Foam::tmp::operator=(tmp&& other) noexcept } +template +inline void Foam::tmp::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; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H index c997b3c800..037b2ab97d 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrc.H +++ b/src/OpenFOAM/memory/tmp/tmpNrc.H @@ -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 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 - 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; //- 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&& 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&& other) noexcept; - //- Conversion to tmp - inline operator tmp(); - - - // 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(); }; diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H index 3ff8924c16..def9c1a67e 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrcI.H +++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H @@ -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::~tmpNrc() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline bool Foam::tmpNrc::isTmp() const noexcept -{ - return type_ == PTR; -} - - -template -inline bool Foam::tmpNrc::empty() const noexcept -{ - return (!ptr_ && isTmp()); -} - - -template -inline bool Foam::tmpNrc::valid() const noexcept -{ - return (ptr_ || type_ == CREF); -} - - template inline bool Foam::tmpNrc::movable() const noexcept { @@ -187,20 +166,6 @@ inline Foam::word Foam::tmpNrc::typeName() const } -template -inline T* Foam::tmpNrc::get() noexcept -{ - return ptr_; // non-const pointer -} - - -template -inline const T* Foam::tmpNrc::get() const noexcept -{ - return ptr_; // const pointer -} - - template inline const T& Foam::tmpNrc::cref() const { @@ -230,7 +195,7 @@ inline T& Foam::tmpNrc::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::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::clear() const noexcept } -template -inline void Foam::tmpNrc::reset() noexcept -{ - clear(); - ptr_ = nullptr; - type_ = PTR; -} - - template inline void Foam::tmpNrc::reset(T* p) noexcept { @@ -405,30 +361,6 @@ inline T* Foam::tmpNrc::operator->() } -template -inline Foam::tmpNrc::operator bool() const noexcept -{ - return (ptr_ || type_ == CREF); -} - - -template -inline void Foam::tmpNrc::operator=(T* p) -{ - clear(); - - if (!p) - { - FatalErrorInFunction - << "Attempted copy of a deallocated " << typeName() - << abort(FatalError); - } - - ptr_ = p; - type_ = PTR; -} - - template inline void Foam::tmpNrc::operator=(const tmpNrc& t) { @@ -479,6 +411,23 @@ inline void Foam::tmpNrc::operator=(tmpNrc&& other) noexcept } +template +inline void Foam::tmpNrc::operator=(T* p) +{ + clear(); + + if (!p) + { + FatalErrorInFunction + << "Attempted copy of a deallocated " << typeName() + << abort(FatalError); + } + + ptr_ = p; + type_ = PTR; +} + + template inline Foam::tmpNrc::operator tmp() {