LibreOffice Module comphelper (master) 1
opropertybag.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
21#include "opropertybag.hxx"
22
23#include <com/sun/star/beans/IllegalTypeException.hpp>
24#include <com/sun/star/beans/PropertyAttribute.hpp>
25#include <com/sun/star/beans/Property.hpp>
26
29
31
32#include <algorithm>
33
34namespace com::sun::star::uno { class XComponentContext; }
35
36using namespace ::com::sun::star;
37
38extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
40 css::uno::XComponentContext *,
41 css::uno::Sequence<css::uno::Any> const &)
42{
43 return cppu::acquire(new comphelper::OPropertyBag());
44}
45
46namespace comphelper
47{
48
49
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::util;
54 using namespace ::com::sun::star::container;
55
57 :OPropertyBag_PBase( GetBroadcastHelper(), this )
58 ,::cppu::IEventNotificationHook()
59 ,m_bAutoAddProperties( false )
60 ,m_NotifyListeners(m_aMutex)
61 ,m_isModified(false)
62
63 {
64 }
65
66
68 {
69 }
70
71
74
75 void SAL_CALL OPropertyBag::initialize( const Sequence< Any >& _rArguments )
76 {
77 Sequence< Type > aTypes;
78 bool AllowEmptyPropertyName(false);
79 bool AutomaticAddition(false);
80
81 if (_rArguments.getLength() == 3
82 && (_rArguments[0] >>= aTypes)
83 && (_rArguments[1] >>= AllowEmptyPropertyName)
84 && (_rArguments[2] >>= AutomaticAddition))
85 {
86 m_aAllowedTypes.insert(std::cbegin(aTypes), std::cend(aTypes));
87 m_bAutoAddProperties = AutomaticAddition;
88
89 } else {
91
92 if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
93 m_aAllowedTypes.insert(std::cbegin(aTypes), std::cend(aTypes));
94
95 aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
96 aArguments.get_ensureType( "AllowEmptyPropertyName",
97 AllowEmptyPropertyName );
98 }
99 if (AllowEmptyPropertyName) {
100 m_aDynamicProperties.setAllowEmptyPropertyName(
101 AllowEmptyPropertyName);
102 }
103 }
104
106 {
107 return "com.sun.star.comp.comphelper.OPropertyBag";
108 }
109
110 sal_Bool SAL_CALL OPropertyBag::supportsService( const OUString& rServiceName )
111 {
112 return cppu::supportsService(this, rServiceName);
113 }
114
115 Sequence< OUString > SAL_CALL OPropertyBag::getSupportedServiceNames( )
116 {
117 return { "com.sun.star.beans.PropertyBag" };
118 }
119
121 sal_Int32 * /*pnHandles*/,
122 sal_Int32 nCount,
123 sal_Bool bVetoable,
124 bool bIgnoreRuntimeExceptionsWhileFiring)
125 {
126 if (nCount && !bVetoable) {
127 setModifiedImpl(true, bIgnoreRuntimeExceptionsWhileFiring);
128 }
129 }
130
131 void OPropertyBag::setModifiedImpl(bool bModified,
132 bool bIgnoreRuntimeExceptionsWhileFiring)
133 {
134 { // do not lock mutex while notifying (#i93514#) to prevent deadlock
135 ::osl::MutexGuard aGuard( m_aMutex );
136 m_isModified = bModified;
137 }
138 if (!bModified)
139 return;
140
141 try {
142 Reference<XInterface> xThis(*this);
143 EventObject event(xThis);
145 &XModifyListener::modified, event);
146 } catch (RuntimeException &) {
147 if (!bIgnoreRuntimeExceptionsWhileFiring) {
148 throw;
149 }
150 } catch (Exception &) {
151 // ignore
152 }
153 }
154
155
157 {
158 ::osl::MutexGuard aGuard( m_aMutex );
159 return m_isModified;
160 }
161
162 void SAL_CALL OPropertyBag::setModified( sal_Bool bModified )
163 {
164 setModifiedImpl(bModified, false);
165 }
166
168 const Reference< XModifyListener > & xListener)
169 {
171 }
172
174 const Reference< XModifyListener > & xListener)
175 {
177 }
178
179
180 Reference< XPropertySetInfo > SAL_CALL OPropertyBag::getPropertySetInfo( )
181 {
182 return createPropertySetInfo( getInfoHelper() );
183 }
184
185
186 sal_Bool SAL_CALL OPropertyBag::has( const Any& /*aElement*/ )
187 {
188 // XSet is only a workaround for addProperty not being able to add default-void properties.
189 // So, everything of XSet except insert is implemented empty
190 return false;
191 }
192
193
194 void SAL_CALL OPropertyBag::insert( const Any& _element )
195 {
196 // This is a workaround for addProperty not being able to add default-void properties.
197 // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
198 Property aProperty;
199 if ( !( _element >>= aProperty ) )
200 throw IllegalArgumentException( "element is not Property", *this, 1 );
201
202 {
203 osl::MutexGuard g(m_aMutex);
204
205 // check whether the type is allowed, everything else will be checked
206 // by m_aDynamicProperties
207 if (!m_aAllowedTypes.empty()
208 && m_aAllowedTypes.find(aProperty.Type) == m_aAllowedTypes.end())
209 throw IllegalArgumentException("not in list of allowed types", *this, 1);
210
211 m_aDynamicProperties.addVoidProperty(aProperty.Name, aProperty.Type, findFreeHandle(),
212 aProperty.Attributes);
213
214 // our property info is dirty
215 m_pArrayHelper.reset();
216 }
217 setModified(true);
218 }
219
220
221 void SAL_CALL OPropertyBag::remove( const Any& /*aElement*/ )
222 {
223 // XSet is only a workaround for addProperty not being able to add default-void properties.
224 // So, everything of XSet except insert is implemented empty
225 throw NoSuchElementException( OUString(), *this );
226 }
227
228
229 Reference< XEnumeration > SAL_CALL OPropertyBag::createEnumeration( )
230 {
231 // XSet is only a workaround for addProperty not being able to add default-void properties.
232 // So, everything of XSet except insert is implemented empty
233 return nullptr;
234 }
235
236
238 {
239 // XSet is only a workaround for addProperty not being able to add default-void properties.
240 // So, everything of XSet except insert is implemented empty
241 return Type();
242 }
243
244
246 {
247 // XSet is only a workaround for addProperty not being able to add default-void properties.
248 // So, everything of XSet except insert is implemented empty
249 return false;
250 }
251
252
253 void SAL_CALL OPropertyBag::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
254 {
255 m_aDynamicProperties.getFastPropertyValue( _nHandle, _rValue );
256 }
257
258 sal_Bool SAL_CALL OPropertyBag::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue )
259 {
260 return m_aDynamicProperties.convertFastPropertyValue( _nHandle, _rValue, _rConvertedValue, _rOldValue );
261 }
262
263 void SAL_CALL OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
264 {
265 m_aDynamicProperties.setFastPropertyValue( nHandle, rValue );
266 }
267
268
270 {
271 if (!m_pArrayHelper)
272 {
273 Sequence< Property > aProperties;
274 m_aDynamicProperties.describeProperties( aProperties );
275 m_pArrayHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties ) );
276 }
277 return *m_pArrayHelper;
278
279 }
280
281
283 {
284 const sal_Int32 nPrime = 1009;
285 const sal_Int32 nSeed = 11;
286
287 sal_Int32 nCheck = nSeed;
288 while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) && ( nCheck != 1 ) )
289 {
290 nCheck = ( nCheck * nSeed ) % nPrime;
291 }
292
293 if ( nCheck == 1 )
294 { // uh ... we already have 1008 handles used up
295 // -> simply count upwards
296 while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) )
297 ++nCheck;
298 }
299
300 return nCheck;
301 }
302
303
304 void SAL_CALL OPropertyBag::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
305 {
306 {
307 osl::MutexGuard g(m_aMutex);
308
309 // check whether the type is allowed, everything else will be checked
310 // by m_aDynamicProperties
311 const Type& aPropertyType = _rInitialValue.getValueType();
312 if (_rInitialValue.hasValue() && !m_aAllowedTypes.empty()
313 && m_aAllowedTypes.find(aPropertyType) == m_aAllowedTypes.end())
314 throw IllegalTypeException(OUString(), *this);
315
316 m_aDynamicProperties.addProperty(_rName, findFreeHandle(), _nAttributes,
317 _rInitialValue);
318
319 // our property info is dirty
320 m_pArrayHelper.reset();
321 }
322 setModified(true);
323 }
324
325
326 void SAL_CALL OPropertyBag::removeProperty( const OUString& _rName )
327 {
328 {
329 osl::MutexGuard g(m_aMutex);
330
331 m_aDynamicProperties.removeProperty(_rName);
332
333 // our property info is dirty
334 m_pArrayHelper.reset();
335 }
336 setModified(true);
337 }
338
339
340 namespace
341 {
342 struct ComparePropertyValueByName
343 {
344 bool operator()( const PropertyValue& _rLHS, const PropertyValue& _rRHS )
345 {
346 return _rLHS.Name < _rRHS.Name;
347 }
348 };
349
350 template< typename CLASS >
351 struct TransformPropertyToName
352 {
353 const OUString& operator()( const CLASS& _rProp )
354 {
355 return _rProp.Name;
356 }
357 };
358
359 struct ExtractPropertyValue
360 {
361 const Any& operator()( const PropertyValue& _rProp )
362 {
363 return _rProp.Value;
364 }
365 };
366 }
367
368
369 Sequence< PropertyValue > SAL_CALL OPropertyBag::getPropertyValues( )
370 {
371 ::osl::MutexGuard aGuard( m_aMutex );
372
373 // all registered properties
374 Sequence< Property > aProperties;
375 m_aDynamicProperties.describeProperties( aProperties );
376
377 // their names
378 Sequence< OUString > aNames( aProperties.getLength() );
379 std::transform(
380 std::cbegin(aProperties),
381 std::cend(aProperties),
382 aNames.getArray(),
383 TransformPropertyToName< Property >()
384 );
385
386 // their values
387 Sequence< Any > aValues;
388 try
389 {
390 aValues = OPropertyBag_PBase::getPropertyValues( aNames );
391 if ( aValues.getLength() != aNames.getLength() )
392 throw RuntimeException();
393 }
394 catch( const RuntimeException& )
395 {
396 throw;
397 }
398 catch( const Exception& )
399 {
400 // ignore
401 }
402
403 // merge names and values, and retrieve the state/handle
405
406 Sequence< PropertyValue > aPropertyValues( aNames.getLength() );
407 const OUString* pName = aNames.getConstArray();
408 const OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
409 const Any* pValue = aValues.getArray();
410 PropertyValue* pPropertyValue = aPropertyValues.getArray();
411
412 for ( ; pName != pNamesEnd; ++pName, ++pValue, ++pPropertyValue )
413 {
414 pPropertyValue->Name = *pName;
415 pPropertyValue->Handle = rPropInfo.getHandleByName( *pName );
416 pPropertyValue->Value = *pValue;
417 pPropertyValue->State = getPropertyStateByHandle( pPropertyValue->Handle );
418 }
419
420 return aPropertyValues;
421 }
422
423
424 void OPropertyBag::impl_setPropertyValues_throw( const Sequence< PropertyValue >& _rProps )
425 {
426 // sort (the XMultiPropertySet interface requires this)
427 Sequence< PropertyValue > aProperties( _rProps );
428 auto [begin, end] = asNonConstRange(aProperties);
429 std::sort(
430 begin,
431 end,
432 ComparePropertyValueByName()
433 );
434
435 // a sequence of names
436 Sequence< OUString > aNames( aProperties.getLength() );
437 std::transform(
438 std::cbegin(aProperties),
439 std::cend(aProperties),
440 aNames.getArray(),
441 TransformPropertyToName< PropertyValue >()
442 );
443
444 try
445 {
446 // check for unknown properties
447 // we cannot simply rely on the XMultiPropertySet::setPropertyValues
448 // implementation of our base class, since it does not throw
449 // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
450 // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
451 // requires it
452 sal_Int32 nCount = aNames.getLength();
453
454 Sequence< sal_Int32 > aHandles( nCount );
455 sal_Int32* pHandle = aHandles.getArray();
456 const PropertyValue* pProperty = aProperties.getConstArray();
457 for ( const OUString* pName = aNames.getConstArray();
458 pName != aNames.getConstArray() + aNames.getLength();
459 ++pName, ++pHandle, ++pProperty
460 )
461 {
463 *pHandle = rPropInfo.getHandleByName( *pName );
464 if ( *pHandle != -1 )
465 continue;
466
467 // there's a property requested which we do not know
469 {
470 // add the property
471 sal_Int16 const nAttributes = PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE | PropertyAttribute::MAYBEDEFAULT;
472 addProperty( *pName, nAttributes, pProperty->Value );
473 continue;
474 }
475
476 // no way out
477 throw UnknownPropertyException( *pName, *this );
478 }
479
480 // a sequence of values
481 Sequence< Any > aValues( aProperties.getLength() );
482 std::transform(
483 std::cbegin(aProperties),
484 std::cend(aProperties),
485 aValues.getArray(),
486 ExtractPropertyValue()
487 );
488
489 setFastPropertyValues( nCount, aHandles.getArray(), aValues.getConstArray(), nCount );
490 }
491 catch( const PropertyVetoException& ) { throw; }
492 catch( const IllegalArgumentException& ) { throw; }
493 catch( const WrappedTargetException& ) { throw; }
494 catch( const RuntimeException& ) { throw; }
495 catch( const UnknownPropertyException& ) { throw; }
496 catch( const Exception& )
497 {
498 throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
499 }
500 }
501
502
503 void SAL_CALL OPropertyBag::setPropertyValues( const Sequence< PropertyValue >& _rProps )
504 {
505 ::osl::MutexGuard aGuard( m_aMutex );
507 }
508
509
510 PropertyState OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle )
511 {
512 // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
513 // assume they're always in DIRECT state.
514 // (Note that this probably would belong into the base class. However, this would mean we would need
515 // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
516 // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
517 // current phase. #i78593#
518
520 sal_Int16 nAttributes(0);
521 OSL_VERIFY( rPropInfo.fillPropertyMembersByHandle( nullptr, &nAttributes, _nHandle ) );
522 if ( ( nAttributes & PropertyAttribute::MAYBEDEFAULT ) == 0 )
523 return PropertyState_DIRECT_VALUE;
524
525 return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle );
526 }
527
528
530 {
531 Any aDefault;
532 m_aDynamicProperties.getPropertyDefaultByHandle( _nHandle, aDefault );
533 return aDefault;
534 }
535
536
537} // namespace comphelper
538
539
540/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertyValueVector_t aPropertyValues
PropertiesInfo aProperties
const char * pName
a collection of named values, packed in various formats.
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
Inserts an element into the container.
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
Removes an element from the container.
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
void setModifiedImpl(bool bModified, bool bIgnoreRuntimeExceptionsWhileFiring)
virtual void SAL_CALL remove(const css::uno::Any &aElement) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual void SAL_CALL removeProperty(const OUString &Name) override
bool m_bAutoAddProperties
should we automatically add properties which are tried to set, if they don't exist previously?
::comphelper::PropertyBag m_aDynamicProperties
TypeBag m_aAllowedTypes
set of allowed property types
virtual css::uno::Any getPropertyDefaultByHandle(sal_Int32 _nHandle) const override
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL addModifyListener(const css::uno::Reference< css::util::XModifyListener > &xListener) override
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< css::beans::PropertyValue > &aProps) override
virtual css::uno::Type SAL_CALL getElementType() override
virtual void fireEvents(sal_Int32 *pnHandles, sal_Int32 nCount, sal_Bool bVetoable, bool bIgnoreRuntimeExceptionsWhileFiring) override
virtual void SAL_CALL setModified(sal_Bool bModified) override
bool m_isModified
modify flag
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual css::beans::PropertyState getPropertyStateByHandle(sal_Int32 _nHandle) override
virtual ~OPropertyBag() override
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getPropertyValues() override
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
std::unique_ptr< ::cppu::OPropertyArrayHelper > m_pArrayHelper
our IPropertyArrayHelper implementation
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
virtual void SAL_CALL removeModifyListener(const css::uno::Reference< css::util::XModifyListener > &xListener) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
::comphelper::OInterfaceContainerHelper3< css::util::XModifyListener > m_NotifyListeners
for notification
virtual void SAL_CALL addProperty(const OUString &Name, ::sal_Int16 Attributes, const css::uno::Any &DefaultValue) override
virtual sal_Bool SAL_CALL isModified() override
virtual sal_Bool SAL_CALL has(const css::uno::Any &aElement) override
sal_Int32 findFreeHandle() const
finds a free property handle @precond our mutex is locked
void impl_setPropertyValues_throw(const css::uno::Sequence< css::beans::PropertyValue > &_rProps)
implements the setPropertyValues method
virtual sal_Bool SAL_CALL hasElements() override
virtual void SAL_CALL insert(const css::uno::Any &aElement) override
virtual sal_Bool SAL_CALL fillPropertyMembersByHandle(::rtl::OUString *pPropName, sal_Int16 *pAttributes, sal_Int32 nHandle)=0
virtual sal_Int32 SAL_CALL getHandleByName(const ::rtl::OUString &rPropertyName)=0
int nCount
::osl::Mutex m_aMutex
Sequence< PropertyValue > aArguments
@ Exception
::cppu::WeakImplHelper< css::beans::XPropertyBag, css::util::XModifiable, css::lang::XServiceInfo, css::lang::XInitialization, css::container::XSet > OPropertyBag_Base
::comphelper::OPropertyStateHelper OPropertyBag_PBase
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
enumrange< T >::Iterator begin(enumrange< T >)
end
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_comphelper_OPropertyBag(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
sal_Int32 nHandle
sal_Int16 nAttributes
unsigned char sal_Bool
#define IMPLEMENT_FORWARD_XTYPEPROVIDER2(classname, baseclass1, baseclass2)
Definition: uno3.hxx:136
#define IMPLEMENT_FORWARD_XINTERFACE2(classname, refcountbase, baseclass2)
Definition: uno3.hxx:99
const SvXMLTokenMapEntry aTypes[]