Bump version to 6.4-15
[LibreOffice.git] / include / svl / poolitem.hxx
blobdd74806464cd8ddaf3963e1b19dc1296b00de04b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <memory>
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 <boost/property_tree/json_parser.hpp>
34 class IntlWrapper;
36 enum class SfxItemKind : sal_Int8
38 NONE,
39 DeleteOnIdle,
40 StaticDefault,
41 PoolDefault
44 #define SFX_ITEMS_OLD_MAXREF 0xffef
45 #define SFX_ITEMS_MAXREF 0xfffffffe
46 #define SFX_ITEMS_SPECIAL 0xffffffff
48 #define CONVERT_TWIPS 0x80 // Uno conversion for measurement (for MemberId)
50 // warning, if there is no boolean inside the any this will always return the value false
51 inline bool Any2Bool( const css::uno::Any&rValue )
53 bool bValue = false;
54 if( !(rValue >>= bValue) )
56 sal_Int32 nNum = 0;
57 if( rValue >>= nNum )
58 bValue = nNum != 0;
61 return bValue;
65 * The values of this enum describe the degree of textual
66 * representation of an item after calling the virtual
67 * method <SfxPoolItem::GetPresentation()const>.
69 enum class SfxItemPresentation
71 Nameless,
72 Complete
75 /**
76 * These values have to match the values in the
77 * css::frame::status::ItemState IDL
78 * to be found at offapi/com/sun/star/frame/status/ItemState.idl
80 enum class SfxItemState {
82 /** Specifies an unknown state. */
83 UNKNOWN = 0,
85 /** Specifies that the property is currently disabled. */
86 DISABLED = 0x0001,
88 /** Specifies that the property is currently read-only. */
89 READONLY = 0x0002,
91 /** Specifies that the property is currently in a don't care state.
92 * <br/>
93 * This is normally used if a selection provides more than one state
94 * for a property at the same time.
96 DONTCARE = 0x0010,
98 /** Specifies that the property is currently in a default state. */
99 DEFAULT = 0x0020,
101 /** The property has been explicitly set to a given value hence we know
102 * we are not taking the default value.
103 * <br/>
104 * For example, you may want to get the font color and it might either
105 * be the default one or one that has been explicitly set.
107 SET = 0x0040
110 #define INVALID_POOL_ITEM reinterpret_cast<SfxPoolItem*>(-1)
112 class SfxItemPool;
113 class SfxItemSet;
114 typedef struct _xmlTextWriter* xmlTextWriterPtr;
116 class SVL_DLLPUBLIC SfxPoolItem
118 friend class SfxItemPool;
119 friend class SfxItemDisruptor_Impl;
120 friend class SfxItemPoolCache;
121 friend class SfxItemSet;
122 friend class SfxVoidItem;
124 mutable sal_uInt32 m_nRefCount;
125 sal_uInt16 m_nWhich;
126 SfxItemKind m_nKind;
128 private:
129 inline void SetRefCount(sal_uInt32 n);
130 inline void SetKind( SfxItemKind n );
131 public:
132 inline void AddRef(sal_uInt32 n = 1) const;
133 private:
134 inline sal_uInt32 ReleaseRef(sal_uInt32 n = 1) const;
136 protected:
137 explicit SfxPoolItem( sal_uInt16 nWhich = 0 );
138 SfxPoolItem( const SfxPoolItem& rCopy)
139 : SfxPoolItem(rCopy.m_nWhich) {}
141 public:
142 virtual ~SfxPoolItem();
144 void SetWhich( sal_uInt16 nId )
146 // can only change the Which before we are in a set
147 assert(m_nRefCount==0);
148 m_nWhich = nId;
150 sal_uInt16 Which() const { return m_nWhich; }
151 virtual bool operator==( const SfxPoolItem& ) const = 0;
152 bool operator!=( const SfxPoolItem& rItem ) const
153 { return !(*this == rItem); }
155 // Sorting is only used for faster searching in a pool which contains large quantities
156 // of a single kind of pool item.
157 virtual bool operator<( const SfxPoolItem& ) const { assert(false); return false; }
158 virtual bool IsSortable() const { return false; }
160 /** @return true if it has a valid string representation */
161 virtual bool GetPresentation( SfxItemPresentation ePresentation,
162 MapUnit eCoreMetric,
163 MapUnit ePresentationMetric,
164 OUString &rText,
165 const IntlWrapper& rIntlWrapper ) const;
167 virtual void ScaleMetrics( long lMult, long lDiv );
168 virtual bool HasMetrics() const;
170 virtual bool QueryValue( css::uno::Any& rVal, sal_uInt8 nMemberId = 0 ) const;
171 virtual bool PutValue( const css::uno::Any& rVal, sal_uInt8 nMemberId );
173 virtual SfxPoolItem* Clone( SfxItemPool *pPool = nullptr ) const = 0;
174 // clone and call SetWhich
175 std::unique_ptr<SfxPoolItem> CloneSetWhich( sal_uInt16 nNewWhich ) const;
176 template<class T> std::unique_ptr<T> CloneSetWhich( TypedWhichId<T> nId ) const
178 return std::unique_ptr<T>(static_cast<T*>(CloneSetWhich(sal_uInt16(nId)).release()));
181 sal_uInt32 GetRefCount() const { return m_nRefCount; }
182 SfxItemKind GetKind() const { return m_nKind; }
183 virtual void dumpAsXml(xmlTextWriterPtr pWriter) const;
184 virtual boost::property_tree::ptree dumpAsJSON() const;
186 /** Only SfxVoidItem shall and must return true for this.
188 This avoids costly calls to dynamic_cast<const SfxVoidItem*>()
189 specifically in SfxItemSet::GetItemState()
191 virtual bool IsVoidItem() const;
193 private:
194 SfxPoolItem& operator=( const SfxPoolItem& ) = delete;
197 inline void SfxPoolItem::SetRefCount(sal_uInt32 n)
199 m_nRefCount = n;
200 m_nKind = SfxItemKind::NONE;
203 inline void SfxPoolItem::SetKind( SfxItemKind n )
205 m_nRefCount = SFX_ITEMS_SPECIAL;
206 m_nKind = n;
209 inline void SfxPoolItem::AddRef(sal_uInt32 n) const
211 assert(m_nRefCount <= SFX_ITEMS_MAXREF && "AddRef with non-Pool-Item");
212 assert(n <= SFX_ITEMS_MAXREF - m_nRefCount && "AddRef: refcount overflow");
213 m_nRefCount += n;
216 inline sal_uInt32 SfxPoolItem::ReleaseRef(sal_uInt32 n) const
218 assert(m_nRefCount <= SFX_ITEMS_MAXREF && "ReleaseRef with non-Pool-Item");
219 assert(n <= m_nRefCount);
220 m_nRefCount -= n;
221 return m_nRefCount;
224 inline bool IsPoolDefaultItem(const SfxPoolItem *pItem )
226 return pItem && pItem->GetKind() == SfxItemKind::PoolDefault;
229 inline bool IsStaticDefaultItem(const SfxPoolItem *pItem )
231 return pItem && pItem->GetKind() == SfxItemKind::StaticDefault;
234 inline bool IsDefaultItem( const SfxPoolItem *pItem )
236 return pItem && (pItem->GetKind() == SfxItemKind::StaticDefault || pItem->GetKind() == SfxItemKind::PoolDefault);
239 inline bool IsPooledItem( const SfxPoolItem *pItem )
241 return pItem && pItem->GetRefCount() > 0 && pItem->GetRefCount() <= SFX_ITEMS_MAXREF;
244 inline bool IsInvalidItem(const SfxPoolItem *pItem)
246 return pItem == INVALID_POOL_ITEM;
249 class SVL_DLLPUBLIC SfxVoidItem final: public SfxPoolItem
251 public:
252 static SfxPoolItem* CreateDefault();
253 explicit SfxVoidItem( sal_uInt16 nWhich );
254 virtual ~SfxVoidItem() override;
256 SfxVoidItem(SfxVoidItem const &) = default;
257 SfxVoidItem(SfxVoidItem &&) = default;
258 SfxVoidItem & operator =(SfxVoidItem const &) = delete; // due to SfxPoolItem
259 SfxVoidItem & operator =(SfxVoidItem &&) = delete; // due to SfxPoolItem
261 virtual bool operator==( const SfxPoolItem& ) const override;
263 virtual bool GetPresentation( SfxItemPresentation ePres,
264 MapUnit eCoreMetric,
265 MapUnit ePresMetric,
266 OUString &rText,
267 const IntlWrapper& ) const override;
268 virtual void dumpAsXml(xmlTextWriterPtr pWriter) const override;
270 // create a copy of itself
271 virtual SfxPoolItem* Clone( SfxItemPool *pPool = nullptr ) const override;
273 /** Always returns true as this is an SfxVoidItem. */
274 virtual bool IsVoidItem() const override;
277 class SVL_DLLPUBLIC SfxSetItem: public SfxPoolItem
279 std::unique_ptr<SfxItemSet> pSet;
281 SfxSetItem & operator=( const SfxSetItem& ) = delete;
283 public:
284 SfxSetItem( sal_uInt16 nWhich, std::unique_ptr<SfxItemSet> &&pSet );
285 SfxSetItem( sal_uInt16 nWhich, const SfxItemSet &rSet );
286 SfxSetItem( const SfxSetItem&, SfxItemPool *pPool = nullptr );
287 virtual ~SfxSetItem() override;
289 virtual bool operator==( const SfxPoolItem& ) const override;
291 virtual bool GetPresentation( SfxItemPresentation ePres,
292 MapUnit eCoreMetric,
293 MapUnit ePresMetric,
294 OUString &rText,
295 const IntlWrapper& ) const override;
297 // create a copy of itself
298 virtual SfxPoolItem* Clone( SfxItemPool *pPool = nullptr ) const override = 0;
300 const SfxItemSet& GetItemSet() const
301 { return *pSet; }
302 SfxItemSet& GetItemSet()
303 { return *pSet; }
306 class SVL_DLLPUBLIC SfxPoolItemHint final : public SfxHint
308 SfxPoolItem* pObj;
309 public:
310 explicit SfxPoolItemHint( SfxPoolItem* Object ) : pObj(Object) {}
311 SfxPoolItem* GetObject() const { return pObj; }
314 #endif
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */