summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2024-06-26 12:31:38 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2024-06-27 12:51:43 +0200
commit534b8f6d8a7cc0bb98c243c8086ed0c81f87901d (patch)
treefafe46e08a04cc868dd619d5a62db9b081e3ae99 /include
parent3a02490e1a04c32e18ce5bad5f3c3cb70501a7a4 (diff)
reduce boilerplate code for type-specific ItemInstanceManagers
create a template class to reduce repetition Change-Id: I8c9c71fdfdac20a3b34432affac07aa1d46e8373 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/169613 Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Tested-by: Jenkins
Diffstat (limited to 'include')
-rw-r--r--include/svl/poolitem.hxx39
1 files changed, 39 insertions, 0 deletions
diff --git a/include/svl/poolitem.hxx b/include/svl/poolitem.hxx
index bdfb814e3c0a..1cb667b5c90c 100644
--- a/include/svl/poolitem.hxx
+++ b/include/svl/poolitem.hxx
@@ -653,6 +653,45 @@ private:
virtual void remove(const SfxPoolItem&) override;
};
+/**
+ Utility template to reduce boilerplate code when creating item instance managers
+ for specific PoolItem subclasses.
+*/
+template<class T>
+class TypeSpecificItemInstanceManager : public ItemInstanceManager
+{
+public:
+ TypeSpecificItemInstanceManager()
+ : ItemInstanceManager(typeid(T).hash_code())
+ {
+ }
+
+ // standard interface, accessed exclusively
+ // by implCreateItemEntry/implCleanupItemEntry
+ virtual const SfxPoolItem* find(const SfxPoolItem& rItem) const override final
+ {
+ auto aHit(maRegistered.find(hashCode(rItem)));
+ if (aHit != maRegistered.end())
+ return aHit->second;
+ return nullptr;
+ }
+ virtual void add(const SfxPoolItem& rItem) override final
+ {
+ maRegistered.insert({hashCode(rItem), &rItem});
+ }
+ virtual void remove(const SfxPoolItem& rItem) override final
+ {
+ maRegistered.erase(hashCode(rItem));
+ }
+
+protected:
+ virtual size_t hashCode(const SfxPoolItem&) const = 0;
+
+private:
+ std::unordered_map<size_t, const SfxPoolItem*> maRegistered;
+};
+
+
inline bool IsStaticDefaultItem(const SfxPoolItem *pItem )
{
return pItem && pItem->isStaticDefault();