1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/propertycontainerhelper.hxx>
27cdf0e10cSrcweir #include <comphelper/property.hxx>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <uno/data.h>
30cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.h>
31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/UnknownPropertyException.hpp>
33cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <algorithm>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //.........................................................................
38cdf0e10cSrcweir namespace comphelper
39cdf0e10cSrcweir {
40cdf0e10cSrcweir //.........................................................................
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::com::sun::star::uno;
43cdf0e10cSrcweir using namespace ::com::sun::star::lang;
44cdf0e10cSrcweir using namespace ::com::sun::star::beans;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //--------------------------------------------------------------------------
47cdf0e10cSrcweir namespace
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	// comparing two property descriptions
50cdf0e10cSrcweir 	struct PropertyDescriptionCompareByHandle : public ::std::binary_function< PropertyDescription, PropertyDescription, bool >
51cdf0e10cSrcweir 	{
operator ()comphelper::__anon2c80fa320111::PropertyDescriptionCompareByHandle52cdf0e10cSrcweir 		bool operator() (const PropertyDescription& x, const PropertyDescription& y) const
53cdf0e10cSrcweir 		{
54cdf0e10cSrcweir 			return x.aProperty.Handle < y.aProperty.Handle;
55cdf0e10cSrcweir 		}
56cdf0e10cSrcweir 	};
57cdf0e10cSrcweir 	// comparing two property descriptions
58cdf0e10cSrcweir 	struct PropertyDescriptionHandleCompare : public ::std::binary_function< PropertyDescription, sal_Int32, bool >
59cdf0e10cSrcweir 	{
operator ()comphelper::__anon2c80fa320111::PropertyDescriptionHandleCompare60cdf0e10cSrcweir 		bool operator() (const PropertyDescription& x, const sal_Int32& y) const
61cdf0e10cSrcweir 		{
62cdf0e10cSrcweir 			return x.aProperty.Handle < y;
63cdf0e10cSrcweir 		}
operator ()comphelper::__anon2c80fa320111::PropertyDescriptionHandleCompare64cdf0e10cSrcweir 		bool operator() (const sal_Int32& x, const PropertyDescription& y) const
65cdf0e10cSrcweir 		{
66cdf0e10cSrcweir 			return x < y.aProperty.Handle;
67cdf0e10cSrcweir 		}
68cdf0e10cSrcweir 	};
69cdf0e10cSrcweir 	// comparing two property descriptions (by name)
70cdf0e10cSrcweir     struct PropertyDescriptionNameMatch : public ::std::unary_function< PropertyDescription, bool >
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir         ::rtl::OUString m_rCompare;
PropertyDescriptionNameMatchcomphelper::__anon2c80fa320111::PropertyDescriptionNameMatch73cdf0e10cSrcweir         PropertyDescriptionNameMatch( const ::rtl::OUString& _rCompare ) : m_rCompare( _rCompare ) { }
74cdf0e10cSrcweir 
operator ()comphelper::__anon2c80fa320111::PropertyDescriptionNameMatch75cdf0e10cSrcweir         bool operator() (const PropertyDescription& x ) const
76cdf0e10cSrcweir 		{
77cdf0e10cSrcweir 			return x.aProperty.Name.equals(m_rCompare);
78cdf0e10cSrcweir 		}
79cdf0e10cSrcweir 	};
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir //==========================================================================
83cdf0e10cSrcweir //= OPropertyContainerHelper
84cdf0e10cSrcweir //==========================================================================
85cdf0e10cSrcweir //--------------------------------------------------------------------------
OPropertyContainerHelper()86cdf0e10cSrcweir OPropertyContainerHelper::OPropertyContainerHelper()
87cdf0e10cSrcweir     :m_bUnused(sal_False)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir // -------------------------------------------------------------------------
~OPropertyContainerHelper()92cdf0e10cSrcweir OPropertyContainerHelper::~OPropertyContainerHelper()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir //--------------------------------------------------------------------------
registerProperty(const::rtl::OUString & _rName,sal_Int32 _nHandle,sal_Int32 _nAttributes,void * _pPointerToMember,const Type & _rMemberType)97cdf0e10cSrcweir void OPropertyContainerHelper::registerProperty(const ::rtl::OUString& _rName, sal_Int32 _nHandle,
98cdf0e10cSrcweir 		sal_Int32 _nAttributes, void* _pPointerToMember, const Type& _rMemberType)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	OSL_ENSURE((_nAttributes & PropertyAttribute::MAYBEVOID) == 0,
101cdf0e10cSrcweir 		"OPropertyContainerHelper::registerProperty: don't use this for properties which may be void ! There is a method called \"registerMayBeVoidProperty\" for this !");
102cdf0e10cSrcweir 	OSL_ENSURE(!_rMemberType.equals(::getCppuType(static_cast< Any* >(NULL))),
103cdf0e10cSrcweir 		"OPropertyContainerHelper::registerProperty: don't give my the type of an uno::Any ! Really can't handle this !");
104cdf0e10cSrcweir 	OSL_ENSURE(_pPointerToMember,
105cdf0e10cSrcweir 		"OPropertyContainerHelper::registerProperty: you gave me nonsense : the pointer must be non-NULL");
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	PropertyDescription aNewProp;
108cdf0e10cSrcweir 	aNewProp.aProperty = Property( _rName, _nHandle, _rMemberType, (sal_Int16)_nAttributes );
109cdf0e10cSrcweir 	aNewProp.eLocated = PropertyDescription::ltDerivedClassRealType;
110cdf0e10cSrcweir 	aNewProp.aLocation.pDerivedClassMember = _pPointerToMember;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	implPushBackProperty(aNewProp);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir //--------------------------------------------------------------------------
revokeProperty(sal_Int32 _nHandle)116cdf0e10cSrcweir void OPropertyContainerHelper::revokeProperty( sal_Int32 _nHandle )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     PropertiesIterator aPos = searchHandle( _nHandle );
119cdf0e10cSrcweir     if ( aPos == m_aProperties.end() )
120cdf0e10cSrcweir         throw UnknownPropertyException();
121cdf0e10cSrcweir     m_aProperties.erase( aPos );
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir //--------------------------------------------------------------------------
registerMayBeVoidProperty(const::rtl::OUString & _rName,sal_Int32 _nHandle,sal_Int32 _nAttributes,Any * _pPointerToMember,const Type & _rExpectedType)125cdf0e10cSrcweir void OPropertyContainerHelper::registerMayBeVoidProperty(const ::rtl::OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes,
126cdf0e10cSrcweir 		Any* _pPointerToMember, const Type& _rExpectedType)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir 	OSL_ENSURE((_nAttributes & PropertyAttribute::MAYBEVOID) != 0,
129cdf0e10cSrcweir 		"OPropertyContainerHelper::registerMayBeVoidProperty: why calling this when the attributes say nothing about may-be-void ?");
130cdf0e10cSrcweir 	OSL_ENSURE(!_rExpectedType.equals(::getCppuType(static_cast< Any* >(NULL))),
131cdf0e10cSrcweir 		"OPropertyContainerHelper::registerMayBeVoidProperty: don't give my the type of an uno::Any ! Really can't handle this !");
132cdf0e10cSrcweir 	OSL_ENSURE(_pPointerToMember,
133cdf0e10cSrcweir 		"OPropertyContainerHelper::registerMayBeVoidProperty: you gave me nonsense : the pointer must be non-NULL");
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	_nAttributes |= PropertyAttribute::MAYBEVOID;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	PropertyDescription aNewProp;
138cdf0e10cSrcweir 	aNewProp.aProperty = Property( _rName, _nHandle, _rExpectedType, (sal_Int16)_nAttributes );
139cdf0e10cSrcweir 	aNewProp.eLocated = PropertyDescription::ltDerivedClassAnyType;
140cdf0e10cSrcweir 	aNewProp.aLocation.pDerivedClassMember = _pPointerToMember;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	implPushBackProperty(aNewProp);
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 
146cdf0e10cSrcweir //--------------------------------------------------------------------------
registerPropertyNoMember(const::rtl::OUString & _rName,sal_Int32 _nHandle,sal_Int32 _nAttributes,const Type & _rType,const void * _pInitialValue)147cdf0e10cSrcweir void OPropertyContainerHelper::registerPropertyNoMember(const ::rtl::OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes,
148cdf0e10cSrcweir 		const Type& _rType, const void* _pInitialValue)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 	OSL_ENSURE(!_rType.equals(::getCppuType(static_cast< Any* >(NULL))),
151cdf0e10cSrcweir 		"OPropertyContainerHelper::registerPropertyNoMember : don't give my the type of an uno::Any ! Really can't handle this !");
152cdf0e10cSrcweir 	OSL_ENSURE(_pInitialValue || ((_nAttributes & PropertyAttribute::MAYBEVOID) != 0),
153cdf0e10cSrcweir 		"OPropertyContainerHelper::registerPropertyNoMember : you should not ommit the initial value if the property can't be void ! This will definitivly crash later !");
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	PropertyDescription aNewProp;
156cdf0e10cSrcweir 	aNewProp.aProperty = Property( _rName, _nHandle, _rType, (sal_Int16)_nAttributes );
157cdf0e10cSrcweir 	aNewProp.eLocated = PropertyDescription::ltHoldMyself;
158cdf0e10cSrcweir 	aNewProp.aLocation.nOwnClassVectorIndex = m_aHoldProperties.size();
159cdf0e10cSrcweir 	if (_pInitialValue)
160cdf0e10cSrcweir 		m_aHoldProperties.push_back(Any(_pInitialValue, _rType));
161cdf0e10cSrcweir 	else
162cdf0e10cSrcweir 		m_aHoldProperties.push_back(Any());
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	implPushBackProperty(aNewProp);
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir //--------------------------------------------------------------------------
isRegisteredProperty(sal_Int32 _nHandle) const168cdf0e10cSrcweir sal_Bool OPropertyContainerHelper::isRegisteredProperty( sal_Int32 _nHandle ) const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     return const_cast< OPropertyContainerHelper* >( this )->searchHandle( _nHandle ) != m_aProperties.end();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir //--------------------------------------------------------------------------
isRegisteredProperty(const::rtl::OUString & _rName) const174cdf0e10cSrcweir sal_Bool OPropertyContainerHelper::isRegisteredProperty( const ::rtl::OUString& _rName ) const
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     // TODO: the current structure is from a time where properties were
177cdf0e10cSrcweir     // static, not dynamic. Since we allow that properties are also dynamic,
178cdf0e10cSrcweir     // i.e. registered and revoked even though the XPropertySet has already been
179cdf0e10cSrcweir     // accessed, a vector is not really the best data structure anymore ...
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     ConstPropertiesIterator pos = ::std::find_if(
182cdf0e10cSrcweir         m_aProperties.begin(),
183cdf0e10cSrcweir         m_aProperties.end(),
184cdf0e10cSrcweir         PropertyDescriptionNameMatch( _rName )
185cdf0e10cSrcweir     );
186cdf0e10cSrcweir     return pos != m_aProperties.end();
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir //--------------------------------------------------------------------------
190cdf0e10cSrcweir namespace
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     struct ComparePropertyWithHandle
193cdf0e10cSrcweir     {
operator ()comphelper::__anon2c80fa320211::ComparePropertyWithHandle194cdf0e10cSrcweir         bool operator()( const PropertyDescription& _rLHS, sal_Int32 _nRHS ) const
195cdf0e10cSrcweir         {
196cdf0e10cSrcweir             return _rLHS.aProperty.Handle < _nRHS;
197cdf0e10cSrcweir         }
operator ()comphelper::__anon2c80fa320211::ComparePropertyWithHandle198cdf0e10cSrcweir         bool operator()( sal_Int32 _nLHS, const PropertyDescription& _rRHS ) const
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             return _nLHS < _rRHS.aProperty.Handle;
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir     };
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir //--------------------------------------------------------------------------
implPushBackProperty(const PropertyDescription & _rProp)206cdf0e10cSrcweir void OPropertyContainerHelper::implPushBackProperty(const PropertyDescription& _rProp)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir #ifdef DBG_UTIL
209cdf0e10cSrcweir 	for	(	PropertiesIterator checkConflicts = m_aProperties.begin();
210cdf0e10cSrcweir 			checkConflicts != m_aProperties.end();
211cdf0e10cSrcweir 			++checkConflicts
212cdf0e10cSrcweir 		)
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 		OSL_ENSURE(checkConflicts->aProperty.Name != _rProp.aProperty.Name, "OPropertyContainerHelper::implPushBackProperty: name already exists!");
215cdf0e10cSrcweir 		OSL_ENSURE(checkConflicts->aProperty.Handle != _rProp.aProperty.Handle, "OPropertyContainerHelper::implPushBackProperty: handle already exists!");
216cdf0e10cSrcweir 	}
217cdf0e10cSrcweir #endif
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     PropertiesIterator pos = ::std::lower_bound(
220cdf0e10cSrcweir         m_aProperties.begin(), m_aProperties.end(),
221cdf0e10cSrcweir         _rProp.aProperty.Handle, ComparePropertyWithHandle() );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     m_aProperties.insert( pos, _rProp );
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //--------------------------------------------------------------------------
227cdf0e10cSrcweir namespace
228cdf0e10cSrcweir {
lcl_throwIllegalPropertyValueTypeException(const PropertyDescription & _rProperty,const Any & _rValue)229cdf0e10cSrcweir     void lcl_throwIllegalPropertyValueTypeException( const PropertyDescription& _rProperty, const Any& _rValue )
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir         ::rtl::OUStringBuffer aErrorMessage;
232cdf0e10cSrcweir         aErrorMessage.appendAscii( "The given value cannot be converted to the required property type." );
233cdf0e10cSrcweir         aErrorMessage.appendAscii( "\n(property name \"" );
234cdf0e10cSrcweir         aErrorMessage.append( _rProperty.aProperty.Name );
235cdf0e10cSrcweir         aErrorMessage.appendAscii( "\", found value type \"" );
236cdf0e10cSrcweir         aErrorMessage.append( _rValue.getValueType().getTypeName() );
237cdf0e10cSrcweir         aErrorMessage.appendAscii( "\", required property type \"" );
238cdf0e10cSrcweir         aErrorMessage.append( _rProperty.aProperty.Type.getTypeName() );
239cdf0e10cSrcweir         aErrorMessage.appendAscii( "\")" );
240cdf0e10cSrcweir 		throw IllegalArgumentException( aErrorMessage.makeStringAndClear(), NULL, 4 );
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir //--------------------------------------------------------------------------
convertFastPropertyValue(Any & _rConvertedValue,Any & _rOldValue,sal_Int32 _nHandle,const Any & _rValue)245cdf0e10cSrcweir sal_Bool OPropertyContainerHelper::convertFastPropertyValue(
246cdf0e10cSrcweir 	Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) SAL_THROW( (IllegalArgumentException) )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir 	sal_Bool bModified = sal_False;
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 	// get the property somebody is asking for
251cdf0e10cSrcweir 	PropertiesIterator aPos = searchHandle(_nHandle);
252cdf0e10cSrcweir 	if (aPos == m_aProperties.end())
253cdf0e10cSrcweir 	{
254cdf0e10cSrcweir 		OSL_ENSURE( false, "OPropertyContainerHelper::convertFastPropertyValue: unknown handle!" );
255cdf0e10cSrcweir 		// should not happen if the derived class has built a correct property set info helper to be used by
256cdf0e10cSrcweir 		// our base class OPropertySetHelper
257cdf0e10cSrcweir 		return bModified;
258cdf0e10cSrcweir 	}
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	switch (aPos->eLocated)
261cdf0e10cSrcweir 	{
262cdf0e10cSrcweir 		// similar handling for the two cases where the value is stored in an any
263cdf0e10cSrcweir 		case PropertyDescription::ltHoldMyself:
264cdf0e10cSrcweir 		case PropertyDescription::ltDerivedClassAnyType:
265cdf0e10cSrcweir 		{
266cdf0e10cSrcweir 			sal_Bool bMayBeVoid = ((aPos->aProperty.Attributes & PropertyAttribute::MAYBEVOID) != 0);
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 			// non modifiable version of the value-to-be-set
270cdf0e10cSrcweir 			Any aNewRequestedValue( _rValue );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 			// normalization
273cdf0e10cSrcweir 			// (#102329# - 2002-08-14 - fs@openoffice.org)
274cdf0e10cSrcweir 			// (#i29490# - 2004-06-16 - fs@openoffice.org)
275cdf0e10cSrcweir 			if ( !aNewRequestedValue.getValueType().equals( aPos->aProperty.Type ) )
276cdf0e10cSrcweir 			{	// the actually given value is not of the same type as the one required
277cdf0e10cSrcweir 				Any aProperlyTyped( NULL, aPos->aProperty.Type.getTypeLibType() );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 				if (	uno_type_assignData(
280cdf0e10cSrcweir 							const_cast< void* >( aProperlyTyped.getValue() ), aProperlyTyped.getValueType().getTypeLibType(),
281cdf0e10cSrcweir 							const_cast< void* >( aNewRequestedValue.getValue() ), aNewRequestedValue.getValueType().getTypeLibType(),
282cdf0e10cSrcweir 							reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
283cdf0e10cSrcweir                             reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
284cdf0e10cSrcweir                             reinterpret_cast< uno_ReleaseFunc >( cpp_release )
285cdf0e10cSrcweir 						)
286cdf0e10cSrcweir 					)
287cdf0e10cSrcweir 				{
288cdf0e10cSrcweir 					// we were able to query the given XInterface-derivee for the interface
289cdf0e10cSrcweir 					// which is required for this property
290cdf0e10cSrcweir 					aNewRequestedValue = aProperlyTyped;
291cdf0e10cSrcweir 				}
292cdf0e10cSrcweir 			}
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 			// argument check
295cdf0e10cSrcweir 			if	(	!	(	(bMayBeVoid && !aNewRequestedValue.hasValue())			            // void is allowed if the attribute says so
296cdf0e10cSrcweir 						||	(aNewRequestedValue.getValueType().equals(aPos->aProperty.Type))	// else the types have to be equal
297cdf0e10cSrcweir 						)
298cdf0e10cSrcweir 				)
299cdf0e10cSrcweir 			{
300cdf0e10cSrcweir                 lcl_throwIllegalPropertyValueTypeException( *aPos, _rValue );
301cdf0e10cSrcweir 			}
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 			Any* pPropContainer = NULL;
304cdf0e10cSrcweir 				// the pointer to the any which holds the property value, no matter if located in the derived clas
305cdf0e10cSrcweir 				// or in out vector
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 			if (PropertyDescription::ltHoldMyself == aPos->eLocated)
308cdf0e10cSrcweir 			{
309cdf0e10cSrcweir 				OSL_ENSURE(aPos->aLocation.nOwnClassVectorIndex < (sal_Int32)m_aHoldProperties.size(),
310cdf0e10cSrcweir 					"OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
311cdf0e10cSrcweir 				PropertyContainerIterator aIter = m_aHoldProperties.begin() + aPos->aLocation.nOwnClassVectorIndex;
312cdf0e10cSrcweir 				pPropContainer = &(*aIter);
313cdf0e10cSrcweir 			}
314cdf0e10cSrcweir 			else
315cdf0e10cSrcweir 				pPropContainer = reinterpret_cast<Any*>(aPos->aLocation.pDerivedClassMember);
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 			// check if the new value differs from the current one
318cdf0e10cSrcweir 			if (!pPropContainer->hasValue() || !aNewRequestedValue.hasValue())
319cdf0e10cSrcweir 				bModified = pPropContainer->hasValue() != aNewRequestedValue.hasValue();
320cdf0e10cSrcweir 			else
321cdf0e10cSrcweir 				bModified = !uno_type_equalData(
322cdf0e10cSrcweir                                 const_cast< void* >( pPropContainer->getValue() ), aPos->aProperty.Type.getTypeLibType(),
323cdf0e10cSrcweir                                 const_cast< void* >( aNewRequestedValue.getValue() ), aPos->aProperty.Type.getTypeLibType(),
324cdf0e10cSrcweir                                 reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
325cdf0e10cSrcweir                                 reinterpret_cast< uno_ReleaseFunc >( cpp_release )
326cdf0e10cSrcweir 							);
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 			if (bModified)
329cdf0e10cSrcweir 			{
330cdf0e10cSrcweir 				_rOldValue = *pPropContainer;
331cdf0e10cSrcweir 				_rConvertedValue = aNewRequestedValue;
332cdf0e10cSrcweir 			}
333cdf0e10cSrcweir 		}
334cdf0e10cSrcweir 		break;
335cdf0e10cSrcweir 		case PropertyDescription::ltDerivedClassRealType:
336cdf0e10cSrcweir 			// let the UNO runtime library do any possible conversion
337cdf0e10cSrcweir 			// this may include a change of the type - for instance, if a LONG is required,
338cdf0e10cSrcweir 			// but a short is given, then this is valid, as it can be converted without any potential
339cdf0e10cSrcweir 			// data loss
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 			Any aProperlyTyped;
342cdf0e10cSrcweir 			const Any* pNewValue = &_rValue;
343cdf0e10cSrcweir 
344cdf0e10cSrcweir 			if (!_rValue.getValueType().equals(aPos->aProperty.Type))
345cdf0e10cSrcweir 			{
346cdf0e10cSrcweir 				sal_Bool bConverted = sal_False;
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 				// a temporary any of the correct (required) type
349cdf0e10cSrcweir 				aProperlyTyped = Any( NULL, aPos->aProperty.Type.getTypeLibType() );
350cdf0e10cSrcweir 					// (need this as we do not want to overwrite the derived class member here)
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 				if (	uno_type_assignData(
353cdf0e10cSrcweir 							const_cast<void*>(aProperlyTyped.getValue()), aProperlyTyped.getValueType().getTypeLibType(),
354cdf0e10cSrcweir 							const_cast<void*>(_rValue.getValue()), _rValue.getValueType().getTypeLibType(),
355cdf0e10cSrcweir 							reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
356cdf0e10cSrcweir                             reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
357cdf0e10cSrcweir                             reinterpret_cast< uno_ReleaseFunc >( cpp_release )
358cdf0e10cSrcweir 						)
359cdf0e10cSrcweir 					)
360cdf0e10cSrcweir 				{
361cdf0e10cSrcweir 					// could query for the requested interface
362cdf0e10cSrcweir 					bConverted = sal_True;
363cdf0e10cSrcweir 					pNewValue = &aProperlyTyped;
364cdf0e10cSrcweir 				}
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 				if ( !bConverted )
367cdf0e10cSrcweir                     lcl_throwIllegalPropertyValueTypeException( *aPos, _rValue );
368cdf0e10cSrcweir 			}
369cdf0e10cSrcweir 
370cdf0e10cSrcweir 			// from here on, we should have the proper type
371cdf0e10cSrcweir 			OSL_ENSURE( pNewValue->getValueType() == aPos->aProperty.Type,
372cdf0e10cSrcweir 				"OPropertyContainerHelper::convertFastPropertyValue: conversion failed!" );
373cdf0e10cSrcweir 			bModified = !uno_type_equalData(
374cdf0e10cSrcweir                             aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type.getTypeLibType(),
375cdf0e10cSrcweir                             const_cast<void*>(pNewValue->getValue()), aPos->aProperty.Type.getTypeLibType(),
376cdf0e10cSrcweir                             reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
377cdf0e10cSrcweir                             reinterpret_cast< uno_ReleaseFunc >( cpp_release )
378cdf0e10cSrcweir 						);
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 			if (bModified)
381cdf0e10cSrcweir 			{
382cdf0e10cSrcweir 				_rOldValue.setValue(aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type);
383cdf0e10cSrcweir 				_rConvertedValue = *pNewValue;
384cdf0e10cSrcweir 			}
385cdf0e10cSrcweir 			break;
386cdf0e10cSrcweir 	}
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	return bModified;
389cdf0e10cSrcweir }
390cdf0e10cSrcweir 
391cdf0e10cSrcweir //--------------------------------------------------------------------------
setFastPropertyValue(sal_Int32 _nHandle,const Any & _rValue)392cdf0e10cSrcweir void OPropertyContainerHelper::setFastPropertyValue(sal_Int32 _nHandle, const Any& _rValue) SAL_THROW( (Exception) )
393cdf0e10cSrcweir {
394cdf0e10cSrcweir 	// get the property somebody is asking for
395cdf0e10cSrcweir 	PropertiesIterator aPos = searchHandle(_nHandle);
396cdf0e10cSrcweir 	if (aPos == m_aProperties.end())
397cdf0e10cSrcweir 	{
398cdf0e10cSrcweir 		OSL_ENSURE( false, "OPropertyContainerHelper::setFastPropertyValue: unknown handle!" );
399cdf0e10cSrcweir 		// should not happen if the derived class has built a correct property set info helper to be used by
400cdf0e10cSrcweir 		// our base class OPropertySetHelper
401cdf0e10cSrcweir 		return;
402cdf0e10cSrcweir 	}
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 	switch (aPos->eLocated)
405cdf0e10cSrcweir 	{
406cdf0e10cSrcweir 		case PropertyDescription::ltHoldMyself:
407cdf0e10cSrcweir 			m_aHoldProperties[aPos->aLocation.nOwnClassVectorIndex] = _rValue;
408cdf0e10cSrcweir 			break;
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 		case PropertyDescription::ltDerivedClassAnyType:
411cdf0e10cSrcweir 			*reinterpret_cast< Any* >(aPos->aLocation.pDerivedClassMember) = _rValue;
412cdf0e10cSrcweir 			break;
413cdf0e10cSrcweir 
414cdf0e10cSrcweir 		case PropertyDescription::ltDerivedClassRealType:
415cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
416cdf0e10cSrcweir 			sal_Bool bSuccess =
417cdf0e10cSrcweir #endif
418cdf0e10cSrcweir 			// copy the data from the to-be-set value
419cdf0e10cSrcweir 			uno_type_assignData(
420cdf0e10cSrcweir 				aPos->aLocation.pDerivedClassMember,		aPos->aProperty.Type.getTypeLibType(),
421cdf0e10cSrcweir 				const_cast< void* >( _rValue.getValue() ),	_rValue.getValueType().getTypeLibType(),
422cdf0e10cSrcweir 				reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
423cdf0e10cSrcweir 				reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
424cdf0e10cSrcweir                 reinterpret_cast< uno_ReleaseFunc >( cpp_release ) );
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 			OSL_ENSURE( bSuccess,
427cdf0e10cSrcweir 				"OPropertyContainerHelper::setFastPropertyValue: ooops .... the value could not be assigned!");
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 			break;
430cdf0e10cSrcweir 	}
431cdf0e10cSrcweir }
432cdf0e10cSrcweir 
433cdf0e10cSrcweir //--------------------------------------------------------------------------
getFastPropertyValue(Any & _rValue,sal_Int32 _nHandle) const434cdf0e10cSrcweir void OPropertyContainerHelper::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
435cdf0e10cSrcweir {
436cdf0e10cSrcweir 	// get the property somebody is asking for
437cdf0e10cSrcweir 	PropertiesIterator aPos = const_cast<OPropertyContainerHelper*>(this)->searchHandle(_nHandle);
438cdf0e10cSrcweir 	if (aPos == m_aProperties.end())
439cdf0e10cSrcweir 	{
440cdf0e10cSrcweir 		OSL_ENSURE( false, "OPropertyContainerHelper::getFastPropertyValue: unknown handle!" );
441cdf0e10cSrcweir 		// should not happen if the derived class has built a correct property set info helper to be used by
442cdf0e10cSrcweir 		// our base class OPropertySetHelper
443cdf0e10cSrcweir 		return;
444cdf0e10cSrcweir 	}
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 	switch (aPos->eLocated)
447cdf0e10cSrcweir 	{
448cdf0e10cSrcweir 		case PropertyDescription::ltHoldMyself:
449cdf0e10cSrcweir 			OSL_ENSURE(aPos->aLocation.nOwnClassVectorIndex < (sal_Int32)m_aHoldProperties.size(),
450cdf0e10cSrcweir 				"OPropertyContainerHelper::convertFastPropertyValue: invalid position !");
451cdf0e10cSrcweir 			_rValue = m_aHoldProperties[aPos->aLocation.nOwnClassVectorIndex];
452cdf0e10cSrcweir 			break;
453cdf0e10cSrcweir 		case PropertyDescription::ltDerivedClassAnyType:
454cdf0e10cSrcweir 			_rValue = *reinterpret_cast<Any*>(aPos->aLocation.pDerivedClassMember);
455cdf0e10cSrcweir 			break;
456cdf0e10cSrcweir 		case PropertyDescription::ltDerivedClassRealType:
457cdf0e10cSrcweir 			_rValue.setValue(aPos->aLocation.pDerivedClassMember, aPos->aProperty.Type);
458cdf0e10cSrcweir 			break;
459cdf0e10cSrcweir 	}
460cdf0e10cSrcweir }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir //--------------------------------------------------------------------------
searchHandle(sal_Int32 _nHandle)463cdf0e10cSrcweir OPropertyContainerHelper::PropertiesIterator OPropertyContainerHelper::searchHandle(sal_Int32 _nHandle)
464cdf0e10cSrcweir {
465cdf0e10cSrcweir 	// search a lower bound
466cdf0e10cSrcweir 	PropertiesIterator aLowerBound = ::std::lower_bound(
467cdf0e10cSrcweir 		m_aProperties.begin(),
468cdf0e10cSrcweir 		m_aProperties.end(),
469cdf0e10cSrcweir 		_nHandle,
470cdf0e10cSrcweir 		PropertyDescriptionHandleCompare());
471cdf0e10cSrcweir 
472cdf0e10cSrcweir 	// check for identity
473cdf0e10cSrcweir 	if ((aLowerBound != m_aProperties.end()) && aLowerBound->aProperty.Handle != _nHandle)
474cdf0e10cSrcweir 		aLowerBound = m_aProperties.end();
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	return aLowerBound;
477cdf0e10cSrcweir }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir //--------------------------------------------------------------------------
getProperty(const::rtl::OUString & _rName) const480cdf0e10cSrcweir const Property& OPropertyContainerHelper::getProperty( const ::rtl::OUString& _rName ) const
481cdf0e10cSrcweir {
482cdf0e10cSrcweir     ConstPropertiesIterator pos = ::std::find_if(
483cdf0e10cSrcweir         m_aProperties.begin(),
484cdf0e10cSrcweir         m_aProperties.end(),
485cdf0e10cSrcweir         PropertyDescriptionNameMatch( _rName )
486cdf0e10cSrcweir     );
487cdf0e10cSrcweir     if ( pos == m_aProperties.end() )
488cdf0e10cSrcweir         throw UnknownPropertyException( _rName, NULL );
489cdf0e10cSrcweir 
490cdf0e10cSrcweir     return pos->aProperty;
491cdf0e10cSrcweir }
492cdf0e10cSrcweir 
493cdf0e10cSrcweir //--------------------------------------------------------------------------
modifyAttributes(sal_Int32 _nHandle,sal_Int32 _nAddAttrib,sal_Int32 _nRemoveAttrib)494cdf0e10cSrcweir void OPropertyContainerHelper::modifyAttributes(sal_Int32 _nHandle, sal_Int32 _nAddAttrib, sal_Int32 _nRemoveAttrib)
495cdf0e10cSrcweir {
496cdf0e10cSrcweir 	// get the property somebody is asking for
497cdf0e10cSrcweir 	PropertiesIterator aPos = searchHandle(_nHandle);
498cdf0e10cSrcweir 	if (aPos == m_aProperties.end())
499cdf0e10cSrcweir 	{
500cdf0e10cSrcweir 		OSL_ENSURE( false, "OPropertyContainerHelper::modifyAttributes: unknown handle!" );
501cdf0e10cSrcweir 		// should not happen if the derived class has built a correct property set info helper to be used by
502cdf0e10cSrcweir 		// our base class OPropertySetHelper
503cdf0e10cSrcweir 		return;
504cdf0e10cSrcweir 	}
505cdf0e10cSrcweir 	aPos->aProperty.Handle |= _nAddAttrib;
506cdf0e10cSrcweir 	aPos->aProperty.Handle &= ~_nRemoveAttrib;
507cdf0e10cSrcweir }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir //--------------------------------------------------------------------------
describeProperties(Sequence<Property> & _rProps) const510cdf0e10cSrcweir void OPropertyContainerHelper::describeProperties(Sequence< Property >& _rProps) const
511cdf0e10cSrcweir {
512cdf0e10cSrcweir 	Sequence< Property > aOwnProps(m_aProperties.size());
513cdf0e10cSrcweir 	Property* pOwnProps = aOwnProps.getArray();
514cdf0e10cSrcweir 
515cdf0e10cSrcweir 	for	(	ConstPropertiesIterator aLoop = m_aProperties.begin();
516cdf0e10cSrcweir 			aLoop != m_aProperties.end();
517cdf0e10cSrcweir 			++aLoop, ++pOwnProps
518cdf0e10cSrcweir 		)
519cdf0e10cSrcweir 	{
520cdf0e10cSrcweir 		pOwnProps->Name = aLoop->aProperty.Name;
521cdf0e10cSrcweir 		pOwnProps->Handle = aLoop->aProperty.Handle;
522cdf0e10cSrcweir 		pOwnProps->Attributes = (sal_Int16)aLoop->aProperty.Attributes;
523cdf0e10cSrcweir 		pOwnProps->Type = aLoop->aProperty.Type;
524cdf0e10cSrcweir 	}
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 	// as our property vector is sorted by handles, not by name, we have to sort aOwnProps
527cdf0e10cSrcweir 	::std::sort(aOwnProps.getArray(), aOwnProps.getArray() + aOwnProps.getLength(), PropertyCompareByName());
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 	// unfortunally the STL merge function does not allow the output range to overlap one of the input ranges,
530cdf0e10cSrcweir 	// so we need an extra sequence
531cdf0e10cSrcweir 	Sequence< Property > aOutput;
532cdf0e10cSrcweir 	aOutput.realloc(_rProps.getLength() + aOwnProps.getLength());
533cdf0e10cSrcweir 	// do the merge
534cdf0e10cSrcweir 	::std::merge(	_rProps.getConstArray(), _rProps.getConstArray() + _rProps.getLength(),			// input 1
535cdf0e10cSrcweir 					aOwnProps.getConstArray(), aOwnProps.getConstArray() + aOwnProps.getLength(),	// input 2
536cdf0e10cSrcweir 					aOutput.getArray(),																// output
537cdf0e10cSrcweir 					PropertyCompareByName()															// compare operator
538cdf0e10cSrcweir 				);
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 	// copy the output
541cdf0e10cSrcweir 	_rProps = aOutput;
542cdf0e10cSrcweir }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir //.........................................................................
545cdf0e10cSrcweir }	// namespace comphelper
546cdf0e10cSrcweir //.........................................................................
547cdf0e10cSrcweir 
548cdf0e10cSrcweir 
549