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_POOLITEM_HXX
21 #define INCLUDED_SVL_POOLITEM_HXX
23 #include <sal/config.h>
28 #include <com/sun/star/uno/Any.hxx>
29 #include <svl/hint.hxx>
30 #include <svl/svldllapi.h>
31 #include <svl/typedwhich.hxx>
32 #include <tools/mapunit.hxx>
33 #include <tools/long.hxx>
34 #include <boost/property_tree/ptree_fwd.hpp>
38 enum class SfxItemKind
: sal_Int8
46 #define SFX_ITEMS_OLD_MAXREF 0xffef
47 #define SFX_ITEMS_MAXREF 0xfffffffe
48 #define SFX_ITEMS_SPECIAL 0xffffffff
50 #define CONVERT_TWIPS 0x80 // Uno conversion for measurement (for MemberId)
52 // warning, if there is no boolean inside the any this will always return the value false
53 inline bool Any2Bool( const css::uno::Any
&rValue
)
56 if( !(rValue
>>= bValue
) )
67 * The values of this enum describe the degree of textual
68 * representation of an item after calling the virtual
69 * method <SfxPoolItem::GetPresentation()const>.
71 enum class SfxItemPresentation
78 * These values have to match the values in the
79 * css::frame::status::ItemState IDL
80 * to be found at offapi/com/sun/star/frame/status/ItemState.idl
82 enum class SfxItemState
{
84 /** Specifies an unknown state. */
87 /** Specifies that the property is currently disabled. */
90 /** Specifies that the property is currently in a don't care state.
92 * This is normally used if a selection provides more than one state
93 * for a property at the same time.
97 /** Specifies that the property is currently in a default state. */
100 /** The property has been explicitly set to a given value hence we know
101 * we are not taking the default value.
103 * For example, you may want to get the font color and it might either
104 * be the default one or one that has been explicitly set.
109 #define INVALID_POOL_ITEM reinterpret_cast<SfxPoolItem*>(-1)
113 typedef struct _xmlTextWriter
* xmlTextWriterPtr
;
115 class SVL_DLLPUBLIC SfxPoolItem
117 friend class SfxItemPool
;
118 friend class SfxItemDisruptor_Impl
;
119 friend class SfxItemPoolCache
;
120 friend class SfxItemSet
;
121 friend class SfxVoidItem
;
123 mutable sal_uInt32 m_nRefCount
;
128 inline void SetRefCount(sal_uInt32 n
);
129 inline void SetKind( SfxItemKind n
);
131 inline void AddRef(sal_uInt32 n
= 1) const;
133 inline sal_uInt32
ReleaseRef(sal_uInt32 n
= 1) const;
136 explicit SfxPoolItem( sal_uInt16 nWhich
= 0 );
137 SfxPoolItem( const SfxPoolItem
& rCopy
)
138 : SfxPoolItem(rCopy
.m_nWhich
) {}
141 virtual ~SfxPoolItem();
143 void SetWhich( sal_uInt16 nId
)
145 // can only change the Which before we are in a set
146 assert(m_nRefCount
==0);
149 sal_uInt16
Which() const { return m_nWhich
; }
150 // StaticWhichCast asserts if the TypedWhichId is not matching its type, otherwise it returns a reference.
151 // You can use StaticWhichCast when you are sure about the type at compile time -- like a static_cast.
152 template<class T
> T
& StaticWhichCast(TypedWhichId
<T
> nId
)
155 assert(nId
== m_nWhich
);
156 assert(dynamic_cast<T
*>(this));
157 return *static_cast<T
*>(this);
159 template<class T
> const T
& StaticWhichCast(TypedWhichId
<T
> nId
) const
162 assert(nId
== m_nWhich
);
163 assert(dynamic_cast<const T
*>(this));
164 return *static_cast<const T
*>(this);
166 // DynamicWhichCast returns nullptr if the TypedWhichId is not matching its type, otherwise it returns a typed pointer.
167 // it asserts if the TypedWhichId matches its Which, but not the RTTI type.
168 // You can use DynamicWhichCast when you are not sure about the type at compile time -- like a dynamic_cast.
169 template<class T
> T
* DynamicWhichCast(TypedWhichId
<T
> nId
)
173 assert(dynamic_cast<T
*>(this));
174 return static_cast<T
*>(this);
176 template<class T
> const T
* DynamicWhichCast(TypedWhichId
<T
> nId
) const
180 assert(dynamic_cast<const T
*>(this));
181 return static_cast<const T
*>(this);
183 virtual bool operator==( const SfxPoolItem
& ) const = 0;
184 bool operator!=( const SfxPoolItem
& rItem
) const
185 { return !(*this == rItem
); }
187 // Sorting is only used for faster searching in a pool which contains large quantities
188 // of a single kind of pool item.
189 virtual bool operator<( const SfxPoolItem
& ) const { assert(false); return false; }
190 virtual bool IsSortable() const { return false; }
192 // Some item types cannot be IsSortable() (such as because they are modified while stored
193 // in a pool, which would change the ordering position, see e.g. 585e0ac43b9bd8a2f714903034).
194 // To improve performance in such cases it is possible to reimplement Lookup() to do a linear
195 // lookup optimized for the specific class (avoiding virtual functions may allow the compiler
196 // to generate better code and class-specific optimizations such as hashing or caching may
198 // If reimplemented, the Lookup() function should search [begin,end) for an item matching
199 // this object and return an iterator pointing to the item or the end iterator.
200 virtual bool HasLookup() const { return false; }
201 typedef std::vector
<SfxPoolItem
*>::const_iterator lookup_iterator
;
202 virtual lookup_iterator
Lookup(lookup_iterator
/*begin*/, lookup_iterator end
) const
203 { assert( false ); return end
; }
205 /** @return true if it has a valid string representation */
206 virtual bool GetPresentation( SfxItemPresentation ePresentation
,
208 MapUnit ePresentationMetric
,
210 const IntlWrapper
& rIntlWrapper
) const;
212 virtual void ScaleMetrics( tools::Long lMult
, tools::Long lDiv
);
213 virtual bool HasMetrics() const;
215 virtual bool QueryValue( css::uno::Any
& rVal
, sal_uInt8 nMemberId
= 0 ) const;
216 virtual bool PutValue( const css::uno::Any
& rVal
, sal_uInt8 nMemberId
);
218 virtual SfxPoolItem
* Clone( SfxItemPool
*pPool
= nullptr ) const = 0;
219 // clone and call SetWhich
220 std::unique_ptr
<SfxPoolItem
> CloneSetWhich( sal_uInt16 nNewWhich
) const;
221 template<class T
> std::unique_ptr
<T
> CloneSetWhich( TypedWhichId
<T
> nId
) const
223 return std::unique_ptr
<T
>(static_cast<T
*>(CloneSetWhich(sal_uInt16(nId
)).release()));
226 sal_uInt32
GetRefCount() const { return m_nRefCount
; }
227 SfxItemKind
GetKind() const { return m_nKind
; }
228 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const;
229 virtual boost::property_tree::ptree
dumpAsJSON() const;
231 /** Only SfxVoidItem shall and must return true for this.
233 This avoids costly calls to dynamic_cast<const SfxVoidItem*>()
234 specifically in SfxItemSet::GetItemState()
236 virtual bool IsVoidItem() const;
239 SfxPoolItem
& operator=( const SfxPoolItem
& ) = delete;
242 inline void SfxPoolItem::SetRefCount(sal_uInt32 n
)
245 m_nKind
= SfxItemKind::NONE
;
248 inline void SfxPoolItem::SetKind( SfxItemKind n
)
250 m_nRefCount
= SFX_ITEMS_SPECIAL
;
254 inline void SfxPoolItem::AddRef(sal_uInt32 n
) const
256 assert(m_nRefCount
<= SFX_ITEMS_MAXREF
&& "AddRef with non-Pool-Item");
257 assert(n
<= SFX_ITEMS_MAXREF
- m_nRefCount
&& "AddRef: refcount overflow");
261 inline sal_uInt32
SfxPoolItem::ReleaseRef(sal_uInt32 n
) const
263 assert(m_nRefCount
<= SFX_ITEMS_MAXREF
&& "ReleaseRef with non-Pool-Item");
264 assert(n
<= m_nRefCount
);
269 inline bool IsPoolDefaultItem(const SfxPoolItem
*pItem
)
271 return pItem
&& pItem
->GetKind() == SfxItemKind::PoolDefault
;
274 inline bool IsStaticDefaultItem(const SfxPoolItem
*pItem
)
276 return pItem
&& pItem
->GetKind() == SfxItemKind::StaticDefault
;
279 inline bool IsDefaultItem( const SfxPoolItem
*pItem
)
281 return pItem
&& (pItem
->GetKind() == SfxItemKind::StaticDefault
|| pItem
->GetKind() == SfxItemKind::PoolDefault
);
284 inline bool IsPooledItem( const SfxPoolItem
*pItem
)
286 return pItem
&& pItem
->GetRefCount() > 0 && pItem
->GetRefCount() <= SFX_ITEMS_MAXREF
;
289 inline bool IsInvalidItem(const SfxPoolItem
*pItem
)
291 return pItem
== INVALID_POOL_ITEM
;
294 class SVL_DLLPUBLIC SfxVoidItem final
: public SfxPoolItem
297 static SfxPoolItem
* CreateDefault();
298 explicit SfxVoidItem( sal_uInt16 nWhich
);
299 virtual ~SfxVoidItem() override
;
301 SfxVoidItem(SfxVoidItem
const &) = default;
302 SfxVoidItem(SfxVoidItem
&&) = default;
303 SfxVoidItem
& operator =(SfxVoidItem
const &) = delete; // due to SfxPoolItem
304 SfxVoidItem
& operator =(SfxVoidItem
&&) = delete; // due to SfxPoolItem
306 virtual bool operator==( const SfxPoolItem
& ) const override
;
308 virtual bool GetPresentation( SfxItemPresentation ePres
,
312 const IntlWrapper
& ) const override
;
313 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const override
;
315 // create a copy of itself
316 virtual SfxVoidItem
* Clone( SfxItemPool
*pPool
= nullptr ) const override
;
318 /** Always returns true as this is an SfxVoidItem. */
319 virtual bool IsVoidItem() const override
;
323 class SVL_DLLPUBLIC SfxPoolItemHint final
: public SfxHint
327 explicit SfxPoolItemHint( SfxPoolItem
* Object
) : pObj(Object
) {}
328 SfxPoolItem
* GetObject() const { return pObj
; }
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */