mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
tmp: Updated to store and preserve the const-ness of the reference to a constant object
This change requires that the de-reference operator '()' returns a const-reference to the object stored irrespective of the const-ness of object stored and the new member function 'ref()' is provided to return an non-const reference to stored object which throws a fatal error if the stored object is const. In order to smooth the transition to this new safer 'tmp' the now deprecated and unsafe non-const de-reference operator '()' is still provided by default but may be switched-off with the compilation switch 'CONST_TMP'. The main OpenFOAM library has already been upgraded and '-DCONST_TMP' option specified in the 'options' file to switch to the new 'tmp' behavior. The rest of OpenFOAM-dev will be upgraded over the following few weeks. Henry G. Weller CFD Direct
This commit is contained in:
@ -52,15 +52,20 @@ class tmp
|
||||
{
|
||||
// Private data
|
||||
|
||||
//- Flag for whether object is a temporary or a constant object
|
||||
bool isTmp_;
|
||||
//- Object types
|
||||
enum type
|
||||
{
|
||||
TMP,
|
||||
REF,
|
||||
CONST_REF
|
||||
};
|
||||
|
||||
//- Pointer to temporary object
|
||||
//- Type of object
|
||||
type type_;
|
||||
|
||||
//- Pointer to object
|
||||
mutable T* ptr_;
|
||||
|
||||
//- Const reference to constant object
|
||||
const T& ref_;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
@ -69,9 +74,15 @@ public:
|
||||
//- Store object pointer
|
||||
inline explicit tmp(T* = 0);
|
||||
|
||||
//- Store object rvalue reference
|
||||
inline tmp(T&&);
|
||||
|
||||
//- Store object const reference
|
||||
inline tmp(const T&);
|
||||
|
||||
//- Construct copy and increment reference count
|
||||
inline tmp(tmp<T>&&);
|
||||
|
||||
//- Construct copy and increment reference count
|
||||
inline tmp(const tmp<T>&);
|
||||
|
||||
@ -100,6 +111,10 @@ public:
|
||||
|
||||
// Edit
|
||||
|
||||
//- Return non-const reference or generate a fatal error
|
||||
// if the object is const.
|
||||
inline T& ref();
|
||||
|
||||
//- Return tmp pointer for reuse.
|
||||
// Returns a clone if the object is not a temporary
|
||||
inline T* ptr() const;
|
||||
@ -111,8 +126,11 @@ public:
|
||||
|
||||
// Member operators
|
||||
|
||||
//- Dereference operator
|
||||
#ifndef CONST_TMP
|
||||
//- Deprecated non-const dereference operator.
|
||||
// Use ref() where non-const access is required
|
||||
inline T& operator()();
|
||||
#endif
|
||||
|
||||
//- Const dereference operator
|
||||
inline const T& operator()() const;
|
||||
|
||||
Reference in New Issue
Block a user