1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_SVL_ITEMSET_HXX
20 #define INCLUDED_SVL_ITEMSET_HXX
22 #include <sal/config.h>
26 #include <initializer_list>
27 #include <type_traits>
30 #include <svl/svldllapi.h>
31 #include <svl/poolitem.hxx>
32 #include <svl/typedwhich.hxx>
40 constexpr bool validRange(sal_uInt16 wid1
, sal_uInt16 wid2
)
41 { return wid1
!= 0 && wid1
<= wid2
; }
43 constexpr bool validGap(sal_uInt16 wid1
, sal_uInt16 wid2
)
44 { return wid2
> wid1
&& wid2
- wid1
> 1; }
46 template<sal_uInt16 WID1
, sal_uInt16 WID2
> constexpr bool validRanges()
47 { return validRange(WID1
, WID2
); }
49 template<sal_uInt16 WID1
, sal_uInt16 WID2
, sal_uInt16 WID3
, sal_uInt16
... WIDs
>
50 constexpr bool validRanges() {
51 return validRange(WID1
, WID2
) && validGap(WID2
, WID3
)
52 && validRanges
<WID3
, WIDs
...>();
55 // The calculations in rangeSize and rangesSize cannot overflow, assuming
56 // std::size_t is no smaller than sal_uInt16:
58 constexpr std::size_t rangeSize(sal_uInt16 wid1
, sal_uInt16 wid2
) {
59 assert(validRange(wid1
, wid2
));
60 return wid2
- wid1
+ 1;
63 template<sal_uInt16 WID1
, sal_uInt16 WID2
> constexpr std::size_t rangesSize()
64 { return rangeSize(WID1
, WID2
); }
66 template<sal_uInt16 WID1
, sal_uInt16 WID2
, sal_uInt16 WID3
, sal_uInt16
... WIDs
>
67 constexpr std::size_t rangesSize()
68 { return rangeSize(WID1
, WID2
) + rangesSize
<WID3
, WIDs
...>(); }
72 template<sal_uInt16
... WIDs
> struct Items
{};
76 class SAL_WARN_UNUSED SVL_DLLPUBLIC SfxItemSet
78 friend class SfxItemIter
;
80 SfxItemPool
* m_pPool
; ///< pool that stores the items
81 const SfxItemSet
* m_pParent
; ///< derivation
82 std::unique_ptr
<SfxPoolItem
const*[]>
83 m_pItems
; ///< array of items
84 sal_uInt16
* m_pWhichRanges
; ///< array of Which Ranges
85 sal_uInt16 m_nCount
; ///< number of items
87 friend class SfxItemPoolCache
;
88 friend class SfxAllItemSet
;
91 SVL_DLLPRIVATE
void InitRanges_Impl(const sal_uInt16
*nWhichPairTable
);
94 SfxItemPool
& pool
, std::initializer_list
<sal_uInt16
> wids
,
98 SfxPoolItem
const** GetItems_Impl() const { return m_pItems
.get(); }
101 const SfxItemSet
& operator=(const SfxItemSet
&) = delete;
104 // Notification-Callback
105 virtual void Changed( const SfxPoolItem
& rOld
, const SfxPoolItem
& rNew
);
107 void PutDirect(const SfxPoolItem
&rItem
);
109 virtual const SfxPoolItem
* PutImpl( const SfxPoolItem
&, sal_uInt16 nWhich
, bool bPassingOwnership
);
112 struct Pair
{ sal_uInt16 wid1
, wid2
; };
114 SfxItemSet( const SfxItemSet
& );
115 SfxItemSet( SfxItemSet
&& );
117 SfxItemSet( SfxItemPool
&);
118 template<sal_uInt16
... WIDs
> SfxItemSet(
119 typename
std::enable_if
<
120 svl::detail::validRanges
<WIDs
...>(), SfxItemPool
&>::type pool
,
121 svl::Items
<WIDs
...>):
122 SfxItemSet(pool
, {WIDs
...}, svl::detail::rangesSize
<WIDs
...>()) {}
123 SfxItemSet( SfxItemPool
&, std::initializer_list
<Pair
> wids
);
124 SfxItemSet( SfxItemPool
&, const sal_uInt16
* nWhichPairTable
);
125 virtual ~SfxItemSet();
127 virtual std::unique_ptr
<SfxItemSet
> Clone(bool bItems
= true, SfxItemPool
*pToPool
= nullptr) const;
129 // Get number of items
130 sal_uInt16
Count() const { return m_nCount
; }
131 sal_uInt16
TotalCount() const;
133 const SfxPoolItem
& Get( sal_uInt16 nWhich
, bool bSrchInParent
= true ) const;
135 const T
& Get( TypedWhichId
<T
> nWhich
, bool bSrchInParent
= true ) const
137 return static_cast<const T
&>(Get(sal_uInt16(nWhich
), bSrchInParent
));
140 /** This method eases accessing single Items in the SfxItemSet.
142 @param nId SlotId or the Item's WhichId
143 @param bSearchInParent also search in parent ItemSets
144 @returns 0 if the ItemSet does not contain an Item with the Id 'nWhich'
146 const SfxPoolItem
* GetItem(sal_uInt16 nWhich
, bool bSearchInParent
= true) const;
148 /// Templatized version of GetItem() to directly return the correct type.
149 template<class T
> const T
* GetItem(sal_uInt16 nWhich
, bool bSearchInParent
= true) const
151 const SfxPoolItem
* pItem
= GetItem(nWhich
, bSearchInParent
);
152 const T
* pCastedItem
= dynamic_cast<const T
*>(pItem
);
154 assert(!pItem
|| pCastedItem
); // if it exists, must have the correct type
157 template<class T
> const T
* GetItem( TypedWhichId
<T
> nWhich
, bool bSearchInParent
= true ) const
159 return GetItem
<T
>(sal_uInt16(nWhich
), bSearchInParent
);
163 /// Templatized static version of GetItem() to directly return the correct type if the SfxItemSet is available.
164 template<class T
> static const T
* GetItem(const SfxItemSet
* pItemSet
, sal_uInt16 nWhich
, bool bSearchInParent
)
167 return pItemSet
->GetItem
<T
>(nWhich
, bSearchInParent
);
172 static const T
* GetItem(const SfxItemSet
* pItemSet
, TypedWhichId
<T
> nWhich
,
173 bool bSearchInParent
)
175 return GetItem
<T
>(pItemSet
, static_cast<sal_uInt16
>(nWhich
), bSearchInParent
);
178 sal_uInt16
GetWhichByPos(sal_uInt16 nPos
) const;
180 SfxItemState
GetItemState( sal_uInt16 nWhich
,
181 bool bSrchInParent
= true,
182 const SfxPoolItem
**ppItem
= nullptr ) const;
184 bool HasItem(sal_uInt16 nWhich
, const SfxPoolItem
** ppItem
= nullptr) const;
186 void DisableItem(sal_uInt16 nWhich
);
187 void InvalidateItem( sal_uInt16 nWhich
);
188 sal_uInt16
ClearItem( sal_uInt16 nWhich
= 0);
189 void ClearInvalidItems();
190 void InvalidateAllItems(); // HACK(via nWhich = 0) ???
192 inline void SetParent( const SfxItemSet
* pNew
);
194 // add, delete items, work on items
196 const SfxPoolItem
* Put( const SfxPoolItem
& rItem
, sal_uInt16 nWhich
)
197 { return PutImpl(rItem
, nWhich
, /*bPassingOwnership*/false); }
198 const SfxPoolItem
* Put( std::unique_ptr
<SfxPoolItem
> xItem
, sal_uInt16 nWhich
)
199 { return PutImpl(*xItem
.release(), nWhich
, /*bPassingOwnership*/true); }
200 const SfxPoolItem
* Put( const SfxPoolItem
& rItem
)
201 { return Put(rItem
, rItem
.Which()); }
202 const SfxPoolItem
* Put( std::unique_ptr
<SfxPoolItem
> xItem
)
203 { auto nWhich
= xItem
->Which(); return Put(std::move(xItem
), nWhich
); }
204 bool Put( const SfxItemSet
&,
205 bool bInvalidAsDefault
= true );
206 void PutExtended( const SfxItemSet
&,
207 SfxItemState eDontCareAs
,
208 SfxItemState eDefaultAs
);
210 bool Set( const SfxItemSet
&, bool bDeep
= true );
212 void Intersect( const SfxItemSet
& rSet
);
213 void MergeValues( const SfxItemSet
& rSet
);
214 void Differentiate( const SfxItemSet
& rSet
);
215 void MergeValue( const SfxPoolItem
& rItem
, bool bOverwriteDefaults
= false );
217 SfxItemPool
* GetPool() const { return m_pPool
; }
218 const sal_uInt16
* GetRanges() const { return m_pWhichRanges
; }
219 void SetRanges( const sal_uInt16
*pRanges
);
220 void MergeRange( sal_uInt16 nFrom
, sal_uInt16 nTo
);
221 const SfxItemSet
* GetParent() const { return m_pParent
; }
223 bool operator==(const SfxItemSet
&) const;
225 /** Compare possibly ignoring SfxItemPool pointer.
227 This can be used to compare the content of two SfxItemSet even if they
228 don't share the same pool. EditTextObject::Equals(...,false) uses this
229 which is needed in ScGlobal::EETextObjEqual() for
230 ScPageHFItem::operator==()
233 if <FALSE/> ignore SfxItemPool pointer,
234 if <TRUE/> compare also SfxItemPool pointer (identical to operator==())
236 bool Equals(const SfxItemSet
&, bool bComparePool
) const;
238 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
241 inline void SfxItemSet::SetParent( const SfxItemSet
* pNew
)
246 class SVL_DLLPUBLIC SfxAllItemSet
: public SfxItemSet
248 // Handles all Ranges. Ranges are automatically modified by putting items.
254 SfxAllItemSet( SfxItemPool
&rPool
);
255 SfxAllItemSet( const SfxItemSet
& );
256 SfxAllItemSet( const SfxAllItemSet
& );
258 virtual std::unique_ptr
<SfxItemSet
> Clone( bool bItems
= true, SfxItemPool
*pToPool
= nullptr ) const override
;
260 virtual const SfxPoolItem
* PutImpl( const SfxPoolItem
&, sal_uInt16 nWhich
, bool bPassingOwnership
) override
;
263 #endif // INCLUDED_SVL_ITEMSET_HXX
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */