LibreOffice Module svl (master) 1
poolitem.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_SVL_POOLITEM_HXX
21#define INCLUDED_SVL_POOLITEM_HXX
22
23#include <sal/config.h>
24
25#include <memory>
26#include <vector>
27
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>
35
36class IntlWrapper;
37
39{
40 NONE,
44};
45
46#define SFX_ITEMS_OLD_MAXREF 0xffef
47#define SFX_ITEMS_MAXREF 0xfffffffe
48#define SFX_ITEMS_SPECIAL 0xffffffff
49
50#define CONVERT_TWIPS 0x80 // Uno conversion for measurement (for MemberId)
51
52// warning, if there is no boolean inside the any this will always return the value false
53inline bool Any2Bool( const css::uno::Any&rValue )
54{
55 bool bValue = false;
56 if( !(rValue >>= bValue) )
57 {
58 sal_Int32 nNum = 0;
59 if( rValue >>= nNum )
60 bValue = nNum != 0;
61 }
62
63 return bValue;
64}
65
66/*
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>.
70 */
72{
75};
76
82enum class SfxItemState {
83
85 UNKNOWN = 0,
86
88 DISABLED = 0x0001,
89
95 DONTCARE = 0x0010,
96
98 DEFAULT = 0x0020,
99
106 SET = 0x0040
107};
108
109#define INVALID_POOL_ITEM reinterpret_cast<SfxPoolItem*>(-1)
110
111class SfxItemPool;
112class SfxItemSet;
113typedef struct _xmlTextWriter* xmlTextWriterPtr;
114
116{
117friend class SfxItemPool;
119friend class SfxItemPoolCache;
120friend class SfxItemSet;
121friend class SfxVoidItem;
122
123 mutable sal_uInt32 m_nRefCount;
124 sal_uInt16 m_nWhich;
126
127private:
128 inline void SetRefCount(sal_uInt32 n);
129 inline void SetKind( SfxItemKind n );
130public:
131 inline void AddRef(sal_uInt32 n = 1) const;
132private:
133 inline sal_uInt32 ReleaseRef(sal_uInt32 n = 1) const;
134
135protected:
136 explicit SfxPoolItem( sal_uInt16 nWhich = 0 );
138 : SfxPoolItem(rCopy.m_nWhich) {}
139
140public:
141 virtual ~SfxPoolItem();
142
143 void SetWhich( sal_uInt16 nId )
144 {
145 // can only change the Which before we are in a set
146 assert(m_nRefCount==0);
147 m_nWhich = nId;
148 }
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)
153 {
154 (void)nId;
155 assert(nId == m_nWhich);
156 assert(dynamic_cast<T*>(this));
157 return *static_cast<T*>(this);
158 }
159 template<class T> const T& StaticWhichCast(TypedWhichId<T> nId) const
160 {
161 (void)nId;
162 assert(nId == m_nWhich);
163 assert(dynamic_cast<const T*>(this));
164 return *static_cast<const T*>(this);
165 }
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)
170 {
171 if(m_nWhich != nId)
172 return nullptr;
173 assert(dynamic_cast<T*>(this));
174 return static_cast<T*>(this);
175 }
176 template<class T> const T* DynamicWhichCast(TypedWhichId<T> nId) const
177 {
178 if(m_nWhich != nId)
179 return nullptr;
180 assert(dynamic_cast<const T*>(this));
181 return static_cast<const T*>(this);
182 }
183 virtual bool operator==( const SfxPoolItem& ) const = 0;
184 bool operator!=( const SfxPoolItem& rItem ) const
185 { return !(*this == rItem); }
186
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; }
191
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
197 // be used.)
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;
203 { assert( false ); return end; }
204
206 virtual bool GetPresentation( SfxItemPresentation ePresentation,
207 MapUnit eCoreMetric,
208 MapUnit ePresentationMetric,
209 OUString &rText,
210 const IntlWrapper& rIntlWrapper ) const;
211
212 virtual void ScaleMetrics( tools::Long lMult, tools::Long lDiv );
213 virtual bool HasMetrics() const;
214
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 );
217
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
222 {
223 return std::unique_ptr<T>(static_cast<T*>(CloneSetWhich(sal_uInt16(nId)).release()));
224 }
225
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;
230
236 virtual bool IsVoidItem() const;
237
238private:
239 SfxPoolItem& operator=( const SfxPoolItem& ) = delete;
240};
241
242inline void SfxPoolItem::SetRefCount(sal_uInt32 n)
243{
244 m_nRefCount = n;
246}
247
249{
251 m_nKind = n;
252}
253
254inline void SfxPoolItem::AddRef(sal_uInt32 n) const
255{
256 assert(m_nRefCount <= SFX_ITEMS_MAXREF && "AddRef with non-Pool-Item");
257 assert(n <= SFX_ITEMS_MAXREF - m_nRefCount && "AddRef: refcount overflow");
258 m_nRefCount += n;
259}
260
261inline sal_uInt32 SfxPoolItem::ReleaseRef(sal_uInt32 n) const
262{
263 assert(m_nRefCount <= SFX_ITEMS_MAXREF && "ReleaseRef with non-Pool-Item");
264 assert(n <= m_nRefCount);
265 m_nRefCount -= n;
266 return m_nRefCount;
267}
268
269inline bool IsPoolDefaultItem(const SfxPoolItem *pItem )
270{
271 return pItem && pItem->GetKind() == SfxItemKind::PoolDefault;
272}
273
274inline bool IsStaticDefaultItem(const SfxPoolItem *pItem )
275{
276 return pItem && pItem->GetKind() == SfxItemKind::StaticDefault;
277}
278
279inline bool IsDefaultItem( const SfxPoolItem *pItem )
280{
281 return pItem && (pItem->GetKind() == SfxItemKind::StaticDefault || pItem->GetKind() == SfxItemKind::PoolDefault);
282}
283
284inline bool IsPooledItem( const SfxPoolItem *pItem )
285{
286 return pItem && pItem->GetRefCount() > 0 && pItem->GetRefCount() <= SFX_ITEMS_MAXREF;
287}
288
289inline bool IsInvalidItem(const SfxPoolItem *pItem)
290{
291 return pItem == INVALID_POOL_ITEM;
292}
293
295{
296public:
297 static SfxPoolItem* CreateDefault();
298 explicit SfxVoidItem( sal_uInt16 nWhich );
299 virtual ~SfxVoidItem() override;
300
301 SfxVoidItem(SfxVoidItem const &) = default;
303 SfxVoidItem & operator =(SfxVoidItem const &) = delete; // due to SfxPoolItem
304 SfxVoidItem & operator =(SfxVoidItem &&) = delete; // due to SfxPoolItem
305
306 virtual bool operator==( const SfxPoolItem& ) const override;
307
308 virtual bool GetPresentation( SfxItemPresentation ePres,
309 MapUnit eCoreMetric,
310 MapUnit ePresMetric,
311 OUString &rText,
312 const IntlWrapper& ) const override;
313 virtual void dumpAsXml(xmlTextWriterPtr pWriter) const override;
314
315 // create a copy of itself
316 virtual SfxVoidItem* Clone( SfxItemPool *pPool = nullptr ) const override;
317
319 virtual bool IsVoidItem() const override;
320};
321
322
324{
326public:
327 explicit SfxPoolItemHint( SfxPoolItem* Object ) : pObj(Object) {}
328 SfxPoolItem* GetObject() const { return pObj; }
329};
330
331#endif
332
333/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class for providers of defaults of SfxPoolItems.
Definition: itempool.hxx:51
SfxPoolItem * pObj
Definition: poolitem.hxx:325
SfxPoolItem * GetObject() const
Definition: poolitem.hxx:328
SfxPoolItemHint(SfxPoolItem *Object)
Definition: poolitem.hxx:327
virtual void ScaleMetrics(tools::Long lMult, tools::Long lDiv)
Definition: poolitem.cxx:600
std::vector< SfxPoolItem * >::const_iterator lookup_iterator
Definition: poolitem.hxx:201
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const
Definition: poolitem.cxx:604
T & StaticWhichCast(TypedWhichId< T > nId)
Definition: poolitem.hxx:152
void SetKind(SfxItemKind n)
Definition: poolitem.hxx:248
void AddRef(sal_uInt32 n=1) const
Definition: poolitem.hxx:254
SfxPoolItem(const SfxPoolItem &rCopy)
Definition: poolitem.hxx:137
sal_uInt32 GetRefCount() const
Definition: poolitem.hxx:226
const T & StaticWhichCast(TypedWhichId< T > nId) const
Definition: poolitem.hxx:159
SfxItemKind m_nKind
Definition: poolitem.hxx:125
void SetRefCount(sal_uInt32 n)
Definition: poolitem.hxx:242
T * DynamicWhichCast(TypedWhichId< T > nId)
Definition: poolitem.hxx:169
const T * DynamicWhichCast(TypedWhichId< T > nId) const
Definition: poolitem.hxx:176
virtual bool HasLookup() const
Definition: poolitem.hxx:200
sal_uInt32 m_nRefCount
Definition: poolitem.hxx:123
virtual lookup_iterator Lookup(lookup_iterator, lookup_iterator end) const
Definition: poolitem.hxx:202
sal_uInt16 Which() const
Definition: poolitem.hxx:149
SfxPoolItem(sal_uInt16 nWhich=0)
Definition: poolitem.cxx:465
bool operator!=(const SfxPoolItem &rItem) const
Definition: poolitem.hxx:184
virtual bool IsSortable() const
Definition: poolitem.hxx:190
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId)
Definition: poolitem.cxx:610
std::unique_ptr< SfxPoolItem > CloneSetWhich(sal_uInt16 nNewWhich) const
Definition: poolitem.cxx:557
virtual bool operator<(const SfxPoolItem &) const
Definition: poolitem.hxx:189
void SetWhich(sal_uInt16 nId)
Definition: poolitem.hxx:143
SfxItemKind GetKind() const
Definition: poolitem.hxx:227
sal_uInt32 ReleaseRef(sal_uInt32 n=1) const
Definition: poolitem.hxx:261
virtual ~SfxPoolItem()
Definition: poolitem.cxx:473
SfxPoolItem & operator=(const SfxPoolItem &)=delete
virtual boost::property_tree::ptree dumpAsJSON() const
Definition: poolitem.cxx:551
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
sal_uInt16 m_nWhich
Definition: poolitem.hxx:124
std::unique_ptr< T > CloneSetWhich(TypedWhichId< T > nId) const
Definition: poolitem.hxx:221
virtual bool HasMetrics() const
Definition: poolitem.cxx:602
virtual bool GetPresentation(SfxItemPresentation ePres, MapUnit eCoreMetric, MapUnit ePresMetric, OUString &rText, const IntlWrapper &) const override
This virtual method allows to get a textual representation of the value for the SfxPoolItem subclasse...
Definition: poolitem.cxx:580
SfxVoidItem(SfxVoidItem &&)=default
SfxVoidItem(SfxVoidItem const &)=default
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: poolitem.cxx:588
virtual bool IsVoidItem() const override
Always returns true as this is an SfxVoidItem.
Definition: poolitem.cxx:598
A very thin wrapper around the sal_uInt16 WhichId whose purpose is mostly to carry type information,...
Definition: typedwhich.hxx:21
UNKNOWN
struct _xmlTextWriter * xmlTextWriterPtr
sal_Int64 n
MapUnit
NONE
end
css::uno::Reference< css::animations::XAnimationNode > Clone(const css::uno::Reference< css::animations::XAnimationNode > &xSourceNode, const SdPage *pSource=nullptr, const SdPage *pTarget=nullptr)
long Long
sal_Int16 nId
SfxItemPresentation
Definition: poolitem.hxx:72
SfxItemKind
Definition: poolitem.hxx:39
#define INVALID_POOL_ITEM
Definition: poolitem.hxx:109
bool IsPoolDefaultItem(const SfxPoolItem *pItem)
Definition: poolitem.hxx:269
struct _xmlTextWriter * xmlTextWriterPtr
Definition: poolitem.hxx:113
SfxItemState
These values have to match the values in the css::frame::status::ItemState IDL to be found at offapi/...
Definition: poolitem.hxx:82
@ SET
The property has been explicitly set to a given value hence we know we are not taking the default val...
@ DONTCARE
Specifies that the property is currently in a don't care state.
bool IsDefaultItem(const SfxPoolItem *pItem)
Definition: poolitem.hxx:279
#define SFX_ITEMS_MAXREF
Definition: poolitem.hxx:47
bool Any2Bool(const css::uno::Any &rValue)
Definition: poolitem.hxx:53
bool IsInvalidItem(const SfxPoolItem *pItem)
Definition: poolitem.hxx:289
bool IsPooledItem(const SfxPoolItem *pItem)
Definition: poolitem.hxx:284
bool IsStaticDefaultItem(const SfxPoolItem *pItem)
Definition: poolitem.hxx:274
#define SFX_ITEMS_SPECIAL
Definition: poolitem.hxx:48
#define SVL_DLLPUBLIC
Definition: svldllapi.h:28
constexpr bool operator==(TypedWhichId< T > const &lhs, TypedWhichId< T > rhs)
Definition: typedwhich.hxx:43
unsigned char sal_uInt8
signed char sal_Int8