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));
|
||||
|
||||
Reference in New Issue
Block a user