include/svl/poolitem.hxx | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-)
New commits: commit dd08e194447355d7f3a009a0415999de3d762fb1 Author: Armin Le Grand (allotropia) <armin.le.grand.ext...@allotropia.de> AuthorDate: Fri Jul 12 18:44:09 2024 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Mon Jul 15 12:31:17 2024 +0200 ITEM: secure HashedItemInstanceManager This one still used just a hash to feed a unordered_map what is not safe. Changed to unordered_set and added hash and equal operators. Change-Id: I2a426c449536747ad59f8bd8781a7eadceea9183 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170417 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/include/svl/poolitem.hxx b/include/svl/poolitem.hxx index 88e7ba855318..bb650eb17563 100644 --- a/include/svl/poolitem.hxx +++ b/include/svl/poolitem.hxx @@ -759,11 +759,27 @@ private: /** Utility template to reduce boilerplate code when creating item instance managers - for specific PoolItem subclasses. + for specific PoolItem subclasses that can be hashed which is fatsre than using + the linear search with operator== that DefaultItemInstanceManager has to do */ class HashedItemInstanceManager : public ItemInstanceManager { - std::unordered_map<size_t, const SfxPoolItem*> maRegistered; + struct ItemHash { + HashedItemInstanceManager& mrManager; + ItemHash(HashedItemInstanceManager& rManager) : mrManager(rManager) {} + size_t operator()(const SfxPoolItem* p) const + { + return mrManager.hashCode(*p); + } + }; + struct ItemEqual { + bool operator()(const SfxPoolItem* lhs, const SfxPoolItem* rhs) const + { + return lhs->Which() == rhs->Which() && (*lhs) == (*rhs); + } + }; + + std::unordered_set<const SfxPoolItem*, ItemHash, ItemEqual> maRegistered; protected: virtual size_t hashCode(const SfxPoolItem&) const = 0; @@ -771,6 +787,7 @@ protected: public: HashedItemInstanceManager(SfxItemType aSfxItemType) : ItemInstanceManager(aSfxItemType) + , maRegistered(0, ItemHash(*this), ItemEqual()) { } @@ -778,18 +795,18 @@ public: // by implCreateItemEntry/implCleanupItemEntry virtual const SfxPoolItem* find(const SfxPoolItem& rItem) const override final { - auto aHit(maRegistered.find(hashCode(rItem))); + auto aHit(maRegistered.find(&rItem)); if (aHit != maRegistered.end()) - return aHit->second; + return *aHit; return nullptr; } virtual void add(const SfxPoolItem& rItem) override final { - maRegistered.insert({hashCode(rItem), &rItem}); + maRegistered.insert(&rItem); } virtual void remove(const SfxPoolItem& rItem) override final { - maRegistered.erase(hashCode(rItem)); + maRegistered.erase(&rItem); } };