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 .
22 #include <rtl/ref.hxx>
23 #include <svl/poolitem.hxx>
24 #include <svl/svldllapi.h>
25 #include <svl/typedwhich.hxx>
26 #include <svl/whichranges.hxx>
29 #include <unordered_set>
30 #include <o3tl/sorted_vector.hxx>
31 #include <salhelper/simplereferenceobject.hxx>
34 struct SfxItemPool_Impl
;
38 // Defines a mapping between WhichID <-> SlotID
41 // Defines if this Item needs to be registered at the pool
42 // to make it accessible for the GetItemSurrogates call. It
43 // will not be included when this flag is not set, but also
44 // needs no registration. There are SAL_INFO calls in the
45 // GetItemSurrogates impl that will mention that
46 bool _bNeedsPoolRegistration
: 1;
48 // Defines if the Item can be shared/RefCounted else it will be cloned.
49 // Default is true - as it should be for all Items. It is needed by some
50 // SW items, so protected to let them set it in constructor. If this could
51 // be fixed at that Items we may remove this again.
56 typedef std::unordered_set
<const SfxPoolItem
*> registeredSfxPoolItems
;
59 SVL_DLLPUBLIC
size_t getAllDirectlyPooledSfxPoolItemCount();
60 SVL_DLLPUBLIC
size_t getRemainingDirectlyPooledSfxPoolItemCount();
63 /** Base class for providers of defaults of SfxPoolItems.
65 * The derived classes hold the concrete (const) instances which are referenced in several places
66 * (usually within a single document).
67 * This helps to lower the amount of calls to lifecycle methods, speeds up comparisons within a document
68 * and facilitates loading and saving of attributes.
70 class SVL_DLLPUBLIC SfxItemPool
: public salhelper::SimpleReferenceObject
72 friend struct SfxItemPool_Impl
;
73 friend class SfxItemSet
;
74 friend class SfxAllItemSet
;
76 // allow ItemSetTooling to access
77 friend SfxPoolItem
const* implCreateItemEntry(SfxItemPool
&, SfxPoolItem
const*, sal_uInt16
, bool);
78 friend void implCleanupItemEntry(SfxItemPool
&, SfxPoolItem
const*);
81 friend class PoolItemTest
;
83 const SfxItemInfo
* pItemInfos
;
84 std::unique_ptr
<SfxItemPool_Impl
> pImpl
;
86 registeredSfxPoolItems
** ppRegisteredSfxPoolItems
;
89 sal_uInt16
GetIndex_Impl(sal_uInt16 nWhich
) const;
90 sal_uInt16
GetSize_Impl() const;
92 SVL_DLLPRIVATE
bool NeedsPoolRegistration_Impl(sal_uInt16 nPos
) const
93 { return pItemInfos
[nPos
]._bNeedsPoolRegistration
; }
94 SVL_DLLPRIVATE
bool Shareable_Impl(sal_uInt16 nPos
) const
95 { return pItemInfos
[nPos
]._bShareable
; }
98 // for default SfxItemSet::CTOR, set default WhichRanges
99 void FillItemIdRanges_Impl( WhichRangesContainer
& pWhichRanges
) const;
100 const WhichRangesContainer
& GetFrozenIdRanges() const;
103 static inline void ClearRefCount(SfxPoolItem
& rItem
);
104 static inline void AddRef(const SfxPoolItem
& rItem
);
105 static inline sal_uInt32
ReleaseRef(const SfxPoolItem
& rItem
, sal_uInt32 n
= 1);
108 SfxItemPool( const SfxItemPool
&rPool
,
109 bool bCloneStaticDefaults
= false );
110 SfxItemPool( const OUString
&rName
,
111 sal_uInt16 nStart
, sal_uInt16 nEnd
,
112 const SfxItemInfo
*pItemInfos
,
113 std::vector
<SfxPoolItem
*> *pDefaults
= nullptr );
116 virtual ~SfxItemPool();
118 SfxBroadcaster
& BC();
120 void SetPoolDefaultItem( const SfxPoolItem
& );
122 const SfxPoolItem
* GetPoolDefaultItem( sal_uInt16 nWhich
) const;
123 template<class T
> const T
* GetPoolDefaultItem( TypedWhichId
<T
> nWhich
) const
124 { return static_cast<const T
*>(GetPoolDefaultItem(sal_uInt16(nWhich
))); }
126 void ResetPoolDefaultItem( sal_uInt16 nWhich
);
128 void SetDefaults(std::vector
<SfxPoolItem
*>* pDefaults
);
129 void ClearDefaults();
130 void ReleaseDefaults( bool bDelete
= false );
131 static void ReleaseDefaults( std::vector
<SfxPoolItem
*> *pDefaults
, bool bDelete
= false );
133 virtual MapUnit
GetMetric( sal_uInt16 nWhich
) const;
134 void SetDefaultMetric( MapUnit eNewMetric
);
135 MapUnit
GetDefaultMetric() const;
137 /** Request string representation of pool items.
139 This virtual function produces a string representation
140 from the respective SfxItemPool subclass' known SfxPoolItems.
142 Subclasses, please override this method, and handle
143 SfxPoolItems that don't return useful/complete information on
144 SfxPoolItem::GetPresentation()
146 This baseclass yields the unmodified string representation of
150 SfxPoolItem to query the string representation of
153 requested kind of representation - see SfxItemPresentation
156 requested unit of measure of the representation
159 string representation of 'rItem'
161 @return true if it has a valid string representation
163 virtual bool GetPresentation( const SfxPoolItem
& rItem
,
164 MapUnit ePresentationMetric
,
166 const IntlWrapper
& rIntlWrapper
) const;
167 virtual rtl::Reference
<SfxItemPool
> Clone() const;
168 const OUString
& GetName() const;
170 template<class T
> const T
& DirectPutItemInPool( std::unique_ptr
<T
> xItem
, sal_uInt16 nWhich
= 0 )
171 { return static_cast<const T
&>(DirectPutItemInPoolImpl( *xItem
.release(), nWhich
, /*bPassingOwnership*/true)); }
172 template<class T
> const T
& DirectPutItemInPool( const T
& rItem
, sal_uInt16 nWhich
= 0 )
173 { return static_cast<const T
&>(DirectPutItemInPoolImpl( rItem
, nWhich
, /*bPassingOwnership*/false)); }
174 void DirectRemoveItemFromPool( const SfxPoolItem
& );
176 const SfxPoolItem
& GetDefaultItem( sal_uInt16 nWhich
) const;
177 template<class T
> const T
& GetDefaultItem( TypedWhichId
<T
> nWhich
) const
178 { return static_cast<const T
&>(GetDefaultItem(sal_uInt16(nWhich
))); }
182 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator m_begin
;
183 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator m_end
;
184 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator
const & begin() const { return m_begin
; }
185 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator
const & end() const { return m_end
; }
187 const SfxPoolItem
* GetItem2Default(sal_uInt16 nWhich
) const;
188 template<class T
> const T
* GetItem2Default( TypedWhichId
<T
> nWhich
) const
189 { return static_cast<const T
*>(GetItem2Default(sal_uInt16(nWhich
))); }
191 const registeredSfxPoolItems
& GetItemSurrogates(sal_uInt16 nWhich
) const;
193 This is only valid for SfxPoolItem that override IsSortable and operator<.
194 Returns a range of items defined by using operator<.
195 @param rNeedle must be the same type or a supertype of the pool items for nWhich.
197 std::vector
<const SfxPoolItem
*> FindItemSurrogate(sal_uInt16 nWhich
, SfxPoolItem
const & rNeedle
) const;
199 sal_uInt16
GetFirstWhich() const;
200 sal_uInt16
GetLastWhich() const;
201 bool IsInRange( sal_uInt16 nWhich
) const;
202 void SetSecondaryPool( SfxItemPool
*pPool
);
203 SfxItemPool
* GetSecondaryPool() const;
204 /* get the last pool by following the GetSecondaryPool chain */
205 SfxItemPool
* GetLastPoolInChain();
206 SfxItemPool
* GetMasterPool() const;
207 void FreezeIdRanges();
211 bool NeedsPoolRegistration(sal_uInt16 nWhich
) const;
212 bool NeedsPoolRegistration(const SfxPoolItem
&rItem
) const
213 { return NeedsPoolRegistration(rItem
.Which()); }
215 bool Shareable(sal_uInt16 nWhich
) const;
216 bool Shareable(const SfxPoolItem
&rItem
) const
217 { return Shareable(rItem
.Which()); }
219 void SetItemInfos( const SfxItemInfo
*pInfos
);
220 sal_uInt16
GetWhich( sal_uInt16 nSlot
, bool bDeep
= true ) const;
222 TypedWhichId
<T
> GetWhich( TypedWhichId
<T
> nSlot
, bool bDeep
= true ) const
223 { return TypedWhichId
<T
>(GetWhich(sal_uInt16(nSlot
), bDeep
)); }
224 sal_uInt16
GetSlotId( sal_uInt16 nWhich
) const;
225 sal_uInt16
GetTrueWhich( sal_uInt16 nSlot
, bool bDeep
= true ) const;
226 sal_uInt16
GetTrueSlotId( sal_uInt16 nWhich
) const;
228 static bool IsWhich(sal_uInt16 nId
) {
229 return nId
&& nId
<= SFX_WHICH_MAX
; }
230 static bool IsSlot(sal_uInt16 nId
) {
231 return nId
&& nId
> SFX_WHICH_MAX
; }
233 // This method will try to register the Item at this Pool.
234 void registerSfxPoolItem(const SfxPoolItem
& rItem
);
236 // this method will unregister an Item from this Pool
237 void unregisterSfxPoolItem(const SfxPoolItem
& rItem
);
239 // check if this Item is registered at this Pool, needed to detect
240 // if an Item is to be set at another Pool and needs to be cloned
241 bool isSfxPoolItemRegisteredAtThisPool(const SfxPoolItem
& rItem
) const;
243 // try to find an equal existing Item to given one in pool
244 const SfxPoolItem
* tryToGetEqualItem(const SfxPoolItem
& rItem
, sal_uInt16 nWhich
) const;
246 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
249 const SfxPoolItem
& DirectPutItemInPoolImpl( const SfxPoolItem
&, sal_uInt16 nWhich
= 0, bool bPassingOwnership
= false );
250 virtual void newItem_Callback(const SfxPoolItem
& rItem
) const;
251 virtual bool newItem_UseDirect(const SfxPoolItem
& rItem
) const;
254 const SfxItemPool
& operator=(const SfxItemPool
&) = delete;
256 //IDs below or equal are Which IDs, IDs above slot IDs
257 static const sal_uInt16 SFX_WHICH_MAX
= 4999;
260 // only the pool may manipulate the reference counts
261 inline void SfxItemPool::ClearRefCount(SfxPoolItem
& rItem
)
263 rItem
.SetRefCount(0);
266 // only the pool may manipulate the reference counts
267 inline void SfxItemPool::AddRef(const SfxPoolItem
& rItem
)
272 // only the pool may manipulate the reference counts
273 inline sal_uInt32
SfxItemPool::ReleaseRef(const SfxPoolItem
& rItem
, sal_uInt32 n
)
275 return rItem
.ReleaseRef(n
);
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */