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
);
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 an 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
, (sal_Int16
)_nAttributes
);
86 aNewProp
.eLocated
= PropertyDescription::ltDerivedClassRealType
;
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();
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 an 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
, (sal_Int16
)_nAttributes
);
116 aNewProp
.eLocated
= PropertyDescription::ltDerivedClassAnyType
;
117 aNewProp
.aLocation
.pDerivedClassMember
= _pPointerToMember
;
119 implPushBackProperty(aNewProp
);
124 void OPropertyContainerHelper::registerPropertyNoMember(const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
,
125 const Type
& _rType
, const void* _pInitialValue
)
127 OSL_ENSURE(!_rType
.equals(cppu::UnoType
<Any
>::get()),
128 "OPropertyContainerHelper::registerPropertyNoMember : don't give my the type of an uno::Any ! Really can't handle this !");
129 OSL_ENSURE(_pInitialValue
|| ((_nAttributes
& PropertyAttribute::MAYBEVOID
) != 0),
130 "OPropertyContainerHelper::registerPropertyNoMember : you should not omit the initial value if the property can't be void ! This will definitivly crash later !");
132 PropertyDescription aNewProp
;
133 aNewProp
.aProperty
= Property( _rName
, _nHandle
, _rType
, (sal_Int16
)_nAttributes
);
134 aNewProp
.eLocated
= PropertyDescription::ltHoldMyself
;
135 aNewProp
.aLocation
.nOwnClassVectorIndex
= m_aHoldProperties
.size();
137 m_aHoldProperties
.push_back(Any(_pInitialValue
, _rType
));
139 m_aHoldProperties
.push_back(Any());
141 implPushBackProperty(aNewProp
);
145 bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle
) const
147 return const_cast< OPropertyContainerHelper
* >( this )->searchHandle( _nHandle
) != m_aProperties
.end();
151 bool OPropertyContainerHelper::isRegisteredProperty( const OUString
& _rName
) const
153 // TODO: the current structure is from a time where properties were
154 // static, not dynamic. Since we allow that properties are also dynamic,
155 // i.e. registered and revoked even though the XPropertySet has already been
156 // accessed, a vector is not really the best data structure anymore ...
158 ConstPropertiesIterator pos
= ::std::find_if(
159 m_aProperties
.begin(),
161 PropertyDescriptionNameMatch( _rName
)
163 return pos
!= m_aProperties
.end();
169 struct ComparePropertyHandles
171 bool operator()( const PropertyDescription
& _rLHS
, const PropertyDescription
& _nRHS
) const
173 return _rLHS
.aProperty
.Handle
< _nRHS
.aProperty
.Handle
;
179 void OPropertyContainerHelper::implPushBackProperty(const PropertyDescription
& _rProp
)
182 for ( PropertiesIterator checkConflicts
= m_aProperties
.begin();
183 checkConflicts
!= m_aProperties
.end();
187 OSL_ENSURE(checkConflicts
->aProperty
.Name
!= _rProp
.aProperty
.Name
, "OPropertyContainerHelper::implPushBackProperty: name already exists!");
188 OSL_ENSURE(checkConflicts
->aProperty
.Handle
!= _rProp
.aProperty
.Handle
, "OPropertyContainerHelper::implPushBackProperty: handle already exists!");
192 PropertiesIterator pos
= ::std::lower_bound(
193 m_aProperties
.begin(), m_aProperties
.end(),
194 _rProp
, ComparePropertyHandles() );
196 m_aProperties
.insert( pos
, _rProp
);
202 void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription
& _rProperty
, const Any
& _rValue
)
204 OUStringBuffer aErrorMessage
;
205 aErrorMessage
.appendAscii( "The given value cannot be converted to the required property type." );
206 aErrorMessage
.appendAscii( "\n(property name \"" );
207 aErrorMessage
.append( _rProperty
.aProperty
.Name
);
208 aErrorMessage
.appendAscii( "\", found value type \"" );
209 aErrorMessage
.append( _rValue
.getValueType().getTypeName() );
210 aErrorMessage
.appendAscii( "\", required property type \"" );
211 aErrorMessage
.append( _rProperty
.aProperty
.Type
.getTypeName() );
212 aErrorMessage
.appendAscii( "\")" );
213 throw IllegalArgumentException( aErrorMessage
.makeStringAndClear(), NULL
, 4 );
218 bool OPropertyContainerHelper::convertFastPropertyValue(
219 Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
)
221 bool bModified
= false;
223 // get the property somebody is asking for
224 PropertiesIterator aPos
= searchHandle(_nHandle
);
225 if (aPos
== m_aProperties
.end())
227 OSL_FAIL( "OPropertyContainerHelper::convertFastPropertyValue: unknown handle!" );
228 // should not happen if the derived class has built a correct property set info helper to be used by
229 // our base class OPropertySetHelper
233 switch (aPos
->eLocated
)
235 // similar handling for the two cases where the value is stored in an any
236 case PropertyDescription::ltHoldMyself
:
237 case PropertyDescription::ltDerivedClassAnyType
:
239 bool bMayBeVoid
= ((aPos
->aProperty
.Attributes
& PropertyAttribute::MAYBEVOID
) != 0);
242 // non modifiable version of the value-to-be-set
243 Any
aNewRequestedValue( _rValue
);
247 if ( !aNewRequestedValue
.getValueType().equals( aPos
->aProperty
.Type
) )
248 { // the actually given value is not of the same type as the one required
249 Any
aProperlyTyped( NULL
, aPos
->aProperty
.Type
.getTypeLibType() );
251 if ( uno_type_assignData(
252 const_cast< void* >( aProperlyTyped
.getValue() ), aProperlyTyped
.getValueType().getTypeLibType(),
253 const_cast< void* >( aNewRequestedValue
.getValue() ), aNewRequestedValue
.getValueType().getTypeLibType(),
254 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
255 reinterpret_cast< uno_AcquireFunc
>( cpp_acquire
),
256 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
260 // we were able to query the given XInterface-derivee for the interface
261 // which is required for this property
262 aNewRequestedValue
= aProperlyTyped
;
267 if ( ! ( (bMayBeVoid
&& !aNewRequestedValue
.hasValue()) // void is allowed if the attribute says so
268 || (aNewRequestedValue
.getValueType().equals(aPos
->aProperty
.Type
)) // else the types have to be equal
272 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
275 Any
* pPropContainer
= NULL
;
276 // the pointer to the any which holds the property value, no matter if located in the derived class
279 if (PropertyDescription::ltHoldMyself
== aPos
->eLocated
)
281 OSL_ENSURE(aPos
->aLocation
.nOwnClassVectorIndex
< (sal_Int32
)m_aHoldProperties
.size(),
282 "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
283 PropertyContainerIterator aIter
= m_aHoldProperties
.begin() + aPos
->aLocation
.nOwnClassVectorIndex
;
284 pPropContainer
= &(*aIter
);
287 pPropContainer
= static_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
289 // check if the new value differs from the current one
290 if (!pPropContainer
->hasValue() || !aNewRequestedValue
.hasValue())
291 bModified
= pPropContainer
->hasValue() != aNewRequestedValue
.hasValue();
293 bModified
= !uno_type_equalData(
294 const_cast< void* >( pPropContainer
->getValue() ), aPos
->aProperty
.Type
.getTypeLibType(),
295 const_cast< void* >( aNewRequestedValue
.getValue() ), aPos
->aProperty
.Type
.getTypeLibType(),
296 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
297 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
302 _rOldValue
= *pPropContainer
;
303 _rConvertedValue
= aNewRequestedValue
;
307 case PropertyDescription::ltDerivedClassRealType
:
308 // let the UNO runtime library do any possible conversion
309 // this may include a change of the type - for instance, if a LONG is required,
310 // but a short is given, then this is valid, as it can be converted without any potential
314 const Any
* pNewValue
= &_rValue
;
316 if (!_rValue
.getValueType().equals(aPos
->aProperty
.Type
))
318 bool bConverted
= false;
320 // a temporary any of the correct (required) type
321 aProperlyTyped
= Any( NULL
, aPos
->aProperty
.Type
.getTypeLibType() );
322 // (need this as we do not want to overwrite the derived class member here)
324 if ( uno_type_assignData(
325 const_cast<void*>(aProperlyTyped
.getValue()), aProperlyTyped
.getValueType().getTypeLibType(),
326 const_cast<void*>(_rValue
.getValue()), _rValue
.getValueType().getTypeLibType(),
327 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
328 reinterpret_cast< uno_AcquireFunc
>( cpp_acquire
),
329 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
333 // could query for the requested interface
335 pNewValue
= &aProperlyTyped
;
339 lcl_throwIllegalPropertyValueTypeException( *aPos
, _rValue
);
342 // from here on, we should have the proper type
343 OSL_ENSURE( pNewValue
->getValueType() == aPos
->aProperty
.Type
,
344 "OPropertyContainerHelper::convertFastPropertyValue: conversion failed!" );
345 bModified
= !uno_type_equalData(
346 aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
.getTypeLibType(),
347 const_cast<void*>(pNewValue
->getValue()), aPos
->aProperty
.Type
.getTypeLibType(),
348 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
349 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
354 _rOldValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
355 _rConvertedValue
= *pNewValue
;
364 bool OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle
, const Any
& _rValue
)
366 // get the property somebody is asking for
367 PropertiesIterator aPos
= searchHandle(_nHandle
);
368 if (aPos
== m_aProperties
.end())
370 OSL_FAIL( "OPropertyContainerHelper::setFastPropertyValue: unknown handle!" );
371 // should not happen if the derived class has built a correct property set info helper to be used by
372 // our base class OPropertySetHelper
376 bool bSuccess
= true;
378 switch (aPos
->eLocated
)
380 case PropertyDescription::ltHoldMyself
:
381 m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
] = _rValue
;
384 case PropertyDescription::ltDerivedClassAnyType
:
385 *static_cast< Any
* >(aPos
->aLocation
.pDerivedClassMember
) = _rValue
;
388 case PropertyDescription::ltDerivedClassRealType
:
389 // copy the data from the to-be-set value
390 bSuccess
= uno_type_assignData(
391 aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
.getTypeLibType(),
392 const_cast< void* >( _rValue
.getValue() ), _rValue
.getValueType().getTypeLibType(),
393 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
394 reinterpret_cast< uno_AcquireFunc
>( cpp_acquire
),
395 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
) );
397 OSL_ENSURE( bSuccess
,
398 "OPropertyContainerHelper::setFastPropertyValue: ooops .... the value could not be assigned!");
406 void OPropertyContainerHelper::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
408 // get the property somebody is asking for
409 PropertiesIterator aPos
= const_cast<OPropertyContainerHelper
*>(this)->searchHandle(_nHandle
);
410 if (aPos
== m_aProperties
.end())
412 OSL_FAIL( "OPropertyContainerHelper::getFastPropertyValue: unknown handle!" );
413 // should not happen if the derived class has built a correct property set info helper to be used by
414 // our base class OPropertySetHelper
418 switch (aPos
->eLocated
)
420 case PropertyDescription::ltHoldMyself
:
421 OSL_ENSURE(aPos
->aLocation
.nOwnClassVectorIndex
< (sal_Int32
)m_aHoldProperties
.size(),
422 "OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
423 _rValue
= m_aHoldProperties
[aPos
->aLocation
.nOwnClassVectorIndex
];
425 case PropertyDescription::ltDerivedClassAnyType
:
426 _rValue
= *static_cast<Any
*>(aPos
->aLocation
.pDerivedClassMember
);
428 case PropertyDescription::ltDerivedClassRealType
:
429 _rValue
.setValue(aPos
->aLocation
.pDerivedClassMember
, aPos
->aProperty
.Type
);
435 OPropertyContainerHelper::PropertiesIterator
OPropertyContainerHelper::searchHandle(sal_Int32 _nHandle
)
437 PropertyDescription aHandlePropDesc
;
438 aHandlePropDesc
.aProperty
.Handle
= _nHandle
;
439 // search a lower bound
440 PropertiesIterator aLowerBound
= ::std::lower_bound(
441 m_aProperties
.begin(),
444 PropertyDescriptionHandleCompare());
446 // check for identity
447 if ((aLowerBound
!= m_aProperties
.end()) && aLowerBound
->aProperty
.Handle
!= _nHandle
)
448 aLowerBound
= m_aProperties
.end();
454 const Property
& OPropertyContainerHelper::getProperty( const OUString
& _rName
) const
456 ConstPropertiesIterator pos
= ::std::find_if(
457 m_aProperties
.begin(),
459 PropertyDescriptionNameMatch( _rName
)
461 if ( pos
== m_aProperties
.end() )
462 throw UnknownPropertyException( _rName
);
464 return pos
->aProperty
;
468 void OPropertyContainerHelper::describeProperties(Sequence
< Property
>& _rProps
) const
470 Sequence
< Property
> aOwnProps(m_aProperties
.size());
471 Property
* pOwnProps
= aOwnProps
.getArray();
473 for ( ConstPropertiesIterator aLoop
= m_aProperties
.begin();
474 aLoop
!= m_aProperties
.end();
478 pOwnProps
->Name
= aLoop
->aProperty
.Name
;
479 pOwnProps
->Handle
= aLoop
->aProperty
.Handle
;
480 pOwnProps
->Attributes
= (sal_Int16
)aLoop
->aProperty
.Attributes
;
481 pOwnProps
->Type
= aLoop
->aProperty
.Type
;
484 // as our property vector is sorted by handles, not by name, we have to sort aOwnProps
485 ::std::sort(aOwnProps
.getArray(), aOwnProps
.getArray() + aOwnProps
.getLength(), PropertyCompareByName());
487 // unfortunately the STL merge function does not allow the output range to overlap one of the input ranges,
488 // so we need an extra sequence
489 Sequence
< Property
> aOutput
;
490 aOutput
.realloc(_rProps
.getLength() + aOwnProps
.getLength());
492 ::std::merge( _rProps
.getConstArray(), _rProps
.getConstArray() + _rProps
.getLength(), // input 1
493 aOwnProps
.getConstArray(), aOwnProps
.getConstArray() + aOwnProps
.getLength(), // input 2
494 aOutput
.getArray(), // output
495 PropertyCompareByName() // compare operator
503 } // namespace comphelper
507 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */