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>
27 #include <com/sun/star/uno/Any.hxx>
28 #include <svl/hint.hxx>
29 #include <svl/svldllapi.h>
30 #include <svl/typedwhich.hxx>
31 #include <tools/mapunit.hxx>
32 #include <tools/long.hxx>
33 #include <boost/property_tree/ptree_fwd.hpp>
37 enum class SfxItemKind
: sal_Int8
45 #define SFX_ITEMS_OLD_MAXREF 0xffef
46 #define SFX_ITEMS_MAXREF 0xfffffffe
47 #define SFX_ITEMS_SPECIAL 0xffffffff
49 #define CONVERT_TWIPS 0x80 // Uno conversion for measurement (for MemberId)
51 // warning, if there is no boolean inside the any this will always return the value false
52 inline bool Any2Bool( const css::uno::Any
&rValue
)
55 if( !(rValue
>>= bValue
) )
66 * The values of this enum describe the degree of textual
67 * representation of an item after calling the virtual
68 * method <SfxPoolItem::GetPresentation()const>.
70 enum class SfxItemPresentation
77 * These values have to match the values in the
78 * css::frame::status::ItemState IDL
79 * to be found at offapi/com/sun/star/frame/status/ItemState.idl
81 enum class SfxItemState
{
83 /** Specifies an unknown state. */
86 /** Specifies that the property is currently disabled. */
89 /** Specifies that the property is currently in a don't care state.
91 * This is normally used if a selection provides more than one state
92 * for a property at the same time.
96 /** Specifies that the property is currently in a default state. */
99 /** The property has been explicitly set to a given value hence we know
100 * we are not taking the default value.
102 * For example, you may want to get the font color and it might either
103 * be the default one or one that has been explicitly set.
108 #define INVALID_POOL_ITEM reinterpret_cast<SfxPoolItem*>(-1)
112 typedef struct _xmlTextWriter
* xmlTextWriterPtr
;
114 class SVL_DLLPUBLIC SfxPoolItem
116 friend class SfxItemPool
;
117 friend class SfxItemDisruptor_Impl
;
118 friend class SfxItemPoolCache
;
119 friend class SfxItemSet
;
120 friend class SfxVoidItem
;
122 mutable sal_uInt32 m_nRefCount
;
127 inline void SetRefCount(sal_uInt32 n
);
128 inline void SetKind( SfxItemKind n
);
130 inline void AddRef(sal_uInt32 n
= 1) const;
132 inline sal_uInt32
ReleaseRef(sal_uInt32 n
= 1) const;
135 explicit SfxPoolItem( sal_uInt16 nWhich
= 0 );
136 SfxPoolItem( const SfxPoolItem
& rCopy
)
137 : SfxPoolItem(rCopy
.m_nWhich
) {}
140 virtual ~SfxPoolItem();
142 void SetWhich( sal_uInt16 nId
)
144 // can only change the Which before we are in a set
145 assert(m_nRefCount
==0);
148 sal_uInt16
Which() const { return m_nWhich
; }
149 // StaticWhichCast asserts if the TypedWhichId is not matching its type, otherwise it returns a reference.
150 // You can use StaticWhichCast when you are sure about the type at compile time -- like a static_cast.
151 template<class T
> T
& StaticWhichCast(TypedWhichId
<T
> nId
)
154 assert(nId
== m_nWhich
);
155 assert(dynamic_cast<T
*>(this));
156 return *static_cast<T
*>(this);
158 template<class T
> const T
& StaticWhichCast(TypedWhichId
<T
> nId
) const
161 assert(nId
== m_nWhich
);
162 assert(dynamic_cast<const T
*>(this));
163 return *static_cast<const T
*>(this);
165 // DynamicWhichCast returns nullptr if the TypedWhichId is not matching its type, otherwise it returns a typed pointer.
166 // it asserts if the TypedWhichId matches its Which, but not the RTTI type.
167 // You can use DynamicWhichCast when you are not sure about the type at compile time -- like a dynamic_cast.
168 template<class T
> T
* DynamicWhichCast(TypedWhichId
<T
> nId
)
172 assert(dynamic_cast<T
*>(this));
173 return static_cast<T
*>(this);
175 template<class T
> const T
* DynamicWhichCast(TypedWhichId
<T
> nId
) const
179 assert(dynamic_cast<const T
*>(this));
180 return static_cast<const T
*>(this);
182 virtual bool operator==( const SfxPoolItem
& ) const = 0;
183 bool operator!=( const SfxPoolItem
& rItem
) const
184 { return !(*this == rItem
); }
186 // Sorting is only used for faster searching in a pool which contains large quantities
187 // of a single kind of pool item.
188 virtual bool operator<( const SfxPoolItem
& ) const { assert(false); return false; }
189 virtual bool IsSortable() const { return false; }
191 /** @return true if it has a valid string representation */
192 virtual bool GetPresentation( SfxItemPresentation ePresentation
,
194 MapUnit ePresentationMetric
,
196 const IntlWrapper
& rIntlWrapper
) const;
198 virtual void ScaleMetrics( tools::Long lMult
, tools::Long lDiv
);
199 virtual bool HasMetrics() const;
201 virtual bool QueryValue( css::uno::Any
& rVal
, sal_uInt8 nMemberId
= 0 ) const;
202 virtual bool PutValue( const css::uno::Any
& rVal
, sal_uInt8 nMemberId
);
204 virtual SfxPoolItem
* Clone( SfxItemPool
*pPool
= nullptr ) const = 0;
205 // clone and call SetWhich
206 std::unique_ptr
<SfxPoolItem
> CloneSetWhich( sal_uInt16 nNewWhich
) const;
207 template<class T
> std::unique_ptr
<T
> CloneSetWhich( TypedWhichId
<T
> nId
) const
209 return std::unique_ptr
<T
>(static_cast<T
*>(CloneSetWhich(sal_uInt16(nId
)).release()));
212 sal_uInt32
GetRefCount() const { return m_nRefCount
; }
213 SfxItemKind
GetKind() const { return m_nKind
; }
214 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const;
215 virtual boost::property_tree::ptree
dumpAsJSON() const;
217 /** Only SfxVoidItem shall and must return true for this.
219 This avoids costly calls to dynamic_cast<const SfxVoidItem*>()
220 specifically in SfxItemSet::GetItemState()
222 virtual bool IsVoidItem() const;
225 SfxPoolItem
& operator=( const SfxPoolItem
& ) = delete;
228 inline void SfxPoolItem::SetRefCount(sal_uInt32 n
)
231 m_nKind
= SfxItemKind::NONE
;
234 inline void SfxPoolItem::SetKind( SfxItemKind n
)
236 m_nRefCount
= SFX_ITEMS_SPECIAL
;
240 inline void SfxPoolItem::AddRef(sal_uInt32 n
) const
242 assert(m_nRefCount
<= SFX_ITEMS_MAXREF
&& "AddRef with non-Pool-Item");
243 assert(n
<= SFX_ITEMS_MAXREF
- m_nRefCount
&& "AddRef: refcount overflow");
247 inline sal_uInt32
SfxPoolItem::ReleaseRef(sal_uInt32 n
) const
249 assert(m_nRefCount
<= SFX_ITEMS_MAXREF
&& "ReleaseRef with non-Pool-Item");
250 assert(n
<= m_nRefCount
);
255 inline bool IsPoolDefaultItem(const SfxPoolItem
*pItem
)
257 return pItem
&& pItem
->GetKind() == SfxItemKind::PoolDefault
;
260 inline bool IsStaticDefaultItem(const SfxPoolItem
*pItem
)
262 return pItem
&& pItem
->GetKind() == SfxItemKind::StaticDefault
;
265 inline bool IsDefaultItem( const SfxPoolItem
*pItem
)
267 return pItem
&& (pItem
->GetKind() == SfxItemKind::StaticDefault
|| pItem
->GetKind() == SfxItemKind::PoolDefault
);
270 inline bool IsPooledItem( const SfxPoolItem
*pItem
)
272 return pItem
&& pItem
->GetRefCount() > 0 && pItem
->GetRefCount() <= SFX_ITEMS_MAXREF
;
275 inline bool IsInvalidItem(const SfxPoolItem
*pItem
)
277 return pItem
== INVALID_POOL_ITEM
;
280 class SVL_DLLPUBLIC SfxVoidItem final
: public SfxPoolItem
283 static SfxPoolItem
* CreateDefault();
284 explicit SfxVoidItem( sal_uInt16 nWhich
);
285 virtual ~SfxVoidItem() override
;
287 SfxVoidItem(SfxVoidItem
const &) = default;
288 SfxVoidItem(SfxVoidItem
&&) = default;
289 SfxVoidItem
& operator =(SfxVoidItem
const &) = delete; // due to SfxPoolItem
290 SfxVoidItem
& operator =(SfxVoidItem
&&) = delete; // due to SfxPoolItem
292 virtual bool operator==( const SfxPoolItem
& ) const override
;
294 virtual bool GetPresentation( SfxItemPresentation ePres
,
298 const IntlWrapper
& ) const override
;
299 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const override
;
301 // create a copy of itself
302 virtual SfxVoidItem
* Clone( SfxItemPool
*pPool
= nullptr ) const override
;
304 /** Always returns true as this is an SfxVoidItem. */
305 virtual bool IsVoidItem() const override
;
309 class SVL_DLLPUBLIC SfxPoolItemHint final
: public SfxHint
313 explicit SfxPoolItemHint( SfxPoolItem
* Object
) : pObj(Object
) {}
314 SfxPoolItem
* GetObject() const { return pObj
; }
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */