1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/propertycontainerhelper.hxx>
21 #include <comphelper/property.hxx>
22 #include <osl/diagnose.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>
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::beans
;
43 // comparing two property descriptions
44 struct PropertyDescriptionHandleCompare
: public ::std::binary_function
< PropertyDescription
, PropertyDescription
, bool >
46 bool operator() (const PropertyDescription
& x
, const PropertyDescription
& y
) const
48 return x
.aProperty
.Handle
< y
.aProperty
.Handle
;
51 // comparing two property descriptions (by name)
52 struct PropertyDescriptionNameMatch
: public ::std::unary_function
< PropertyDescription
, bool >
55 PropertyDescriptionNameMatch( const OUString
& _rCompare
) : m_rCompare( _rCompare
) { }
57 bool operator() (const PropertyDescription
& x
) const
59 return x
.aProperty
.Name
.equals(m_rCompare
);
65 //= OPropertyContainerHelper
68 OPropertyContainerHelper::OPropertyContainerHelper()
74 OPropertyContainerHelper::~OPropertyContainerHelper()
79 void OPropertyContainerHelper::registerProperty(const OUString
& _rName
, sal_Int32 _nHandle
,
80 sal_Int32 _nAttributes
, void* _pPointerToMember
, const Type
& _rMemberType
)
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");
89 PropertyDescription aNewProp
;
90 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rMemberType
, (sal_Int16
)_nAttributes
);
91 aNewProp
.eLocated
= PropertyDescription::ltDerivedClassRealType
;
92 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
94 implPushBackProperty(aNewProp
);
98 void OPropertyContainerHelper::revokeProperty( sal_Int32 _nHandle
)
100 PropertiesIterator aPos
= searchHandle( _nHandle
);
101 if ( aPos
== m_aProperties
.end() )
102 throw UnknownPropertyException();
103 m_aProperties
.erase( aPos
);
107 void OPropertyContainerHelper::registerMayBeVoidProperty(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
108 Any
* _pPointerToMember
, const Type
& _rExpectedType
)
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");
117 _nAttributes
|= PropertyAttribute::MAYBEVOID
;
119 PropertyDescription aNewProp
;
120 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rExpectedType
, (sal_Int16
)_nAttributes
);
121 aNewProp
.eLocated
= PropertyDescription::ltDerivedClassAnyType
;
122 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
124 implPushBackProperty(aNewProp
);
129 void OPropertyContainerHelper::registerPropertyNoMember(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
130 const Type
& _rType
, const void* _pInitialValue
)
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 !");
137 PropertyDescription aNewProp
;
138 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rType
, (sal_Int16
)_nAttributes
);
139 aNewProp
.eLocated
= PropertyDescription::ltHoldMyself
;
140 aNewProp
.aLocation
.nOwnClassVectorIndex
= m_aHoldProperties
.size();
142 m_aHoldProperties
.push_back(Any(_pInitialValue
, _rType
));
144 m_aHoldProperties
.push_back(Any());
146 implPushBackProperty(aNewProp
);
150 bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle
) const
152 return const_cast< OPropertyContainerHelper
* >( this )->searchHandle( _nHandle
) != m_aProperties
.end();
156 bool OPropertyContainerHelper::isRegisteredProperty( const OUString
& _rName
) const
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 ...
163 ConstPropertiesIterator pos
= ::std::find_if(
164 m_aProperties
.begin(),
166 PropertyDescriptionNameMatch( _rName
)
168 return pos
!= m_aProperties
.end();
174 struct ComparePropertyHandles
176 bool operator()( const PropertyDescription
& _rLHS
, const PropertyDescription
& _nRHS
) const
178 return _rLHS
.aProperty
.Handle
< _nRHS
.aProperty
.Handle
;
184 void OPropertyContainerHelper::implPushBackProperty(const PropertyDescription
& _rProp
)
187 for ( PropertiesIterator checkConflicts
= m_aProperties
.begin();
188 checkConflicts
!= m_aProperties
.end();
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!");
197 PropertiesIterator pos
= ::std::lower_bound(
198 m_aProperties
.begin(), m_aProperties
.end(),
199 _rProp
, ComparePropertyHandles() );
201 m_aProperties
.insert( pos
, _rProp
);
207 void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription
& _rProperty
, const Any
& _rValue
)
209 OUStringBuffer aErrorMessage
;
210 aErrorMessage
.appendAscii( "The given value cannot be converted to the required property type." );
211 aErrorMessage
.appendAscii( "\n(property name \"" );
212 aErrorMessage
.append( _rProperty
.aProperty
.Name
);
213 aErrorMessage
.appendAscii( "\", found value type \"" );
214 aErrorMessage
.append( _rValue
.getValueType().getTypeName() );
215 aErrorMessage
.appendAscii( "\", required property type \"" );
216 aErrorMessage
.append( _rProperty
.aProperty
.Type
.getTypeName() );
217 aErrorMessage
.appendAscii( "\")" );
218 throw IllegalArgumentException( aErrorMessage
.makeStringAndClear(), NULL
, 4 );
223 bool OPropertyContainerHelper::convertFastPropertyValue(
224 Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
) SAL_THROW( (IllegalArgumentException
) )
226 bool bModified
= false;
228 // get the property somebody is asking for
229 PropertiesIterator aPos
= searchHandle(_nHandle
);
230 if (aPos
== m_aProperties
.end())
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
238 switch (aPos
->eLocated
)
240 // similar handling for the two cases where the value is stored in an any
241 case PropertyDescription::ltHoldMyself
:
242 case PropertyDescription::ltDerivedClassAnyType
:
244 bool bMayBeVoid
= ((aPos
->aProperty
.Attributes
& PropertyAttribute::MAYBEVOID
) != 0);
247 // non modifiable version of the value-to-be-set
248 Any
aNewRequestedValue( _rValue
);
252 if ( !aNewRequestedValue
.getValueType().equals( aPos
->aProperty
.Type
) )
253 { // the actually given value is not of the same type as the one required
254 Any
aProperlyTyped( NULL
, aPos
->aProperty
.Type
.getTypeLibType() );
256 if ( uno_type_assignData(
257 const_cast< void* >( aProperlyTyped
.getValue() ), aProperlyTyped
.getValueType().getTypeLibType(),
258 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
)
265 // we were able to query the given XInterface-derivee for the interface
266 // which is required for this property
267 aNewRequestedValue
= aProperlyTyped
;
272 if ( ! ( (bMayBeVoid
&& !aNewRequestedValue
.hasValue()) // void is allowed if the attribute says so
273 || (aNewRequestedValue
.getValueType().equals(aPos
->aProperty
.Type
)) // else the types have to be equal
277 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
280 Any
* pPropContainer
= NULL
;
281 // the pointer to the any which holds the property value, no matter if located in the derived clas
284 if (PropertyDescription::ltHoldMyself
== aPos
->eLocated
)
286 OSL_ENSURE(aPos
->aLocation
.nOwnClassVectorIndex
< (sal_Int32
)m_aHoldProperties
.size(),
287 "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
288 PropertyContainerIterator aIter
= m_aHoldProperties
.begin() + aPos
->aLocation
.nOwnClassVectorIndex
;
289 pPropContainer
= &(*aIter
);
292 pPropContainer
= reinterpret_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
294 // check if the new value differs from the current one
295 if (!pPropContainer
->hasValue() || !aNewRequestedValue
.hasValue())
296 bModified
= pPropContainer
->hasValue() != aNewRequestedValue
.hasValue();
298 bModified
= !uno_type_equalData(
299 const_cast< void* >( pPropContainer
->getValue() ), aPos
->aProperty
.Type
.getTypeLibType(),
300 const_cast< void* >( aNewRequestedValue
.getValue() ), aPos
->aProperty
.Type
.getTypeLibType(),
301 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
302 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
307 _rOldValue
= *pPropContainer
;
308 _rConvertedValue
= aNewRequestedValue
;
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
319 const Any
* pNewValue
= &_rValue
;
321 if (!_rValue
.getValueType().equals(aPos
->aProperty
.Type
))
323 bool bConverted
= false;
325 // a temporary any of the correct (required) type
326 aProperlyTyped
= Any( NULL
, aPos
->aProperty
.Type
.getTypeLibType() );
327 // (need this as we do not want to overwrite the derived class member here)
329 if ( uno_type_assignData(
330 const_cast<void*>(aProperlyTyped
.getValue()), aProperlyTyped
.getValueType().getTypeLibType(),
331 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
)
338 // could query for the requested interface
340 pNewValue
= &aProperlyTyped
;
344 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
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 aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
.getTypeLibType(),
352 const_cast<void*>(pNewValue
->getValue()), aPos
->aProperty
.Type
.getTypeLibType(),
353 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
354 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
359 _rOldValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
360 _rConvertedValue
= *pNewValue
;
369 void OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle
, const Any
& _rValue
) SAL_THROW( (Exception
) )
371 // get the property somebody is asking for
372 PropertiesIterator aPos
= searchHandle(_nHandle
);
373 if (aPos
== m_aProperties
.end())
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
381 switch (aPos
->eLocated
)
383 case PropertyDescription::ltHoldMyself
:
384 m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
] = _rValue
;
387 case PropertyDescription::ltDerivedClassAnyType
:
388 *reinterpret_cast< Any
* >(aPos
->aLocation
.pDerivedClassMember
) = _rValue
;
391 case PropertyDescription::ltDerivedClassRealType
:
392 #if OSL_DEBUG_LEVEL > 0
395 // copy the data from the to-be-set value
397 aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
.getTypeLibType(),
398 const_cast< void* >( _rValue
.getValue() ), _rValue
.getValueType().getTypeLibType(),
399 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
400 reinterpret_cast< uno_AcquireFunc
>( cpp_acquire
),
401 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
) );
403 OSL_ENSURE( bSuccess
,
404 "OPropertyContainerHelper::setFastPropertyValue: ooops .... the value could not be assigned!");
411 void OPropertyContainerHelper::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
413 // get the property somebody is asking for
414 PropertiesIterator aPos
= const_cast<OPropertyContainerHelper
*>(this)->searchHandle(_nHandle
);
415 if (aPos
== m_aProperties
.end())
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
423 switch (aPos
->eLocated
)
425 case PropertyDescription::ltHoldMyself
:
426 OSL_ENSURE(aPos
->aLocation
.nOwnClassVectorIndex
< (sal_Int32
)m_aHoldProperties
.size(),
427 "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
428 _rValue
= m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
];
430 case PropertyDescription::ltDerivedClassAnyType
:
431 _rValue
= *reinterpret_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
433 case PropertyDescription::ltDerivedClassRealType
:
434 _rValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
440 OPropertyContainerHelper::PropertiesIterator
OPropertyContainerHelper::searchHandle(sal_Int32 _nHandle
)
442 PropertyDescription aHandlePropDesc
;
443 aHandlePropDesc
.aProperty
.Handle
= _nHandle
;
444 // search a lower bound
445 PropertiesIterator aLowerBound
= ::std::lower_bound(
446 m_aProperties
.begin(),
449 PropertyDescriptionHandleCompare());
451 // check for identity
452 if ((aLowerBound
!= m_aProperties
.end()) && aLowerBound
->aProperty
.Handle
!= _nHandle
)
453 aLowerBound
= m_aProperties
.end();
459 const Property
& OPropertyContainerHelper::getProperty( const OUString
& _rName
) const
461 ConstPropertiesIterator pos
= ::std::find_if(
462 m_aProperties
.begin(),
464 PropertyDescriptionNameMatch( _rName
)
466 if ( pos
== m_aProperties
.end() )
467 throw UnknownPropertyException( _rName
, NULL
);
469 return pos
->aProperty
;
473 void OPropertyContainerHelper::describeProperties(Sequence
< Property
>& _rProps
) const
475 Sequence
< Property
> aOwnProps(m_aProperties
.size());
476 Property
* pOwnProps
= aOwnProps
.getArray();
478 for ( ConstPropertiesIterator aLoop
= m_aProperties
.begin();
479 aLoop
!= m_aProperties
.end();
483 pOwnProps
->Name
= aLoop
->aProperty
.Name
;
484 pOwnProps
->Handle
= aLoop
->aProperty
.Handle
;
485 pOwnProps
->Attributes
= (sal_Int16
)aLoop
->aProperty
.Attributes
;
486 pOwnProps
->Type
= aLoop
->aProperty
.Type
;
489 // as our property vector is sorted by handles, not by name, we have to sort aOwnProps
490 ::std::sort(aOwnProps
.getArray(), aOwnProps
.getArray() + aOwnProps
.getLength(), PropertyCompareByName());
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 Sequence
< Property
> aOutput
;
495 aOutput
.realloc(_rProps
.getLength() + aOwnProps
.getLength());
497 ::std::merge( _rProps
.getConstArray(), _rProps
.getConstArray() + _rProps
.getLength(), // input 1
498 aOwnProps
.getConstArray(), aOwnProps
.getConstArray() + aOwnProps
.getLength(), // input 2
499 aOutput
.getArray(), // output
500 PropertyCompareByName() // compare operator
508 } // namespace comphelper
512 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */