LibreOffice Module svl (master) 1
itemprop.cxx
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#include <sal/config.h>
21
22#include <o3tl/any.hxx>
23#include <svl/itemprop.hxx>
24#include <svl/itempool.hxx>
25#include <svl/itemset.hxx>
26#include <com/sun/star/beans/PropertyAttribute.hpp>
27#include <com/sun/star/lang/IllegalArgumentException.hpp>
28#include <memory>
29
30/*
31 * UNO III Implementation
32 */
33using namespace com::sun::star;
34using namespace com::sun::star::beans;
35using namespace com::sun::star::lang;
36using namespace com::sun::star::uno;
37
39{
40 m_aMap.reserve(pEntries.size());
41 for (const auto & pEntry : pEntries)
42 {
43 assert(!pEntry.aName.isEmpty() && "empty name? might be something left an empty entry at the end of this array");
44 m_aMap.insert( &pEntry );
45 }
46}
47
49
51{
52}
53
54const SfxItemPropertyMapEntry* SfxItemPropertyMap::getByName( std::u16string_view rName ) const
55{
56 struct Compare
57 {
58 bool operator() ( const SfxItemPropertyMapEntry* lhs, std::u16string_view rhs ) const
59 {
60 return lhs->aName < rhs;
61 }
62 bool operator() ( std::u16string_view lhs, const SfxItemPropertyMapEntry* rhs ) const
63 {
64 return lhs < rhs->aName;
65 }
66 };
67 auto it = std::lower_bound(m_aMap.begin(), m_aMap.end(), rName, Compare());
68 if (it == m_aMap.end() || Compare()(rName, *it))
69 return nullptr;
70 return *it;
71}
72
73uno::Sequence<beans::Property> const & SfxItemPropertyMap::getProperties() const
74{
75 if( !m_aPropSeq.hasElements() )
76 {
77 m_aPropSeq.realloc( m_aMap.size() );
78 beans::Property* pPropArray = m_aPropSeq.getArray();
79 sal_uInt32 n = 0;
80 for( const SfxItemPropertyMapEntry* pEntry : m_aMap )
81 {
82 pPropArray[n].Name = pEntry->aName;
83 pPropArray[n].Handle = pEntry->nWID;
84 pPropArray[n].Type = pEntry->aType;
85 pPropArray[n].Attributes =
86 sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
87 n++;
88 }
89 }
90
91 return m_aPropSeq;
92}
93
94beans::Property SfxItemPropertyMap::getPropertyByName( const OUString& rName ) const
95{
96 const SfxItemPropertyMapEntry* pEntry = getByName(rName);
97 if( !pEntry )
98 throw UnknownPropertyException(rName);
99 beans::Property aProp;
100 aProp.Name = rName;
101 aProp.Handle = pEntry->nWID;
102 aProp.Type = pEntry->aType;
103 aProp.Attributes = sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
104 return aProp;
105}
106
107bool SfxItemPropertyMap::hasPropertyByName( std::u16string_view rName ) const
108{
109 return getByName(rName) != nullptr;
110}
111
113{
114}
115
117 const SfxItemSet& rSet, Any& rAny ) const
118{
119 // get the SfxPoolItem
120 const SfxPoolItem* pItem = nullptr;
121 SfxItemState eState = rSet.GetItemState( rEntry.nWID, true, &pItem );
122 if (SfxItemState::SET != eState && SfxItemPool::IsWhich(rEntry.nWID) )
123 pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
124 // return item values as uno::Any
125 if(eState >= SfxItemState::DEFAULT && pItem)
126 {
127 pItem->QueryValue( rAny, rEntry.nMemberId );
128 }
129 else if(0 == (rEntry.nFlags & PropertyAttribute::MAYBEVOID))
130 {
131 throw RuntimeException(
132 "Property not found in ItemSet but not MAYBEVOID?", nullptr);
133 }
134
135 // convert general SfxEnumItem values to specific values
136 if( rEntry.aType.getTypeClass() == TypeClass_ENUM &&
137 rAny.getValueTypeClass() == TypeClass_LONG )
138 {
139 sal_Int32 nTmp = *o3tl::forceAccess<sal_Int32>(rAny);
140 rAny.setValue( &nTmp, rEntry.aType );
141 }
142}
143
144void SfxItemPropertySet::getPropertyValue( const OUString &rName,
145 const SfxItemSet& rSet, Any& rAny ) const
146{
147 // detect which-id
148 const SfxItemPropertyMapEntry* pEntry = m_aMap.getByName( rName );
149 if ( !pEntry )
150 throw UnknownPropertyException(rName);
151 getPropertyValue( *pEntry,rSet, rAny );
152}
153
155 const SfxItemSet& rSet ) const
156{
157 Any aVal;
158 getPropertyValue( rName,rSet, aVal );
159 return aVal;
160}
161
163 const Any& aVal,
164 SfxItemSet& rSet ) const
165{
166 // get the SfxPoolItem
167 const SfxPoolItem* pItem = nullptr;
168 std::unique_ptr<SfxPoolItem> pNewItem;
169 SfxItemState eState = rSet.GetItemState( rEntry.nWID, true, &pItem );
170 if (SfxItemState::SET != eState && SfxItemPool::IsWhich(rEntry.nWID))
171 pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
172 if (pItem)
173 {
174 pNewItem.reset(pItem->Clone());
175 }
176 if(pNewItem)
177 {
178 if( !pNewItem->PutValue( aVal, rEntry.nMemberId ) )
179 {
180 throw IllegalArgumentException();
181 }
182 // apply new item
183 rSet.Put( std::move(pNewItem) );
184 }
185}
186
187void SfxItemPropertySet::setPropertyValue( const OUString &rName,
188 const Any& aVal,
189 SfxItemSet& rSet ) const
190{
191 const SfxItemPropertyMapEntry* pEntry = m_aMap.getByName( rName );
192 if ( !pEntry )
193 {
194 throw UnknownPropertyException(rName);
195 }
196 setPropertyValue(*pEntry, aVal, rSet);
197}
198
199PropertyState SfxItemPropertySet::getPropertyState(const SfxItemPropertyMapEntry& rEntry, const SfxItemSet& rSet) const
200 noexcept
201{
202 PropertyState eRet = PropertyState_DIRECT_VALUE;
203 sal_uInt16 nWhich = rEntry.nWID;
204
205 // Get item state
206 SfxItemState eState = rSet.GetItemState( nWhich, false );
207 // Return item value as UnoAny
208 if(eState == SfxItemState::DEFAULT)
209 eRet = PropertyState_DEFAULT_VALUE;
210 else if(eState < SfxItemState::DEFAULT)
211 eRet = PropertyState_AMBIGUOUS_VALUE;
212 return eRet;
213}
214
215PropertyState SfxItemPropertySet::getPropertyState(const OUString& rName, const SfxItemSet& rSet) const
216{
217 PropertyState eRet = PropertyState_DIRECT_VALUE;
218
219 // Get WhichId
220 const SfxItemPropertyMapEntry* pEntry = m_aMap.getByName( rName );
221 if( !pEntry || !pEntry->nWID )
222 {
223 throw UnknownPropertyException(rName);
224 }
225 sal_uInt16 nWhich = pEntry->nWID;
226
227 // Get item state
228 SfxItemState eState = rSet.GetItemState(nWhich, false);
229 // Return item value as UnoAny
230 if(eState == SfxItemState::DEFAULT)
231 eRet = PropertyState_DEFAULT_VALUE;
232 else if(eState < SfxItemState::DEFAULT)
233 eRet = PropertyState_AMBIGUOUS_VALUE;
234 return eRet;
235}
236
238{
239 if( !m_xInfo.is() )
241 return m_xInfo;
242}
243
245 : m_aOwnMap( rMap )
246{
247}
248
250 : m_aOwnMap( pEntries )
251{
252}
253
255{
256 return m_aOwnMap.getProperties();
257}
258
260{
261}
262
264{
265 return m_aOwnMap.getPropertyByName( rName );
266}
267
269{
270 return m_aOwnMap.hasPropertyByName( rName );
271}
272
274 const Sequence<Property>& rPropSeq )
275{
276 maMap.reserve(pEntries.size() + rPropSeq.getLength());
277 for (const auto & rEntry : pEntries )
278 {
279 assert(!rEntry.aName.isEmpty() && "empty name? might be something left an empty entry at the end of this array");
280 maMap.insert( rEntry );
281 }
282 for( const auto & rProp : rPropSeq )
283 {
285 rProp.Name,
286 sal::static_int_cast< sal_Int16 >( rProp.Handle ), //nWID
287 rProp.Type, //aType
288 rProp.Attributes,
289 0); //nFlags
290 maMap.insert( aTemp );
291 }
292}
293
295{
296}
297
299{
300 if( !m_aPropSeq.hasElements() )
301 {
302 m_aPropSeq.realloc( maMap.size() );
303 beans::Property* pPropArray = m_aPropSeq.getArray();
304 sal_uInt32 n = 0;
305 for( const SfxItemPropertyMapEntry& rEntry : maMap )
306 {
307 pPropArray[n].Name = rEntry.aName;
308 pPropArray[n].Handle = rEntry.nWID;
309 pPropArray[n].Type = rEntry.aType;
310 pPropArray[n].Attributes =
311 sal::static_int_cast< sal_Int16 >(rEntry.nFlags);
312 n++;
313 }
314 }
315
316 return m_aPropSeq;
317}
318
319Property SAL_CALL SfxExtItemPropertySetInfo::getPropertyByName( const OUString& rPropertyName )
320{
321 const SfxItemPropertyMapEntry* pEntry = getByName(rPropertyName);
322 if( !pEntry )
323 throw UnknownPropertyException(rPropertyName);
324 beans::Property aProp;
325 aProp.Name = rPropertyName;
326 aProp.Handle = pEntry->nWID;
327 aProp.Type = pEntry->aType;
328 aProp.Attributes = sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
329 return aProp;
330}
331
332sal_Bool SAL_CALL SfxExtItemPropertySetInfo::hasPropertyByName( const OUString& rPropertyName )
333{
334 return getByName(rPropertyName) != nullptr;
335}
336
337const SfxItemPropertyMapEntry* SfxExtItemPropertySetInfo::getByName( std::u16string_view rName ) const
338{
339 struct Compare
340 {
341 bool operator() ( const SfxItemPropertyMapEntry& lhs, std::u16string_view rhs ) const
342 {
343 return lhs.aName < rhs;
344 }
345 bool operator() ( std::u16string_view lhs, const SfxItemPropertyMapEntry& rhs ) const
346 {
347 return lhs < rhs.aName;
348 }
349 };
350 auto it = std::lower_bound(maMap.begin(), maMap.end(), rName, Compare());
351 if (it == maMap.end() || Compare()(rName, *it))
352 return nullptr;
353 return &*it;
354}
355
356/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Bool SAL_CALL hasPropertyByName(const OUString &Name) override
Definition: itemprop.cxx:332
css::uno::Sequence< css::beans::Property > m_aPropSeq
Definition: itemprop.hxx:199
const SfxItemPropertyMapEntry * getByName(std::u16string_view rName) const
Definition: itemprop.cxx:337
virtual css::beans::Property SAL_CALL getPropertyByName(const OUString &aName) override
Definition: itemprop.cxx:319
virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties() override
Definition: itemprop.cxx:298
virtual ~SfxExtItemPropertySetInfo() override
Definition: itemprop.cxx:294
o3tl::sorted_vector< SfxItemPropertyMapEntry, SfxItemPropertyMapCompare2 > maMap
Definition: itemprop.hxx:198
SfxExtItemPropertySetInfo(o3tl::span< const SfxItemPropertyMapEntry > pMap, const css::uno::Sequence< css::beans::Property > &rPropSeq)
Definition: itemprop.cxx:273
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
Definition: itempool.cxx:816
static bool IsWhich(sal_uInt16 nId)
Definition: itempool.hxx:194
css::uno::Sequence< css::beans::Property > const & getProperties() const
Definition: itemprop.cxx:73
const SfxItemPropertyMapEntry * getByName(std::u16string_view rName) const
Definition: itemprop.cxx:54
SfxItemPropertyMap(o3tl::span< const SfxItemPropertyMapEntry > pEntries)
Definition: itemprop.cxx:38
css::uno::Sequence< css::beans::Property > m_aPropSeq
Definition: itemprop.hxx:82
bool hasPropertyByName(std::u16string_view rName) const
Definition: itemprop.cxx:107
css::beans::Property getPropertyByName(const OUString &rName) const
Definition: itemprop.cxx:94
o3tl::sorted_vector< const SfxItemPropertyMapEntry *, SfxItemPropertyMapCompare > m_aMap
Definition: itemprop.hxx:81
virtual ~SfxItemPropertySetInfo() override
Definition: itemprop.cxx:259
virtual sal_Bool SAL_CALL hasPropertyByName(const OUString &Name) override
Definition: itemprop.cxx:268
SfxItemPropertySetInfo(const SfxItemPropertyMap &rMap)
Definition: itemprop.cxx:244
virtual css::beans::Property SAL_CALL getPropertyByName(const OUString &aName) override
Definition: itemprop.cxx:263
virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties() override
Definition: itemprop.cxx:254
SfxItemPropertyMap m_aOwnMap
Definition: itemprop.hxx:150
css::beans::PropertyState getPropertyState(const OUString &rName, const SfxItemSet &rSet) const
Definition: itemprop.cxx:215
css::uno::Reference< css::beans::XPropertySetInfo > m_xInfo
Definition: itemprop.hxx:100
void setPropertyValue(const SfxItemPropertyMapEntry &rEntry, const css::uno::Any &aVal, SfxItemSet &rSet) const
SfxItemPropertyMap m_aMap
Definition: itemprop.hxx:99
void getPropertyValue(const SfxItemPropertyMapEntry &rEntry, const SfxItemSet &rSet, css::uno::Any &rAny) const
css::uno::Reference< css::beans::XPropertySetInfo > const & getPropertySetInfo() const
Definition: itemprop.cxx:237
SfxItemPool * GetPool() const
Definition: itemset.hxx:202
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
Definition: itemset.cxx:321
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
Definition: itemset.hxx:181
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const
Definition: poolitem.cxx:604
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
const_iterator begin() const
void reserve(size_type amount)
const_iterator end() const
size_type size() const
std::pair< const_iterator, bool > insert(Value &&x)
constexpr size_type size() const noexcept
sal_Int64 n
SfxItemState
These values have to match the values in the css::frame::status::ItemState IDL to be found at offapi/...
Definition: poolitem.hxx:82
@ DEFAULT
Specifies that the property is currently in a default state.
@ SET
The property has been explicitly set to a given value hence we know we are not taking the default val...
static SfxItemSet & rSet
map a property between beans::XPropertySet and SfxPoolItem
Definition: itemprop.hxx:38
sal_uInt8 nMemberId
"member ID" to tell QueryValue/PutValue which property it is (when multiple properties map to the sam...
Definition: itemprop.hxx:46
OUString aName
name of property
Definition: itemprop.hxx:39
sal_uInt16 nWID
WhichId of SfxPoolItem.
Definition: itemprop.hxx:41
css::uno::Type aType
UNO type of property.
Definition: itemprop.hxx:40
sal_Int16 nFlags
flag bitmap,
Definition: itemprop.hxx:43
unsigned char sal_Bool