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 
30 #include <cppuhelper/exc_hlp.hxx>
31 
32 #include <algorithm>
33 #include <iterator>
34 
35 namespace com::sun::star::uno { class XComponentContext; }
36 
37 using namespace ::com::sun::star;
38 
39 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
41  css::uno::XComponentContext *,
42  css::uno::Sequence<css::uno::Any> const &)
43 {
44  return cppu::acquire(new comphelper::OPropertyBag());
45 }
46 
47 namespace comphelper
48 {
49 
50 
51  using namespace ::com::sun::star::uno;
52  using namespace ::com::sun::star::lang;
53  using namespace ::com::sun::star::beans;
54  using namespace ::com::sun::star::util;
55  using namespace ::com::sun::star::container;
56 
58  :OPropertyBag_PBase( GetBroadcastHelper(), this )
59  ,::cppu::IEventNotificationHook()
60  ,m_bAutoAddProperties( false )
61  ,m_NotifyListeners(m_aMutex)
62  ,m_isModified(false)
63 
64  {
65  }
66 
67 
69  {
70  }
71 
72 
75 
76  void SAL_CALL OPropertyBag::initialize( const Sequence< Any >& _rArguments )
77  {
78  Sequence< Type > aTypes;
79  bool AllowEmptyPropertyName(false);
80  bool AutomaticAddition(false);
81 
82  if (_rArguments.getLength() == 3
83  && (_rArguments[0] >>= aTypes)
84  && (_rArguments[1] >>= AllowEmptyPropertyName)
85  && (_rArguments[2] >>= AutomaticAddition))
86  {
87  m_aAllowedTypes.insert(aTypes.begin(), aTypes.end());
88  m_bAutoAddProperties = AutomaticAddition;
89 
90  } else {
92 
93  if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
94  m_aAllowedTypes.insert( aTypes.begin(), aTypes.end());
95 
96  aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
97  aArguments.get_ensureType( "AllowEmptyPropertyName",
98  AllowEmptyPropertyName );
99  }
100  if (AllowEmptyPropertyName) {
101  m_aDynamicProperties.setAllowEmptyPropertyName(
102  AllowEmptyPropertyName);
103  }
104  }
105 
107  {
108  return "com.sun.star.comp.comphelper.OPropertyBag";
109  }
110 
111  sal_Bool SAL_CALL OPropertyBag::supportsService( const OUString& rServiceName )
112  {
113  return cppu::supportsService(this, rServiceName);
114  }
115 
117  {
118  return { "com.sun.star.beans.PropertyBag" };
119  }
120 
122  sal_Int32 * /*pnHandles*/,
123  sal_Int32 nCount,
124  sal_Bool bVetoable,
125  bool bIgnoreRuntimeExceptionsWhileFiring)
126  {
127  if (nCount && !bVetoable) {
128  setModifiedImpl(true, bIgnoreRuntimeExceptionsWhileFiring);
129  }
130  }
131 
132  void OPropertyBag::setModifiedImpl(bool bModified,
133  bool bIgnoreRuntimeExceptionsWhileFiring)
134  {
135  { // do not lock mutex while notifying (#i93514#) to prevent deadlock
136  ::osl::MutexGuard aGuard( m_aMutex );
137  m_isModified = bModified;
138  }
139  if (!bModified)
140  return;
141 
142  try {
143  Reference<XInterface> xThis(*this);
144  EventObject event(xThis);
146  &XModifyListener::modified, event);
147  } catch (RuntimeException &) {
148  if (!bIgnoreRuntimeExceptionsWhileFiring) {
149  throw;
150  }
151  } catch (Exception &) {
152  // ignore
153  }
154  }
155 
156 
158  {
159  ::osl::MutexGuard aGuard( m_aMutex );
160  return m_isModified;
161  }
162 
163  void SAL_CALL OPropertyBag::setModified( sal_Bool bModified )
164  {
165  setModifiedImpl(bModified, false);
166  }
167 
169  const Reference< XModifyListener > & xListener)
170  {
171  m_NotifyListeners.addInterface(xListener);
172  }
173 
175  const Reference< XModifyListener > & xListener)
176  {
178  }
179 
180 
181  Reference< XPropertySetInfo > SAL_CALL OPropertyBag::getPropertySetInfo( )
182  {
183  return createPropertySetInfo( getInfoHelper() );
184  }
185 
186 
187  sal_Bool SAL_CALL OPropertyBag::has( const Any& /*aElement*/ )
188  {
189  // XSet is only a workaround for addProperty not being able to add default-void properties.
190  // So, everything of XSet except insert is implemented empty
191  return false;
192  }
193 
194 
195  void SAL_CALL OPropertyBag::insert( const Any& _element )
196  {
197  // This is a workaround for addProperty not being able to add default-void properties.
198  // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
199  Property aProperty;
200  if ( !( _element >>= aProperty ) )
201  throw IllegalArgumentException( OUString(), *this, 1 );
202 
203  {
204  osl::MutexGuard g(m_aMutex);
205 
206  // check whether the type is allowed, everything else will be checked
207  // by m_aDynamicProperties
208  if (!m_aAllowedTypes.empty()
209  && m_aAllowedTypes.find(aProperty.Type) == m_aAllowedTypes.end())
210  throw IllegalArgumentException(OUString(), *this, 1);
211 
212  m_aDynamicProperties.addVoidProperty(aProperty.Name, aProperty.Type, findFreeHandle(),
213  aProperty.Attributes);
214 
215  // our property info is dirty
216  m_pArrayHelper.reset();
217  }
218  setModified(true);
219  }
220 
221 
222  void SAL_CALL OPropertyBag::remove( const Any& /*aElement*/ )
223  {
224  // XSet is only a workaround for addProperty not being able to add default-void properties.
225  // So, everything of XSet except insert is implemented empty
226  throw NoSuchElementException( OUString(), *this );
227  }
228 
229 
230  Reference< XEnumeration > SAL_CALL OPropertyBag::createEnumeration( )
231  {
232  // XSet is only a workaround for addProperty not being able to add default-void properties.
233  // So, everything of XSet except insert is implemented empty
234  return nullptr;
235  }
236 
237 
239  {
240  // XSet is only a workaround for addProperty not being able to add default-void properties.
241  // So, everything of XSet except insert is implemented empty
242  return Type();
243  }
244 
245 
247  {
248  // XSet is only a workaround for addProperty not being able to add default-void properties.
249  // So, everything of XSet except insert is implemented empty
250  return false;
251  }
252 
253 
254  void SAL_CALL OPropertyBag::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
255  {
256  m_aDynamicProperties.getFastPropertyValue( _nHandle, _rValue );
257  }
258 
259  sal_Bool SAL_CALL OPropertyBag::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue )
260  {
261  return m_aDynamicProperties.convertFastPropertyValue( _nHandle, _rValue, _rConvertedValue, _rOldValue );
262  }
263 
264  void SAL_CALL OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
265  {
266  m_aDynamicProperties.setFastPropertyValue( nHandle, rValue );
267  }
268 
269 
271  {
272  if (!m_pArrayHelper)
273  {
274  Sequence< Property > aProperties;
275  m_aDynamicProperties.describeProperties( aProperties );
276  m_pArrayHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties ) );
277  }
278  return *m_pArrayHelper;
279 
280  }
281 
282 
284  {
285  const sal_Int32 nPrime = 1009;
286  const sal_Int32 nSeed = 11;
287 
288  sal_Int32 nCheck = nSeed;
289  while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) && ( nCheck != 1 ) )
290  {
291  nCheck = ( nCheck * nSeed ) % nPrime;
292  }
293 
294  if ( nCheck == 1 )
295  { // uh ... we already have 1008 handles used up
296  // -> simply count upwards
297  while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) )
298  ++nCheck;
299  }
300 
301  return nCheck;
302  }
303 
304 
305  void SAL_CALL OPropertyBag::addProperty( const OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue )
306  {
307  {
308  osl::MutexGuard g(m_aMutex);
309 
310  // check whether the type is allowed, everything else will be checked
311  // by m_aDynamicProperties
312  const Type& aPropertyType = _rInitialValue.getValueType();
313  if (_rInitialValue.hasValue() && !m_aAllowedTypes.empty()
314  && m_aAllowedTypes.find(aPropertyType) == m_aAllowedTypes.end())
315  throw IllegalTypeException(OUString(), *this);
316 
317  m_aDynamicProperties.addProperty(_rName, findFreeHandle(), _nAttributes,
318  _rInitialValue);
319 
320  // our property info is dirty
321  m_pArrayHelper.reset();
322  }
323  setModified(true);
324  }
325 
326 
327  void SAL_CALL OPropertyBag::removeProperty( const OUString& _rName )
328  {
329  {
330  osl::MutexGuard g(m_aMutex);
331 
332  m_aDynamicProperties.removeProperty(_rName);
333 
334  // our property info is dirty
335  m_pArrayHelper.reset();
336  }
337  setModified(true);
338  }
339 
340 
341  namespace
342  {
343  struct ComparePropertyValueByName
344  {
345  bool operator()( const PropertyValue& _rLHS, const PropertyValue& _rRHS )
346  {
347  return _rLHS.Name < _rRHS.Name;
348  }
349  };
350 
351  template< typename CLASS >
352  struct TransformPropertyToName
353  {
354  const OUString& operator()( const CLASS& _rProp )
355  {
356  return _rProp.Name;
357  }
358  };
359 
360  struct ExtractPropertyValue
361  {
362  const Any& operator()( const PropertyValue& _rProp )
363  {
364  return _rProp.Value;
365  }
366  };
367  }
368 
369 
370  Sequence< PropertyValue > SAL_CALL OPropertyBag::getPropertyValues( )
371  {
372  ::osl::MutexGuard aGuard( m_aMutex );
373 
374  // all registered properties
375  Sequence< Property > aProperties;
376  m_aDynamicProperties.describeProperties( aProperties );
377 
378  // their names
379  Sequence< OUString > aNames( aProperties.getLength() );
380  std::transform(
381  aProperties.begin(),
382  aProperties.end(),
383  aNames.getArray(),
384  TransformPropertyToName< Property >()
385  );
386 
387  // their values
388  Sequence< Any > aValues;
389  try
390  {
391  aValues = OPropertyBag_PBase::getPropertyValues( aNames );
392  if ( aValues.getLength() != aNames.getLength() )
393  throw RuntimeException();
394  }
395  catch( const RuntimeException& )
396  {
397  throw;
398  }
399  catch( const Exception& )
400  {
401  // ignore
402  }
403 
404  // merge names and values, and retrieve the state/handle
406 
407  Sequence< PropertyValue > aPropertyValues( aNames.getLength() );
408  const OUString* pName = aNames.getConstArray();
409  const OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
410  const Any* pValue = aValues.getArray();
411  PropertyValue* pPropertyValue = aPropertyValues.getArray();
412 
413  for ( ; pName != pNamesEnd; ++pName, ++pValue, ++pPropertyValue )
414  {
415  pPropertyValue->Name = *pName;
416  pPropertyValue->Handle = rPropInfo.getHandleByName( *pName );
417  pPropertyValue->Value = *pValue;
418  pPropertyValue->State = getPropertyStateByHandle( pPropertyValue->Handle );
419  }
420 
421  return aPropertyValues;
422  }
423 
424 
425  void OPropertyBag::impl_setPropertyValues_throw( const Sequence< PropertyValue >& _rProps )
426  {
427  // sort (the XMultiPropertySet interface requires this)
428  Sequence< PropertyValue > aProperties( _rProps );
429  std::sort(
430  aProperties.begin(),
431  aProperties.end(),
432  ComparePropertyValueByName()
433  );
434 
435  // a sequence of names
436  Sequence< OUString > aNames( aProperties.getLength() );
437  std::transform(
438  aProperties.begin(),
439  aProperties.end(),
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
468  if ( m_bAutoAddProperties )
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  aProperties.begin(),
484  aProperties.end(),
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 );
506  impl_setPropertyValues_throw( _rProps );
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 
529  Any OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
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: */
Type
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
virtual void SAL_CALL addProperty(const OUString &Name,::sal_Int16 Attributes, const css::uno::Any &DefaultValue) override
::osl::Mutex m_aMutex
virtual void fireEvents(sal_Int32 *pnHandles, sal_Int32 nCount, sal_Bool bVetoable, bool bIgnoreRuntimeExceptionsWhileFiring) override
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
void setModifiedImpl(bool bModified, bool bIgnoreRuntimeExceptionsWhileFiring)
::comphelper::OInterfaceContainerHelper2 m_NotifyListeners
for notification
virtual OUString SAL_CALL getImplementationName() override
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
Inserts an element into the container.
a collection of named values, packed in various formats.
#define IMPLEMENT_FORWARD_XTYPEPROVIDER2(classname, baseclass1, baseclass2)
Definition: uno3.hxx:136
virtual void SAL_CALL remove(const css::uno::Any &aElement) override
sal_Int32 removeInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
Removes an element from the container.
::comphelper::OPropertyStateHelper OPropertyBag_PBase
::comphelper::PropertyBag m_aDynamicProperties
virtual css::uno::Any getPropertyDefaultByHandle(sal_Int32 _nHandle) const override
virtual ~OPropertyBag() override
virtual void SAL_CALL insert(const css::uno::Any &aElement) override
Sequence< PropertyValue > aArguments
PropertiesInfo aProperties
Any SAL_CALL getCaughtException()
int nCount
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual void SAL_CALL addModifyListener(const css::uno::Reference< css::util::XModifyListener > &xListener) override
virtual void SAL_CALL setModified(sal_Bool bModified) override
std::unique_ptr< ::cppu::OPropertyArrayHelper > m_pArrayHelper
our IPropertyArrayHelper implementation
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL removeProperty(const OUString &Name) override
sal_uInt16 char * pName
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< css::beans::PropertyValue > &aProps) override
virtual sal_Bool SAL_CALL fillPropertyMembersByHandle(::rtl::OUString *pPropName, sal_Int16 *pAttributes, sal_Int32 nHandle)=0
bool get_ensureType(const char *_pAsciiValueName, VALUE_TYPE &_out_rValue) const
retrieves a value with a given name from the collection, if it is present
virtual sal_Bool SAL_CALL hasElements() override
virtual void SAL_CALL removeModifyListener(const css::uno::Reference< css::util::XModifyListener > &xListener) override
sal_Int16 nAttributes
unsigned char sal_Bool
virtual css::beans::PropertyState getPropertyStateByHandle(sal_Int32 _nHandle) override
TypeBag m_aAllowedTypes
set of allowed property types
PropertyValueVector_t aPropertyValues
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
#define IMPLEMENT_FORWARD_XINTERFACE2(classname, refcountbase, baseclass2)
Definition: uno3.hxx:99
sal_Int32 findFreeHandle() const
finds a free property handle our mutex is locked
virtual sal_Bool SAL_CALL isModified() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
void impl_setPropertyValues_throw(const css::uno::Sequence< css::beans::PropertyValue > &_rProps)
implements the setPropertyValues method
bool m_bAutoAddProperties
should we automatically add properties which are tried to set, if they don't exist previously...
virtual sal_Bool SAL_CALL has(const css::uno::Any &aElement) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_comphelper_OPropertyBag(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
bool m_isModified
modify flag
virtual css::uno::Type SAL_CALL getElementType() override
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual sal_Int32 SAL_CALL getHandleByName(const ::rtl::OUString &rPropertyName)=0
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getPropertyValues() override