sc: filter: rtf: add method "AddFont"
[LibreOffice.git] / include / svl / itemset.hxx
blob5ce13bb1f4c8917565fe8217c54aa57b3f676e48
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #pragma once
21 #include <sal/config.h>
23 #include <cassert>
24 #include <cstddef>
25 #include <memory>
26 #include <utility>
28 #include <svl/svldllapi.h>
29 #include <svl/poolitem.hxx>
30 #include <svl/typedwhich.hxx>
31 #include <svl/whichranges.hxx>
33 class SfxItemPool;
35 class SAL_WARN_UNUSED SVL_DLLPUBLIC SfxItemSet
37 friend class SfxItemIter;
39 SfxItemPool* m_pPool; ///< pool that stores the items
40 const SfxItemSet* m_pParent; ///< derivation
41 SfxPoolItem const** m_ppItems; ///< pointer to array of items, we allocate and free this unless m_bItemsFixed==true
42 WhichRangesContainer m_pWhichRanges; ///< array of Which Ranges
43 sal_uInt16 m_nCount; ///< number of items
44 bool m_bItemsFixed; ///< true if this is a SfxItemSetFixed object
46 friend class SfxItemPoolCache;
47 friend class SfxAllItemSet;
49 private:
50 SVL_DLLPRIVATE void RecreateRanges_Impl(const WhichRangesContainer& pNewRanges);
52 SfxItemSet( SfxItemPool & pool, const WhichRangesContainer& wids, std::size_t items );
54 public:
55 SfxPoolItem const** GetItems_Impl() const { return m_ppItems; }
57 private:
58 const SfxItemSet& operator=(const SfxItemSet &) = delete;
60 protected:
61 // Notification-Callback
62 virtual void Changed( const SfxPoolItem& rOld, const SfxPoolItem& rNew );
64 void PutDirect(const SfxPoolItem &rItem);
66 virtual const SfxPoolItem* PutImpl( const SfxPoolItem&, sal_uInt16 nWhich, bool bPassingOwnership );
68 /** special constructor for SfxAllItemSet */
69 enum class SfxAllItemSetFlag { Flag };
70 SfxItemSet( SfxItemPool&, SfxAllItemSetFlag );
71 /** special constructor for SfxItemSetFixed */
72 SfxItemSet( SfxItemPool&, WhichRangesContainer&& ranges, SfxPoolItem const ** ppItems );
74 public:
75 SfxItemSet( const SfxItemSet& );
76 SfxItemSet( SfxItemSet&& ) noexcept;
77 SfxItemSet( SfxItemPool& );
78 SfxItemSet( SfxItemPool&, const WhichRangesContainer& ranges );
79 SfxItemSet( SfxItemPool&, WhichRangesContainer&& ranges );
81 SfxItemSet( SfxItemPool& rPool, sal_uInt16 nWhichStart, sal_uInt16 nWhichEnd )
82 : SfxItemSet(rPool, WhichRangesContainer(nWhichStart, nWhichEnd)) {}
84 template<sal_uInt16... WIDs>
85 SfxItemSet(SfxItemPool& pool, svl::Items_t<WIDs...> wids)
86 : SfxItemSet(pool, WhichRangesContainer(wids)) {}
88 virtual ~SfxItemSet();
90 virtual std::unique_ptr<SfxItemSet> Clone(bool bItems = true, SfxItemPool *pToPool = nullptr) const;
91 virtual SfxItemSet CloneAsValue(bool bItems = true, SfxItemPool *pToPool = nullptr) const;
93 // Get number of items
94 sal_uInt16 Count() const { return m_nCount; }
95 sal_uInt16 TotalCount() const;
97 const SfxPoolItem& Get( sal_uInt16 nWhich, bool bSrchInParent = true ) const;
98 template<class T>
99 const T& Get( TypedWhichId<T> nWhich, bool bSrchInParent = true ) const
101 return static_cast<const T&>(Get(sal_uInt16(nWhich), bSrchInParent));
104 /** This method eases accessing single Items in the SfxItemSet.
106 @param nId SlotId or the Item's WhichId
107 @param bSearchInParent also search in parent ItemSets
108 @returns 0 if the ItemSet does not contain an Item with the Id 'nWhich'
110 const SfxPoolItem* GetItem(sal_uInt16 nWhich, bool bSearchInParent = true) const;
112 /// Templatized version of GetItem() to directly return the correct type.
113 template<class T> const T* GetItem(sal_uInt16 nWhich, bool bSearchInParent = true) const
115 const SfxPoolItem* pItem = GetItem(nWhich, bSearchInParent);
116 const T* pCastedItem = dynamic_cast<const T*>(pItem);
118 assert(!pItem || pCastedItem); // if it exists, must have the correct type
119 return pCastedItem;
121 template<class T> const T* GetItem( TypedWhichId<T> nWhich, bool bSearchInParent = true ) const
123 return GetItem<T>(sal_uInt16(nWhich), bSearchInParent);
127 /// Templatized static version of GetItem() to directly return the correct type if the SfxItemSet is available.
128 template<class T> static const T* GetItem(const SfxItemSet* pItemSet, sal_uInt16 nWhich, bool bSearchInParent)
130 if (pItemSet)
131 return pItemSet->GetItem<T>(nWhich, bSearchInParent);
133 return nullptr;
135 template <class T>
136 static const T* GetItem(const SfxItemSet* pItemSet, TypedWhichId<T> nWhich,
137 bool bSearchInParent)
139 return GetItem<T>(pItemSet, static_cast<sal_uInt16>(nWhich), bSearchInParent);
142 sal_uInt16 GetWhichByPos(sal_uInt16 nPos) const;
144 SfxItemState GetItemState( sal_uInt16 nWhich,
145 bool bSrchInParent = true,
146 const SfxPoolItem **ppItem = nullptr ) const;
148 bool HasItem(sal_uInt16 nWhich, const SfxPoolItem** ppItem = nullptr) const;
150 void DisableItem(sal_uInt16 nWhich);
151 void InvalidateItem( sal_uInt16 nWhich );
152 sal_uInt16 ClearItem( sal_uInt16 nWhich = 0);
153 void ClearInvalidItems();
154 void InvalidateAllItems(); // HACK(via nWhich = 0) ???
156 inline void SetParent( const SfxItemSet* pNew );
158 // add, delete items, work on items
159 public:
160 const SfxPoolItem* Put( const SfxPoolItem& rItem, sal_uInt16 nWhich )
161 { return PutImpl(rItem, nWhich, /*bPassingOwnership*/false); }
162 const SfxPoolItem* Put( std::unique_ptr<SfxPoolItem> xItem, sal_uInt16 nWhich )
163 { return PutImpl(*xItem.release(), nWhich, /*bPassingOwnership*/true); }
164 const SfxPoolItem* Put( const SfxPoolItem& rItem )
165 { return Put(rItem, rItem.Which()); }
166 const SfxPoolItem* Put( std::unique_ptr<SfxPoolItem> xItem )
167 { auto nWhich = xItem->Which(); return Put(std::move(xItem), nWhich); }
168 bool Put( const SfxItemSet&,
169 bool bInvalidAsDefault = true );
170 void PutExtended( const SfxItemSet&,
171 SfxItemState eDontCareAs,
172 SfxItemState eDefaultAs );
174 bool Set( const SfxItemSet&, bool bDeep = true );
176 void Intersect( const SfxItemSet& rSet );
177 void MergeValues( const SfxItemSet& rSet );
178 void Differentiate( const SfxItemSet& rSet );
179 void MergeValue( const SfxPoolItem& rItem, bool bOverwriteDefaults = false );
181 SfxItemPool* GetPool() const { return m_pPool; }
182 const WhichRangesContainer & GetRanges() const { return m_pWhichRanges; }
183 void SetRanges( const WhichRangesContainer& );
184 void SetRanges( WhichRangesContainer&& );
185 void MergeRange( sal_uInt16 nFrom, sal_uInt16 nTo );
186 const SfxItemSet* GetParent() const { return m_pParent; }
188 bool operator==(const SfxItemSet &) const;
190 /** Compare possibly ignoring SfxItemPool pointer.
192 This can be used to compare the content of two SfxItemSet even if they
193 don't share the same pool. EditTextObject::Equals(...,false) uses this
194 which is needed in ScGlobal::EETextObjEqual() for
195 ScPageHFItem::operator==()
197 @param bComparePool
198 if <FALSE/> ignore SfxItemPool pointer,
199 if <TRUE/> compare also SfxItemPool pointer (identical to operator==())
201 bool Equals(const SfxItemSet &, bool bComparePool) const;
203 void dumpAsXml(xmlTextWriterPtr pWriter) const;
206 inline void SfxItemSet::SetParent( const SfxItemSet* pNew )
208 m_pParent = pNew;
211 class SVL_DLLPUBLIC SfxAllItemSet final : public SfxItemSet
213 // Handles all Ranges. Ranges are automatically modified by putting items.
216 public:
217 SfxAllItemSet( SfxItemPool &rPool );
218 SfxAllItemSet( const SfxItemSet & );
219 SfxAllItemSet( const SfxAllItemSet & );
221 virtual std::unique_ptr<SfxItemSet> Clone( bool bItems = true, SfxItemPool *pToPool = nullptr ) const override;
222 virtual SfxItemSet CloneAsValue( bool bItems = true, SfxItemPool *pToPool = nullptr ) const override;
223 private:
224 virtual const SfxPoolItem* PutImpl( const SfxPoolItem&, sal_uInt16 nWhich, bool bPassingOwnership ) override;
228 namespace svl::detail
231 * Determines the number of sal_uInt16s in a container of pairs of
232 * sal_uInt16s, each representing a range of sal_uInt16s, and total capacity of the ranges.
234 template <sal_uInt16 WID1, sal_uInt16 WID2, sal_uInt16... Rest>
235 static constexpr sal_uInt16 CountRanges1()
237 sal_uInt16 nCapacity = rangeSize(WID1, WID2);
238 if constexpr (sizeof...(Rest) > 0)
239 nCapacity += CountRanges1<Rest...>();
240 return nCapacity;
243 // Allocate the items array inside the object, to reduce allocation cost.
245 template<sal_uInt16... WIDs>
246 class SfxItemSetFixed : public SfxItemSet
248 public:
249 SfxItemSetFixed( SfxItemPool& rPool)
250 : SfxItemSet(rPool, WhichRangesContainer(svl::Items_t<WIDs...>{}), m_aItems) {}
251 private:
252 static constexpr sal_uInt16 NITEMS = svl::detail::CountRanges1<WIDs...>();
253 const SfxPoolItem* m_aItems[NITEMS] = {};
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */