ENH: HashTable::emplace_set() method, HashPtrTable support for unique_ptr

- forwarding like the emplace() method, but overwriting existing
  entries as required

- propagate similar changes to HashPtrTable

  For example, with HashPtrTable<labelList> table(...) :

  With 'insert' semantics

      table.emplace("list1", 1000);

  vs
      if (!table.found("list1"))
      {
          table.set("list1", new labelList(1000));
      }
  or
      table.insert("list1", autoPtr<labelList>::New(1000));

  Note that the last example invokes an unnecessary allocation/deletion
  if the insertion is unsuccessful.

  With 'set' semantics:

      table.emplace_set("list1", 15);

  vs
      table.set("list1", new labelList(15));
This commit is contained in:
Mark Olesen
2020-07-27 07:50:53 +02:00
parent c77afff48f
commit 4110699d90
6 changed files with 172 additions and 6 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,6 +35,45 @@ Description
using namespace Foam;
class Scalar
{
scalar data_;
public:
Scalar()
:
data_(0)
{}
Scalar(scalar val)
:
data_(val)
{}
~Scalar()
{
Info<<"delete Scalar: " << data_ << endl;
}
const scalar& value() const
{
return data_;
}
scalar& value()
{
return data_;
}
friend Ostream& operator<<(Ostream& os, const Scalar& val)
{
os << val.data_;
return os;
}
};
template<class T>
void printTable(const HashPtrTable<T>& table)
{
@ -147,6 +186,38 @@ int main()
Info<<"old" << nl;
printTable(moved);
Info<< "Verifying deletion characteristics" << nl;
{
HashPtrTable<Scalar> tbl;
tbl.set("abc", new Scalar(42.1));
tbl.set("def", nullptr);
tbl.set("pi", new Scalar(3.14159));
tbl.set("natlog", new Scalar(2.718282));
tbl.insert("sqrt2", autoPtr<Scalar>::New(1.414214));
Info<< "Table: " << tbl << nl;
Info<< nl << "... overwrite again" << nl;
tbl.set("abc", new Scalar(42.1));
tbl.set("def", nullptr);
tbl.set("pi", new Scalar(3.14159));
tbl.set("natlog", new Scalar(2.718282));
tbl.emplace("other", 15.6);
Info<< "Table: " << tbl << nl;
Info << nl << "Test emplace and emplace_set" << nl;
tbl.emplace("abc", 100);
tbl.emplace_set("def", 100);
tbl.emplace_set("other", 100);
Info<< "Table: " << tbl << nl;
}
return 0;
}