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 <o3tl/sorted_vector.hxx>
30 #include <salhelper/simplereferenceobject.hxx>
33 struct SfxItemPool_Impl
;
43 /** Base class for providers of defaults of SfxPoolItems.
45 * The derived classes hold the concrete (const) instances which are referenced in several places
46 * (usually within a single document).
47 * This helps to lower the amount of calls to lifecycle methods, speeds up comparisons within a document
48 * and facilitates loading and saving of attributes.
50 class SVL_DLLPUBLIC SfxItemPool
: public salhelper::SimpleReferenceObject
52 friend struct SfxItemPool_Impl
;
53 friend class SfxItemSet
;
54 friend class SfxAllItemSet
;
56 const SfxItemInfo
* pItemInfos
;
57 std::unique_ptr
<SfxItemPool_Impl
> pImpl
;
60 sal_uInt16
GetIndex_Impl(sal_uInt16 nWhich
) const;
61 sal_uInt16
GetSize_Impl() const;
63 SVL_DLLPRIVATE
bool IsItemPoolable_Impl( sal_uInt16 nWhich
) const;
66 // for default SfxItemSet::CTOR, set default WhichRanges
67 void FillItemIdRanges_Impl( WhichRangesContainer
& pWhichRanges
) const;
68 const WhichRangesContainer
& GetFrozenIdRanges() const;
71 static inline void ClearRefCount(SfxPoolItem
& rItem
);
72 static inline void AddRef(const SfxPoolItem
& rItem
);
73 static inline sal_uInt32
ReleaseRef(const SfxPoolItem
& rItem
, sal_uInt32 n
= 1);
76 SfxItemPool( const SfxItemPool
&rPool
,
77 bool bCloneStaticDefaults
= false );
78 SfxItemPool( const OUString
&rName
,
79 sal_uInt16 nStart
, sal_uInt16 nEnd
,
80 const SfxItemInfo
*pItemInfos
,
81 std::vector
<SfxPoolItem
*> *pDefaults
= nullptr );
84 virtual ~SfxItemPool();
88 void SetPoolDefaultItem( const SfxPoolItem
& );
90 const SfxPoolItem
* GetPoolDefaultItem( sal_uInt16 nWhich
) const;
91 template<class T
> const T
* GetPoolDefaultItem( TypedWhichId
<T
> nWhich
) const
92 { return static_cast<const T
*>(GetPoolDefaultItem(sal_uInt16(nWhich
))); }
94 void ResetPoolDefaultItem( sal_uInt16 nWhich
);
96 void SetDefaults(std::vector
<SfxPoolItem
*>* pDefaults
);
98 void ReleaseDefaults( bool bDelete
= false );
99 static void ReleaseDefaults( std::vector
<SfxPoolItem
*> *pDefaults
, bool bDelete
= false );
101 virtual MapUnit
GetMetric( sal_uInt16 nWhich
) const;
102 void SetDefaultMetric( MapUnit eNewMetric
);
103 MapUnit
GetDefaultMetric() const;
105 /** Request string representation of pool items.
107 This virtual function produces a string representation
108 from the respective SfxItemPool subclass' known SfxPoolItems.
110 Subclasses, please override this method, and handle
111 SfxPoolItems that don't return useful/complete information on
112 SfxPoolItem::GetPresentation()
114 This baseclass yields the unmodified string representation of
118 SfxPoolItem to query the string representation of
121 requested kind of representation - see SfxItemPresentation
124 requested unit of measure of the representation
127 string representation of 'rItem'
129 @return true if it has a valid string representation
131 virtual bool GetPresentation( const SfxPoolItem
& rItem
,
132 MapUnit ePresentationMetric
,
134 const IntlWrapper
& rIntlWrapper
) const;
135 virtual rtl::Reference
<SfxItemPool
> Clone() const;
136 const OUString
& GetName() const;
138 template<class T
> const T
& Put( std::unique_ptr
<T
> xItem
, sal_uInt16 nWhich
= 0 )
139 { return static_cast<const T
&>(PutImpl( *xItem
.release(), nWhich
, /*bPassingOwnership*/true)); }
140 template<class T
> const T
& Put( const T
& rItem
, sal_uInt16 nWhich
= 0 )
141 { return static_cast<const T
&>(PutImpl( rItem
, nWhich
, /*bPassingOwnership*/false)); }
142 void Remove( const SfxPoolItem
& );
144 const SfxPoolItem
& GetDefaultItem( sal_uInt16 nWhich
) const;
145 template<class T
> const T
& GetDefaultItem( TypedWhichId
<T
> nWhich
) const
146 { return static_cast<const T
&>(GetDefaultItem(sal_uInt16(nWhich
))); }
148 bool CheckItemInPool(const SfxPoolItem
*) const;
152 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator m_begin
;
153 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator m_end
;
154 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator
const & begin() const { return m_begin
; }
155 o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator
const & end() const { return m_end
; }
157 const SfxPoolItem
* GetItem2Default(sal_uInt16 nWhich
) const;
158 template<class T
> const T
* GetItem2Default( TypedWhichId
<T
> nWhich
) const
159 { return static_cast<const T
*>(GetItem2Default(sal_uInt16(nWhich
))); }
161 sal_uInt32
GetItemCount2(sal_uInt16 nWhich
) const;
162 Item2Range
GetItemSurrogates(sal_uInt16 nWhich
) const;
164 This is only valid for SfxPoolItem that override IsSortable and operator<.
165 Returns a range of items defined by using operator<.
166 @param rNeedle must be the same type or a supertype of the pool items for nWhich.
168 std::vector
<const SfxPoolItem
*> FindItemSurrogate(sal_uInt16 nWhich
, SfxPoolItem
const & rNeedle
) const;
170 sal_uInt16
GetFirstWhich() const;
171 sal_uInt16
GetLastWhich() const;
172 bool IsInRange( sal_uInt16 nWhich
) const;
173 void SetSecondaryPool( SfxItemPool
*pPool
);
174 SfxItemPool
* GetSecondaryPool() const;
175 /* get the last pool by following the GetSecondaryPool chain */
176 SfxItemPool
* GetLastPoolInChain();
177 SfxItemPool
* GetMasterPool() const;
178 void FreezeIdRanges();
182 bool IsItemPoolable( sal_uInt16 nWhich
) const;
183 bool IsItemPoolable( const SfxPoolItem
&rItem
) const
184 { return IsItemPoolable( rItem
.Which() ); }
185 void SetItemInfos( const SfxItemInfo
*pInfos
);
186 sal_uInt16
GetWhich( sal_uInt16 nSlot
, bool bDeep
= true ) const;
188 TypedWhichId
<T
> GetWhich( TypedWhichId
<T
> nSlot
, bool bDeep
= true ) const
189 { return TypedWhichId
<T
>(GetWhich(sal_uInt16(nSlot
), bDeep
)); }
190 sal_uInt16
GetSlotId( sal_uInt16 nWhich
) const;
191 sal_uInt16
GetTrueWhich( sal_uInt16 nSlot
, bool bDeep
= true ) const;
192 sal_uInt16
GetTrueSlotId( sal_uInt16 nWhich
) const;
194 static bool IsWhich(sal_uInt16 nId
) {
195 return nId
&& nId
<= SFX_WHICH_MAX
; }
196 static bool IsSlot(sal_uInt16 nId
) {
197 return nId
&& nId
> SFX_WHICH_MAX
; }
199 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
202 virtual const SfxPoolItem
& PutImpl( const SfxPoolItem
&, sal_uInt16 nWhich
= 0, bool bPassingOwnership
= false );
204 const SfxItemPool
& operator=(const SfxItemPool
&) = delete;
206 //IDs below or equal are Which IDs, IDs above slot IDs
207 static const sal_uInt16 SFX_WHICH_MAX
= 4999;
210 // only the pool may manipulate the reference counts
211 inline void SfxItemPool::ClearRefCount(SfxPoolItem
& rItem
)
213 rItem
.SetRefCount(0);
216 // only the pool may manipulate the reference counts
217 inline void SfxItemPool::AddRef(const SfxPoolItem
& rItem
)
222 // only the pool may manipulate the reference counts
223 inline sal_uInt32
SfxItemPool::ReleaseRef(const SfxPoolItem
& rItem
, sal_uInt32 n
)
225 return rItem
.ReleaseRef(n
);
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */