ENH: return const pointer instead of bool from PtrList::set(label)

- can still test as a bool or use to 'peek' at the content.
This commit is contained in:
Mark Olesen
2019-01-15 16:37:15 +01:00
parent 1ce8440cec
commit c6c30a0692
4 changed files with 23 additions and 21 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -150,21 +150,22 @@ public:
//- Transfer into this list and annul the argument list //- Transfer into this list and annul the argument list
inline void transfer(PtrList<T>& list); inline void transfer(PtrList<T>& list);
//- Return true if element is set (ie, not a nullptr) //- Return const pointer to element (if set) or nullptr.
inline bool set(const label i) const; // The return value can be tested as a bool.
inline const T* set(const label i) const;
//- Set element to given pointer and return old element (can be null) //- Set element to given pointer and return old element (can be null)
// No-op if the new pointer value is identical to the current content. // No-op if the new pointer value is identical to the current content.
inline autoPtr<T> set(label i, T* ptr); inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given autoPtr and return old element //- Set element to given autoPtr and return old element
inline autoPtr<T> set(label i, autoPtr<T>& aptr); inline autoPtr<T> set(const label i, autoPtr<T>& aptr);
//- Set element to given autoPtr and return old element //- Set element to given autoPtr and return old element
inline autoPtr<T> set(label i, autoPtr<T>&& aptr); inline autoPtr<T> set(const label i, autoPtr<T>&& aptr);
//- Set element to given tmp and return old element //- Set element to given tmp and return old element
inline autoPtr<T> set(label i, const tmp<T>& tptr); inline autoPtr<T> set(const label i, const tmp<T>& tptr);
// Member Operators // Member Operators

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -123,35 +123,35 @@ inline void Foam::PtrList<T>::append(const tmp<T>& tptr)
template<class T> template<class T>
inline bool Foam::PtrList<T>::set(const label i) const inline const T* Foam::PtrList<T>::set(const label i) const
{ {
return UPtrList<T>::set(i); return UPtrList<T>::set(i);
} }
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(label i, T* ptr) inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
{ {
return autoPtr<T>(UPtrList<T>::set(i, ptr)); return autoPtr<T>(UPtrList<T>::set(i, ptr));
} }
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(label i, autoPtr<T>& aptr) inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, autoPtr<T>& aptr)
{ {
return set(i, aptr.release()); return set(i, aptr.release());
} }
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(label i, autoPtr<T>&& aptr) inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, autoPtr<T>&& aptr)
{ {
return set(i, aptr.release()); return set(i, aptr.release());
} }
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(label i, const tmp<T>& tptr) inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, const tmp<T>& tptr)
{ {
return set(i, tptr.ptr()); return set(i, tptr.ptr());
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -177,8 +177,9 @@ public:
//- Transfer contents into this list and annul the argument //- Transfer contents into this list and annul the argument
inline void transfer(UPtrList<T>& list); inline void transfer(UPtrList<T>& list);
//- Return true if element is set (not a nullptr) //- Return const pointer to element (if set) or nullptr.
inline bool set(const label i) const; // The return value can be tested as a bool.
inline const T* set(const label i) const;
//- Set element to specified pointer and return the old list element, //- Set element to specified pointer and return the old list element,
//- which can be a nullptr. //- which can be a nullptr.

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -170,9 +170,9 @@ inline void Foam::UPtrList<T>::append(T* ptr)
template<class T> template<class T>
inline bool Foam::UPtrList<T>::set(const label i) const inline const T* Foam::UPtrList<T>::set(const label i) const
{ {
return ptrs_[i] != nullptr; return ptrs_[i];
} }
@ -199,7 +199,7 @@ inline const T& Foam::UPtrList<T>::operator[](const label i) const
if (!ptr) if (!ptr)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "cannot dereference nullptr at index " << i << "Cannot dereference nullptr at index " << i
<< " in range [0," << size() << ")" << " in range [0," << size() << ")"
<< abort(FatalError); << abort(FatalError);
} }
@ -216,7 +216,7 @@ inline T& Foam::UPtrList<T>::operator[](const label i)
if (!ptr) if (!ptr)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "cannot dereference nullptr at index " << i << "Cannot dereference nullptr at index " << i
<< " in range [0," << size() << ")" << " in range [0," << size() << ")"
<< abort(FatalError); << abort(FatalError);
} }