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