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:
Mark Olesen
2020-07-27 07:51:10 +02:00
parent 872c9d370b
commit fa71840d8b
8 changed files with 69 additions and 6 deletions

View File

@ -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));