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