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 
34 namespace com::sun::star::uno { class XComponentContext; }
35 
36 using namespace ::com::sun::star;
37 
38 extern "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 
46 namespace 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(aTypes.begin(), aTypes.end());
87  m_bAutoAddProperties = AutomaticAddition;
88 
89  } else {
91 
92  if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
93  m_aAllowedTypes.insert( aTypes.begin(), aTypes.end());
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 
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  {
170  m_NotifyListeners.addInterface(xListener);
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( OUString(), *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(OUString(), *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  aProperties.begin(),
381  aProperties.end(),
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  std::sort(
429  aProperties.begin(),
430  aProperties.end(),
431  ComparePropertyValueByName()
432  );
433 
434  // a sequence of names
435  Sequence< OUString > aNames( aProperties.getLength() );
436  std::transform(
437  aProperties.begin(),
438  aProperties.end(),
439  aNames.getArray(),
440  TransformPropertyToName< PropertyValue >()
441  );
442 
443  try
444  {
445  // check for unknown properties
446  // we cannot simply rely on the XMultiPropertySet::setPropertyValues
447  // implementation of our base class, since it does not throw
448  // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
449  // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
450  // requires it
451  sal_Int32 nCount = aNames.getLength();
452 
453  Sequence< sal_Int32 > aHandles( nCount );
454  sal_Int32* pHandle = aHandles.getArray();
455  const PropertyValue* pProperty = aProperties.getConstArray();
456  for ( const OUString* pName = aNames.getConstArray();
457  pName != aNames.getConstArray() + aNames.getLength();
458  ++pName, ++pHandle, ++pProperty
459  )
460  {
462  *pHandle = rPropInfo.getHandleByName( *pName );
463  if ( *pHandle != -1 )
464  continue;
465 
466  // there's a property requested which we do not know
467  if ( m_bAutoAddProperties )
468  {
469  // add the property
470  sal_Int16 const nAttributes = PropertyAttribute::BOUND | PropertyAttribute::REMOVABLE | PropertyAttribute::MAYBEDEFAULT;
471  addProperty( *pName, nAttributes, pProperty->Value );
472  continue;
473  }
474 
475  // no way out
476  throw UnknownPropertyException( *pName, *this );
477  }
478 
479  // a sequence of values
480  Sequence< Any > aValues( aProperties.getLength() );
481  std::transform(
482  aProperties.begin(),
483  aProperties.end(),
484  aValues.getArray(),
485  ExtractPropertyValue()
486  );
487 
488  setFastPropertyValues( nCount, aHandles.getArray(), aValues.getConstArray(), nCount );
489  }
490  catch( const PropertyVetoException& ) { throw; }
491  catch( const IllegalArgumentException& ) { throw; }
492  catch( const WrappedTargetException& ) { throw; }
493  catch( const RuntimeException& ) { throw; }
494  catch( const UnknownPropertyException& ) { throw; }
495  catch( const Exception& )
496  {
497  throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
498  }
499  }
500 
501 
502  void SAL_CALL OPropertyBag::setPropertyValues( const Sequence< PropertyValue >& _rProps )
503  {
504  ::osl::MutexGuard aGuard( m_aMutex );
505  impl_setPropertyValues_throw( _rProps );
506  }
507 
508 
509  PropertyState OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle )
510  {
511  // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
512  // assume they're always in DIRECT state.
513  // (Note that this probably would belong into the base class. However, this would mean we would need
514  // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
515  // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
516  // current phase. #i78593#
517 
519  sal_Int16 nAttributes(0);
520  OSL_VERIFY( rPropInfo.fillPropertyMembersByHandle( nullptr, &nAttributes, _nHandle ) );
521  if ( ( nAttributes & PropertyAttribute::MAYBEDEFAULT ) == 0 )
522  return PropertyState_DIRECT_VALUE;
523 
524  return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle );
525  }
526 
527 
528  Any OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
529  {
530  Any aDefault;
531  m_aDynamicProperties.getPropertyDefaultByHandle( _nHandle, aDefault );
532  return aDefault;
533  }
534 
535 
536 } // namespace comphelper
537 
538 
539 /* 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
const SvXMLTokenMapEntry aTypes[]
#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