mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add get() retrieval of a pointer from PtrLists, HashPtrTable
- naming similarity with autoPtr, unique_ptr and other containers.
For UPtrList derivatives, this is equivalent to the existing
operator(). The read-only variant is also equivalent to the
single-parameter 'set(label)' method.
With PtrList<T> list(...) :
const T* ptr = list.get(10);
if (ptr)
{
ptr->method();
}
vs.
if (list.set(10))
{
list[10].method();
}
For HashPtrTable there is only a read-only variant which is equivalent
to testing for existence and for value.
With HashPtrTable<T> hash(...) :
const T* ptr = list.get("key");
if (ptr)
{
ptr->method();
}
vs.
if (list.found("key"))
{
// Fails on null pointer!!
list["key"].method();
}
Use of get() is largely a matter of taste or local coding requirements
This commit is contained in:
@ -198,6 +198,24 @@ int main()
|
||||
|
||||
Info<< "Table: " << tbl << nl;
|
||||
|
||||
Info<< nl << "Check exists, non-null" << nl;
|
||||
|
||||
for (const word& k : { "abc", "foo", "pi" })
|
||||
{
|
||||
Info<< " " << k << ' ';
|
||||
|
||||
const auto* inspect = tbl.get(k);
|
||||
|
||||
if (inspect)
|
||||
{
|
||||
Info<< *inspect << nl;
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "(null)" << nl;
|
||||
}
|
||||
}
|
||||
|
||||
Info<< nl << "... overwrite again" << nl;
|
||||
|
||||
tbl.set("abc", new Scalar(42.1));
|
||||
|
||||
@ -120,6 +120,13 @@ public:
|
||||
|
||||
// Member Functions
|
||||
|
||||
// Access
|
||||
|
||||
//- Return const pointer associated with given entry,
|
||||
//- returning a nullptr if the key does not exist in the table.
|
||||
inline const T* get(const Key& key) const;
|
||||
|
||||
|
||||
// Edit
|
||||
|
||||
//- Remove entry specified by given iterator.
|
||||
|
||||
@ -45,6 +45,18 @@ inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const
|
||||
{
|
||||
const const_iterator iter(this->cfind(key));
|
||||
if (iter.good())
|
||||
{
|
||||
return iter.val();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
template<class... Args>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::emplace
|
||||
|
||||
@ -108,10 +108,14 @@ public:
|
||||
//- Size of the underlying storage.
|
||||
inline label capacity() const noexcept;
|
||||
|
||||
//- Return const pointer to element (can be nullptr),
|
||||
// with bounds checking.
|
||||
inline const T* get(const label i) const;
|
||||
|
||||
//- Return const pointer to element (if set) or nullptr,
|
||||
// with bounds checking.
|
||||
// The return value can be tested as a bool.
|
||||
inline const T* set(const label i) const;
|
||||
const T* set(const label i) const { return this->get(i); }
|
||||
|
||||
|
||||
// Edit
|
||||
|
||||
@ -100,7 +100,7 @@ inline Foam::label Foam::PtrDynList<T, SizeMin>::capacity() const noexcept
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
inline const T* Foam::PtrDynList<T, SizeMin>::set(const label i) const
|
||||
inline const T* Foam::PtrDynList<T, SizeMin>::get(const label i) const
|
||||
{
|
||||
return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::get(i) : nullptr;
|
||||
}
|
||||
|
||||
@ -134,9 +134,9 @@ public:
|
||||
// Access
|
||||
|
||||
//- Return const pointer to element (can be nullptr),
|
||||
// without bounds checking.
|
||||
// without bounds checking - same as get().
|
||||
// The return value can also be tested as a bool.
|
||||
const T* set(const label i) const { return UPtrList<T>::set(i); }
|
||||
const T* set(const label i) const { return this->get(i); }
|
||||
|
||||
|
||||
// Edit
|
||||
|
||||
@ -157,10 +157,18 @@ public:
|
||||
//- Return reference to the last element of the list
|
||||
inline const T& last() const;
|
||||
|
||||
//- Return pointer to element (can be nullptr),
|
||||
// without bounds checking.
|
||||
inline T* get(const label i);
|
||||
|
||||
//- Return const pointer to element (can be nullptr),
|
||||
// without bounds checking.
|
||||
inline const T* get(const label i) const;
|
||||
|
||||
//- Return const pointer to element (can be nullptr),
|
||||
// without bounds checking - same as get().
|
||||
// The return value can also be tested as a bool.
|
||||
const T* set(const label i) const { return ptrs_[i]; }
|
||||
const T* set(const label i) const { return this->get(i); }
|
||||
|
||||
|
||||
// Edit
|
||||
@ -212,7 +220,7 @@ public:
|
||||
//- Return reference to the element
|
||||
inline T& operator[](const label i);
|
||||
|
||||
//- Return const pointer to the element.
|
||||
//- Return const pointer to the element - same as get().
|
||||
inline const T* operator()(const label i) const;
|
||||
|
||||
//- Copy assignment (shallow copies addresses)
|
||||
|
||||
@ -107,6 +107,20 @@ inline bool Foam::UPtrList<T>::empty() const noexcept
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline T* Foam::UPtrList<T>::get(const label i)
|
||||
{
|
||||
return ptrs_[i];
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline const T* Foam::UPtrList<T>::get(const label i) const
|
||||
{
|
||||
return ptrs_[i];
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::UPtrList<T>::clear()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user