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 0 : bool operator() (const PropertyDescription& x, const PropertyDescription& y) const
47 : {
48 0 : return x.aProperty.Handle < y.aProperty.Handle;
49 : }
50 : };
51 : // comparing two property descriptions (by name)
52 0 : struct PropertyDescriptionNameMatch : public ::std::unary_function< PropertyDescription, bool >
53 : {
54 : OUString m_rCompare;
55 0 : PropertyDescriptionNameMatch( const OUString& _rCompare ) : m_rCompare( _rCompare ) { }
56 :
57 0 : bool operator() (const PropertyDescription& x ) const
58 : {
59 0 : return x.aProperty.Name.equals(m_rCompare);
60 : }
61 : };
62 : }
63 :
64 :
65 : //= OPropertyContainerHelper
66 :
67 :
68 0 : OPropertyContainerHelper::OPropertyContainerHelper()
69 0 : :m_bUnused(false)
70 : {
71 0 : }
72 :
73 :
74 0 : OPropertyContainerHelper::~OPropertyContainerHelper()
75 : {
76 0 : }
77 :
78 :
79 0 : 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(::getCppuType(static_cast< Any* >(NULL))),
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 0 : PropertyDescription aNewProp;
90 0 : aNewProp.aProperty = Property( _rName, _nHandle, _rMemberType, (sal_Int16)_nAttributes );
91 0 : aNewProp.eLocated = PropertyDescription::ltDerivedClassRealType;
92 0 : aNewProp.aLocation.pDerivedClassMember = _pPointerToMember;
93 :
94 0 : implPushBackProperty(aNewProp);
95 0 : }
96 :
97 :
98 0 : void OPropertyContainerHelper::revokeProperty( sal_Int32 _nHandle )
99 : {
100 0 : PropertiesIterator aPos = searchHandle( _nHandle );
101 0 : if ( aPos == m_aProperties.end() )
102 0 : throw UnknownPropertyException();
103 0 : m_aProperties.erase( aPos );
104 0 : }
105 :
106 :
107 0 : 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(::getCppuType(static_cast< Any* >(NULL))),
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 0 : _nAttributes |= PropertyAttribute::MAYBEVOID;
118 :
119 0 : PropertyDescription aNewProp;
120 0 : aNewProp.aProperty = Property( _rName, _nHandle, _rExpectedType, (sal_Int16)_nAttributes );
121 0 : aNewProp.eLocated = PropertyDescription::ltDerivedClassAnyType;
122 0 : aNewProp.aLocation.pDerivedClassMember = _pPointerToMember;
123 :
124 0 : implPushBackProperty(aNewProp);
125 0 : }
126 :
127 :
128 :
129 0 : 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(::getCppuType(static_cast< Any* >(NULL))),
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 ommit the initial value if the property can't be void ! This will definitivly crash later !");
136 :
137 0 : PropertyDescription aNewProp;
138 0 : aNewProp.aProperty = Property( _rName, _nHandle, _rType, (sal_Int16)_nAttributes );
139 0 : aNewProp.eLocated = PropertyDescription::ltHoldMyself;
140 0 : aNewProp.aLocation.nOwnClassVectorIndex = m_aHoldProperties.size();
141 0 : if (_pInitialValue)
142 0 : m_aHoldProperties.push_back(Any(_pInitialValue, _rType));
143 : else
144 0 : m_aHoldProperties.push_back(Any());
145 :
146 0 : implPushBackProperty(aNewProp);
147 0 : }
148 :
149 :
150 0 : bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle ) const
151 : {
152 0 : return const_cast< OPropertyContainerHelper* >( this )->searchHandle( _nHandle ) != m_aProperties.end();
153 : }
154 :
155 :
156 0 : 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 0 : );
168 0 : return pos != m_aProperties.end();
169 : }
170 :
171 :
172 : namespace
173 : {
174 : struct ComparePropertyHandles
175 : {
176 0 : bool operator()( const PropertyDescription& _rLHS, const PropertyDescription& _nRHS ) const
177 : {
178 0 : return _rLHS.aProperty.Handle < _nRHS.aProperty.Handle;
179 : }
180 : };
181 : }
182 :
183 :
184 0 : 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 0 : _rProp, ComparePropertyHandles() );
200 :
201 0 : m_aProperties.insert( pos, _rProp );
202 0 : }
203 :
204 :
205 : namespace
206 : {
207 0 : void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription& _rProperty, const Any& _rValue )
208 : {
209 0 : OUStringBuffer aErrorMessage;
210 0 : aErrorMessage.appendAscii( "The given value cannot be converted to the required property type." );
211 0 : aErrorMessage.appendAscii( "\n(property name \"" );
212 0 : aErrorMessage.append( _rProperty.aProperty.Name );
213 0 : aErrorMessage.appendAscii( "\", found value type \"" );
214 0 : aErrorMessage.append( _rValue.getValueType().getTypeName() );
215 0 : aErrorMessage.appendAscii( "\", required property type \"" );
216 0 : aErrorMessage.append( _rProperty.aProperty.Type.getTypeName() );
217 0 : aErrorMessage.appendAscii( "\")" );
218 0 : throw IllegalArgumentException( aErrorMessage.makeStringAndClear(), NULL, 4 );
219 : }
220 : }
221 :
222 :
223 0 : bool OPropertyContainerHelper::convertFastPropertyValue(
224 : Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) SAL_THROW( (IllegalArgumentException) )
225 : {
226 0 : bool bModified = false;
227 :
228 : // get the property somebody is asking for
229 0 : PropertiesIterator aPos = searchHandle(_nHandle);
230 0 : 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 0 : 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 0 : bool bMayBeVoid = ((aPos->aProperty.Attributes & PropertyAttribute::MAYBEVOID) != 0);
245 :
246 :
247 : // non modifiable version of the value-to-be-set
248 0 : Any aNewRequestedValue( _rValue );
249 :
250 : // normalization
251 : // #i29490#
252 0 : if ( !aNewRequestedValue.getValueType().equals( aPos->aProperty.Type ) )
253 : { // the actually given value is not of the same type as the one required
254 0 : Any aProperlyTyped( NULL, aPos->aProperty.Type.getTypeLibType() );
255 :
256 0 : if ( uno_type_assignData(
257 0 : const_cast< void* >( aProperlyTyped.getValue() ), aProperlyTyped.getValueType().getTypeLibType(),
258 0 : 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 0 : )
263 : )
264 : {
265 : // we were able to query the given XInterface-derivee for the interface
266 : // which is required for this property
267 0 : aNewRequestedValue = aProperlyTyped;
268 0 : }
269 : }
270 :
271 : // argument check
272 0 : if ( ! ( (bMayBeVoid && !aNewRequestedValue.hasValue()) // void is allowed if the attribute says so
273 0 : || (aNewRequestedValue.getValueType().equals(aPos->aProperty.Type)) // else the types have to be equal
274 0 : )
275 : )
276 : {
277 0 : lcl_throwIllegalPropertyValueTypeException( *aPos, _rValue );
278 : }
279 :
280 0 : 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 0 : if (PropertyDescription::ltHoldMyself == aPos->eLocated)
285 : {
286 : OSL_ENSURE(aPos->aLocation.nOwnClassVectorIndex < (sal_Int32)m_aHoldProperties.size(),
287 : "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
288 0 : PropertyContainerIterator aIter = m_aHoldProperties.begin() + aPos->aLocation.nOwnClassVectorIndex;
289 0 : pPropContainer = &(*aIter);
290 : }
291 : else
292 0 : pPropContainer = reinterpret_cast<Any*>(aPos->aLocation.pDerivedClassMember);
293 :
294 : // check if the new value differs from the current one
295 0 : if (!pPropContainer->hasValue() || !aNewRequestedValue.hasValue())
296 0 : bModified = pPropContainer->hasValue() != aNewRequestedValue.hasValue();
297 : else
298 : bModified = !uno_type_equalData(
299 0 : const_cast< void* >( pPropContainer->getValue() ), aPos->aProperty.Type.getTypeLibType(),
300 0 : const_cast< void* >( aNewRequestedValue.getValue() ), aPos->aProperty.Type.getTypeLibType(),
301 : reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
302 : reinterpret_cast< uno_ReleaseFunc >( cpp_release )
303 0 : );
304 :
305 0 : if (bModified)
306 : {
307 0 : _rOldValue = *pPropContainer;
308 0 : _rConvertedValue = aNewRequestedValue;
309 0 : }
310 : }
311 0 : 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 0 : Any aProperlyTyped;
319 0 : const Any* pNewValue = &_rValue;
320 :
321 0 : if (!_rValue.getValueType().equals(aPos->aProperty.Type))
322 : {
323 0 : bool bConverted = false;
324 :
325 : // a temporary any of the correct (required) type
326 0 : aProperlyTyped = Any( NULL, aPos->aProperty.Type.getTypeLibType() );
327 : // (need this as we do not want to overwrite the derived class member here)
328 :
329 0 : if ( uno_type_assignData(
330 0 : const_cast<void*>(aProperlyTyped.getValue()), aProperlyTyped.getValueType().getTypeLibType(),
331 0 : 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 0 : )
336 : )
337 : {
338 : // could query for the requested interface
339 0 : bConverted = true;
340 0 : pNewValue = &aProperlyTyped;
341 : }
342 :
343 0 : if ( !bConverted )
344 0 : 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 0 : aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type.getTypeLibType(),
352 0 : const_cast<void*>(pNewValue->getValue()), aPos->aProperty.Type.getTypeLibType(),
353 : reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
354 : reinterpret_cast< uno_ReleaseFunc >( cpp_release )
355 0 : );
356 :
357 0 : if (bModified)
358 : {
359 0 : _rOldValue.setValue(aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type);
360 0 : _rConvertedValue = *pNewValue;
361 : }
362 0 : break;
363 : }
364 :
365 0 : return bModified;
366 : }
367 :
368 :
369 0 : void OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle, const Any& _rValue) SAL_THROW( (Exception) )
370 : {
371 : // get the property somebody is asking for
372 0 : PropertiesIterator aPos = searchHandle(_nHandle);
373 0 : 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;
379 : }
380 :
381 0 : switch (aPos->eLocated)
382 : {
383 : case PropertyDescription::ltHoldMyself:
384 0 : m_aHoldProperties[aPos->aLocation.nOwnClassVectorIndex] = _rValue;
385 0 : break;
386 :
387 : case PropertyDescription::ltDerivedClassAnyType:
388 0 : *reinterpret_cast< Any* >(aPos->aLocation.pDerivedClassMember) = _rValue;
389 0 : break;
390 :
391 : case PropertyDescription::ltDerivedClassRealType:
392 : #if OSL_DEBUG_LEVEL > 0
393 : bool bSuccess =
394 : #endif
395 : // copy the data from the to-be-set value
396 : uno_type_assignData(
397 0 : aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type.getTypeLibType(),
398 0 : const_cast< void* >( _rValue.getValue() ), _rValue.getValueType().getTypeLibType(),
399 : reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
400 : reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
401 0 : reinterpret_cast< uno_ReleaseFunc >( cpp_release ) );
402 :
403 : OSL_ENSURE( bSuccess,
404 : "OPropertyContainerHelper::setFastPropertyValue: ooops .... the value could not be assigned!");
405 :
406 0 : break;
407 : }
408 : }
409 :
410 :
411 0 : void OPropertyContainerHelper::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
412 : {
413 : // get the property somebody is asking for
414 0 : PropertiesIterator aPos = const_cast<OPropertyContainerHelper*>(this)->searchHandle(_nHandle);
415 0 : 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 0 : return;
421 : }
422 :
423 0 : 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 0 : _rValue = m_aHoldProperties[aPos->aLocation.nOwnClassVectorIndex];
429 0 : break;
430 : case PropertyDescription::ltDerivedClassAnyType:
431 0 : _rValue = *reinterpret_cast<Any*>(aPos->aLocation.pDerivedClassMember);
432 0 : break;
433 : case PropertyDescription::ltDerivedClassRealType:
434 0 : _rValue.setValue(aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type);
435 0 : break;
436 : }
437 : }
438 :
439 :
440 0 : OPropertyContainerHelper::PropertiesIterator OPropertyContainerHelper::searchHandle(sal_Int32 _nHandle)
441 : {
442 0 : PropertyDescription aHandlePropDesc;
443 0 : 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 0 : PropertyDescriptionHandleCompare());
450 :
451 : // check for identity
452 0 : if ((aLowerBound != m_aProperties.end()) && aLowerBound->aProperty.Handle != _nHandle)
453 0 : aLowerBound = m_aProperties.end();
454 :
455 0 : return aLowerBound;
456 : }
457 :
458 :
459 0 : 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 0 : );
466 0 : if ( pos == m_aProperties.end() )
467 0 : throw UnknownPropertyException( _rName, NULL );
468 :
469 0 : return pos->aProperty;
470 : }
471 :
472 :
473 0 : void OPropertyContainerHelper::describeProperties(Sequence< Property >& _rProps) const
474 : {
475 0 : Sequence< Property > aOwnProps(m_aProperties.size());
476 0 : Property* pOwnProps = aOwnProps.getArray();
477 :
478 0 : for ( ConstPropertiesIterator aLoop = m_aProperties.begin();
479 0 : aLoop != m_aProperties.end();
480 : ++aLoop, ++pOwnProps
481 : )
482 : {
483 0 : pOwnProps->Name = aLoop->aProperty.Name;
484 0 : pOwnProps->Handle = aLoop->aProperty.Handle;
485 0 : pOwnProps->Attributes = (sal_Int16)aLoop->aProperty.Attributes;
486 0 : 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 0 : ::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 0 : Sequence< Property > aOutput;
495 0 : aOutput.realloc(_rProps.getLength() + aOwnProps.getLength());
496 : // do the merge
497 0 : ::std::merge( _rProps.getConstArray(), _rProps.getConstArray() + _rProps.getLength(), // input 1
498 0 : aOwnProps.getConstArray(), aOwnProps.getConstArray() + aOwnProps.getLength(), // input 2
499 : aOutput.getArray(), // output
500 : PropertyCompareByName() // compare operator
501 0 : );
502 :
503 : // copy the output
504 0 : _rProps = aOutput;
505 0 : }
506 :
507 :
508 : } // namespace comphelper
509 :
510 :
511 :
512 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|