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 read-only. */
92 /** Specifies that the property is currently in a don't care state.
94 * This is normally used if a selection provides more than one state
95 * for a property at the same time.
99 /** Specifies that the property is currently in a default state. */
102 /** The property has been explicitly set to a given value hence we know
103 * we are not taking the default value.
105 * For example, you may want to get the font color and it might either
106 * be the default one or one that has been explicitly set.
111 #define INVALID_POOL_ITEM reinterpret_cast<SfxPoolItem*>(-1)
115 typedef struct _xmlTextWriter
* xmlTextWriterPtr
;
117 class SVL_DLLPUBLIC SfxPoolItem
119 friend class SfxItemPool
;
120 friend class SfxItemDisruptor_Impl
;
121 friend class SfxItemPoolCache
;
122 friend class SfxItemSet
;
123 friend class SfxVoidItem
;
125 mutable sal_uInt32 m_nRefCount
;
130 inline void SetRefCount(sal_uInt32 n
);
131 inline void SetKind( SfxItemKind n
);
133 inline void AddRef(sal_uInt32 n
= 1) const;
135 inline sal_uInt32
ReleaseRef(sal_uInt32 n
= 1) const;
138 explicit SfxPoolItem( sal_uInt16 nWhich
= 0 );
139 SfxPoolItem( const SfxPoolItem
& rCopy
)
140 : SfxPoolItem(rCopy
.m_nWhich
) {}
143 virtual ~SfxPoolItem();
145 void SetWhich( sal_uInt16 nId
)
147 // can only change the Which before we are in a set
148 assert(m_nRefCount
==0);
151 sal_uInt16
Which() const { return m_nWhich
; }
152 virtual bool operator==( const SfxPoolItem
& ) const = 0;
153 bool operator!=( const SfxPoolItem
& rItem
) const
154 { return !(*this == rItem
); }
156 // Sorting is only used for faster searching in a pool which contains large quantities
157 // of a single kind of pool item.
158 virtual bool operator<( const SfxPoolItem
& ) const { assert(false); return false; }
159 virtual bool IsSortable() const { return false; }
161 /** @return true if it has a valid string representation */
162 virtual bool GetPresentation( SfxItemPresentation ePresentation
,
164 MapUnit ePresentationMetric
,
166 const IntlWrapper
& rIntlWrapper
) const;
168 virtual void ScaleMetrics( tools::Long lMult
, tools::Long lDiv
);
169 virtual bool HasMetrics() const;
171 virtual bool QueryValue( css::uno::Any
& rVal
, sal_uInt8 nMemberId
= 0 ) const;
172 virtual bool PutValue( const css::uno::Any
& rVal
, sal_uInt8 nMemberId
);
174 virtual SfxPoolItem
* Clone( SfxItemPool
*pPool
= nullptr ) const = 0;
175 // clone and call SetWhich
176 std::unique_ptr
<SfxPoolItem
> CloneSetWhich( sal_uInt16 nNewWhich
) const;
177 template<class T
> std::unique_ptr
<T
> CloneSetWhich( TypedWhichId
<T
> nId
) const
179 return std::unique_ptr
<T
>(static_cast<T
*>(CloneSetWhich(sal_uInt16(nId
)).release()));
182 sal_uInt32
GetRefCount() const { return m_nRefCount
; }
183 SfxItemKind
GetKind() const { return m_nKind
; }
184 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const;
185 virtual boost::property_tree::ptree
dumpAsJSON() const;
187 /** Only SfxVoidItem shall and must return true for this.
189 This avoids costly calls to dynamic_cast<const SfxVoidItem*>()
190 specifically in SfxItemSet::GetItemState()
192 virtual bool IsVoidItem() const;
195 SfxPoolItem
& operator=( const SfxPoolItem
& ) = delete;
198 inline void SfxPoolItem::SetRefCount(sal_uInt32 n
)
201 m_nKind
= SfxItemKind::NONE
;
204 inline void SfxPoolItem::SetKind( SfxItemKind n
)
206 m_nRefCount
= SFX_ITEMS_SPECIAL
;
210 inline void SfxPoolItem::AddRef(sal_uInt32 n
) const
212 assert(m_nRefCount
<= SFX_ITEMS_MAXREF
&& "AddRef with non-Pool-Item");
213 assert(n
<= SFX_ITEMS_MAXREF
- m_nRefCount
&& "AddRef: refcount overflow");
217 inline sal_uInt32
SfxPoolItem::ReleaseRef(sal_uInt32 n
) const
219 assert(m_nRefCount
<= SFX_ITEMS_MAXREF
&& "ReleaseRef with non-Pool-Item");
220 assert(n
<= m_nRefCount
);
225 inline bool IsPoolDefaultItem(const SfxPoolItem
*pItem
)
227 return pItem
&& pItem
->GetKind() == SfxItemKind::PoolDefault
;
230 inline bool IsStaticDefaultItem(const SfxPoolItem
*pItem
)
232 return pItem
&& pItem
->GetKind() == SfxItemKind::StaticDefault
;
235 inline bool IsDefaultItem( const SfxPoolItem
*pItem
)
237 return pItem
&& (pItem
->GetKind() == SfxItemKind::StaticDefault
|| pItem
->GetKind() == SfxItemKind::PoolDefault
);
240 inline bool IsPooledItem( const SfxPoolItem
*pItem
)
242 return pItem
&& pItem
->GetRefCount() > 0 && pItem
->GetRefCount() <= SFX_ITEMS_MAXREF
;
245 inline bool IsInvalidItem(const SfxPoolItem
*pItem
)
247 return pItem
== INVALID_POOL_ITEM
;
250 class SVL_DLLPUBLIC SfxVoidItem final
: public SfxPoolItem
253 static SfxPoolItem
* CreateDefault();
254 explicit SfxVoidItem( sal_uInt16 nWhich
);
255 virtual ~SfxVoidItem() override
;
257 SfxVoidItem(SfxVoidItem
const &) = default;
258 SfxVoidItem(SfxVoidItem
&&) = default;
259 SfxVoidItem
& operator =(SfxVoidItem
const &) = delete; // due to SfxPoolItem
260 SfxVoidItem
& operator =(SfxVoidItem
&&) = delete; // due to SfxPoolItem
262 virtual bool operator==( const SfxPoolItem
& ) const override
;
264 virtual bool GetPresentation( SfxItemPresentation ePres
,
268 const IntlWrapper
& ) const override
;
269 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const override
;
271 // create a copy of itself
272 virtual SfxVoidItem
* Clone( SfxItemPool
*pPool
= nullptr ) const override
;
274 /** Always returns true as this is an SfxVoidItem. */
275 virtual bool IsVoidItem() const override
;
278 class SVL_DLLPUBLIC SfxSetItem
: public SfxPoolItem
280 std::unique_ptr
<SfxItemSet
> pSet
;
282 SfxSetItem
& operator=( const SfxSetItem
& ) = delete;
285 SfxSetItem( sal_uInt16 nWhich
, std::unique_ptr
<SfxItemSet
> &&pSet
);
286 SfxSetItem( sal_uInt16 nWhich
, const SfxItemSet
&rSet
);
287 SfxSetItem( const SfxSetItem
&, SfxItemPool
*pPool
= nullptr );
288 virtual ~SfxSetItem() override
;
290 virtual bool operator==( const SfxPoolItem
& ) const override
;
292 virtual bool GetPresentation( SfxItemPresentation ePres
,
296 const IntlWrapper
& ) const override
;
298 // create a copy of itself
299 virtual SfxSetItem
* Clone( SfxItemPool
*pPool
= nullptr ) const override
= 0;
301 const SfxItemSet
& GetItemSet() const
303 SfxItemSet
& GetItemSet()
307 class SVL_DLLPUBLIC SfxPoolItemHint final
: public SfxHint
311 explicit SfxPoolItemHint( SfxPoolItem
* Object
) : pObj(Object
) {}
312 SfxPoolItem
* GetObject() const { return pObj
; }
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */