ENH: support writable reference for tmp (#1775)

- improves flexibility. Can tag a tmp as allowing non-const access to
  the reference and skip additional const_cast in following code. For
  example,

      tmp<volScalarField> tfld(nullptr);
      auto* ptr = getObjectPtr<volScalarField>("field");
      if (ptr)
      {
          tfld.ref(*ptr);
      }
      else
      {
          tfld.reset(volScalarField::New(...));
      }
      auto& fld = tfld.ref();

ENH: renamed tmpNrc to refPtr

- the name 'refPtr' (reference|pointer) should be easier to remember
  than tmpNrc (tmp, but non-ref-counted).

- provide tmpNrc typedef and header for code compatibility

NOTE

- in some places refPtr and tmp can be used instead of a
  std::reference_wrapper for handling external references.

  Unlike std::reference_wrapper, it can be default constructed
  (holding nothing), whereas reference_wrapper may need a dummy
  reference. However, the lifetime extension of references _may_ be
  better with reference_wrapper.
This commit is contained in:
Mark Olesen
2020-07-21 09:11:26 +02:00
parent 5acb5f3580
commit be058bec7d
14 changed files with 206 additions and 161 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,18 +27,19 @@ Class
Foam::PrecisionAdaptor Foam::PrecisionAdaptor
Description Description
Conversion adaptor for Field that either wraps as a tmp reference Conversion adaptor for Field/List that either wrap the input as a
or creates the necessary tmp and copies the values on construction reference, or creates a temporary pointer and copies the values
and destruction. on construction/destruction.
This provides automatic conversion between (scalar) types for use
with linear solvers able to run mixed precision. This provides, for example, automatic conversion between types
for linear solvers able to run mixed precision.
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef PrecisionAdaptor_H #ifndef PrecisionAdaptor_H
#define PrecisionAdaptor_H #define PrecisionAdaptor_H
#include "tmpNrc.H" #include "refPtr.H"
#include "Field.H" #include "Field.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -46,22 +47,19 @@ Description
namespace Foam namespace Foam
{ {
//- A const Field wrapper with possible data conversion //- A const Field/List wrapper with possible data conversion
template<class Type, class InputType, template<class> class Container = Field> template<class Type, class InputType, template<class> class Container = Field>
class ConstPrecisionAdaptor class ConstPrecisionAdaptor
: :
public tmpNrc<Container<Type>> public refPtr<Container<Type>>
{ {
// Private Member Functions // Private Member Functions
//- Copy in field //- Copy in field
void copyInput(const Container<InputType>& input) void copyInput(const Container<InputType>& input)
{ {
this->clear(); this->reset(new Container<Type>(input.size()));
std::copy(input.cbegin(), input.cend(), this->ref().begin());
Container<Type>* p = new Container<Type>(input.size());
this->reset(p);
std::copy(input.cbegin(), input.cend(), p->begin());
} }
//- Construct from tmp Field, copy/move as required //- Construct from tmp Field, copy/move as required
@ -71,19 +69,20 @@ class ConstPrecisionAdaptor
{ {
auto& tinput = reinterpret_cast<tmp<Container<Type>>&>(input); auto& tinput = reinterpret_cast<tmp<Container<Type>>&>(input);
if (tinput.isTmp()) if (tinput.is_pointer())
{ {
// Reset to tmp // Acquire control of the managed pointer
this->reset(tinput.ptr()); this->reset(tinput.ptr());
} }
else else
{ {
// Use const reference
this->cref(tinput.cref()); this->cref(tinput.cref());
} }
} }
else else
{ {
this->copyInput(input()); this->copyInput(input.cref());
} }
input.clear(); input.clear();
} }
@ -100,10 +99,11 @@ public:
//- Construct from Container<InputType>, copying on input as required //- Construct from Container<InputType>, copying on input as required
ConstPrecisionAdaptor(const Container<InputType>& input) ConstPrecisionAdaptor(const Container<InputType>& input)
: :
tmpNrc<Container<Type>>() refPtr<Container<Type>>()
{ {
if (std::is_same<Type, InputType>::value) if (std::is_same<Type, InputType>::value)
{ {
// Use const reference directly
this->cref(reinterpret_cast<const FieldType&>(input)); this->cref(reinterpret_cast<const FieldType&>(input));
} }
else else
@ -116,7 +116,7 @@ public:
//- Construct from tmp Container, copy/move as required //- Construct from tmp Container, copy/move as required
ConstPrecisionAdaptor(tmp<Container<InputType>>&& input) ConstPrecisionAdaptor(tmp<Container<InputType>>&& input)
: :
tmpNrc<Container<Type>>() refPtr<Container<Type>>()
{ {
this->moveInput(input); this->moveInput(input);
} }
@ -125,7 +125,7 @@ public:
//- Construct from tmp Container, copy/move as required //- Construct from tmp Container, copy/move as required
ConstPrecisionAdaptor(const tmp<Container<InputType>>& input) ConstPrecisionAdaptor(const tmp<Container<InputType>>& input)
: :
tmpNrc<Container<Type>>() refPtr<Container<Type>>()
{ {
this->moveInput(const_cast<tmp<Container<InputType>>&>(input)); this->moveInput(const_cast<tmp<Container<InputType>>&>(input));
} }
@ -133,6 +133,8 @@ public:
// Member Functions // Member Functions
// May need in the future: using refPtr<Container<Type>>::get;
//- Return the field //- Return the field
static const Container<Type>& get static const Container<Type>& 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 Type, class InputType, template<class> class Container = Field> template<class Type, class InputType, template<class> class Container = Field>
class PrecisionAdaptor class PrecisionAdaptor
: :
public tmpNrc<Container<Type>> public refPtr<Container<Type>>
{ {
// Private Data // Private Data
//- Reference to underlying field //- Reference to underlying (input) data
Container<InputType>& ref_; Container<InputType>& ref_;
@ -171,11 +173,10 @@ class PrecisionAdaptor
//- Copy in field //- Copy in field
void copyInput(const Container<InputType>& input, const bool copy) void copyInput(const Container<InputType>& input, const bool copy)
{ {
Container<Type>* p = new Container<Type>(input.size()); this->reset(new Container<Type>(input.size()));
this->reset(p);
if (copy) 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<InputType>, copying on input if required //- Construct from Container<InputType>, copying on input if required
PrecisionAdaptor(Container<InputType>& input, const bool copy = true) PrecisionAdaptor(Container<InputType>& input, const bool copy = true)
: :
tmpNrc<Container<Type>>(), refPtr<Container<Type>>(),
ref_(input) ref_(input)
{ {
if (std::is_same<Type, InputType>::value) if (std::is_same<Type, InputType>::value)
{ {
this->cref(reinterpret_cast<const FieldType&>(input)); // Use non-const reference directly
this->ref(reinterpret_cast<FieldType&>(ref_));
} }
else else
{ {
@ -205,25 +207,16 @@ public:
} }
//- Destructor, copying on destroy //- Destructor, copy back on destroy
~PrecisionAdaptor() ~PrecisionAdaptor()
{ {
if (this->isTmp()) if (this->is_pointer())
{ {
const FieldType& store = this->cref(); const FieldType& store = this->cref();
ref_.resize(store.size()); ref_.resize(store.size()); // extra safety
std::copy(store.cbegin(), store.cend(), ref_.begin()); std::copy(store.cbegin(), store.cend(), ref_.begin());
} }
} }
// Member Functions
//- Allow modification without const-ref check
FieldType& ref()
{
return this->constCast();
}
}; };

View File

@ -233,7 +233,7 @@ bool Foam::fileOperation::isFileOrDir(const bool isFile, const fileName& f)
} }
Foam::tmpNrc<Foam::fileOperation::dirIndexList> Foam::refPtr<Foam::fileOperation::dirIndexList>
Foam::fileOperation::lookupAndCacheProcessorsPath Foam::fileOperation::lookupAndCacheProcessorsPath
( (
const fileName& fName, const fileName& fName,
@ -361,11 +361,12 @@ Foam::fileOperation::lookupAndCacheProcessorsPath
return procsDirs_[procPath]; return procsDirs_[procPath];
} }
} }
return tmpNrc<dirIndexList>(new dirIndexList(0, dirIndex()));
return refPtr<dirIndexList>::New();
} }
Foam::tmpNrc<Foam::fileOperation::dirIndexList> Foam::refPtr<Foam::fileOperation::dirIndexList>
Foam::fileOperation::lookupProcessorsPath(const fileName& fName) const Foam::fileOperation::lookupProcessorsPath(const fileName& fName) const
{ {
// Use parallel synchronisation // Use parallel synchronisation
@ -531,7 +532,7 @@ Foam::fileName Foam::fileOperation::filePath(const fileName& fName) const
if (proci != -1) if (proci != -1)
{ {
// Get all processor directories // Get all processor directories
tmpNrc<dirIndexList> procDirs(lookupProcessorsPath(fName)); refPtr<dirIndexList> procDirs(lookupProcessorsPath(fName));
forAll(procDirs(), i) forAll(procDirs(), i)
{ {
const fileName& procDir = procDirs()[i].first(); const fileName& procDir = procDirs()[i].first();
@ -688,7 +689,7 @@ Foam::instantList Foam::fileOperation::findTimes
// Get all processor directories // Get all processor directories
tmpNrc<dirIndexList> procDirs(lookupProcessorsPath(directory)); refPtr<dirIndexList> procDirs(lookupProcessorsPath(directory));
forAll(procDirs(), i) forAll(procDirs(), i)
{ {
const fileName& procDir = procDirs()[i].first(); const fileName& procDir = procDirs()[i].first();

View File

@ -49,7 +49,7 @@ Description
#include "fileMonitor.H" #include "fileMonitor.H"
#include "labelList.H" #include "labelList.H"
#include "Switch.H" #include "Switch.H"
#include "tmpNrc.H" #include "refPtr.H"
#include "Enum.H" #include "Enum.H"
#include "Tuple2.H" #include "Tuple2.H"
@ -135,17 +135,18 @@ protected:
//- Helper: check for file (isFile) or directory (!isFile) //- Helper: check for file (isFile) or directory (!isFile)
static bool isFileOrDir(const bool isFile, const fileName&); static bool isFileOrDir(const bool isFile, const fileName&);
//- Lookup name of processorsDDD using cache. Return empty fileName //- Lookup name of processorsDDD using cache.
// if not found. // \return empty fileName if not found.
tmpNrc<dirIndexList> lookupAndCacheProcessorsPath refPtr<dirIndexList> lookupAndCacheProcessorsPath
( (
const fileName&, const fileName&,
const bool syncPar const bool syncPar
) const; ) const;
//- Lookup name of processorsDDD using cache. Return empty fileName //- Lookup name of processorsDDD using cache.
// if not found. To be called on all processors // \note To be called on all processors
virtual tmpNrc<dirIndexList> lookupProcessorsPath // \return empty fileName if not found.
virtual refPtr<dirIndexList> lookupProcessorsPath
( (
const fileName& const fileName&
) const; ) const;

View File

@ -199,7 +199,7 @@ Foam::fileOperations::masterUncollatedFileOperation::filePathInfo
// 2. Check processors/ // 2. Check processors/
if (io.time().processorCase()) if (io.time().processorCase())
{ {
tmpNrc<dirIndexList> pDirs(lookupProcessorsPath(io.objectPath())); refPtr<dirIndexList> pDirs(lookupProcessorsPath(io.objectPath()));
forAll(pDirs(), i) forAll(pDirs(), i)
{ {
const fileName& pDir = pDirs()[i].first(); const fileName& pDir = pDirs()[i].first();
@ -268,7 +268,7 @@ Foam::fileOperations::masterUncollatedFileOperation::filePathInfo
if (newInstancePath.size() && newInstancePath != io.instance()) if (newInstancePath.size() && newInstancePath != io.instance())
{ {
// 1. Try processors equivalent // 1. Try processors equivalent
tmpNrc<dirIndexList> pDirs refPtr<dirIndexList> pDirs
( (
lookupProcessorsPath(io.objectPath()) lookupProcessorsPath(io.objectPath())
); );
@ -1499,7 +1499,7 @@ Foam::fileOperations::masterUncollatedFileOperation::findInstance
// parent directory in case of parallel) // parent directory in case of parallel)
tmpNrc<dirIndexList> pDirs(lookupProcessorsPath(io.objectPath())); refPtr<dirIndexList> pDirs(lookupProcessorsPath(io.objectPath()));
word foundInstance; word foundInstance;

View File

@ -114,7 +114,7 @@ Foam::fileName Foam::fileOperations::uncollatedFileOperation::filePathInfo
// Check if parallel "procesors" directory // Check if parallel "procesors" directory
if (io.time().processorCase()) if (io.time().processorCase())
{ {
tmpNrc<dirIndexList> pDirs refPtr<dirIndexList> pDirs
( (
fileOperation::lookupAndCacheProcessorsPath fileOperation::lookupAndCacheProcessorsPath
( (
@ -166,7 +166,7 @@ Foam::fileName Foam::fileOperations::uncollatedFileOperation::filePathInfo
} }
Foam::tmpNrc<Foam::fileOperation::dirIndexList> Foam::refPtr<Foam::fileOperation::dirIndexList>
Foam::fileOperations::uncollatedFileOperation::lookupProcessorsPath Foam::fileOperations::uncollatedFileOperation::lookupProcessorsPath
( (
const fileName& fName const fileName& fName

View File

@ -68,9 +68,10 @@ protected:
const bool search const bool search
) const; ) const;
//- Lookup name of processorsDDD using cache. Return empty fileName //- Lookup name of processorsDDD using cache.
// if not found. Do not use any parallel synchronisation // \not Do not use any parallel synchronisation
virtual tmpNrc<dirIndexList> lookupProcessorsPath // \return empty fileName if not found.
virtual refPtr<dirIndexList> lookupProcessorsPath
( (
const fileName& const fileName&
) const; ) const;

View File

@ -25,13 +25,13 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class Class
Foam::tmpNrc Foam::refPtr
Description Description
A class for managing temporary objects without reference counting. A class for managing references or pointers (no reference counting)
SourceFiles SourceFiles
tmpNrcI.H refPtrI.H
See also See also
Foam::autoPtr Foam::autoPtr
@ -39,8 +39,8 @@ See also
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef tmpNrc_H #ifndef refPtr_H
#define tmpNrc_H #define refPtr_H
#include "tmp.H" #include "tmp.H"
@ -50,11 +50,11 @@ namespace Foam
{ {
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class tmpNrc Declaration Class refPtr Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class T> template<class T>
class tmpNrc class refPtr
{ {
// Private Data // Private Data
@ -62,7 +62,8 @@ class tmpNrc
enum refType enum refType
{ {
PTR, //!< Managing a pointer (not ref-counted) 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) //- The managed pointer or address of the object (reference)
@ -76,10 +77,10 @@ public:
// STL type definitions // STL type definitions
//- Type of object being managed //- Type of object being managed or referenced
typedef T element_type; typedef T element_type;
//- Pointer to type of object being managed //- Pointer to type of object being managed or referenced
typedef T* pointer; typedef T* pointer;
@ -89,50 +90,50 @@ public:
// Factory Methods // 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 // \param args list of arguments with which an instance of T
// will be constructed. // will be constructed.
// //
// \note Similar to std::make_shared, but the overload for // \note Similar to std::make_shared, but the overload for
// array types is not disabled. // array types is not disabled.
template<class... Args> template<class... Args>
inline static tmpNrc<T> New(Args&&... args); inline static refPtr<T> 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 // \param args list of arguments with which an instance of U
// will be constructed. // will be constructed.
// //
// \note Similar to New but for derived types // \note Similar to New but for derived types
template<class U, class... Args> template<class U, class... Args>
inline static tmpNrc<T> NewFrom(Args&&... args); inline static refPtr<T> NewFrom(Args&&... args);
// Constructors // Constructors
//- Default construct, no managed pointer. //- Default construct, no managed pointer.
inline constexpr tmpNrc() noexcept; inline constexpr refPtr() noexcept;
//- Construct with no managed pointer. //- 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. //- 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. //- Construct for a const reference to an object.
inline tmpNrc(const T& obj) noexcept; inline refPtr(const T& obj) noexcept;
//- Move construct, transferring ownership. //- Move construct, transferring ownership.
inline tmpNrc(tmpNrc<T>&& t) noexcept; inline refPtr(refPtr<T>&& t) noexcept;
//- Copy construct //- Copy construct
inline tmpNrc(const tmpNrc<T>& t); inline refPtr(const refPtr<T>& t);
//- Copy construct. Optionally reusing pointer. //- Copy construct. Optionally reusing pointer.
inline tmpNrc(const tmpNrc<T>& t, bool reuse); inline refPtr(const refPtr<T>& t, bool reuse);
//- Destructor: deletes managed pointer //- Destructor: deletes managed pointer
inline ~tmpNrc(); inline ~refPtr();
// Member Functions // Member Functions
@ -148,6 +149,9 @@ public:
//- True for non-null pointer/reference //- True for non-null pointer/reference
bool valid() const noexcept { return ptr_; } 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) //- True if this is a managed pointer (not a reference)
bool isTmp() const noexcept { return type_ == PTR; } bool isTmp() const noexcept { return type_ == PTR; }
@ -166,8 +170,8 @@ public:
//- Return const pointer without nullptr checking. //- Return const pointer without nullptr checking.
const T* get() const noexcept { return ptr_; } const T* get() const noexcept { return ptr_; }
//- Return the const object reference or a const reference to the //- Return const reference to the object or to the contents
//- contents of a non-null managed pointer. //- of a (non-null) managed pointer.
// Fatal for a null managed pointer // Fatal for a null managed pointer
inline const T& cref() const; inline const T& cref() const;
@ -185,8 +189,7 @@ public:
// Edit // Edit
//- Return managed pointer for reuse, or clone() the const object //- Return managed pointer for reuse, or clone() the object reference.
//- reference.
inline T* ptr() const; inline T* ptr() const;
//- If object pointer points to valid object: //- If object pointer points to valid object:
@ -197,19 +200,21 @@ public:
inline void reset(T* p = nullptr) 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(refPtr<T>&& 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; 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. //- Swaps the managed object with other.
inline void swap(tmpNrc<T>& other) noexcept; inline void swap(refPtr<T>& other) noexcept;
// Member Operators // Member Operators
//- Return const reference to the object. //- Identical to cref() method.
// Identical to cref() method.
inline const T& operator()() const; inline const T& operator()() const;
//- Cast to underlying data type, using the cref() method. //- Cast to underlying data type, using the cref() method.
@ -228,10 +233,10 @@ public:
//- 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.
inline void operator=(const tmpNrc<T>& t); inline void operator=(const refPtr<T>& t);
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void operator=(tmpNrc<T>&& other) noexcept; inline void operator=(refPtr<T>&& other) noexcept;
//- Take ownership of the pointer. //- Take ownership of the pointer.
// Fatal for a null pointer // Fatal for a null pointer
@ -247,10 +252,10 @@ public:
// Global Functions // 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) // Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs)
template<class T> template<class T>
void Swap(tmpNrc<T>& lhs, tmpNrc<T>& rhs) void Swap(refPtr<T>& lhs, refPtr<T>& rhs)
{ {
lhs.swap(rhs); lhs.swap(rhs);
} }
@ -262,7 +267,7 @@ void Swap(tmpNrc<T>& lhs, tmpNrc<T>& rhs)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "tmpNrcI.H" #include "refPtrI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -33,24 +33,24 @@ License
template<class T> template<class T>
template<class... Args> template<class... Args>
inline Foam::tmpNrc<T> Foam::tmpNrc<T>::New(Args&&... args) inline Foam::refPtr<T> Foam::refPtr<T>::New(Args&&... args)
{ {
return tmpNrc<T>(new T(std::forward<Args>(args)...)); return refPtr<T>(new T(std::forward<Args>(args)...));
} }
template<class T> template<class T>
template<class U, class... Args> template<class U, class... Args>
inline Foam::tmpNrc<T> Foam::tmpNrc<T>::NewFrom(Args&&... args) inline Foam::refPtr<T> Foam::refPtr<T>::NewFrom(Args&&... args)
{ {
return tmpNrc<T>(new U(std::forward<Args>(args)...)); return refPtr<T>(new U(std::forward<Args>(args)...));
} }
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T> template<class T>
inline constexpr Foam::tmpNrc<T>::tmpNrc() noexcept inline constexpr Foam::refPtr<T>::refPtr() noexcept
: :
ptr_(nullptr), ptr_(nullptr),
type_(PTR) type_(PTR)
@ -58,7 +58,7 @@ inline constexpr Foam::tmpNrc<T>::tmpNrc() noexcept
template<class T> template<class T>
inline constexpr Foam::tmpNrc<T>::tmpNrc(std::nullptr_t) noexcept inline constexpr Foam::refPtr<T>::refPtr(std::nullptr_t) noexcept
: :
ptr_(nullptr), ptr_(nullptr),
type_(PTR) type_(PTR)
@ -66,7 +66,7 @@ inline constexpr Foam::tmpNrc<T>::tmpNrc(std::nullptr_t) noexcept
template<class T> template<class T>
inline Foam::tmpNrc<T>::tmpNrc(T* p) noexcept inline Foam::refPtr<T>::refPtr(T* p) noexcept
: :
ptr_(p), ptr_(p),
type_(PTR) type_(PTR)
@ -74,7 +74,7 @@ inline Foam::tmpNrc<T>::tmpNrc(T* p) noexcept
template<class T> template<class T>
inline Foam::tmpNrc<T>::tmpNrc(const T& obj) noexcept inline Foam::refPtr<T>::refPtr(const T& obj) noexcept
: :
ptr_(const_cast<T*>(&obj)), ptr_(const_cast<T*>(&obj)),
type_(CREF) type_(CREF)
@ -82,7 +82,7 @@ inline Foam::tmpNrc<T>::tmpNrc(const T& obj) noexcept
template<class T> template<class T>
inline Foam::tmpNrc<T>::tmpNrc(tmpNrc<T>&& t) noexcept inline Foam::refPtr<T>::refPtr(refPtr<T>&& t) noexcept
: :
ptr_(t.ptr_), ptr_(t.ptr_),
type_(t.type_) type_(t.type_)
@ -93,7 +93,7 @@ inline Foam::tmpNrc<T>::tmpNrc(tmpNrc<T>&& t) noexcept
template<class T> template<class T>
inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t) inline Foam::refPtr<T>::refPtr(const refPtr<T>& t)
: :
ptr_(t.ptr_), ptr_(t.ptr_),
type_(t.type_) type_(t.type_)
@ -115,7 +115,7 @@ inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t)
template<class T> template<class T>
inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t, bool reuse) inline Foam::refPtr<T>::refPtr(const refPtr<T>& t, bool reuse)
: :
ptr_(t.ptr_), ptr_(t.ptr_),
type_(t.type_) type_(t.type_)
@ -144,7 +144,7 @@ inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t, bool reuse)
template<class T> template<class T>
inline Foam::tmpNrc<T>::~tmpNrc() inline Foam::refPtr<T>::~refPtr()
{ {
clear(); clear();
} }
@ -153,21 +153,21 @@ inline Foam::tmpNrc<T>::~tmpNrc()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T> template<class T>
inline bool Foam::tmpNrc<T>::movable() const noexcept inline bool Foam::refPtr<T>::movable() const noexcept
{ {
return (type_ == PTR && ptr_); return (type_ == PTR && ptr_);
} }
template<class T> template<class T>
inline Foam::word Foam::tmpNrc<T>::typeName() const inline Foam::word Foam::refPtr<T>::typeName() const
{ {
return "tmpNrc<" + word(typeid(T).name()) + '>'; return "refPtr<" + word(typeid(T).name()) + '>';
} }
template<class T> template<class T>
inline const T& Foam::tmpNrc<T>::cref() const inline const T& Foam::refPtr<T>::cref() const
{ {
if (isTmp()) if (isTmp())
{ {
@ -184,7 +184,7 @@ inline const T& Foam::tmpNrc<T>::cref() const
template<class T> template<class T>
inline T& Foam::tmpNrc<T>::ref() const inline T& Foam::refPtr<T>::ref() const
{ {
if (isTmp()) if (isTmp())
{ {
@ -195,7 +195,7 @@ inline T& Foam::tmpNrc<T>::ref() const
<< abort(FatalError); << abort(FatalError);
} }
} }
else // if (type_ == CREF) else if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempted non-const reference to const object from a " << "Attempted non-const reference to const object from a "
@ -208,21 +208,14 @@ inline T& Foam::tmpNrc<T>::ref() const
template<class T> template<class T>
inline T& Foam::tmpNrc<T>::constCast() const inline T& Foam::refPtr<T>::constCast() const
{ {
if (isTmp() && !ptr_) return const_cast<T&>(cref());
{
FatalErrorInFunction
<< typeName() << " deallocated"
<< abort(FatalError);
}
return const_cast<T&>(*ptr_);
} }
template<class T> template<class T>
inline T* Foam::tmpNrc<T>::ptr() const inline T* Foam::refPtr<T>::ptr() const
{ {
if (!ptr_) if (!ptr_)
{ {
@ -244,7 +237,7 @@ inline T* Foam::tmpNrc<T>::ptr() const
template<class T> template<class T>
inline void Foam::tmpNrc<T>::clear() const noexcept inline void Foam::refPtr<T>::clear() const noexcept
{ {
if (isTmp() && ptr_) if (isTmp() && ptr_)
{ {
@ -255,7 +248,7 @@ inline void Foam::tmpNrc<T>::clear() const noexcept
template<class T> template<class T>
inline void Foam::tmpNrc<T>::reset(T* p) noexcept inline void Foam::refPtr<T>::reset(T* p) noexcept
{ {
clear(); clear();
ptr_ = p; ptr_ = p;
@ -264,7 +257,7 @@ inline void Foam::tmpNrc<T>::reset(T* p) noexcept
template<class T> template<class T>
inline void Foam::tmpNrc<T>::reset(tmpNrc<T>&& other) noexcept inline void Foam::refPtr<T>::reset(refPtr<T>&& other) noexcept
{ {
if (&other == this) if (&other == this)
{ {
@ -281,7 +274,7 @@ inline void Foam::tmpNrc<T>::reset(tmpNrc<T>&& other) noexcept
template<class T> template<class T>
inline void Foam::tmpNrc<T>::cref(const T& obj) noexcept inline void Foam::refPtr<T>::cref(const T& obj) noexcept
{ {
clear(); clear();
ptr_ = const_cast<T*>(&obj); ptr_ = const_cast<T*>(&obj);
@ -290,7 +283,16 @@ inline void Foam::tmpNrc<T>::cref(const T& obj) noexcept
template<class T> template<class T>
inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept inline void Foam::refPtr<T>::ref(T& obj) noexcept
{
clear();
ptr_ = &obj;
type_ = REF;
}
template<class T>
inline void Foam::refPtr<T>::swap(refPtr<T>& other) noexcept
{ {
if (&other == this) if (&other == this)
{ {
@ -311,21 +313,21 @@ inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T> template<class T>
inline const T& Foam::tmpNrc<T>::operator()() const inline const T& Foam::refPtr<T>::operator()() const
{ {
return cref(); return cref();
} }
template<class T> template<class T>
inline Foam::tmpNrc<T>::operator const T&() const inline Foam::refPtr<T>::operator const T&() const
{ {
return cref(); return cref();
} }
template<class T> template<class T>
inline const T* Foam::tmpNrc<T>::operator->() const inline const T* Foam::refPtr<T>::operator->() const
{ {
if (!ptr_ && isTmp()) if (!ptr_ && isTmp())
{ {
@ -339,7 +341,7 @@ inline const T* Foam::tmpNrc<T>::operator->() const
template<class T> template<class T>
inline T* Foam::tmpNrc<T>::operator->() inline T* Foam::refPtr<T>::operator->()
{ {
if (isTmp()) if (isTmp())
{ {
@ -362,7 +364,7 @@ inline T* Foam::tmpNrc<T>::operator->()
template<class T> template<class T>
inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>& t) inline void Foam::refPtr<T>::operator=(const refPtr<T>& t)
{ {
if (&t == this) if (&t == this)
{ {
@ -395,7 +397,7 @@ inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>& t)
template<class T> template<class T>
inline void Foam::tmpNrc<T>::operator=(tmpNrc<T>&& other) noexcept inline void Foam::refPtr<T>::operator=(refPtr<T>&& other) noexcept
{ {
if (&other == this) if (&other == this)
{ {
@ -412,7 +414,7 @@ inline void Foam::tmpNrc<T>::operator=(tmpNrc<T>&& other) noexcept
template<class T> template<class T>
inline void Foam::tmpNrc<T>::operator=(T* p) inline void Foam::refPtr<T>::operator=(T* p)
{ {
if (!p) if (!p)
{ {
@ -426,14 +428,14 @@ inline void Foam::tmpNrc<T>::operator=(T* p)
template<class T> template<class T>
inline void Foam::tmpNrc<T>::operator=(std::nullptr_t) noexcept inline void Foam::refPtr<T>::operator=(std::nullptr_t) noexcept
{ {
reset(nullptr); reset(nullptr);
} }
template<class T> template<class T>
inline Foam::tmpNrc<T>::operator tmp<T>() inline Foam::refPtr<T>::operator tmp<T>()
{ {
if (isTmp()) if (isTmp())
{ {

View File

@ -40,6 +40,7 @@ SourceFiles
See also See also
Foam::autoPtr Foam::autoPtr
Foam::refPtr
Foam::refCount Foam::refCount
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -69,7 +70,8 @@ class tmp
enum refType enum refType
{ {
PTR, //!< Managing a pointer (ref-counted) 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) //- The managed pointer or address of the object (reference)
@ -169,6 +171,9 @@ public:
//- True for non-null pointer/reference //- True for non-null pointer/reference
bool valid() const noexcept { return ptr_; } 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) //- True if this is a managed pointer (not a reference)
bool isTmp() const noexcept { return type_ == PTR; } bool isTmp() const noexcept { return type_ == PTR; }
@ -187,8 +192,8 @@ public:
//- Return const pointer without nullptr checking. //- Return const pointer without nullptr checking.
const T* get() const noexcept { return ptr_; } const T* get() const noexcept { return ptr_; }
//- Return the const object reference or a const reference to the //- Return const reference to the object or to the contents
//- contents of a non-null managed pointer. //- of a (non-null) managed pointer.
// Fatal for a null managed pointer // Fatal for a null managed pointer
inline const T& cref() const; inline const T& cref() const;
@ -200,14 +205,13 @@ public:
//- Non-const dereference, even if the object is const. //- Non-const dereference, even if the object is const.
// This is similar to ref(), but applies a const_cast to access // This is similar to ref(), but applies a const_cast to access
// const objects. // const objects.
// Fatal for a null managed pointer. // Fatal for a null pointer.
inline T& constCast() const; inline T& constCast() const;
// Edit // Edit
//- Return managed pointer for reuse, or clone() the const object //- Return managed pointer for reuse, or clone() the object reference.
//- reference.
inline T* ptr() const; inline T* ptr() const;
//- If object pointer points to valid object: //- If object pointer points to valid object:
@ -220,17 +224,19 @@ public:
//- Clear existing and transfer ownership. //- Clear existing and transfer ownership.
inline void reset(tmp<T>&& other) noexcept; inline void reset(tmp<T>&& 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; 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. //- Swaps the managed object with other.
inline void swap(tmp<T>& other) noexcept; inline void swap(tmp<T>& other) noexcept;
// Member Operators // Member Operators
//- Return const reference to the object. //- Identical to cref() method.
// Identical to cref() method.
inline const T& operator()() const; inline const T& operator()() const;
//- Cast to underlying data type, using the cref() method. //- Cast to underlying data type, using the cref() method.

View File

@ -231,7 +231,7 @@ inline T& Foam::tmp<T>::ref() const
<< abort(FatalError); << abort(FatalError);
} }
} }
else // if (type_ == CREF) else if (type_ == CREF)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Attempted non-const reference to const object from a " << "Attempted non-const reference to const object from a "
@ -246,14 +246,7 @@ inline T& Foam::tmp<T>::ref() const
template<class T> template<class T>
inline T& Foam::tmp<T>::constCast() const inline T& Foam::tmp<T>::constCast() const
{ {
if (isTmp() && !ptr_) return const_cast<T&>(cref());
{
FatalErrorInFunction
<< typeName() << " deallocated"
<< abort(FatalError);
}
return const_cast<T&>(*ptr_);
} }
@ -340,6 +333,15 @@ inline void Foam::tmp<T>::cref(const T& obj) noexcept
} }
template<class T>
inline void Foam::tmp<T>::ref(T& obj) noexcept
{
clear();
ptr_ = &obj;
type_ = REF;
}
template<class T> template<class T>
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
{ {

View File

@ -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;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1 @@
#warning File removed - left for old dependency check only

View File

@ -748,7 +748,7 @@ bool Foam::AMIInterpolation::calculate
{ {
srcPatchPts_ = srcPatch.points(); srcPatchPts_ = srcPatch.points();
projectPointsToSurface(surfPtr(), srcPatchPts_); projectPointsToSurface(surfPtr(), srcPatchPts_);
tsrcPatch0_ = tmpNrc<primitivePatch>::New tsrcPatch0_ = refPtr<primitivePatch>::New
( (
SubList<face>(srcPatch), SubList<face>(srcPatch),
srcPatchPts_ srcPatchPts_
@ -756,7 +756,7 @@ bool Foam::AMIInterpolation::calculate
tgtPatchPts_ = tgtPatch.points(); tgtPatchPts_ = tgtPatch.points();
projectPointsToSurface(surfPtr(), tgtPatchPts_); projectPointsToSurface(surfPtr(), tgtPatchPts_);
ttgtPatch0_ = tmpNrc<primitivePatch>::New ttgtPatch0_ = refPtr<primitivePatch>::New
( (
SubList<face>(tgtPatch), SubList<face>(tgtPatch),
tgtPatchPts_ tgtPatchPts_

View File

@ -41,7 +41,6 @@ Description
orientations (opposite normals). The 'reverseTarget' flag can be used to orientations (opposite normals). The 'reverseTarget' flag can be used to
reverse the orientation of the target patch. reverse the orientation of the target patch.
SourceFiles SourceFiles
AMIInterpolation.C AMIInterpolation.C
AMIInterpolationName.C AMIInterpolationName.C
@ -60,6 +59,7 @@ SourceFiles
#include "faceAreaIntersect.H" #include "faceAreaIntersect.H"
#include "globalIndex.H" #include "globalIndex.H"
#include "ops.H" #include "ops.H"
#include "refPtr.H"
#include "Enum.H" #include "Enum.H"
#include "pointList.H" #include "pointList.H"
#include "indexedOctree.H" #include "indexedOctree.H"
@ -131,7 +131,7 @@ protected:
pointField srcPatchPts_; pointField srcPatchPts_;
//- Source patch using manipulated input points //- Source patch using manipulated input points
tmpNrc<primitivePatch> tsrcPatch0_; refPtr<primitivePatch> tsrcPatch0_;
//- Source map pointer - parallel running only //- Source map pointer - parallel running only
autoPtr<mapDistribute> srcMapPtr_; autoPtr<mapDistribute> srcMapPtr_;
@ -159,7 +159,7 @@ protected:
pointField tgtPatchPts_; pointField tgtPatchPts_;
//- Target patch using manipulated input points //- Target patch using manipulated input points
tmpNrc<primitivePatch> ttgtPatch0_; refPtr<primitivePatch> ttgtPatch0_;
//- Target map pointer - parallel running only //- Target map pointer - parallel running only
autoPtr<mapDistribute> tgtMapPtr_; autoPtr<mapDistribute> tgtMapPtr_;