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 .
20 #ifndef INCLUDED_SVL_ITEMPOOL_HXX
21 #define INCLUDED_SVL_ITEMPOOL_HXX
23 #include <svl/poolitem.hxx>
24 #include <svl/svldllapi.h>
25 #include <svl/typedwhich.hxx>
28 #include <o3tl/sorted_vector.hxx>
31 struct SfxItemPool_Impl
;
41 class SVL_DLLPUBLIC SfxItemPoolUser
44 virtual void ObjectInDestruction(const SfxItemPool
& rSfxItemPool
) = 0;
50 /** Base class for providers of defaults of SfxPoolItems.
52 * The derived classes hold the concrete (const) instances which are referenced in several places
53 * (usually within a single document).
54 * This helps to lower the amount of calls to lifecycle methods, speeds up comparisons within a document
55 * and facilitates loading and saving of attributes.
57 class SVL_DLLPUBLIC SfxItemPool
59 friend struct SfxItemPool_Impl
;
60 friend class SfxItemSet
;
61 friend class SfxAllItemSet
;
63 const SfxItemInfo
* pItemInfos
;
64 std::unique_ptr
<SfxItemPool_Impl
> pImpl
;
67 void AddSfxItemPoolUser(SfxItemPoolUser
& rNewUser
);
68 void RemoveSfxItemPoolUser(SfxItemPoolUser
& rOldUser
);
71 sal_uInt16
GetIndex_Impl(sal_uInt16 nWhich
) const;
72 sal_uInt16
GetSize_Impl() const;
74 SVL_DLLPRIVATE
bool IsItemPoolable_Impl( sal_uInt16 nWhich
) const;
77 // for default SfxItemSet::CTOR, set default WhichRanges
78 void FillItemIdRanges_Impl( std::unique_ptr
<sal_uInt16
[]>& pWhichRanges
) const;
79 const sal_uInt16
* GetFrozenIdRanges() const;
82 static inline void ClearRefCount(SfxPoolItem
& rItem
);
83 static inline void AddRef(const SfxPoolItem
& rItem
);
84 static inline sal_uInt32
ReleaseRef(const SfxPoolItem
& rItem
, sal_uInt32 n
= 1);
87 SfxItemPool( const SfxItemPool
&rPool
,
88 bool bCloneStaticDefaults
= false );
89 SfxItemPool( const OUString
&rName
,
90 sal_uInt16 nStart
, sal_uInt16 nEnd
,
91 const SfxItemInfo
*pItemInfos
,
92 std::vector
<SfxPoolItem
*> *pDefaults
= nullptr );
95 virtual ~SfxItemPool();
98 static void Free(SfxItemPool
* pPool
);
100 SfxBroadcaster
& BC();
102 void SetPoolDefaultItem( const SfxPoolItem
& );
104 const SfxPoolItem
* GetPoolDefaultItem( sal_uInt16 nWhich
) const;
105 template<class T
> const T
* GetPoolDefaultItem( TypedWhichId
<T
> nWhich
) const
106 { return static_cast<const T
*>(GetPoolDefaultItem(sal_uInt16(nWhich
))); }
108 void ResetPoolDefaultItem( sal_uInt16 nWhich
);
110 void SetDefaults(std::vector
<SfxPoolItem
*>* pDefaults
);
111 void ClearDefaults();
112 void ReleaseDefaults( bool bDelete
= false );
113 static void ReleaseDefaults( std::vector
<SfxPoolItem
*> *pDefaults
, bool bDelete
= false );
115 virtual MapUnit
GetMetric( sal_uInt16 nWhich
) const;
116 void SetDefaultMetric( MapUnit eNewMetric
);
118 /** Request string representation of pool items.
120 This virtual function produces a string representation
121 from the respective SfxItemPool subclass' known SfxPoolItems.
123 Subclasses, please override this method, and handle
124 SfxPoolItems that don't return useful/complete information on
125 SfxPoolItem::GetPresentation()
127 This baseclass yields the unmodified string representation of
131 SfxPoolItem to query the string representation of
134 requested kind of representation - see SfxItemPresentation
137 requested unit of measure of the representation
140 string representation of 'rItem'
142 @return true if it has a valid string representation
144 virtual bool GetPresentation( const SfxPoolItem
& rItem
,
145 MapUnit ePresentationMetric
,
147 const IntlWrapper
& rIntlWrapper
) const;
148 virtual SfxItemPool
* Clone() const;
149 const OUString
& GetName() const;
151 template<class T
> const T
& Put( std::unique_ptr
<T
> xItem
, sal_uInt16 nWhich
= 0 )
152 { return static_cast<const T
&>(PutImpl( *xItem
.release(), nWhich
, /*bPassingOwnership*/true)); }
153 template<class T
> const T
& Put( const T
& rItem
, sal_uInt16 nWhich
= 0 )
154 { return static_cast<const T
&>(PutImpl( rItem
, nWhich
, /*bPassingOwnership*/false)); }
155 void Remove( const SfxPoolItem
& );
157 const SfxPoolItem
& GetDefaultItem( sal_uInt16 nWhich
) const;
158 template<class T
> const T
& GetDefaultItem( TypedWhichId
<T
> nWhich
) const
159 { return static_cast<const T
&>(GetDefaultItem(sal_uInt16(nWhich
))); }
161 bool CheckItemInPool(const SfxPoolItem
*) const;
165 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator m_begin
;
166 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator m_end
;
167 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator
begin() { return m_begin
; }
168 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator
end() { return m_end
; }
170 const SfxPoolItem
* GetItem2Default(sal_uInt16 nWhich
) const;
171 template<class T
> const T
* GetItem2Default( TypedWhichId
<T
> nWhich
) const
172 { return static_cast<const T
*>(GetItem2Default(sal_uInt16(nWhich
))); }
174 sal_uInt32
GetItemCount2(sal_uInt16 nWhich
) const;
175 Item2Range
GetItemSurrogates(sal_uInt16 nWhich
) const;
177 This is only valid for SfxPoolItem that override IsSortable and operator<.
178 Returns a range of items defined by using operator<.
179 @param rNeedle must be the same type or a supertype of the pool items for nWhich.
181 std::vector
<const SfxPoolItem
*> FindItemSurrogate(sal_uInt16 nWhich
, SfxPoolItem
const & rNeedle
) const;
183 sal_uInt16
GetFirstWhich() const;
184 sal_uInt16
GetLastWhich() const;
185 bool IsInRange( sal_uInt16 nWhich
) const;
186 void SetSecondaryPool( SfxItemPool
*pPool
);
187 SfxItemPool
* GetSecondaryPool() const;
188 SfxItemPool
* GetMasterPool() const;
189 void FreezeIdRanges();
193 bool IsItemPoolable( sal_uInt16 nWhich
) const;
194 bool IsItemPoolable( const SfxPoolItem
&rItem
) const
195 { return IsItemPoolable( rItem
.Which() ); }
196 void SetItemInfos( const SfxItemInfo
*pInfos
);
197 sal_uInt16
GetWhich( sal_uInt16 nSlot
, bool bDeep
= true ) const;
198 sal_uInt16
GetSlotId( sal_uInt16 nWhich
) const;
199 sal_uInt16
GetTrueWhich( sal_uInt16 nSlot
, bool bDeep
= true ) const;
200 sal_uInt16
GetTrueSlotId( sal_uInt16 nWhich
) const;
202 static bool IsWhich(sal_uInt16 nId
) {
203 return nId
&& nId
<= SFX_WHICH_MAX
; }
204 static bool IsSlot(sal_uInt16 nId
) {
205 return nId
&& nId
> SFX_WHICH_MAX
; }
207 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
210 virtual const SfxPoolItem
& PutImpl( const SfxPoolItem
&, sal_uInt16 nWhich
= 0, bool bPassingOwnership
= false );
212 const SfxItemPool
& operator=(const SfxItemPool
&) = delete;
214 //IDs below or equal are Which IDs, IDs above slot IDs
215 static const sal_uInt16 SFX_WHICH_MAX
= 4999;
218 // only the pool may manipulate the reference counts
219 inline void SfxItemPool::ClearRefCount(SfxPoolItem
& rItem
)
221 rItem
.SetRefCount(0);
224 // only the pool may manipulate the reference counts
225 inline void SfxItemPool::AddRef(const SfxPoolItem
& rItem
)
230 // only the pool may manipulate the reference counts
231 inline sal_uInt32
SfxItemPool::ReleaseRef(const SfxPoolItem
& rItem
, sal_uInt32 n
)
233 return rItem
.ReleaseRef(n
);
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */