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>
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
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
54 OUString
const m_rCompare
;
55 explicit PropertyDescriptionNameMatch( OUString _aCompare
) : m_rCompare(std::move( _aCompare
)) { }
57 bool operator() (const PropertyDescription
& x
) const
59 return x
.aProperty
.Name
== m_rCompare
;
64 OPropertyContainerHelper::OPropertyContainerHelper()
69 OPropertyContainerHelper::~OPropertyContainerHelper()
74 void OPropertyContainerHelper::registerProperty(const OUString
& _rName
, sal_Int32 _nHandle
,
75 sal_Int32 _nAttributes
, void* _pPointerToMember
, const Type
& _rMemberType
)
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");
84 PropertyDescription aNewProp
;
85 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rMemberType
, static_cast<sal_Int16
>(_nAttributes
) );
86 aNewProp
.eLocated
= PropertyDescription::LocationType::DerivedClassRealType
;
87 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
89 implPushBackProperty(aNewProp
);
93 void OPropertyContainerHelper::revokeProperty( sal_Int32 _nHandle
)
95 PropertiesIterator aPos
= searchHandle( _nHandle
);
96 if ( aPos
== m_aProperties
.end() )
97 throw UnknownPropertyException(OUString::number(_nHandle
));
98 m_aProperties
.erase( aPos
);
102 void OPropertyContainerHelper::registerMayBeVoidProperty(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
103 Any
* _pPointerToMember
, const Type
& _rExpectedType
)
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");
112 _nAttributes
|= PropertyAttribute::MAYBEVOID
;
114 PropertyDescription aNewProp
;
115 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rExpectedType
, static_cast<sal_Int16
>(_nAttributes
) );
116 aNewProp
.eLocated
= PropertyDescription::LocationType::DerivedClassAnyType
;
117 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
119 implPushBackProperty(aNewProp
);
123 void OPropertyContainerHelper::registerPropertyNoMember(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
124 const Type
& _rType
, css::uno::Any
const & _pInitialValue
)
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 !");
129 (_pInitialValue
.isExtractableTo(_rType
)
130 || (!_pInitialValue
.hasValue()
131 && (_nAttributes
& PropertyAttribute::MAYBEVOID
) != 0)),
132 "bad initial value");
134 PropertyDescription aNewProp
;
135 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rType
, static_cast<sal_Int16
>(_nAttributes
) );
136 aNewProp
.eLocated
= PropertyDescription::LocationType::HoldMyself
;
137 aNewProp
.aLocation
.nOwnClassVectorIndex
= m_aHoldProperties
.size();
138 m_aHoldProperties
.push_back(_pInitialValue
);
140 implPushBackProperty(aNewProp
);
144 bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle
) const
146 return const_cast< OPropertyContainerHelper
* >( this )->searchHandle( _nHandle
) != m_aProperties
.end();
150 bool OPropertyContainerHelper::isRegisteredProperty( const OUString
& _rName
) const
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 ...
158 m_aProperties
.begin(),
160 PropertyDescriptionNameMatch( _rName
)
167 struct ComparePropertyHandles
169 bool operator()( const PropertyDescription
& _rLHS
, const PropertyDescription
& _nRHS
) const
171 return _rLHS
.aProperty
.Handle
< _nRHS
.aProperty
.Handle
;
177 void OPropertyContainerHelper::implPushBackProperty(const PropertyDescription
& _rProp
)
180 for (const auto& checkConflicts
: m_aProperties
)
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!");
187 PropertiesIterator pos
= std::lower_bound(
188 m_aProperties
.begin(), m_aProperties
.end(),
189 _rProp
, ComparePropertyHandles() );
191 m_aProperties
.insert( pos
, _rProp
);
197 void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription
& _rProperty
, const Any
& _rValue
)
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()
210 bool OPropertyContainerHelper::convertFastPropertyValue(
211 Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
)
213 bool bModified
= false;
215 // get the property somebody is asking for
216 PropertiesIterator aPos
= searchHandle(_nHandle
);
217 if (aPos
== m_aProperties
.end())
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
225 switch (aPos
->eLocated
)
227 // similar handling for the two cases where the value is stored in an any
228 case PropertyDescription::LocationType::HoldMyself
:
229 case PropertyDescription::LocationType::DerivedClassAnyType
:
231 bool bMayBeVoid
= ((aPos
->aProperty
.Attributes
& PropertyAttribute::MAYBEVOID
) != 0);
234 // non modifiable version of the value-to-be-set
235 Any
aNewRequestedValue( _rValue
);
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() );
243 if ( uno_type_assignData(
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
)
252 // we were able to query the given XInterface-derivee for the interface
253 // which is required for this property
254 aNewRequestedValue
= aProperlyTyped
;
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
264 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
267 Any
* pPropContainer
= nullptr;
268 // the pointer to the any which holds the property value, no matter if located in the derived class
271 if (PropertyDescription::LocationType::HoldMyself
== aPos
->eLocated
)
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
);
279 pPropContainer
= static_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
281 // check if the new value differs from the current one
282 if (!pPropContainer
->hasValue() || !aNewRequestedValue
.hasValue())
283 bModified
= pPropContainer
->hasValue() != aNewRequestedValue
.hasValue();
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
)
294 _rOldValue
= *pPropContainer
;
295 _rConvertedValue
= aNewRequestedValue
;
299 case PropertyDescription::LocationType::DerivedClassRealType
:
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
306 const Any
* pNewValue
= &_rValue
;
308 if (!_rValue
.getValueType().equals(aPos
->aProperty
.Type
))
310 bool bConverted
= false;
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)
316 if ( uno_type_assignData(
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
)
325 // could query for the requested interface
327 pNewValue
= &aProperlyTyped
;
331 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
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
)
346 _rOldValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
347 _rConvertedValue
= *pNewValue
;
356 void OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle
, const Any
& _rValue
)
358 // get the property somebody is asking for
359 PropertiesIterator aPos
= searchHandle(_nHandle
);
360 if (aPos
== m_aProperties
.end())
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
368 bool bSuccess
= true;
370 switch (aPos
->eLocated
)
372 case PropertyDescription::LocationType::HoldMyself
:
373 m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
] = _rValue
;
376 case PropertyDescription::LocationType::DerivedClassAnyType
:
377 *static_cast< Any
* >(aPos
->aLocation
.pDerivedClassMember
) = _rValue
;
380 case PropertyDescription::LocationType::DerivedClassRealType
:
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
) );
389 OSL_ENSURE( bSuccess
,
390 "OPropertyContainerHelper::setFastPropertyValue: ooops... the value could not be assigned!");
396 void OPropertyContainerHelper::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
398 // get the property somebody is asking for
399 PropertiesIterator aPos
= const_cast<OPropertyContainerHelper
*>(this)->searchHandle(_nHandle
);
400 if (aPos
== m_aProperties
.end())
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
408 switch (aPos
->eLocated
)
410 case PropertyDescription::LocationType::HoldMyself
:
411 OSL_ENSURE(aPos
->aLocation
.nOwnClassVectorIndex
< m_aHoldProperties
.size(),
412 "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
413 _rValue
= m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
];
415 case PropertyDescription::LocationType::DerivedClassAnyType
:
416 _rValue
= *static_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
418 case PropertyDescription::LocationType::DerivedClassRealType
:
419 _rValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
425 OPropertyContainerHelper::PropertiesIterator
OPropertyContainerHelper::searchHandle(sal_Int32 _nHandle
)
427 PropertyDescription aHandlePropDesc
;
428 aHandlePropDesc
.aProperty
.Handle
= _nHandle
;
429 // search a lower bound
430 PropertiesIterator aLowerBound
= std::lower_bound(
431 m_aProperties
.begin(),
434 PropertyDescriptionHandleCompare());
436 // check for identity
437 if ((aLowerBound
!= m_aProperties
.end()) && aLowerBound
->aProperty
.Handle
!= _nHandle
)
438 aLowerBound
= m_aProperties
.end();
444 const Property
& OPropertyContainerHelper::getProperty( const OUString
& _rName
) const
446 ConstPropertiesIterator pos
= std::find_if(
447 m_aProperties
.begin(),
449 PropertyDescriptionNameMatch( _rName
)
451 if ( pos
== m_aProperties
.end() )
452 throw UnknownPropertyException( _rName
);
454 return pos
->aProperty
;
458 void OPropertyContainerHelper::describeProperties(Sequence
< Property
>& _rProps
) const
460 Sequence
< Property
> aOwnProps(m_aProperties
.size());
461 Property
* pOwnProps
= aOwnProps
.getArray();
463 for (const auto& rProp
: m_aProperties
)
465 pOwnProps
->Name
= rProp
.aProperty
.Name
;
466 pOwnProps
->Handle
= rProp
.aProperty
.Handle
;
467 pOwnProps
->Attributes
= rProp
.aProperty
.Attributes
;
468 pOwnProps
->Type
= rProp
.aProperty
.Type
;
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());
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());
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
491 } // namespace comphelper
494 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */