LibreOffice Module comphelper (master)  1
propertycontainerhelper.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 
21 #include <comphelper/property.hxx>
22 #include <osl/diagnose.h>
23 #include <uno/data.h>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/UnknownPropertyException.hpp>
27 
28 #include <algorithm>
29 
30 
31 namespace comphelper
32 {
33 
34 
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::beans;
38 
39 
40 namespace
41 {
42  // comparing two property descriptions
43  struct PropertyDescriptionHandleCompare
44  {
45  bool operator() (const PropertyDescription& x, const PropertyDescription& y) const
46  {
47  return x.aProperty.Handle < y.aProperty.Handle;
48  }
49  };
50  // comparing two property descriptions (by name)
51  struct PropertyDescriptionNameMatch
52  {
53  OUString const m_rCompare;
54  explicit PropertyDescriptionNameMatch( const OUString& _rCompare ) : m_rCompare( _rCompare ) { }
55 
56  bool operator() (const PropertyDescription& x ) const
57  {
58  return x.aProperty.Name == m_rCompare;
59  }
60  };
61 }
62 
64 {
65 }
66 
67 
69 {
70 }
71 
72 
73 void OPropertyContainerHelper::registerProperty(const OUString& _rName, sal_Int32 _nHandle,
74  sal_Int32 _nAttributes, void* _pPointerToMember, const Type& _rMemberType)
75 {
76  OSL_ENSURE((_nAttributes & PropertyAttribute::MAYBEVOID) == 0,
77  "OPropertyContainerHelper::registerProperty: don't use this for properties which may be void ! There is a method called \"registerMayBeVoidProperty\" for this !");
78  OSL_ENSURE(!_rMemberType.equals(cppu::UnoType<Any>::get()),
79  "OPropertyContainerHelper::registerProperty: don't give my the type of a uno::Any ! Really can't handle this !");
80  OSL_ENSURE(_pPointerToMember,
81  "OPropertyContainerHelper::registerProperty: you gave me nonsense : the pointer must be non-NULL");
82 
83  PropertyDescription aNewProp;
84  aNewProp.aProperty = Property( _rName, _nHandle, _rMemberType, static_cast<sal_Int16>(_nAttributes) );
86  aNewProp.aLocation.pDerivedClassMember = _pPointerToMember;
87 
88  implPushBackProperty(aNewProp);
89 }
90 
91 
92 void OPropertyContainerHelper::revokeProperty( sal_Int32 _nHandle )
93 {
94  PropertiesIterator aPos = searchHandle( _nHandle );
95  if ( aPos == m_aProperties.end() )
96  throw UnknownPropertyException(OUString::number(_nHandle));
97  m_aProperties.erase( aPos );
98 }
99 
100 
101 void OPropertyContainerHelper::registerMayBeVoidProperty(const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes,
102  Any* _pPointerToMember, const Type& _rExpectedType)
103 {
104  OSL_ENSURE((_nAttributes & PropertyAttribute::MAYBEVOID) != 0,
105  "OPropertyContainerHelper::registerMayBeVoidProperty: why calling this when the attributes say nothing about may-be-void ?");
106  OSL_ENSURE(!_rExpectedType.equals(cppu::UnoType<Any>::get()),
107  "OPropertyContainerHelper::registerMayBeVoidProperty: don't give my the type of a uno::Any ! Really can't handle this !");
108  OSL_ENSURE(_pPointerToMember,
109  "OPropertyContainerHelper::registerMayBeVoidProperty: you gave me nonsense : the pointer must be non-NULL");
110 
111  _nAttributes |= PropertyAttribute::MAYBEVOID;
112 
113  PropertyDescription aNewProp;
114  aNewProp.aProperty = Property( _rName, _nHandle, _rExpectedType, static_cast<sal_Int16>(_nAttributes) );
116  aNewProp.aLocation.pDerivedClassMember = _pPointerToMember;
117 
118  implPushBackProperty(aNewProp);
119 }
120 
121 
122 void OPropertyContainerHelper::registerPropertyNoMember(const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes,
123  const Type& _rType, css::uno::Any const & _pInitialValue)
124 {
125  OSL_ENSURE(!_rType.equals(cppu::UnoType<Any>::get()),
126  "OPropertyContainerHelper::registerPropertyNoMember : don't give my the type of a uno::Any ! Really can't handle this !");
127  OSL_ENSURE(
128  (_pInitialValue.isExtractableTo(_rType)
129  || (!_pInitialValue.hasValue()
130  && (_nAttributes & PropertyAttribute::MAYBEVOID) != 0)),
131  "bad initial value");
132 
133  PropertyDescription aNewProp;
134  aNewProp.aProperty = Property( _rName, _nHandle, _rType, static_cast<sal_Int16>(_nAttributes) );
137  m_aHoldProperties.push_back(_pInitialValue);
138 
139  implPushBackProperty(aNewProp);
140 }
141 
142 
143 bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle ) const
144 {
145  return const_cast< OPropertyContainerHelper* >( this )->searchHandle( _nHandle ) != m_aProperties.end();
146 }
147 
148 
149 bool OPropertyContainerHelper::isRegisteredProperty( const OUString& _rName ) const
150 {
151  // TODO: the current structure is from a time where properties were
152  // static, not dynamic. Since we allow that properties are also dynamic,
153  // i.e. registered and revoked even though the XPropertySet has already been
154  // accessed, a vector is not really the best data structure anymore ...
155 
156  return std::any_of(
157  m_aProperties.begin(),
158  m_aProperties.end(),
159  PropertyDescriptionNameMatch( _rName )
160  );
161 }
162 
163 
164 namespace
165 {
166  struct ComparePropertyHandles
167  {
168  bool operator()( const PropertyDescription& _rLHS, const PropertyDescription& _nRHS ) const
169  {
170  return _rLHS.aProperty.Handle < _nRHS.aProperty.Handle;
171  }
172  };
173 }
174 
175 
177 {
178 #ifdef DBG_UTIL
179  for (const auto& checkConflicts : m_aProperties)
180  {
181  OSL_ENSURE(checkConflicts.aProperty.Name != _rProp.aProperty.Name, "OPropertyContainerHelper::implPushBackProperty: name already exists!");
182  OSL_ENSURE(checkConflicts.aProperty.Handle != _rProp.aProperty.Handle, "OPropertyContainerHelper::implPushBackProperty: handle already exists!");
183  }
184 #endif
185 
186  PropertiesIterator pos = std::lower_bound(
187  m_aProperties.begin(), m_aProperties.end(),
188  _rProp, ComparePropertyHandles() );
189 
190  m_aProperties.insert( pos, _rProp );
191 }
192 
193 
194 namespace
195 {
196  void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription& _rProperty, const Any& _rValue )
197  {
198  throw IllegalArgumentException(
199  "The given value cannot be converted to the required property type."
200  " (property name \"" + _rProperty.aProperty.Name
201  + "\", found value type \"" + _rValue.getValueType().getTypeName()
202  + "\", required property type \"" + _rProperty.aProperty.Type.getTypeName()
203  + "\")",
204  nullptr, 4 );
205  }
206 }
207 
208 
210  Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue )
211 {
212  bool bModified = false;
213 
214  // get the property somebody is asking for
215  PropertiesIterator aPos = searchHandle(_nHandle);
216  if (aPos == m_aProperties.end())
217  {
218  OSL_FAIL( "OPropertyContainerHelper::convertFastPropertyValue: unknown handle!" );
219  // should not happen if the derived class has built a correct property set info helper to be used by
220  // our base class OPropertySetHelper
221  return bModified;
222  }
223 
224  switch (aPos->eLocated)
225  {
226  // similar handling for the two cases where the value is stored in an any
229  {
230  bool bMayBeVoid = ((aPos->aProperty.Attributes & PropertyAttribute::MAYBEVOID) != 0);
231 
232 
233  // non modifiable version of the value-to-be-set
234  Any aNewRequestedValue( _rValue );
235 
236  // normalization
237  // #i29490#
238  if ( !aNewRequestedValue.getValueType().equals( aPos->aProperty.Type ) )
239  { // the actually given value is not of the same type as the one required
240  Any aProperlyTyped( nullptr, aPos->aProperty.Type.getTypeLibType() );
241 
242  if ( uno_type_assignData(
243  const_cast< void* >( aProperlyTyped.getValue() ), aProperlyTyped.getValueType().getTypeLibType(),
244  const_cast< void* >( aNewRequestedValue.getValue() ), aNewRequestedValue.getValueType().getTypeLibType(),
245  reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
246  reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
247  reinterpret_cast< uno_ReleaseFunc >( cpp_release )
248  )
249  )
250  {
251  // we were able to query the given XInterface-derivee for the interface
252  // which is required for this property
253  aNewRequestedValue = aProperlyTyped;
254  }
255  }
256 
257  // argument check
258  if ( ! ( (bMayBeVoid && !aNewRequestedValue.hasValue()) // void is allowed if the attribute says so
259  || (aNewRequestedValue.getValueType().equals(aPos->aProperty.Type)) // else the types have to be equal
260  )
261  )
262  {
263  lcl_throwIllegalPropertyValueTypeException( *aPos, _rValue );
264  }
265 
266  Any* pPropContainer = nullptr;
267  // the pointer to the any which holds the property value, no matter if located in the derived class
268  // or in out vector
269 
270  if (PropertyDescription::LocationType::HoldMyself == aPos->eLocated)
271  {
272  OSL_ENSURE(aPos->aLocation.nOwnClassVectorIndex < static_cast<sal_Int32>(m_aHoldProperties.size()),
273  "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
274  auto aIter = m_aHoldProperties.begin() + aPos->aLocation.nOwnClassVectorIndex;
275  pPropContainer = &(*aIter);
276  }
277  else
278  pPropContainer = static_cast<Any*>(aPos->aLocation.pDerivedClassMember);
279 
280  // check if the new value differs from the current one
281  if (!pPropContainer->hasValue() || !aNewRequestedValue.hasValue())
282  bModified = pPropContainer->hasValue() != aNewRequestedValue.hasValue();
283  else
284  bModified = !uno_type_equalData(
285  const_cast< void* >( pPropContainer->getValue() ), aPos->aProperty.Type.getTypeLibType(),
286  const_cast< void* >( aNewRequestedValue.getValue() ), aPos->aProperty.Type.getTypeLibType(),
287  reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
288  reinterpret_cast< uno_ReleaseFunc >( cpp_release )
289  );
290 
291  if (bModified)
292  {
293  _rOldValue = *pPropContainer;
294  _rConvertedValue = aNewRequestedValue;
295  }
296  }
297  break;
299  // let the UNO runtime library do any possible conversion
300  // this may include a change of the type - for instance, if a LONG is required,
301  // but a short is given, then this is valid, as it can be converted without any potential
302  // data loss
303 
304  Any aProperlyTyped;
305  const Any* pNewValue = &_rValue;
306 
307  if (!_rValue.getValueType().equals(aPos->aProperty.Type))
308  {
309  bool bConverted = false;
310 
311  // a temporary any of the correct (required) type
312  aProperlyTyped = Any( nullptr, aPos->aProperty.Type.getTypeLibType() );
313  // (need this as we do not want to overwrite the derived class member here)
314 
315  if ( uno_type_assignData(
316  const_cast<void*>(aProperlyTyped.getValue()), aProperlyTyped.getValueType().getTypeLibType(),
317  const_cast<void*>(_rValue.getValue()), _rValue.getValueType().getTypeLibType(),
318  reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
319  reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
320  reinterpret_cast< uno_ReleaseFunc >( cpp_release )
321  )
322  )
323  {
324  // could query for the requested interface
325  bConverted = true;
326  pNewValue = &aProperlyTyped;
327  }
328 
329  if ( !bConverted )
330  lcl_throwIllegalPropertyValueTypeException( *aPos, _rValue );
331  }
332 
333  // from here on, we should have the proper type
334  OSL_ENSURE( pNewValue->getValueType() == aPos->aProperty.Type,
335  "OPropertyContainerHelper::convertFastPropertyValue: conversion failed!" );
336  bModified = !uno_type_equalData(
337  aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type.getTypeLibType(),
338  const_cast<void*>(pNewValue->getValue()), aPos->aProperty.Type.getTypeLibType(),
339  reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
340  reinterpret_cast< uno_ReleaseFunc >( cpp_release )
341  );
342 
343  if (bModified)
344  {
345  _rOldValue.setValue(aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type);
346  _rConvertedValue = *pNewValue;
347  }
348  break;
349  }
350 
351  return bModified;
352 }
353 
354 
355 void OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle, const Any& _rValue)
356 {
357  // get the property somebody is asking for
358  PropertiesIterator aPos = searchHandle(_nHandle);
359  if (aPos == m_aProperties.end())
360  {
361  OSL_FAIL( "OPropertyContainerHelper::setFastPropertyValue: unknown handle!" );
362  // should not happen if the derived class has built a correct property set info helper to be used by
363  // our base class OPropertySetHelper
364  return;
365  }
366 
367  bool bSuccess = true;
368 
369  switch (aPos->eLocated)
370  {
372  m_aHoldProperties[aPos->aLocation.nOwnClassVectorIndex] = _rValue;
373  break;
374 
376  *static_cast< Any* >(aPos->aLocation.pDerivedClassMember) = _rValue;
377  break;
378 
380  // copy the data from the to-be-set value
381  bSuccess = uno_type_assignData(
382  aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type.getTypeLibType(),
383  const_cast< void* >( _rValue.getValue() ), _rValue.getValueType().getTypeLibType(),
384  reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
385  reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
386  reinterpret_cast< uno_ReleaseFunc >( cpp_release ) );
387 
388  OSL_ENSURE( bSuccess,
389  "OPropertyContainerHelper::setFastPropertyValue: ooops... the value could not be assigned!");
390 
391  break;
392  }
393 }
394 
395 void OPropertyContainerHelper::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
396 {
397  // get the property somebody is asking for
398  PropertiesIterator aPos = const_cast<OPropertyContainerHelper*>(this)->searchHandle(_nHandle);
399  if (aPos == m_aProperties.end())
400  {
401  OSL_FAIL( "OPropertyContainerHelper::getFastPropertyValue: unknown handle!" );
402  // should not happen if the derived class has built a correct property set info helper to be used by
403  // our base class OPropertySetHelper
404  return;
405  }
406 
407  switch (aPos->eLocated)
408  {
410  OSL_ENSURE(aPos->aLocation.nOwnClassVectorIndex < static_cast<sal_Int32>(m_aHoldProperties.size()),
411  "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
412  _rValue = m_aHoldProperties[aPos->aLocation.nOwnClassVectorIndex];
413  break;
415  _rValue = *static_cast<Any*>(aPos->aLocation.pDerivedClassMember);
416  break;
418  _rValue.setValue(aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type);
419  break;
420  }
421 }
422 
423 
425 {
426  PropertyDescription aHandlePropDesc;
427  aHandlePropDesc.aProperty.Handle = _nHandle;
428  // search a lower bound
429  PropertiesIterator aLowerBound = std::lower_bound(
430  m_aProperties.begin(),
431  m_aProperties.end(),
432  aHandlePropDesc,
433  PropertyDescriptionHandleCompare());
434 
435  // check for identity
436  if ((aLowerBound != m_aProperties.end()) && aLowerBound->aProperty.Handle != _nHandle)
437  aLowerBound = m_aProperties.end();
438 
439  return aLowerBound;
440 }
441 
442 
443 const Property& OPropertyContainerHelper::getProperty( const OUString& _rName ) const
444 {
445  ConstPropertiesIterator pos = std::find_if(
446  m_aProperties.begin(),
447  m_aProperties.end(),
448  PropertyDescriptionNameMatch( _rName )
449  );
450  if ( pos == m_aProperties.end() )
451  throw UnknownPropertyException( _rName );
452 
453  return pos->aProperty;
454 }
455 
456 
457 void OPropertyContainerHelper::describeProperties(Sequence< Property >& _rProps) const
458 {
459  Sequence< Property > aOwnProps(m_aProperties.size());
460  Property* pOwnProps = aOwnProps.getArray();
461 
462  for (const auto& rProp : m_aProperties)
463  {
464  pOwnProps->Name = rProp.aProperty.Name;
465  pOwnProps->Handle = rProp.aProperty.Handle;
466  pOwnProps->Attributes = static_cast<sal_Int16>(rProp.aProperty.Attributes);
467  pOwnProps->Type = rProp.aProperty.Type;
468  ++pOwnProps;
469  }
470 
471  // as our property vector is sorted by handles, not by name, we have to sort aOwnProps
472  std::sort(aOwnProps.begin(), aOwnProps.end(), PropertyCompareByName());
473 
474  // unfortunately the STL merge function does not allow the output range to overlap one of the input ranges,
475  // so we need an extra sequence
476  Sequence< Property > aOutput;
477  aOutput.realloc(_rProps.getLength() + aOwnProps.getLength());
478  // do the merge
479  std::merge( _rProps.begin(), _rProps.end(), // input 1
480  aOwnProps.begin(), aOwnProps.end(), // input 2
481  aOutput.getArray(), // output
482  PropertyCompareByName() // compare operator
483  );
484 
485  // copy the output
486  _rProps = aOutput;
487 }
488 
489 
490 } // namespace comphelper
491 
492 
493 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Bool SAL_CALL uno_type_equalData(void *pVal1, typelib_TypeDescriptionReference *pVal1Type, void *pVal2, typelib_TypeDescriptionReference *pVal2Type, uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Type
void registerProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void *_pPointerToMember, const css::uno::Type &_rMemberType)
register a property.
void describeProperties(css::uno::Sequence< css::beans::Property > &_rProps) const
appends the descriptions of all properties which were registered 'til that moment to the given sequen...
sal_Bool SAL_CALL uno_type_assignData(void *pDest, typelib_TypeDescriptionReference *pDestType, void *pSource, typelib_TypeDescriptionReference *pSourceType, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
size_t pos
COMPHELPER_DLLPRIVATE PropertiesIterator searchHandle(sal_Int32 _nHandle)
search the PropertyDescription for the given handle (within m_aProperties)
OUString const m_rCompare
const css::beans::Property & getProperty(const OUString &_rName) const
retrieves the description for a registered property
helper class for managing property values, and implementing most of the X*Property* interfaces ...
bool isRegisteredProperty(sal_Int32 _nHandle) const
checks whether a property with the given handle has been registered
Properties::const_iterator ConstPropertiesIterator
COMPHELPER_DLLPRIVATE void implPushBackProperty(const PropertyDescription &_rProp)
insertion of _rProp into m_aProperties, keeping the sort order
void registerMayBeVoidProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, css::uno::Any *_pPointerToMember, const css::uno::Type &_rExpectedType)
register a property.
void revokeProperty(sal_Int32 _nHandle)
revokes a previously registered property
void getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const
void registerPropertyNoMember(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const css::uno::Type &_rType, css::uno::Any const &_pInitialValue)
register a property.
bool convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue)
void setFastPropertyValue(sal_Int32 nHandle, const css::uno::Any &rValue)