ENH: add operator* to refPtr - makes it more like autoPtr

- would ideally like the same for tmp, but there is too much existing
  code that uses a multiply operation that would be interpreted as a
  dereference.
This commit is contained in:
Mark Olesen
2021-03-10 11:43:05 +01:00
committed by Andrew Heather
parent d15a396a6a
commit a76a3d760c
3 changed files with 60 additions and 23 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -63,6 +63,7 @@ int main()
auto tfld1 = refPtr<scalarField>::New(10, scalar(1));
printInfo(tfld1, true);
Info<< nl << "Dereferenced: " << *tfld1 << nl;
Info<< nl << "Construct from autoPtr" << nl;
refPtr<scalarField> tfld2(autoPtr<scalarField>::New(10, scalar(2)));

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -132,7 +132,7 @@ public:
//- Move construct, transferring ownership.
inline refPtr(refPtr<T>&& rhs) noexcept;
//- Copy construct
//- Copy construct (shallow copy)
inline refPtr(const refPtr<T>& rhs);
//- Copy/move construct. Optionally reusing pointer.
@ -227,12 +227,20 @@ public:
// Member Operators
//- Return const reference to the object.
// Fatal if nothing is managed
inline const T& operator*() const;
//- Return reference to the managed object.
// Fatal if nothing is managed or if the object is const.
inline T& operator*();
//- Dereferences (const) pointer to the managed object.
// Fatal for a null managed pointer.
// Fatal if nothing is managed.
inline const T* operator->() const;
//- Dereferences (non-const) pointer to the managed object.
// Fatal for a null managed pointer or if the object is const.
// Fatal if nothing is managed or if the object is const.
inline T* operator->();
//- Return const reference to the object - same as cref() method.

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -326,17 +326,48 @@ inline void Foam::refPtr<T>::swap(refPtr<T>& other) noexcept
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
inline const T& Foam::refPtr<T>::operator*() const
{
if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return *ptr_;
}
template<class T>
inline T& Foam::refPtr<T>::operator*()
{
if (type_ == CREF)
{
FatalErrorInFunction
<< "Attempt to cast const object to non-const for a "
<< this->typeName()
<< abort(FatalError);
}
else if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return *ptr_;
}
template<class T>
inline const T* Foam::refPtr<T>::operator->() const
{
if (type_ == PTR)
if (!ptr_)
{
if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return ptr_;
@ -346,22 +377,19 @@ inline const T* Foam::refPtr<T>::operator->() const
template<class T>
inline T* Foam::refPtr<T>::operator->()
{
if (type_ == PTR)
{
if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
}
else if (type_ == CREF)
if (type_ == CREF)
{
FatalErrorInFunction
<< "Attempt to cast const object to non-const for a "
<< this->typeName()
<< abort(FatalError);
}
else if (!ptr_)
{
FatalErrorInFunction
<< this->typeName() << " deallocated"
<< abort(FatalError);
}
return ptr_;
}