diff --git a/src/OpenFOAM/fields/Fields/Field/PrecisionAdaptor/PrecisionAdaptor.H b/src/OpenFOAM/fields/Fields/Field/PrecisionAdaptor/PrecisionAdaptor.H index 75e6eface2..a326c7a403 100644 --- a/src/OpenFOAM/fields/Fields/Field/PrecisionAdaptor/PrecisionAdaptor.H +++ b/src/OpenFOAM/fields/Fields/Field/PrecisionAdaptor/PrecisionAdaptor.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,18 +27,19 @@ Class Foam::PrecisionAdaptor Description - Conversion adaptor for Field that either wraps as a tmp reference - or creates the necessary tmp and copies the values on construction - and destruction. - This provides automatic conversion between (scalar) types for use - with linear solvers able to run mixed precision. + Conversion adaptor for Field/List that either wrap the input as a + reference, or creates a temporary pointer and copies the values + on construction/destruction. + + This provides, for example, automatic conversion between types + for linear solvers able to run mixed precision. \*---------------------------------------------------------------------------*/ #ifndef PrecisionAdaptor_H #define PrecisionAdaptor_H -#include "tmpNrc.H" +#include "refPtr.H" #include "Field.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -46,22 +47,19 @@ Description namespace Foam { -//- A const Field wrapper with possible data conversion +//- A const Field/List wrapper with possible data conversion template class Container = Field> class ConstPrecisionAdaptor : - public tmpNrc> + public refPtr> { // Private Member Functions //- Copy in field void copyInput(const Container& input) { - this->clear(); - - Container* p = new Container(input.size()); - this->reset(p); - std::copy(input.cbegin(), input.cend(), p->begin()); + this->reset(new Container(input.size())); + std::copy(input.cbegin(), input.cend(), this->ref().begin()); } //- Construct from tmp Field, copy/move as required @@ -71,19 +69,20 @@ class ConstPrecisionAdaptor { auto& tinput = reinterpret_cast>&>(input); - if (tinput.isTmp()) + if (tinput.is_pointer()) { - // Reset to tmp + // Acquire control of the managed pointer this->reset(tinput.ptr()); } else { + // Use const reference this->cref(tinput.cref()); } } else { - this->copyInput(input()); + this->copyInput(input.cref()); } input.clear(); } @@ -100,10 +99,11 @@ public: //- Construct from Container, copying on input as required ConstPrecisionAdaptor(const Container& input) : - tmpNrc>() + refPtr>() { if (std::is_same::value) { + // Use const reference directly this->cref(reinterpret_cast(input)); } else @@ -116,7 +116,7 @@ public: //- Construct from tmp Container, copy/move as required ConstPrecisionAdaptor(tmp>&& input) : - tmpNrc>() + refPtr>() { this->moveInput(input); } @@ -125,7 +125,7 @@ public: //- Construct from tmp Container, copy/move as required ConstPrecisionAdaptor(const tmp>& input) : - tmpNrc>() + refPtr>() { this->moveInput(const_cast>&>(input)); } @@ -133,6 +133,8 @@ public: // Member Functions + // May need in the future: using refPtr>::get; + //- Return the field static const Container& get ( @@ -154,15 +156,15 @@ public: }; -//- A Field wrapper with possible data conversion +//- A non-const Field/List wrapper with possible data conversion template class Container = Field> class PrecisionAdaptor : - public tmpNrc> + public refPtr> { // Private Data - //- Reference to underlying field + //- Reference to underlying (input) data Container& ref_; @@ -171,11 +173,10 @@ class PrecisionAdaptor //- Copy in field void copyInput(const Container& input, const bool copy) { - Container* p = new Container(input.size()); - this->reset(p); + this->reset(new Container(input.size())); if (copy) { - std::copy(input.cbegin(), input.cend(), p->begin()); + std::copy(input.cbegin(), input.cend(), this->ref().begin()); } } @@ -191,12 +192,13 @@ public: //- Construct from Container, copying on input if required PrecisionAdaptor(Container& input, const bool copy = true) : - tmpNrc>(), + refPtr>(), ref_(input) { if (std::is_same::value) { - this->cref(reinterpret_cast(input)); + // Use non-const reference directly + this->ref(reinterpret_cast(ref_)); } else { @@ -205,25 +207,16 @@ public: } - //- Destructor, copying on destroy + //- Destructor, copy back on destroy ~PrecisionAdaptor() { - if (this->isTmp()) + if (this->is_pointer()) { const FieldType& store = this->cref(); - ref_.resize(store.size()); + ref_.resize(store.size()); // extra safety std::copy(store.cbegin(), store.cend(), ref_.begin()); } } - - - // Member Functions - - //- Allow modification without const-ref check - FieldType& ref() - { - return this->constCast(); - } }; diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C index 677b299534..8071a10c4d 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C @@ -233,7 +233,7 @@ bool Foam::fileOperation::isFileOrDir(const bool isFile, const fileName& f) } -Foam::tmpNrc +Foam::refPtr Foam::fileOperation::lookupAndCacheProcessorsPath ( const fileName& fName, @@ -361,11 +361,12 @@ Foam::fileOperation::lookupAndCacheProcessorsPath return procsDirs_[procPath]; } } - return tmpNrc(new dirIndexList(0, dirIndex())); + + return refPtr::New(); } -Foam::tmpNrc +Foam::refPtr Foam::fileOperation::lookupProcessorsPath(const fileName& fName) const { // Use parallel synchronisation @@ -531,7 +532,7 @@ Foam::fileName Foam::fileOperation::filePath(const fileName& fName) const if (proci != -1) { // Get all processor directories - tmpNrc procDirs(lookupProcessorsPath(fName)); + refPtr procDirs(lookupProcessorsPath(fName)); forAll(procDirs(), i) { const fileName& procDir = procDirs()[i].first(); @@ -688,7 +689,7 @@ Foam::instantList Foam::fileOperation::findTimes // Get all processor directories - tmpNrc procDirs(lookupProcessorsPath(directory)); + refPtr procDirs(lookupProcessorsPath(directory)); forAll(procDirs(), i) { const fileName& procDir = procDirs()[i].first(); diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H index 81a21e99a5..2748bd580f 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H @@ -49,7 +49,7 @@ Description #include "fileMonitor.H" #include "labelList.H" #include "Switch.H" -#include "tmpNrc.H" +#include "refPtr.H" #include "Enum.H" #include "Tuple2.H" @@ -135,17 +135,18 @@ protected: //- Helper: check for file (isFile) or directory (!isFile) static bool isFileOrDir(const bool isFile, const fileName&); - //- Lookup name of processorsDDD using cache. Return empty fileName - // if not found. - tmpNrc lookupAndCacheProcessorsPath + //- Lookup name of processorsDDD using cache. + // \return empty fileName if not found. + refPtr lookupAndCacheProcessorsPath ( const fileName&, const bool syncPar ) const; - //- Lookup name of processorsDDD using cache. Return empty fileName - // if not found. To be called on all processors - virtual tmpNrc lookupProcessorsPath + //- Lookup name of processorsDDD using cache. + // \note To be called on all processors + // \return empty fileName if not found. + virtual refPtr lookupProcessorsPath ( const fileName& ) const; diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C index 74f46370af..15be6750a4 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C @@ -199,7 +199,7 @@ Foam::fileOperations::masterUncollatedFileOperation::filePathInfo // 2. Check processors/ if (io.time().processorCase()) { - tmpNrc pDirs(lookupProcessorsPath(io.objectPath())); + refPtr pDirs(lookupProcessorsPath(io.objectPath())); forAll(pDirs(), i) { const fileName& pDir = pDirs()[i].first(); @@ -268,7 +268,7 @@ Foam::fileOperations::masterUncollatedFileOperation::filePathInfo if (newInstancePath.size() && newInstancePath != io.instance()) { // 1. Try processors equivalent - tmpNrc pDirs + refPtr pDirs ( lookupProcessorsPath(io.objectPath()) ); @@ -1499,7 +1499,7 @@ Foam::fileOperations::masterUncollatedFileOperation::findInstance // parent directory in case of parallel) - tmpNrc pDirs(lookupProcessorsPath(io.objectPath())); + refPtr pDirs(lookupProcessorsPath(io.objectPath())); word foundInstance; diff --git a/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.C index bd891efd6e..0a33c54642 100644 --- a/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.C @@ -114,7 +114,7 @@ Foam::fileName Foam::fileOperations::uncollatedFileOperation::filePathInfo // Check if parallel "procesors" directory if (io.time().processorCase()) { - tmpNrc pDirs + refPtr pDirs ( fileOperation::lookupAndCacheProcessorsPath ( @@ -166,7 +166,7 @@ Foam::fileName Foam::fileOperations::uncollatedFileOperation::filePathInfo } -Foam::tmpNrc +Foam::refPtr Foam::fileOperations::uncollatedFileOperation::lookupProcessorsPath ( const fileName& fName diff --git a/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.H b/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.H index 325648160b..496f64014e 100644 --- a/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.H +++ b/src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.H @@ -68,9 +68,10 @@ protected: const bool search ) const; - //- Lookup name of processorsDDD using cache. Return empty fileName - // if not found. Do not use any parallel synchronisation - virtual tmpNrc lookupProcessorsPath + //- Lookup name of processorsDDD using cache. + // \not Do not use any parallel synchronisation + // \return empty fileName if not found. + virtual refPtr lookupProcessorsPath ( const fileName& ) const; diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/refPtr/refPtr.H similarity index 77% rename from src/OpenFOAM/memory/tmp/tmpNrc.H rename to src/OpenFOAM/memory/refPtr/refPtr.H index b12e881245..4a05d9af6e 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrc.H +++ b/src/OpenFOAM/memory/refPtr/refPtr.H @@ -25,13 +25,13 @@ License along with OpenFOAM. If not, see . Class - Foam::tmpNrc + Foam::refPtr Description - A class for managing temporary objects without reference counting. + A class for managing references or pointers (no reference counting) SourceFiles - tmpNrcI.H + refPtrI.H See also Foam::autoPtr @@ -39,8 +39,8 @@ See also \*---------------------------------------------------------------------------*/ -#ifndef tmpNrc_H -#define tmpNrc_H +#ifndef refPtr_H +#define refPtr_H #include "tmp.H" @@ -50,11 +50,11 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class tmpNrc Declaration + Class refPtr Declaration \*---------------------------------------------------------------------------*/ template -class tmpNrc +class refPtr { // Private Data @@ -62,7 +62,8 @@ class tmpNrc enum refType { PTR, //!< Managing a pointer (not ref-counted) - CREF //!< Using (const) reference to an object + CREF, //!< Using (const) reference to an object + REF //!< Using (non-const) reference to an object }; //- The managed pointer or address of the object (reference) @@ -76,10 +77,10 @@ public: // STL type definitions - //- Type of object being managed + //- Type of object being managed or referenced typedef T element_type; - //- Pointer to type of object being managed + //- Pointer to type of object being managed or referenced typedef T* pointer; @@ -89,50 +90,50 @@ public: // Factory Methods - //- Construct tmpNrc of T with forwarding arguments + //- Construct refPtr of T with forwarding arguments // \param args list of arguments with which an instance of T // will be constructed. // // \note Similar to std::make_shared, but the overload for // array types is not disabled. template - inline static tmpNrc New(Args&&... args); + inline static refPtr New(Args&&... args); - //- Construct tmpNrc from derived type with forwarding arguments + //- Construct refPtr from derived type with forwarding arguments // \param args list of arguments with which an instance of U // will be constructed. // // \note Similar to New but for derived types template - inline static tmpNrc NewFrom(Args&&... args); + inline static refPtr NewFrom(Args&&... args); // Constructors //- Default construct, no managed pointer. - inline constexpr tmpNrc() noexcept; + inline constexpr refPtr() noexcept; //- Construct with no managed pointer. - inline constexpr tmpNrc(std::nullptr_t) noexcept; + inline constexpr refPtr(std::nullptr_t) noexcept; //- Construct, taking ownership of the pointer. - inline explicit tmpNrc(T* p) noexcept; + inline explicit refPtr(T* p) noexcept; //- Construct for a const reference to an object. - inline tmpNrc(const T& obj) noexcept; + inline refPtr(const T& obj) noexcept; //- Move construct, transferring ownership. - inline tmpNrc(tmpNrc&& t) noexcept; + inline refPtr(refPtr&& t) noexcept; //- Copy construct - inline tmpNrc(const tmpNrc& t); + inline refPtr(const refPtr& t); //- Copy construct. Optionally reusing pointer. - inline tmpNrc(const tmpNrc& t, bool reuse); + inline refPtr(const refPtr& t, bool reuse); //- Destructor: deletes managed pointer - inline ~tmpNrc(); + inline ~refPtr(); // Member Functions @@ -148,6 +149,9 @@ public: //- True for non-null pointer/reference bool valid() const noexcept { return ptr_; } + //- True if this is a managed pointer (not a reference) + bool is_pointer() const noexcept { return type_ == PTR; } + //- True if this is a managed pointer (not a reference) bool isTmp() const noexcept { return type_ == PTR; } @@ -166,8 +170,8 @@ public: //- Return const pointer without nullptr checking. 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. + //- Return const reference to the object or to the contents + //- of a (non-null) managed pointer. // Fatal for a null managed pointer inline const T& cref() const; @@ -185,8 +189,7 @@ public: // Edit - //- Return managed pointer for reuse, or clone() the const object - //- reference. + //- Return managed pointer for reuse, or clone() the object reference. inline T* ptr() const; //- If object pointer points to valid object: @@ -197,19 +200,21 @@ public: inline void reset(T* p = nullptr) noexcept; //- Clear existing and transfer ownership. - inline void reset(tmpNrc&& other) noexcept; + inline void reset(refPtr&& other) noexcept; - //- Delete managed temporary object and set to const reference + //- Delete managed temporary object and set to (const) reference inline void cref(const T& obj) noexcept; + //- Delete managed temporary object and set to (non-const) reference + inline void ref(T& obj) noexcept; + //- Swaps the managed object with other. - inline void swap(tmpNrc& other) noexcept; + inline void swap(refPtr& other) noexcept; // Member Operators - //- Return const reference to the object. - // Identical to cref() method. + //- Identical to cref() method. inline const T& operator()() const; //- Cast to underlying data type, using the cref() method. @@ -228,10 +233,10 @@ public: //- Transfer ownership of the managed pointer. // Fatal for a null managed pointer or if the object is const. - inline void operator=(const tmpNrc& t); + inline void operator=(const refPtr& t); //- Clear existing and transfer ownership. - inline void operator=(tmpNrc&& other) noexcept; + inline void operator=(refPtr&& other) noexcept; //- Take ownership of the pointer. // Fatal for a null pointer @@ -247,10 +252,10 @@ public: // Global Functions -//- Specializes the Swap algorithm for tmpNrc. +//- Specializes the Swap algorithm for refPtr. // Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs) template -void Swap(tmpNrc& lhs, tmpNrc& rhs) +void Swap(refPtr& lhs, refPtr& rhs) { lhs.swap(rhs); } @@ -262,7 +267,7 @@ void Swap(tmpNrc& lhs, tmpNrc& rhs) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#include "tmpNrcI.H" +#include "refPtrI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/refPtr/refPtrI.H similarity index 77% rename from src/OpenFOAM/memory/tmp/tmpNrcI.H rename to src/OpenFOAM/memory/refPtr/refPtrI.H index 36ec5e8b8a..a16e0e6dc0 100644 --- a/src/OpenFOAM/memory/tmp/tmpNrcI.H +++ b/src/OpenFOAM/memory/refPtr/refPtrI.H @@ -33,24 +33,24 @@ License template template -inline Foam::tmpNrc Foam::tmpNrc::New(Args&&... args) +inline Foam::refPtr Foam::refPtr::New(Args&&... args) { - return tmpNrc(new T(std::forward(args)...)); + return refPtr(new T(std::forward(args)...)); } template template -inline Foam::tmpNrc Foam::tmpNrc::NewFrom(Args&&... args) +inline Foam::refPtr Foam::refPtr::NewFrom(Args&&... args) { - return tmpNrc(new U(std::forward(args)...)); + return refPtr(new U(std::forward(args)...)); } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -inline constexpr Foam::tmpNrc::tmpNrc() noexcept +inline constexpr Foam::refPtr::refPtr() noexcept : ptr_(nullptr), type_(PTR) @@ -58,7 +58,7 @@ inline constexpr Foam::tmpNrc::tmpNrc() noexcept template -inline constexpr Foam::tmpNrc::tmpNrc(std::nullptr_t) noexcept +inline constexpr Foam::refPtr::refPtr(std::nullptr_t) noexcept : ptr_(nullptr), type_(PTR) @@ -66,7 +66,7 @@ inline constexpr Foam::tmpNrc::tmpNrc(std::nullptr_t) noexcept template -inline Foam::tmpNrc::tmpNrc(T* p) noexcept +inline Foam::refPtr::refPtr(T* p) noexcept : ptr_(p), type_(PTR) @@ -74,7 +74,7 @@ inline Foam::tmpNrc::tmpNrc(T* p) noexcept template -inline Foam::tmpNrc::tmpNrc(const T& obj) noexcept +inline Foam::refPtr::refPtr(const T& obj) noexcept : ptr_(const_cast(&obj)), type_(CREF) @@ -82,7 +82,7 @@ inline Foam::tmpNrc::tmpNrc(const T& obj) noexcept template -inline Foam::tmpNrc::tmpNrc(tmpNrc&& t) noexcept +inline Foam::refPtr::refPtr(refPtr&& t) noexcept : ptr_(t.ptr_), type_(t.type_) @@ -93,7 +93,7 @@ inline Foam::tmpNrc::tmpNrc(tmpNrc&& t) noexcept template -inline Foam::tmpNrc::tmpNrc(const tmpNrc& t) +inline Foam::refPtr::refPtr(const refPtr& t) : ptr_(t.ptr_), type_(t.type_) @@ -115,7 +115,7 @@ inline Foam::tmpNrc::tmpNrc(const tmpNrc& t) template -inline Foam::tmpNrc::tmpNrc(const tmpNrc& t, bool reuse) +inline Foam::refPtr::refPtr(const refPtr& t, bool reuse) : ptr_(t.ptr_), type_(t.type_) @@ -144,7 +144,7 @@ inline Foam::tmpNrc::tmpNrc(const tmpNrc& t, bool reuse) template -inline Foam::tmpNrc::~tmpNrc() +inline Foam::refPtr::~refPtr() { clear(); } @@ -153,21 +153,21 @@ inline Foam::tmpNrc::~tmpNrc() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -inline bool Foam::tmpNrc::movable() const noexcept +inline bool Foam::refPtr::movable() const noexcept { return (type_ == PTR && ptr_); } template -inline Foam::word Foam::tmpNrc::typeName() const +inline Foam::word Foam::refPtr::typeName() const { - return "tmpNrc<" + word(typeid(T).name()) + '>'; + return "refPtr<" + word(typeid(T).name()) + '>'; } template -inline const T& Foam::tmpNrc::cref() const +inline const T& Foam::refPtr::cref() const { if (isTmp()) { @@ -184,7 +184,7 @@ inline const T& Foam::tmpNrc::cref() const template -inline T& Foam::tmpNrc::ref() const +inline T& Foam::refPtr::ref() const { if (isTmp()) { @@ -195,7 +195,7 @@ inline T& Foam::tmpNrc::ref() const << abort(FatalError); } } - else // if (type_ == CREF) + else if (type_ == CREF) { FatalErrorInFunction << "Attempted non-const reference to const object from a " @@ -208,21 +208,14 @@ inline T& Foam::tmpNrc::ref() const template -inline T& Foam::tmpNrc::constCast() const +inline T& Foam::refPtr::constCast() const { - if (isTmp() && !ptr_) - { - FatalErrorInFunction - << typeName() << " deallocated" - << abort(FatalError); - } - - return const_cast(*ptr_); + return const_cast(cref()); } template -inline T* Foam::tmpNrc::ptr() const +inline T* Foam::refPtr::ptr() const { if (!ptr_) { @@ -244,7 +237,7 @@ inline T* Foam::tmpNrc::ptr() const template -inline void Foam::tmpNrc::clear() const noexcept +inline void Foam::refPtr::clear() const noexcept { if (isTmp() && ptr_) { @@ -255,7 +248,7 @@ inline void Foam::tmpNrc::clear() const noexcept template -inline void Foam::tmpNrc::reset(T* p) noexcept +inline void Foam::refPtr::reset(T* p) noexcept { clear(); ptr_ = p; @@ -264,7 +257,7 @@ inline void Foam::tmpNrc::reset(T* p) noexcept template -inline void Foam::tmpNrc::reset(tmpNrc&& other) noexcept +inline void Foam::refPtr::reset(refPtr&& other) noexcept { if (&other == this) { @@ -281,7 +274,7 @@ inline void Foam::tmpNrc::reset(tmpNrc&& other) noexcept template -inline void Foam::tmpNrc::cref(const T& obj) noexcept +inline void Foam::refPtr::cref(const T& obj) noexcept { clear(); ptr_ = const_cast(&obj); @@ -290,7 +283,16 @@ inline void Foam::tmpNrc::cref(const T& obj) noexcept template -inline void Foam::tmpNrc::swap(tmpNrc& other) noexcept +inline void Foam::refPtr::ref(T& obj) noexcept +{ + clear(); + ptr_ = &obj; + type_ = REF; +} + + +template +inline void Foam::refPtr::swap(refPtr& other) noexcept { if (&other == this) { @@ -311,21 +313,21 @@ inline void Foam::tmpNrc::swap(tmpNrc& other) noexcept // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template -inline const T& Foam::tmpNrc::operator()() const +inline const T& Foam::refPtr::operator()() const { return cref(); } template -inline Foam::tmpNrc::operator const T&() const +inline Foam::refPtr::operator const T&() const { return cref(); } template -inline const T* Foam::tmpNrc::operator->() const +inline const T* Foam::refPtr::operator->() const { if (!ptr_ && isTmp()) { @@ -339,7 +341,7 @@ inline const T* Foam::tmpNrc::operator->() const template -inline T* Foam::tmpNrc::operator->() +inline T* Foam::refPtr::operator->() { if (isTmp()) { @@ -362,7 +364,7 @@ inline T* Foam::tmpNrc::operator->() template -inline void Foam::tmpNrc::operator=(const tmpNrc& t) +inline void Foam::refPtr::operator=(const refPtr& t) { if (&t == this) { @@ -395,7 +397,7 @@ inline void Foam::tmpNrc::operator=(const tmpNrc& t) template -inline void Foam::tmpNrc::operator=(tmpNrc&& other) noexcept +inline void Foam::refPtr::operator=(refPtr&& other) noexcept { if (&other == this) { @@ -412,7 +414,7 @@ inline void Foam::tmpNrc::operator=(tmpNrc&& other) noexcept template -inline void Foam::tmpNrc::operator=(T* p) +inline void Foam::refPtr::operator=(T* p) { if (!p) { @@ -426,14 +428,14 @@ inline void Foam::tmpNrc::operator=(T* p) template -inline void Foam::tmpNrc::operator=(std::nullptr_t) noexcept +inline void Foam::refPtr::operator=(std::nullptr_t) noexcept { reset(nullptr); } template -inline Foam::tmpNrc::operator tmp() +inline Foam::refPtr::operator tmp() { if (isTmp()) { diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index c2f9f6b187..aa6fc29f4d 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -40,6 +40,7 @@ SourceFiles See also Foam::autoPtr + Foam::refPtr Foam::refCount \*---------------------------------------------------------------------------*/ @@ -69,7 +70,8 @@ class tmp enum refType { PTR, //!< Managing a pointer (ref-counted) - CREF //!< Using (const) reference to an object + CREF, //!< Using (const) reference to an object + REF //!< Using (non-const) reference to an object }; //- The managed pointer or address of the object (reference) @@ -169,6 +171,9 @@ public: //- True for non-null pointer/reference bool valid() const noexcept { return ptr_; } + //- True if this is a managed pointer (not a reference) + bool is_pointer() const noexcept { return type_ == PTR; } + //- True if this is a managed pointer (not a reference) bool isTmp() const noexcept { return type_ == PTR; } @@ -187,8 +192,8 @@ public: //- Return const pointer without nullptr checking. 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. + //- Return const reference to the object or to the contents + //- of a (non-null) managed pointer. // Fatal for a null managed pointer inline const T& cref() const; @@ -200,14 +205,13 @@ public: //- Non-const dereference, even if the object is const. // This is similar to ref(), but applies a const_cast to access // const objects. - // Fatal for a null managed pointer. + // Fatal for a null pointer. inline T& constCast() const; // Edit - //- Return managed pointer for reuse, or clone() the const object - //- reference. + //- Return managed pointer for reuse, or clone() the object reference. inline T* ptr() const; //- If object pointer points to valid object: @@ -220,17 +224,19 @@ public: //- Clear existing and transfer ownership. inline void reset(tmp&& other) noexcept; - //- Delete managed temporary object and set to const reference + //- Delete managed temporary object and set to (const) reference inline void cref(const T& obj) noexcept; + //- Delete managed temporary object and set to (non-const) reference + inline void ref(T& obj) noexcept; + //- Swaps the managed object with other. inline void swap(tmp& other) noexcept; // Member Operators - //- Return const reference to the object. - // Identical to cref() method. + //- Identical to cref() method. inline const T& operator()() const; //- Cast to underlying data type, using the cref() method. diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index 729a13fedb..4f45557b36 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -231,7 +231,7 @@ inline T& Foam::tmp::ref() const << abort(FatalError); } } - else // if (type_ == CREF) + else if (type_ == CREF) { FatalErrorInFunction << "Attempted non-const reference to const object from a " @@ -246,14 +246,7 @@ inline T& Foam::tmp::ref() const template inline T& Foam::tmp::constCast() const { - if (isTmp() && !ptr_) - { - FatalErrorInFunction - << typeName() << " deallocated" - << abort(FatalError); - } - - return const_cast(*ptr_); + return const_cast(cref()); } @@ -340,6 +333,15 @@ inline void Foam::tmp::cref(const T& obj) noexcept } +template +inline void Foam::tmp::ref(T& obj) noexcept +{ + clear(); + ptr_ = &obj; + type_ = REF; +} + + template inline void Foam::tmp::swap(tmp& other) noexcept { diff --git a/src/OpenFOAM/memory/tmpNrc/tmpNrc.H b/src/OpenFOAM/memory/tmpNrc/tmpNrc.H new file mode 100644 index 0000000000..5756480033 --- /dev/null +++ b/src/OpenFOAM/memory/tmpNrc/tmpNrc.H @@ -0,0 +1,33 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2002 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM, distributed under GPL-3.0-or-later. + +Typedef + Foam::tmpNrc + +Description + Compatibility name. Superseded (JUL-2020) by Foam::refPtr + +\*---------------------------------------------------------------------------*/ + +#ifndef tmpNrc_H +#define tmpNrc_H + +#include "refPtr.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + typedef refPtr tmpNrc; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H b/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H new file mode 100644 index 0000000000..7cec1b4541 --- /dev/null +++ b/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H @@ -0,0 +1 @@ +#warning File removed - left for old dependency check only diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C index 535be31536..642f844f62 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C @@ -748,7 +748,7 @@ bool Foam::AMIInterpolation::calculate { srcPatchPts_ = srcPatch.points(); projectPointsToSurface(surfPtr(), srcPatchPts_); - tsrcPatch0_ = tmpNrc::New + tsrcPatch0_ = refPtr::New ( SubList(srcPatch), srcPatchPts_ @@ -756,7 +756,7 @@ bool Foam::AMIInterpolation::calculate tgtPatchPts_ = tgtPatch.points(); projectPointsToSurface(surfPtr(), tgtPatchPts_); - ttgtPatch0_ = tmpNrc::New + ttgtPatch0_ = refPtr::New ( SubList(tgtPatch), tgtPatchPts_ diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H index 8ce226bbb4..7a2259066e 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.H @@ -41,7 +41,6 @@ Description orientations (opposite normals). The 'reverseTarget' flag can be used to reverse the orientation of the target patch. - SourceFiles AMIInterpolation.C AMIInterpolationName.C @@ -60,6 +59,7 @@ SourceFiles #include "faceAreaIntersect.H" #include "globalIndex.H" #include "ops.H" +#include "refPtr.H" #include "Enum.H" #include "pointList.H" #include "indexedOctree.H" @@ -131,7 +131,7 @@ protected: pointField srcPatchPts_; //- Source patch using manipulated input points - tmpNrc tsrcPatch0_; + refPtr tsrcPatch0_; //- Source map pointer - parallel running only autoPtr srcMapPtr_; @@ -159,7 +159,7 @@ protected: pointField tgtPatchPts_; //- Target patch using manipulated input points - tmpNrc ttgtPatch0_; + refPtr ttgtPatch0_; //- Target map pointer - parallel running only autoPtr tgtMapPtr_;