LibreOffice Module svl (master) 1
itemset.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#pragma once
20
21#include <sal/config.h>
22
23#include <cassert>
24#include <cstddef>
25#include <memory>
26#include <optional>
27#include <utility>
28
29#include <svl/svldllapi.h>
30#include <svl/poolitem.hxx>
31#include <svl/typedwhich.hxx>
32#include <svl/whichranges.hxx>
33
34class SfxItemPool;
35
37{
38 friend class SfxItemIter;
39 friend class SfxWhichIter;
40
45 sal_uInt16 m_nCount;
47
48friend class SfxItemPoolCache;
49friend class SfxAllItemSet;
50
51private:
53
54public:
55 SfxPoolItem const** GetItems_Impl() const { return m_ppItems; }
56
57private:
58 const SfxItemSet& operator=(const SfxItemSet &) = delete;
59
60protected:
61 // Notification-Callback
62 virtual void Changed( const SfxPoolItem& rOld, const SfxPoolItem& rNew );
63
64 void PutDirect(const SfxPoolItem &rItem);
65
66 virtual const SfxPoolItem* PutImpl( const SfxPoolItem&, sal_uInt16 nWhich, bool bPassingOwnership );
67
69 enum class SfxAllItemSetFlag { Flag };
72 SfxItemSet( SfxItemPool&, WhichRangesContainer&& ranges, SfxPoolItem const ** ppItems );
73
74public:
75 SfxItemSet( const SfxItemSet& );
76 SfxItemSet( SfxItemSet&& ) noexcept;
79
80 SfxItemSet( SfxItemPool& rPool, sal_uInt16 nWhichStart, sal_uInt16 nWhichEnd )
81 : SfxItemSet(rPool, WhichRangesContainer(nWhichStart, nWhichEnd)) {}
82
83 template<sal_uInt16... WIDs>
85 : SfxItemSet(pool, WhichRangesContainer(wids)) {}
86
87 virtual ~SfxItemSet();
88
89 virtual std::unique_ptr<SfxItemSet> Clone(bool bItems = true, SfxItemPool *pToPool = nullptr) const;
92 SfxItemSet CloneAsValue(bool bItems = true, SfxItemPool *pToPool = nullptr) const;
93
94 // Get number of items
95 sal_uInt16 Count() const { return m_nCount; }
96 sal_uInt16 TotalCount() const;
97
98 const SfxPoolItem& Get( sal_uInt16 nWhich, bool bSrchInParent = true ) const;
99 template<class T>
100 const T& Get( TypedWhichId<T> nWhich, bool bSrchInParent = true ) const
101 {
102 return static_cast<const T&>(Get(sal_uInt16(nWhich), bSrchInParent));
103 }
104
111 const SfxPoolItem* GetItem(sal_uInt16 nWhich, bool bSearchInParent = true) const;
112
114 template<class T> const T* GetItem(sal_uInt16 nWhich, bool bSearchInParent = true) const
115 {
116 const SfxPoolItem* pItem = GetItem(nWhich, bSearchInParent);
117 const T* pCastedItem = dynamic_cast<const T*>(pItem);
118
119 assert(!pItem || pCastedItem); // if it exists, must have the correct type
120 return pCastedItem;
121 }
122 template<class T> const T* GetItem( TypedWhichId<T> nWhich, bool bSearchInParent = true ) const
123 {
124 return GetItem<T>(sal_uInt16(nWhich), bSearchInParent);
125 }
126
127
129 template<class T> static const T* GetItem(const SfxItemSet* pItemSet, sal_uInt16 nWhich, bool bSearchInParent)
130 {
131 if (pItemSet)
132 return pItemSet->GetItem<T>(nWhich, bSearchInParent);
133
134 return nullptr;
135 }
136 template <class T>
137 static const T* GetItem(const SfxItemSet* pItemSet, TypedWhichId<T> nWhich,
138 bool bSearchInParent)
139 {
140 return GetItem<T>(pItemSet, static_cast<sal_uInt16>(nWhich), bSearchInParent);
141 }
142
143 sal_uInt16 GetWhichByPos(sal_uInt16 nPos) const;
144
145 SfxItemState GetItemState( sal_uInt16 nWhich,
146 bool bSrchInParent = true,
147 const SfxPoolItem **ppItem = nullptr ) const;
148
149 template <class T>
151 bool bSrchInParent = true,
152 const T **ppItem = nullptr ) const
153 { return GetItemState(sal_uInt16(nWhich), bSrchInParent, reinterpret_cast<SfxPoolItem const**>(ppItem)); }
154
156 template<class T>
157 const T * GetItemIfSet( TypedWhichId<T> nWhich,
158 bool bSrchInParent = true ) const
159 {
160 const SfxPoolItem * pItem = nullptr;
161 if( SfxItemState::SET == GetItemState(sal_uInt16(nWhich), bSrchInParent, &pItem) )
162 return static_cast<const T*>(pItem);
163 return nullptr;
164 }
165
166 bool HasItem(sal_uInt16 nWhich, const SfxPoolItem** ppItem = nullptr) const;
167 template<class T>
168 bool HasItem(TypedWhichId<T> nWhich, const T** ppItem = nullptr) const
169 { return HasItem(sal_uInt16(nWhich), reinterpret_cast<const SfxPoolItem**>(ppItem)); }
170
171 void DisableItem(sal_uInt16 nWhich);
172 void InvalidateItem( sal_uInt16 nWhich );
173 sal_uInt16 ClearItem( sal_uInt16 nWhich = 0);
174 void ClearInvalidItems();
175 void InvalidateAllItems(); // HACK(via nWhich = 0) ???
176
177 inline void SetParent( const SfxItemSet* pNew );
178
179 // add, delete items, work on items
180public:
181 const SfxPoolItem* Put( const SfxPoolItem& rItem, sal_uInt16 nWhich )
182 { return PutImpl(rItem, nWhich, /*bPassingOwnership*/false); }
183 const SfxPoolItem* Put( std::unique_ptr<SfxPoolItem> xItem, sal_uInt16 nWhich )
184 { return PutImpl(*xItem.release(), nWhich, /*bPassingOwnership*/true); }
185 const SfxPoolItem* Put( const SfxPoolItem& rItem )
186 { return Put(rItem, rItem.Which()); }
187 const SfxPoolItem* Put( std::unique_ptr<SfxPoolItem> xItem )
188 { auto nWhich = xItem->Which(); return Put(std::move(xItem), nWhich); }
189 bool Put( const SfxItemSet&,
190 bool bInvalidAsDefault = true );
191 void PutExtended( const SfxItemSet&,
192 SfxItemState eDontCareAs,
193 SfxItemState eDefaultAs );
194
195 bool Set( const SfxItemSet&, bool bDeep = true );
196
197 void Intersect( const SfxItemSet& rSet );
198 void MergeValues( const SfxItemSet& rSet );
199 void Differentiate( const SfxItemSet& rSet );
200 void MergeValue( const SfxPoolItem& rItem, bool bOverwriteDefaults = false );
201
202 SfxItemPool* GetPool() const { return m_pPool; }
204 void SetRanges( const WhichRangesContainer& );
206 void MergeRange( sal_uInt16 nFrom, sal_uInt16 nTo );
207 const SfxItemSet* GetParent() const { return m_pParent; }
208
209 bool operator==(const SfxItemSet &) const;
210
222 bool Equals(const SfxItemSet &, bool bComparePool) const;
223
224 void dumpAsXml(xmlTextWriterPtr pWriter) const;
225
226private:
227 sal_uInt16 ClearSingleItemImpl( sal_uInt16 nWhich, std::optional<sal_uInt16> oItemOffsetHint );
228 sal_uInt16 ClearAllItemsImpl();
229 SfxItemState GetItemStateImpl( sal_uInt16 nWhich,
230 bool bSrchInParent,
231 const SfxPoolItem **ppItem,
232 std::optional<sal_uInt16> oItemsOffsetHint) const;
233};
234
235inline void SfxItemSet::SetParent( const SfxItemSet* pNew )
236{
237 m_pParent = pNew;
238}
239
241
242// Handles all Ranges. Ranges are automatically modified by putting items.
243
244{
245public:
246 SfxAllItemSet( SfxItemPool &rPool );
247 SfxAllItemSet( const SfxItemSet & );
248 SfxAllItemSet( const SfxAllItemSet & );
249
250 virtual std::unique_ptr<SfxItemSet> Clone( bool bItems = true, SfxItemPool *pToPool = nullptr ) const override;
251private:
252 virtual const SfxPoolItem* PutImpl( const SfxPoolItem&, sal_uInt16 nWhich, bool bPassingOwnership ) override;
253};
254
255
256namespace svl::detail
257{
262template <sal_uInt16 WID1, sal_uInt16 WID2, sal_uInt16... Rest>
263static constexpr sal_uInt16 CountRanges1()
264{
265 sal_uInt16 nCapacity = rangeSize(WID1, WID2);
266 if constexpr (sizeof...(Rest) > 0)
267 nCapacity += CountRanges1<Rest...>();
268 return nCapacity;
269}}
270
271// Allocate the items array inside the object, to reduce allocation cost.
272//
273template<sal_uInt16... WIDs>
275{
276public:
278 : SfxItemSet(rPool, WhichRangesContainer(svl::Items_t<WIDs...>{}), m_aItems) {}
279private:
280 static constexpr sal_uInt16 NITEMS = svl::detail::CountRanges1<WIDs...>();
282};
283
284/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual const SfxPoolItem * PutImpl(const SfxPoolItem &, sal_uInt16 nWhich, bool bPassingOwnership) override
Putting with automatic extension of the WhichId with the ID of the Item.
Definition: itemset.cxx:1411
virtual std::unique_ptr< SfxItemSet > Clone(bool bItems=true, SfxItemPool *pToPool=nullptr) const override
Definition: itemset.cxx:1426
Base class for providers of defaults of SfxPoolItems.
Definition: itempool.hxx:51
static constexpr sal_uInt16 NITEMS
Definition: itemset.hxx:280
const SfxPoolItem * m_aItems[NITEMS]
Definition: itemset.hxx:281
SfxItemSetFixed(SfxItemPool &rPool)
Definition: itemset.hxx:277
virtual void Changed(const SfxPoolItem &rOld, const SfxPoolItem &rNew)
Notification callback.
Definition: itemset.cxx:847
void MergeValues(const SfxItemSet &rSet)
Definition: itemset.cxx:1103
SfxItemPool * m_pPool
pool that stores the items
Definition: itemset.hxx:41
SfxItemSet(SfxItemPool &, SfxAllItemSetFlag)
Definition: itemset.cxx:57
const WhichRangesContainer & GetRanges() const
Definition: itemset.hxx:203
SfxItemPool * GetPool() const
Definition: itemset.hxx:202
sal_uInt16 ClearAllItemsImpl()
Definition: itemset.cxx:254
void SetParent(const SfxItemSet *pNew)
Definition: itemset.hxx:235
sal_uInt16 Count() const
Definition: itemset.hxx:95
void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: itemset.cxx:1367
static const T * GetItem(const SfxItemSet *pItemSet, sal_uInt16 nWhich, bool bSearchInParent)
Templatized static version of GetItem() to directly return the correct type if the SfxItemSet is avai...
Definition: itemset.hxx:129
bool m_bItemsFixed
true if this is a SfxItemSetFixed object
Definition: itemset.hxx:46
void PutExtended(const SfxItemSet &, SfxItemState eDontCareAs, SfxItemState eDefaultAs)
This method takes the Items from the 'rSet' and adds to '*this'.
Definition: itemset.cxx:544
virtual std::unique_ptr< SfxItemSet > Clone(bool bItems=true, SfxItemPool *pToPool=nullptr) const
Definition: itemset.cxx:1273
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
Templatized version of GetItemState() to directly return the correct type.
Definition: itemset.hxx:157
static const T * GetItem(const SfxItemSet *pItemSet, TypedWhichId< T > nWhich, bool bSearchInParent)
Definition: itemset.hxx:137
const SfxPoolItem * Put(std::unique_ptr< SfxPoolItem > xItem, sal_uInt16 nWhich)
Definition: itemset.hxx:183
const SfxItemSet * GetParent() const
Definition: itemset.hxx:207
sal_uInt16 ClearSingleItemImpl(sal_uInt16 nWhich, std::optional< sal_uInt16 > oItemOffsetHint)
Definition: itemset.cxx:200
sal_uInt16 ClearItem(sal_uInt16 nWhich=0)
Delete single Items or all Items (nWhich == 0)
Definition: itemset.cxx:190
void InvalidateAllItems()
Definition: itemset.cxx:314
const SfxItemSet * m_pParent
derivation
Definition: itemset.hxx:42
void ClearInvalidItems()
Definition: itemset.cxx:300
const SfxItemSet & operator=(const SfxItemSet &)=delete
SfxPoolItem const ** GetItems_Impl() const
Definition: itemset.hxx:55
bool HasItem(sal_uInt16 nWhich, const SfxPoolItem **ppItem=nullptr) const
Definition: itemset.cxx:392
void MergeValue(const SfxPoolItem &rItem, bool bOverwriteDefaults=false)
Definition: itemset.cxx:1140
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
Definition: itemset.cxx:321
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
This method eases accessing single Items in the SfxItemSet.
Definition: itemset.cxx:779
void PutDirect(const SfxPoolItem &rItem)
Definition: itemset.cxx:1327
sal_uInt16 TotalCount() const
Definition: itemset.cxx:851
SfxItemSet(SfxItemPool &pool, svl::Items_t< WIDs... > wids)
Definition: itemset.hxx:84
SfxItemState GetItemState(TypedWhichId< T > nWhich, bool bSrchInParent=true, const T **ppItem=nullptr) const
Definition: itemset.hxx:150
bool HasItem(TypedWhichId< T > nWhich, const T **ppItem=nullptr) const
Definition: itemset.hxx:168
SfxItemSet CloneAsValue(bool bItems=true, SfxItemPool *pToPool=nullptr) const
note that this only works if you know for sure that you are dealing with an SfxItemSet and not one of...
Definition: itemset.cxx:1298
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
Definition: itemset.hxx:181
SfxItemState GetItemStateImpl(sal_uInt16 nWhich, bool bSrchInParent, const SfxPoolItem **ppItem, std::optional< sal_uInt16 > oItemsOffsetHint) const
Definition: itemset.cxx:328
SfxAllItemSetFlag
special constructor for SfxAllItemSet
Definition: itemset.hxx:69
void Intersect(const SfxItemSet &rSet)
Only retain the Items that are also present in rSet (nevermind their value).
Definition: itemset.cxx:860
WhichRangesContainer m_pWhichRanges
array of Which Ranges
Definition: itemset.hxx:44
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
Definition: itemset.cxx:795
bool Set(const SfxItemSet &, bool bDeep=true)
The SfxItemSet takes over exactly those SfxPoolItems that are set in rSet and are in their own Which ...
Definition: itemset.cxx:728
const T * GetItem(TypedWhichId< T > nWhich, bool bSearchInParent=true) const
Definition: itemset.hxx:122
void DisableItem(sal_uInt16 nWhich)
Disable Item Using a VoidItem with Which value 0.
Definition: itemset.cxx:1421
void SetRanges(const WhichRangesContainer &)
Modifies the ranges of settable items.
Definition: itemset.cxx:627
const SfxPoolItem * Put(std::unique_ptr< SfxPoolItem > xItem)
Definition: itemset.hxx:187
virtual ~SfxItemSet()
Definition: itemset.cxx:156
sal_uInt16 GetWhichByPos(sal_uInt16 nPos) const
Definition: itemset.cxx:1186
SfxPoolItem const ** m_ppItems
pointer to array of items, we allocate and free this unless m_bItemsFixed==true
Definition: itemset.hxx:43
const T * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
Templatized version of GetItem() to directly return the correct type.
Definition: itemset.hxx:114
virtual const SfxPoolItem * PutImpl(const SfxPoolItem &, sal_uInt16 nWhich, bool bPassingOwnership)
Definition: itemset.cxx:400
SVL_DLLPRIVATE void RecreateRanges_Impl(const WhichRangesContainer &pNewRanges)
Definition: itemset.cxx:647
const SfxPoolItem * Put(const SfxPoolItem &rItem)
Definition: itemset.hxx:185
friend class SfxAllItemSet
Definition: itemset.hxx:49
sal_uInt16 m_nCount
number of items
Definition: itemset.hxx:45
void Differentiate(const SfxItemSet &rSet)
Definition: itemset.cxx:914
void MergeRange(sal_uInt16 nFrom, sal_uInt16 nTo)
Expands the ranges of settable items by 'nFrom' to 'nTo'.
Definition: itemset.cxx:610
bool Equals(const SfxItemSet &, bool bComparePool) const
Compare possibly ignoring SfxItemPool pointer.
Definition: itemset.cxx:1205
void InvalidateItem(sal_uInt16 nWhich)
Definition: itemset.cxx:1157
const T & Get(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
Definition: itemset.hxx:100
sal_uInt16 Which() const
Definition: poolitem.hxx:149
Iterates over the which ids and the pool items arrays together (which are stored in parallel arrays).
Definition: whiter.hxx:34
A very thin wrapper around the sal_uInt16 WhichId whose purpose is mostly to carry type information,...
Definition: typedwhich.hxx:21
struct _xmlTextWriter * xmlTextWriterPtr
static constexpr sal_uInt16 CountRanges1()
Determines the number of sal_uInt16s in a container of pairs of sal_uInt16s, each representing a rang...
Definition: itemset.hxx:263
constexpr std::size_t rangeSize(sal_uInt16 wid1, sal_uInt16 wid2)
Definition: whichranges.hxx:41
SfxItemState
These values have to match the values in the css::frame::status::ItemState IDL to be found at offapi/...
Definition: poolitem.hxx:82
@ SET
The property has been explicitly set to a given value hence we know we are not taking the default val...
Most of the time, the which ranges we point at are a compile-time literal.
Definition: whichranges.hxx:79
#define SVL_DLLPRIVATE
Definition: svldllapi.h:30
#define SVL_DLLPUBLIC
Definition: svldllapi.h:28
constexpr bool operator==(TypedWhichId< T > const &lhs, TypedWhichId< T > rhs)
Definition: typedwhich.hxx:43
#define SAL_WARN_UNUSED