diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-11-10 12:48:21 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-11-15 08:24:29 +0100 |
commit | 3588a48a82d37f940595570758bc1d1179d18b84 (patch) | |
tree | daa314ae437771157a53f8a5d5043a685f399ac3 /include/svl | |
parent | a8f31d5120c2ae9109d316db73b5adf9cb26c892 (diff) |
TypedWhichId
use a strong-typedef template to give which IDs a type, which we can
carry around to do a
(a) little bit more convenience when Get()'ing them
and
(b) a little bit of enforcement of which PoolItem subclass each ID uses
Fix a bug in casting EE_PARA_BULLETSTATE to the wrong subclass
in AccessibleEditableTextPara::_correctValues
Change-Id: I015ce8b3b0f6d21308af182afa3caf122c877a5b
Reviewed-on: https://gerrit.libreoffice.org/44587
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include/svl')
-rw-r--r-- | include/svl/itempool.hxx | 17 | ||||
-rw-r--r-- | include/svl/itemset.hxx | 26 | ||||
-rw-r--r-- | include/svl/poolitem.hxx | 9 | ||||
-rw-r--r-- | include/svl/typedwhich.hxx | 61 |
4 files changed, 113 insertions, 0 deletions
diff --git a/include/svl/itempool.hxx b/include/svl/itempool.hxx index 0cd5bfc13342..22ea12cf4e24 100644 --- a/include/svl/itempool.hxx +++ b/include/svl/itempool.hxx @@ -23,6 +23,7 @@ #include <rtl/string.hxx> #include <svl/poolitem.hxx> #include <svl/svldllapi.h> +#include <svl/typedwhich.hxx> #include <tools/solar.h> #include <memory> #include <vector> @@ -101,7 +102,11 @@ public: SfxBroadcaster& BC(); void SetPoolDefaultItem( const SfxPoolItem& ); + const SfxPoolItem* GetPoolDefaultItem( sal_uInt16 nWhich ) const; + template<class T> const T* GetPoolDefaultItem( TypedWhichId<T> nWhich ) const + { return static_cast<const T*>(GetPoolDefaultItem(nWhich.Which())); } + void ResetPoolDefaultItem( sal_uInt16 nWhich ); void SetDefaults(std::vector<SfxPoolItem*>* pDefaults); @@ -147,11 +152,21 @@ public: virtual const SfxPoolItem& Put( const SfxPoolItem&, sal_uInt16 nWhich = 0 ); void Remove( const SfxPoolItem& ); + const SfxPoolItem& GetDefaultItem( sal_uInt16 nWhich ) const; + template<class T> const T& GetDefaultItem( TypedWhichId<T> nWhich ) const + { return static_cast<const T&>(GetDefaultItem(nWhich.Which())); } bool CheckItemInPool(const SfxPoolItem *) const; + const SfxPoolItem * GetItem2(sal_uInt16 nWhich, sal_uInt32 nSurrogate) const; + template<class T> const T* GetItem2( TypedWhichId<T> nWhich, sal_uInt32 nSurrogate ) const + { return dynamic_cast<const T*>(GetItem2(nWhich.Which(), nSurrogate)); } + const SfxPoolItem * GetItem2Default(sal_uInt16 nWhich) const; + template<class T> const T* GetItem2Default( TypedWhichId<T> nWhich ) const + { return static_cast<const T*>(GetItem2Default(nWhich.Which())); } + sal_uInt32 GetItemCount2(sal_uInt16 nWhich) const; sal_uInt16 GetFirstWhich() const; @@ -169,6 +184,8 @@ public: { return IsItemPoolable( rItem.Which() ); } void SetItemInfos( const SfxItemInfo *pInfos ); sal_uInt16 GetWhich( sal_uInt16 nSlot, bool bDeep = true ) const; + template<class T> sal_uInt16 GetWhich( TypedWhichId<T> nSlot, bool bDeep = true ) const + { return GetWhich(nSlot.Which(), bDeep); } sal_uInt16 GetSlotId( sal_uInt16 nWhich ) const; sal_uInt16 GetTrueWhich( sal_uInt16 nSlot, bool bDeep = true ) const; sal_uInt16 GetTrueSlotId( sal_uInt16 nWhich ) const; diff --git a/include/svl/itemset.hxx b/include/svl/itemset.hxx index 89c6149f44a2..3254263c38ab 100644 --- a/include/svl/itemset.hxx +++ b/include/svl/itemset.hxx @@ -28,6 +28,7 @@ #include <svl/svldllapi.h> #include <svl/poolitem.hxx> +#include <svl/typedwhich.hxx> class SfxItemPool; class SfxPoolItem; @@ -131,6 +132,11 @@ public: sal_uInt16 TotalCount() const; const SfxPoolItem& Get( sal_uInt16 nWhich, bool bSrchInParent = true ) const; + template<class T> + const T& Get( TypedWhichId<T> nWhich, bool bSrchInParent = true ) const + { + return static_cast<const T&>(Get(nWhich.Which(), bSrchInParent)); + } /** This method eases accessing single Items in the SfxItemSet. @@ -149,6 +155,15 @@ public: assert(!pItem || pCastedItem); // if it exists, must have the correct type return pCastedItem; } + template<class T> const T* GetItem( TypedWhichId<T> nWhich, bool bSearchInParent = true ) const + { + const SfxPoolItem* pItem = GetItem(nWhich.Which(), bSearchInParent); + const T* pCastedItem = dynamic_cast<const T*>(pItem); + + assert(!pItem || pCastedItem); // if it exists, must have the correct type + return pCastedItem; + } + /// Templatized static version of GetItem() to directly return the correct type if the SfxItemSet is available. template<class T> static const T* GetItem(const SfxItemSet* pItemSet, sal_uInt16 nWhich, bool bSearchInParent = true) @@ -166,12 +181,23 @@ public: SfxItemState GetItemState( sal_uInt16 nWhich, bool bSrchInParent = true, const SfxPoolItem **ppItem = nullptr ) const; + template<class T> + SfxItemState GetItemState( TypedWhichId<T> nWhich, + bool bSrchInParent = true, + const SfxPoolItem **ppItem = nullptr ) const + { return GetItemState(nWhich.Which(), bSrchInParent, ppItem); } bool HasItem(sal_uInt16 nWhich, const SfxPoolItem** ppItem = nullptr) const; void DisableItem(sal_uInt16 nWhich); + template<class T> void DisableItem( TypedWhichId<T> nWhich ) + { DisableItem(nWhich.Which()); } void InvalidateItem( sal_uInt16 nWhich ); + template<class T> void InvalidateItem( TypedWhichId<T> nWhich ) + { InvalidateItem(nWhich.Which()); } sal_uInt16 ClearItem( sal_uInt16 nWhich = 0); + template<class T> sal_uInt16 ClearItem( TypedWhichId<T> nWhich ) + { return ClearItem(nWhich.Which()); } void ClearInvalidItems(); void InvalidateAllItems(); // HACK(via nWhich = 0) ??? diff --git a/include/svl/poolitem.hxx b/include/svl/poolitem.hxx index 7f9540c166a2..2d5a6f567c0b 100644 --- a/include/svl/poolitem.hxx +++ b/include/svl/poolitem.hxx @@ -28,6 +28,7 @@ #include <com/sun/star/uno/Any.hxx> #include <svl/hint.hxx> #include <svl/svldllapi.h> +#include <svl/typedwhich.hxx> #include <tools/debug.hxx> #include <tools/mapunit.hxx> #include <tools/solar.h> @@ -149,6 +150,10 @@ public: assert(m_nRefCount==0); m_nWhich = nId; } + template<class T> void SetWhich( TypedWhichId<T> nId ) + { + SetWhich(nId.Which()); + } sal_uInt16 Which() const { return m_nWhich; } virtual bool operator==( const SfxPoolItem& ) const = 0; bool operator!=( const SfxPoolItem& rItem ) const @@ -173,6 +178,10 @@ public: virtual SfxPoolItem* Clone( SfxItemPool *pPool = nullptr ) const = 0; // clone and call SetWhich SfxPoolItem* CloneSetWhich( sal_uInt16 nNewWhich ) const; + template<class T> T* CloneSetWhich( TypedWhichId<T> nId ) const + { + return static_cast<T*>(CloneSetWhich(nId.Which())); + } sal_uInt32 GetRefCount() const { return m_nRefCount; } SfxItemKind GetKind() const { return m_nKind; } diff --git a/include/svl/typedwhich.hxx b/include/svl/typedwhich.hxx new file mode 100644 index 000000000000..43318cc52303 --- /dev/null +++ b/include/svl/typedwhich.hxx @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#ifndef INCLUDED_SVL_TYPEDWHICH_HXX +#define INCLUDED_SVL_TYPEDWHICH_HXX + +#include <sal/config.h> +#include <sal/types.h> + +/** + * A very thin wrapper around the sal_uInt16 WhichId whose purpose is mostly to carry type information, + * so that we Put() and Get() the right subclasses of SfxPoolItem for each WhichId. + */ +template <class T> class TypedWhichId final +{ +public: + constexpr TypedWhichId(sal_uInt16 nWhich) + : mnWhich(nWhich) + { + } + constexpr sal_uInt16 Which() const { return mnWhich; } + //constexpr operator sal_uInt16() const { return mnWhich; } +private: + sal_uInt16 const mnWhich; +}; + +template <class T> constexpr bool operator==(sal_uInt16 lhs, TypedWhichId<T> const& rhs) +{ + return lhs == rhs.Which(); +} +template <class T> constexpr bool operator!=(sal_uInt16 lhs, TypedWhichId<T> const& rhs) +{ + return lhs != rhs.Which(); +} +template <class T> constexpr bool operator==(TypedWhichId<T> const& lhs, sal_uInt16 rhs) +{ + return lhs.Which() == rhs; +} +template <class T> constexpr bool operator!=(TypedWhichId<T> const& lhs, sal_uInt16 rhs) +{ + return lhs.Which() != rhs; +} + +#endif // INCLUDED_SVL_TYPEDWHICH_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |