ENH: prefer PtrList set/get/test instead of PtrList::operator() access

- clearer coding intent. Mark operator() as 'deprecated'

- add bounds checking to get(label) and set(label) methods.

  This gives failsafe behaviour for get() that is symmetric with
  HashPtrTable, autoPtr etc and aligns the set(label) methods
  for UPtrList, PtrList and PtrDynList.

- use top-level PtrList::clone() instead of cloning individual elements

ENH: support HashPtrTable set with refPtr/tmp (flexibility)
This commit is contained in:
Mark Olesen
2022-09-12 12:50:10 +02:00
parent b9ca63b118
commit a0282c7e41
18 changed files with 239 additions and 207 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd. Copyright (C) 2017-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -31,7 +31,10 @@ Description
#include <memory> #include <memory>
#include <iostream> #include <iostream>
#include "autoPtr.H" #include "autoPtr.H"
#include "HashPtrTable.H" #include "refPtr.H"
#include "tmp.H"
#include "PtrMap.H"
#include "primitiveFields.H"
using namespace Foam; using namespace Foam;
@ -250,6 +253,42 @@ int main()
Info<< "Table: " << tbl << nl; Info<< "Table: " << tbl << nl;
} }
{
PtrMap<scalarField> fields;
{
const label patchi = 2;
scalarField fld1(patchi, 5.0);
scalarField fld2(patchi, 8.0);
// assign from tmp<>
fields.set( patchi, (fld1 * fld2));
}
{
const label patchi = 3;
scalarField fld1(patchi, 6.0);
// From tmp (clone)
fields.set(patchi, tmp<scalarField>(fld1));
}
{
const label patchi = 4;
// From refPtr
fields.set(patchi, refPtr<scalarField>::New(patchi, 10.0));
}
Info<< nl
<< "PtrMap:" << nl
<< fields << endl;
}
Info<< "\nEnd" << nl;
return 0; return 0;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd. Copyright (C) 2017-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -38,8 +38,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef HashPtrTable_H #ifndef Foam_HashPtrTable_H
#define HashPtrTable_H #define Foam_HashPtrTable_H
#include "HashTable.H" #include "HashTable.H"
#include <memory> #include <memory>
@ -50,8 +50,9 @@ namespace Foam
{ {
// Forward Declarations // Forward Declarations
template<class T> class autoPtr; template<class T> class autoPtr;
template<class T> class refPtr;
template<class T> class tmp;
template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> class HashPtrTable;
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
@ -123,8 +124,12 @@ public:
// Access // Access
//- Return const pointer associated with given entry, //- Return const pointer associated with given entry or a nullptr
//- returning a nullptr if the key does not exist in the table. //- if the key does not exist in the table.
inline const T* test(const Key& key) const;
//- Return const pointer associated with given entry or a nullptr
//- if the key does not exist in the table.
inline const T* get(const Key& key) const; inline const T* get(const Key& key) const;
@ -211,31 +216,43 @@ public:
inline bool insert(const Key&, T*) = delete; inline bool insert(const Key&, T*) = delete;
//- Insert a new entry, not overwriting existing entries. //- Insert a new entry, not overwriting existing entries.
//
// \return True if the entry inserted (not previously in table)
inline bool insert(const Key& key, autoPtr<T>& ptr);
//- Insert a new entry, not overwriting existing entries.
//
// \return True if the entry inserted (not previously in table)
inline bool insert(const Key& key, autoPtr<T>&& ptr);
//- Insert a new entry, not overwriting existing entries.
//
// \return True if the entry inserted (not previously in table) // \return True if the entry inserted (not previously in table)
inline bool insert(const Key& key, std::unique_ptr<T>&& ptr); inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
//- Assign a new entry, overwriting existing entries. //- Insert a new entry, not overwriting existing entries.
// \return True if the entry inserted (not previously in table)
inline bool insert(const Key& key, autoPtr<T>&& ptr);
//- Assign a new entry, overwrites existing
inline bool set(const Key& key, T* ptr); inline bool set(const Key& key, T* ptr);
//- Assign a new entry, overwriting existing entries. //- Assign a new entry, overwrites existing
inline bool set(const Key& key, autoPtr<T>& ptr); inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
//- Assign a new entry, overwriting existing entries. //- Assign a new entry, overwrites existing
inline bool set(const Key& key, autoPtr<T>&& ptr); inline bool set(const Key& key, autoPtr<T>&& ptr);
//- Assign a new entry, overwriting existing entries. //- Assign a new entry from refPtr (move or clone), overwrites existing
inline bool set(const Key& key, std::unique_ptr<T>&& ptr); inline bool set(const Key& key, const refPtr<T>& ptr);
//- Assign a new entry from tmp (move or clone), overwrites existing
inline bool set(const Key& key, const tmp<T>& ptr);
// Housekeeping
//- Insert a new entry, not overwriting existing entries.
// \return True if the entry inserted (not previously in table)
bool insert(const Key& key, autoPtr<T>& ptr)
{
return this->insert(key, std::move(ptr));
}
//- Assign a new entry, overwrites existing
bool set(const Key& key, autoPtr<T>& ptr)
{
return this->set(key, std::move(ptr));
}
}; };

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd. Copyright (C) 2018-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -26,6 +26,8 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "autoPtr.H" #include "autoPtr.H"
#include "refPtr.H"
#include "tmp.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -38,9 +40,23 @@ inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline const T* Foam::HashPtrTable<T, Key, Hash>::test(const Key& key) const
{
// Like lookup() with a nullptr
const const_iterator iter(this->cfind(key));
if (iter.good())
{
return iter.val();
}
return nullptr;
}
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const
{ {
// Like lookup() with a nullptr
const const_iterator iter(this->cfind(key)); const const_iterator iter(this->cfind(key));
if (iter.good()) if (iter.good())
{ {
@ -83,7 +99,7 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::insert inline bool Foam::HashPtrTable<T, Key, Hash>::insert
( (
const Key& key, const Key& key,
autoPtr<T>& ptr std::unique_ptr<T>&& ptr
) )
{ {
if (parent_type::insert(key, ptr.get())) if (parent_type::insert(key, ptr.get()))
@ -113,23 +129,6 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::insert
} }
template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::insert
(
const Key& key,
std::unique_ptr<T>&& ptr
)
{
if (parent_type::insert(key, ptr.get()))
{
ptr.release(); // Now owned by HashPtrTable
return true;
}
return false;
}
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::set inline bool Foam::HashPtrTable<T, Key, Hash>::set
( (
@ -154,7 +153,7 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::set inline bool Foam::HashPtrTable<T, Key, Hash>::set
( (
const Key& key, const Key& key,
autoPtr<T>& ptr std::unique_ptr<T>&& ptr
) )
{ {
return this->set(key, ptr.release()); return this->set(key, ptr.release());
@ -176,10 +175,21 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::set inline bool Foam::HashPtrTable<T, Key, Hash>::set
( (
const Key& key, const Key& key,
std::unique_ptr<T>&& ptr const refPtr<T>& ptr
) )
{ {
return this->set(key, ptr.release()); return this->set(key, ptr.ptr()); // release or clone
}
template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::set
(
const Key& key,
const tmp<T>& ptr
)
{
return this->set(key, ptr.ptr()); // release or clone
} }

View File

@ -178,9 +178,9 @@ public:
//- Same as found() - return true if key exists in the set. //- Same as found() - return true if key exists in the set.
// Method name compatibility with bitSet and boolList. // Method name compatibility with bitSet and boolList.
bool test(const Key& key) const noexcept bool test(const Key& key) const
{ {
return found(key); return this->found(key);
} }

View File

@ -100,24 +100,11 @@ public:
// Member Functions // Member Functions
// Access // Sizing
//- Size of the underlying storage. //- Size of the underlying storage.
inline label capacity() const noexcept; inline label capacity() const noexcept;
//- Return const pointer to element (can be nullptr),
//- with bounds checking.
// The return value can be tested as a bool.
const T* get(const label i) const { return this->test(i); }
//- Return const pointer to element (if set) or nullptr,
//- with bounds checking.
// The return value can be tested as a bool.
const T* set(const label i) const { return this->test(i); }
// Sizing
//- Reserve allocation space for at least this size. //- Reserve allocation space for at least this size.
inline void reserve(const label len); inline void reserve(const label len);
@ -164,14 +151,11 @@ public:
inline void append(T* ptr); inline void append(T* ptr);
//- Move append an element to the end of the list //- Move append an element to the end of the list
inline void append(autoPtr<T>& ptr); inline void append(std::unique_ptr<T>&& ptr);
//- Move append an element to the end of the list //- Move append an element to the end of the list
inline void append(autoPtr<T>&& ptr); inline void append(autoPtr<T>&& ptr);
//- Move append an element to the end of the list
inline void append(std::unique_ptr<T>&& ptr);
//- Move or clone append a tmp to the end of the list //- Move or clone append a tmp to the end of the list
inline void append(const refPtr<T>& ptr); inline void append(const refPtr<T>& ptr);
@ -192,22 +176,24 @@ public:
template<class... Args> template<class... Args>
inline autoPtr<T> emplace(const label i, Args&&... args); inline autoPtr<T> emplace(const label i, Args&&... args);
//- Set element to given pointer and return old element (can be null) //- Set element to given pointer and return old element (can be null).
//- Auto-sizes list as required.
inline autoPtr<T> set(const label i, T* ptr); inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>& ptr);
//- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
//- Set element to given pointer and return old element //- Set element to given pointer and return old element
//- Auto-sizes list as required.
inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr); inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
//- Set element to given autoPtr and return old element
//- Auto-sizes list as required.
inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
//- Set element to given refPtr and return old element //- Set element to given refPtr and return old element
//- Auto-sizes list as required.
inline autoPtr<T> set(const label i, const refPtr<T>& ptr); inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
//- Set element to given tmp and return old element //- Set element to given tmp and return old element
//- Auto-sizes list as required.
inline autoPtr<T> set(const label i, const tmp<T>& ptr); inline autoPtr<T> set(const label i, const tmp<T>& ptr);
//- Reorder elements. Reordering must be unique (ie, shuffle). //- Reorder elements. Reordering must be unique (ie, shuffle).
@ -235,6 +221,21 @@ public:
//- Move assignment with different sizing parameters //- Move assignment with different sizing parameters
template<int AnySizeMin> template<int AnySizeMin>
inline void operator=(PtrDynList<T, AnySizeMin>&& list); inline void operator=(PtrDynList<T, AnySizeMin>&& list);
// Housekeeping
//- Move append an element to the end of the list
void append(autoPtr<T>& ptr)
{
this->append(std::move(ptr));
}
//- Set element to given autoPtr and return old element
autoPtr<T> set(const label i, autoPtr<T>& ptr)
{
return this->set(i, std::move(ptr));
}
}; };

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2021 OpenCFD Ltd. Copyright (C) 2018-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -241,7 +241,7 @@ inline void Foam::PtrDynList<T, SizeMin>::append(T* ptr)
template<class T, int SizeMin> template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(autoPtr<T>& ptr) inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
{ {
this->append(ptr.release()); this->append(ptr.release());
} }
@ -254,24 +254,17 @@ inline void Foam::PtrDynList<T, SizeMin>::append(autoPtr<T>&& ptr)
} }
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
{
this->append(ptr.release());
}
template<class T, int SizeMin> template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const refPtr<T>& ptr) inline void Foam::PtrDynList<T, SizeMin>::append(const refPtr<T>& ptr)
{ {
this->append(ptr.ptr()); this->append(ptr.ptr()); // release or clone
} }
template<class T, int SizeMin> template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const tmp<T>& ptr) inline void Foam::PtrDynList<T, SizeMin>::append(const tmp<T>& ptr)
{ {
this->append(ptr.ptr()); this->append(ptr.ptr()); // release or clone
} }
@ -375,7 +368,7 @@ template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
( (
const label i, const label i,
autoPtr<T>& ptr std::unique_ptr<T>&& ptr
) )
{ {
return this->set(i, ptr.release()); return this->set(i, ptr.release());
@ -393,17 +386,6 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
} }
template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
std::unique_ptr<T>&& ptr
)
{
return this->set(i, ptr.release());
}
template<class T, int SizeMin> template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
( (
@ -411,7 +393,8 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
const refPtr<T>& ptr const refPtr<T>& ptr
) )
{ {
return this->set(i, ptr.ptr()); return this->set(i, ptr.ptr()); // release or clone
} }
@ -422,7 +405,7 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
const tmp<T>& ptr const tmp<T>& ptr
) )
{ {
return this->set(i, ptr.ptr()); return this->set(i, ptr.ptr()); // release or clone
} }

View File

@ -120,7 +120,7 @@ public:
PtrList(Istream& is); PtrList(Istream& is);
//- Destructor //- Destructor. Frees all pointers
~PtrList(); ~PtrList();
@ -134,9 +134,9 @@ public:
// Access // Access
//- Return const pointer to element (can be nullptr), //- Return const pointer to element (can be nullptr),
//- without bounds checking - same as get(). //- or nullptr for out-of-range access (ie, \em with bounds checking).
// The return value can be tested as a bool. // The return value can be tested as a bool.
const T* set(const label i) const { return this->get(i); } const T* set(const label i) const { return UPtrList<T>::set(i); }
// Edit // Edit
@ -159,14 +159,11 @@ public:
inline void append(T* ptr); inline void append(T* ptr);
//- Move append an element to the end of the list //- Move append an element to the end of the list
inline void append(autoPtr<T>& ptr); inline void append(std::unique_ptr<T>&& ptr);
//- Move append an element to the end of the list //- Move append an element to the end of the list
inline void append(autoPtr<T>&& ptr); inline void append(autoPtr<T>&& ptr);
//- Move append an element to the end of the list
inline void append(std::unique_ptr<T>&& ptr);
//- Move or clone append a refPtr to the end of the list //- Move or clone append a refPtr to the end of the list
inline void append(const refPtr<T>& ptr); inline void append(const refPtr<T>& ptr);
@ -184,15 +181,12 @@ public:
// 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(const 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 unique_ptr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>& ptr); inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
//- Set element to given autoPtr and return old element //- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>&& ptr); inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
//- Set element to given unique_ptr and return old element
inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
//- Set element to given refPtr and return old element //- Set element to given refPtr and return old element
inline autoPtr<T> set(const label i, const refPtr<T>& ptr); inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
@ -222,6 +216,21 @@ public:
//- Read from Istream, discarding contents of existing list //- Read from Istream, discarding contents of existing list
friend Istream& operator>> <T>(Istream& is, PtrList<T>& list); friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
// Housekeeping
//- Move append an element to the end of the list
void append(autoPtr<T>& ptr)
{
this->append(std::move(ptr));
}
//- Set element to given autoPtr and return old element
autoPtr<T> set(const label i, autoPtr<T>& ptr)
{
return this->set(i, std::move(ptr));
}
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd. Copyright (C) 2018-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -117,7 +117,7 @@ inline void Foam::PtrList<T>::append(T* ptr)
template<class T> template<class T>
inline void Foam::PtrList<T>::append(autoPtr<T>& ptr) inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
{ {
UPtrList<T>::append(ptr.release()); UPtrList<T>::append(ptr.release());
} }
@ -130,24 +130,17 @@ inline void Foam::PtrList<T>::append(autoPtr<T>&& ptr)
} }
template<class T>
inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
{
UPtrList<T>::append(ptr.release());
}
template<class T> template<class T>
inline void Foam::PtrList<T>::append(const refPtr<T>& ptr) inline void Foam::PtrList<T>::append(const refPtr<T>& ptr)
{ {
UPtrList<T>::append(ptr.ptr()); UPtrList<T>::append(ptr.ptr()); // release or clone
} }
template<class T> template<class T>
inline void Foam::PtrList<T>::append(const tmp<T>& ptr) inline void Foam::PtrList<T>::append(const tmp<T>& ptr)
{ {
UPtrList<T>::append(ptr.ptr()); UPtrList<T>::append(ptr.ptr()); // release or clone
} }
@ -202,7 +195,7 @@ template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set inline Foam::autoPtr<T> Foam::PtrList<T>::set
( (
const label i, const label i,
autoPtr<T>& ptr std::unique_ptr<T>&& ptr
) )
{ {
return set(i, ptr.release()); return set(i, ptr.release());
@ -220,17 +213,6 @@ inline Foam::autoPtr<T> Foam::PtrList<T>::set
} }
template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set
(
const label i,
std::unique_ptr<T>&& ptr
)
{
return set(i, ptr.release());
}
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set inline Foam::autoPtr<T> Foam::PtrList<T>::set
( (
@ -238,7 +220,7 @@ inline Foam::autoPtr<T> Foam::PtrList<T>::set
const refPtr<T>& ptr const refPtr<T>& ptr
) )
{ {
return set(i, ptr.ptr()); return set(i, ptr.ptr()); // release or clone
} }
@ -249,7 +231,7 @@ inline Foam::autoPtr<T> Foam::PtrList<T>::set
const tmp<T>& ptr const tmp<T>& ptr
) )
{ {
return set(i, ptr.ptr()); return set(i, ptr.ptr()); // release or clone
} }

View File

@ -234,19 +234,20 @@ public:
// The return value can be tested as a bool. // The return value can be tested as a bool.
inline const T* test(const label i) const; inline const T* test(const label i) const;
//- Return const pointer to element (can be nullptr),
//- or nullptr for out-of-range access (ie, \em with bounds checking).
// The return value can be tested as a bool.
inline const T* get(const label i) const;
//- Return pointer to element (can be nullptr), //- Return pointer to element (can be nullptr),
//- \em without bounds checking. //- or nullptr for out-of-range access (ie, \em with bounds checking).
// The return value can be tested as a bool. // The return value can be tested as a bool.
inline T* get(const label i); inline T* get(const label i);
//- Return const pointer to element (can be nullptr), //- Return const pointer to element (can be nullptr),
//- \em without bounds checking. //- or nullptr for out-of-range access (ie, \em with bounds checking).
inline const T* get(const label i) const;
//- Return const pointer to element (can be nullptr),
//- \em without bounds checking - same as get().
// The return value can be tested as a bool. // The return value can be tested as a bool.
const T* set(const label i) const { return this->get(i); } inline const T* set(const label i) const;
// Edit // Edit
@ -307,8 +308,10 @@ public:
//- Return reference to the element //- Return reference to the element
inline T& operator[](const label i); inline T& operator[](const label i);
//- Return const pointer to the element - same as get(). //- Deprecated(2022-09) - same as get()
inline const T* operator()(const label i) const; // \deprecated(2022-09) - use get(), set() or test() methods
FOAM_DEPRECATED_FOR(2022-09, "get(), set() or test() methods")
const T* operator()(const label i) const { return this->get(i); }
//- Copy assignment (shallow copies addresses) //- Copy assignment (shallow copies addresses)
inline void operator=(const UPtrList<T>& list); inline void operator=(const UPtrList<T>& list);

View File

@ -124,16 +124,36 @@ inline const T* Foam::UPtrList<T>::test(const label i) const
template<class T> template<class T>
inline T* Foam::UPtrList<T>::get(const label i) inline const T* Foam::UPtrList<T>::get(const label i) const
{ {
return ptrs_[i]; return (i >= 0 && i < ptrs_.size()) ? ptrs_[i] : nullptr;
} }
template<class T> template<class T>
inline const T* Foam::UPtrList<T>::get(const label i) const inline T* Foam::UPtrList<T>::get(const label i)
{ {
return ptrs_[i]; return (i >= 0 && i < ptrs_.size()) ? ptrs_[i] : nullptr;
}
template<class T>
inline const T* Foam::UPtrList<T>::set(const label i) const
{
return (i >= 0 && i < ptrs_.size()) ? ptrs_[i] : nullptr;
}
template<class T>
inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
{
T* old = ptrs_[i];
if (old == ptr)
{
return nullptr; // Content did not change
}
ptrs_[i] = ptr;
return old;
} }
@ -208,19 +228,6 @@ inline void Foam::UPtrList<T>::append(UPtrList<T>&& other)
} }
template<class T>
inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
{
T* old = ptrs_[i];
if (old == ptr)
{
return nullptr; // Content did not change
}
ptrs_[i] = ptr;
return old;
}
template<class T> template<class T>
inline void Foam::UPtrList<T>::checkNonNull() const inline void Foam::UPtrList<T>::checkNonNull() const
{ {
@ -264,13 +271,6 @@ inline T& Foam::UPtrList<T>::operator[](const label i)
} }
template<class T>
inline const T* Foam::UPtrList<T>::operator()(const label i) const
{
return ptrs_[i];
}
// * * * * * * * * * * * * * * * * iterator * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * iterator * * * * * * * * * * * * * * * * //
template<class T> template<class T>

View File

@ -245,12 +245,8 @@ Foam::coordinateSystems::cfind(const word& name) const
<< name << '=' << index << endl; << name << '=' << index << endl;
} }
if (index < 0) // Return nullptr if not found
{ return PtrList<coordinateSystem>::test(index);
return nullptr;
}
return this->operator()(index);
} }

View File

@ -193,7 +193,7 @@ void Foam::fvMeshAdder::MapVolField
const polyPatch& oldPatch = const polyPatch& oldPatch =
fldToAdd.mesh().boundaryMesh()[patchi]; fldToAdd.mesh().boundaryMesh()[patchi];
if (!bfld(newPatchi)) if (!bfld.set(newPatchi))
{ {
// First occurrence of newPatchi. Map from existing // First occurrence of newPatchi. Map from existing
// patchField // patchField
@ -503,7 +503,7 @@ void Foam::fvMeshAdder::MapSurfaceField
const polyPatch& oldPatch = const polyPatch& oldPatch =
fldToAdd.mesh().boundaryMesh()[patchi]; fldToAdd.mesh().boundaryMesh()[patchi];
if (!bfld(newPatchi)) if (!bfld.set(newPatchi))
{ {
// First occurrence of newPatchi. Map from existing // First occurrence of newPatchi. Map from existing
// patchField // patchField

View File

@ -314,7 +314,7 @@ void Foam::lduPrimitiveMeshAssembly::update
interfaces().set interfaces().set
( (
globalPatchId, globalPatchId,
interfacesLst(patchI) interfacesLst.get(patchI)
); );
} }
} }

View File

@ -121,7 +121,7 @@ Foam::faFieldReconstructor::reconstructField
{ {
// Regular patch. Fast looping // Regular patch. Fast looping
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -195,7 +195,7 @@ Foam::faFieldReconstructor::reconstructField
} }
} }
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -230,7 +230,7 @@ Foam::faFieldReconstructor::reconstructField
if if
( (
isA<emptyFaPatch>(mesh_.boundary()[patchI]) isA<emptyFaPatch>(mesh_.boundary()[patchI])
&& !patchFields(patchI) && !patchFields.set(patchI)
) )
{ {
patchFields.set patchFields.set
@ -362,7 +362,7 @@ Foam::faFieldReconstructor::reconstructField
{ {
// Regular patch. Fast looping // Regular patch. Fast looping
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -439,7 +439,7 @@ Foam::faFieldReconstructor::reconstructField
} }
} }
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -481,7 +481,7 @@ Foam::faFieldReconstructor::reconstructField
if if
( (
isA<emptyFaPatch>(mesh_.boundary()[patchI]) isA<emptyFaPatch>(mesh_.boundary()[patchI])
&& !patchFields(patchI) && !patchFields.set(patchI)
) )
{ {
patchFields.set patchFields.set

View File

@ -116,7 +116,7 @@ Foam::fvFieldReconstructor::reconstructField
{ {
// Regular patch. Fast looping // Regular patch. Fast looping
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -184,7 +184,7 @@ Foam::fvFieldReconstructor::reconstructField
{ {
label curBPatch = mesh_.boundaryMesh().whichPatch(curF); label curBPatch = mesh_.boundaryMesh().whichPatch(curF);
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -217,7 +217,7 @@ Foam::fvFieldReconstructor::reconstructField
if if
( (
isType<emptyFvPatch>(mesh_.boundary()[patchi]) isType<emptyFvPatch>(mesh_.boundary()[patchi])
&& !patchFields(patchi) && !patchFields.set(patchi)
) )
{ {
patchFields.set patchFields.set
@ -314,7 +314,7 @@ Foam::fvFieldReconstructor::reconstructField
{ {
// Regular patch. Fast looping // Regular patch. Fast looping
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -370,7 +370,7 @@ Foam::fvFieldReconstructor::reconstructField
label curBPatch = label curBPatch =
mesh_.boundaryMesh().whichPatch(curF); mesh_.boundaryMesh().whichPatch(curF);
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set patchFields.set
( (
@ -410,7 +410,7 @@ Foam::fvFieldReconstructor::reconstructField
if if
( (
isType<emptyFvPatch>(mesh_.boundary()[patchi]) isType<emptyFvPatch>(mesh_.boundary()[patchi])
&& !patchFields(patchi) && !patchFields.set(patchi)
) )
{ {
patchFields.set patchFields.set

View File

@ -70,9 +70,10 @@ Foam::pointFieldReconstructor::reconstructField
// check if the boundary patch is not a processor patch // check if the boundary patch is not a processor patch
if (curBPatch >= 0) if (curBPatch >= 0)
{ {
if (!patchFields(curBPatch)) if (!patchFields.set(curBPatch))
{ {
patchFields.set( patchFields.set
(
curBPatch, curBPatch,
pointPatchField<Type>::New pointPatchField<Type>::New
( (

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -76,13 +76,8 @@ Foam::liquidMixtureProperties::liquidMixtureProperties
) )
: :
components_(lm.components_), components_(lm.components_),
properties_(lm.properties_.size()) properties_(lm.properties_.clone())
{ {}
forAll(properties_, i)
{
properties_.set(i, lm.properties_(i)->clone());
}
}
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -65,13 +66,8 @@ Foam::solidMixtureProperties::solidMixtureProperties
) )
: :
components_(s.components_), components_(s.components_),
properties_(s.properties_.size()) properties_(s.properties_.clone())
{ {}
forAll(properties_, i)
{
properties_.set(i, s.properties_(i)->clone());
}
}
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //