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/Sequence.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/UnknownPropertyException.hpp>
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::beans
;
42 // comparing two property descriptions
43 struct PropertyDescriptionHandleCompare
45 bool operator() (const PropertyDescription
& x
, const PropertyDescription
& y
) const
47 return x
.aProperty
.Handle
< y
.aProperty
.Handle
;
50 // comparing two property descriptions (by name)
51 struct PropertyDescriptionNameMatch
53 OUString
const m_rCompare
;
54 explicit PropertyDescriptionNameMatch( const OUString
& _rCompare
) : m_rCompare( _rCompare
) { }
56 bool operator() (const PropertyDescription
& x
) const
58 return x
.aProperty
.Name
== m_rCompare
;
63 OPropertyContainerHelper::OPropertyContainerHelper()
68 OPropertyContainerHelper::~OPropertyContainerHelper()
73 void OPropertyContainerHelper::registerProperty(const OUString
& _rName
, sal_Int32 _nHandle
,
74 sal_Int32 _nAttributes
, void* _pPointerToMember
, const Type
& _rMemberType
)
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");
83 PropertyDescription aNewProp
;
84 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rMemberType
, static_cast<sal_Int16
>(_nAttributes
) );
85 aNewProp
.eLocated
= PropertyDescription::LocationType::DerivedClassRealType
;
86 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
88 implPushBackProperty(aNewProp
);
92 void OPropertyContainerHelper::revokeProperty( sal_Int32 _nHandle
)
94 PropertiesIterator aPos
= searchHandle( _nHandle
);
95 if ( aPos
== m_aProperties
.end() )
96 throw UnknownPropertyException();
97 m_aProperties
.erase( aPos
);
101 void OPropertyContainerHelper::registerMayBeVoidProperty(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
102 Any
* _pPointerToMember
, const Type
& _rExpectedType
)
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");
111 _nAttributes
|= PropertyAttribute::MAYBEVOID
;
113 PropertyDescription aNewProp
;
114 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rExpectedType
, static_cast<sal_Int16
>(_nAttributes
) );
115 aNewProp
.eLocated
= PropertyDescription::LocationType::DerivedClassAnyType
;
116 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
118 implPushBackProperty(aNewProp
);
122 void OPropertyContainerHelper::registerPropertyNoMember(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
123 const Type
& _rType
, css::uno::Any
const & _pInitialValue
)
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 !");
128 (_pInitialValue
.isExtractableTo(_rType
)
129 || (!_pInitialValue
.hasValue()
130 && (_nAttributes
& PropertyAttribute::MAYBEVOID
) != 0)),
131 "bad initial value");
133 PropertyDescription aNewProp
;
134 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rType
, static_cast<sal_Int16
>(_nAttributes
) );
135 aNewProp
.eLocated
= PropertyDescription::LocationType::HoldMyself
;
136 aNewProp
.aLocation
.nOwnClassVectorIndex
= m_aHoldProperties
.size();
137 m_aHoldProperties
.push_back(_pInitialValue
);
139 implPushBackProperty(aNewProp
);
143 bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle
) const
145 return const_cast< OPropertyContainerHelper
* >( this )->searchHandle( _nHandle
) != m_aProperties
.end();
149 bool OPropertyContainerHelper::isRegisteredProperty( const OUString
& _rName
) const
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 ...
157 m_aProperties
.begin(),
159 PropertyDescriptionNameMatch( _rName
)
166 struct ComparePropertyHandles
168 bool operator()( const PropertyDescription
& _rLHS
, const PropertyDescription
& _nRHS
) const
170 return _rLHS
.aProperty
.Handle
< _nRHS
.aProperty
.Handle
;
176 void OPropertyContainerHelper::implPushBackProperty(const PropertyDescription
& _rProp
)
179 for (auto& checkConflicts
: m_aProperties
)
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!");
186 PropertiesIterator pos
= std::lower_bound(
187 m_aProperties
.begin(), m_aProperties
.end(),
188 _rProp
, ComparePropertyHandles() );
190 m_aProperties
.insert( pos
, _rProp
);
196 void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription
& _rProperty
, const Any
& _rValue
)
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()
209 bool OPropertyContainerHelper::convertFastPropertyValue(
210 Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
)
212 bool bModified
= false;
214 // get the property somebody is asking for
215 PropertiesIterator aPos
= searchHandle(_nHandle
);
216 if (aPos
== m_aProperties
.end())
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
224 switch (aPos
->eLocated
)
226 // similar handling for the two cases where the value is stored in an any
227 case PropertyDescription::LocationType::HoldMyself
:
228 case PropertyDescription::LocationType::DerivedClassAnyType
:
230 bool bMayBeVoid
= ((aPos
->aProperty
.Attributes
& PropertyAttribute::MAYBEVOID
) != 0);
233 // non modifiable version of the value-to-be-set
234 Any
aNewRequestedValue( _rValue
);
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() );
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
)
251 // we were able to query the given XInterface-derivee for the interface
252 // which is required for this property
253 aNewRequestedValue
= aProperlyTyped
;
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
263 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
266 Any
* pPropContainer
= nullptr;
267 // the pointer to the any which holds the property value, no matter if located in the derived class
270 if (PropertyDescription::LocationType::HoldMyself
== aPos
->eLocated
)
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
);
278 pPropContainer
= static_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
280 // check if the new value differs from the current one
281 if (!pPropContainer
->hasValue() || !aNewRequestedValue
.hasValue())
282 bModified
= pPropContainer
->hasValue() != aNewRequestedValue
.hasValue();
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
)
293 _rOldValue
= *pPropContainer
;
294 _rConvertedValue
= aNewRequestedValue
;
298 case PropertyDescription::LocationType::DerivedClassRealType
:
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
305 const Any
* pNewValue
= &_rValue
;
307 if (!_rValue
.getValueType().equals(aPos
->aProperty
.Type
))
309 bool bConverted
= false;
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)
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
)
324 // could query for the requested interface
326 pNewValue
= &aProperlyTyped
;
330 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
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
)
345 _rOldValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
346 _rConvertedValue
= *pNewValue
;
355 void OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle
, const Any
& _rValue
)
357 // get the property somebody is asking for
358 PropertiesIterator aPos
= searchHandle(_nHandle
);
359 if (aPos
== m_aProperties
.end())
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
367 bool bSuccess
= true;
369 switch (aPos
->eLocated
)
371 case PropertyDescription::LocationType::HoldMyself
:
372 m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
] = _rValue
;
375 case PropertyDescription::LocationType::DerivedClassAnyType
:
376 *static_cast< Any
* >(aPos
->aLocation
.pDerivedClassMember
) = _rValue
;
379 case PropertyDescription::LocationType::DerivedClassRealType
:
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
) );
388 OSL_ENSURE( bSuccess
,
389 "OPropertyContainerHelper::setFastPropertyValue: ooops .... the value could not be assigned!");
395 void OPropertyContainerHelper::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
397 // get the property somebody is asking for
398 PropertiesIterator aPos
= const_cast<OPropertyContainerHelper
*>(this)->searchHandle(_nHandle
);
399 if (aPos
== m_aProperties
.end())
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
407 switch (aPos
->eLocated
)
409 case PropertyDescription::LocationType::HoldMyself
:
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
];
414 case PropertyDescription::LocationType::DerivedClassAnyType
:
415 _rValue
= *static_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
417 case PropertyDescription::LocationType::DerivedClassRealType
:
418 _rValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
424 OPropertyContainerHelper::PropertiesIterator
OPropertyContainerHelper::searchHandle(sal_Int32 _nHandle
)
426 PropertyDescription aHandlePropDesc
;
427 aHandlePropDesc
.aProperty
.Handle
= _nHandle
;
428 // search a lower bound
429 PropertiesIterator aLowerBound
= std::lower_bound(
430 m_aProperties
.begin(),
433 PropertyDescriptionHandleCompare());
435 // check for identity
436 if ((aLowerBound
!= m_aProperties
.end()) && aLowerBound
->aProperty
.Handle
!= _nHandle
)
437 aLowerBound
= m_aProperties
.end();
443 const Property
& OPropertyContainerHelper::getProperty( const OUString
& _rName
) const
445 ConstPropertiesIterator pos
= std::find_if(
446 m_aProperties
.begin(),
448 PropertyDescriptionNameMatch( _rName
)
450 if ( pos
== m_aProperties
.end() )
451 throw UnknownPropertyException( _rName
);
453 return pos
->aProperty
;
457 void OPropertyContainerHelper::describeProperties(Sequence
< Property
>& _rProps
) const
459 Sequence
< Property
> aOwnProps(m_aProperties
.size());
460 Property
* pOwnProps
= aOwnProps
.getArray();
462 for (const auto& rProp
: m_aProperties
)
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
;
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());
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());
479 std::merge( _rProps
.begin(), _rProps
.end(), // input 1
480 aOwnProps
.begin(), aOwnProps
.end(), // input 2
481 aOutput
.getArray(), // output
482 PropertyCompareByName() // compare operator
490 } // namespace comphelper
493 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */