CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / property / property.cxx
blob6ab48aa1965b3003ba512696b7cbb5a3f12f9391
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
32 #include <comphelper/property.hxx>
33 #include <comphelper/sequence.hxx>
34 #include <comphelper/types.hxx>
35 #include <osl/diagnose.h>
37 #if OSL_DEBUG_LEVEL > 0
38 #include <rtl/strbuf.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
40 #include <osl/thread.h>
41 #include <com/sun/star/lang/XServiceInfo.hpp>
42 #include <typeinfo>
43 #endif
44 #include <com/sun/star/beans/PropertyAttribute.hpp>
45 #include <com/sun/star/lang/IllegalArgumentException.hpp>
46 #include <com/sun/star/uno/genfunc.h>
48 #include <algorithm>
49 #include <boost/bind.hpp>
51 //.........................................................................
52 namespace comphelper
55 /** === begin UNO using === **/
56 using ::com::sun::star::uno::Reference;
57 using ::com::sun::star::beans::XPropertySet;
58 using ::com::sun::star::beans::XPropertySetInfo;
59 using ::com::sun::star::beans::Property;
60 using ::com::sun::star::uno::Sequence;
61 using ::com::sun::star::uno::Exception;
62 using ::com::sun::star::uno::Any;
63 using ::com::sun::star::uno::Type;
64 using ::com::sun::star::uno::cpp_queryInterface;
65 using ::com::sun::star::uno::cpp_acquire;
66 using ::com::sun::star::uno::cpp_release;
67 #if OSL_DEBUG_LEVEL > 0
68 using ::com::sun::star::lang::XServiceInfo;
69 #endif
70 using ::com::sun::star::uno::UNO_QUERY;
71 /** === end UNO using === **/
72 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
74 //------------------------------------------------------------------
75 void copyProperties(const Reference<XPropertySet>& _rxSource,
76 const Reference<XPropertySet>& _rxDest)
78 if (!_rxSource.is() || !_rxDest.is())
80 OSL_ENSURE(sal_False, "copyProperties: invalid arguments !");
81 return;
84 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
85 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
87 Sequence< Property > aSourceProps = xSourceProps->getProperties();
88 const Property* pSourceProps = aSourceProps.getConstArray();
89 Property aDestProp;
90 for (sal_Int32 i=0; i<aSourceProps.getLength(); ++i, ++pSourceProps)
92 if ( xDestProps->hasPropertyByName(pSourceProps->Name) )
94 try
96 aDestProp = xDestProps->getPropertyByName(pSourceProps->Name);
97 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
99 const Any aSourceValue = _rxSource->getPropertyValue(pSourceProps->Name);
100 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
101 _rxDest->setPropertyValue(pSourceProps->Name, aSourceValue);
104 catch (Exception&)
106 #if OSL_DEBUG_LEVEL > 0
107 ::rtl::OStringBuffer aBuffer;
108 aBuffer.append( "::comphelper::copyProperties: could not copy property '" );
109 aBuffer.append( ::rtl::OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US ) );
110 aBuffer.append( "' to the destination set (a '" );
112 Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
113 if ( xSI.is() )
115 aBuffer.append( ::rtl::OUStringToOString( xSI->getImplementationName(), osl_getThreadTextEncoding() ) );
117 else
119 aBuffer.append( typeid( *_rxDest.get() ).name() );
121 aBuffer.append( "' implementation).\n" );
123 Any aException( ::cppu::getCaughtException() );
124 aBuffer.append( "Caught an exception of type '" );
125 ::rtl::OUString sExceptionType( aException.getValueTypeName() );
126 aBuffer.append( ::rtl::OString( sExceptionType.getStr(), sExceptionType.getLength(), RTL_TEXTENCODING_ASCII_US ) );
127 aBuffer.append( "'" );
129 Exception aBaseException;
130 if ( ( aException >>= aBaseException ) && aBaseException.Message.getLength() )
132 aBuffer.append( ", saying '" );
133 aBuffer.append( ::rtl::OString( aBaseException.Message.getStr(), aBaseException.Message.getLength(), osl_getThreadTextEncoding() ) );
134 aBuffer.append( "'" );
136 aBuffer.append( "." );
138 OSL_ENSURE( sal_False, aBuffer.getStr() );
139 #endif
145 //------------------------------------------------------------------
146 sal_Bool hasProperty(const rtl::OUString& _rName, const Reference<XPropertySet>& _rxSet)
148 if (_rxSet.is())
150 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
151 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
153 return sal_False;
156 //------------------------------------------------------------------
157 bool findProperty(Property& o_rProp,
158 Sequence<Property>& i_seqProps,
159 const ::rtl::OUString& i_rPropName)
161 const Property* pAry(i_seqProps.getConstArray());
162 const sal_Int32 nLen(i_seqProps.getLength());
163 const Property* pRes(
164 std::find_if(pAry,pAry+nLen,
165 boost::bind(PropertyStringEqualFunctor(),
167 boost::cref(i_rPropName))));
168 if( pRes == pAry+nLen )
169 return false;
171 o_rProp = *pRes;
172 return true;
175 //------------------------------------------------------------------
176 void RemoveProperty(Sequence<Property>& _rProps, const rtl::OUString& _rPropName)
178 sal_Int32 nLen = _rProps.getLength();
180 // binaere Suche
181 const Property* pProperties = _rProps.getConstArray();
182 const Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen, _rPropName,PropertyStringLessFunctor());
184 // gefunden ?
185 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == _rPropName) )
187 OSL_ENSURE(pResult->Name.equals(_rPropName), "::RemoveProperty Properties nicht sortiert");
188 removeElementAt(_rProps, pResult - pProperties);
192 //------------------------------------------------------------------
193 void ModifyPropertyAttributes(Sequence<Property>& seqProps, const ::rtl::OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
195 sal_Int32 nLen = seqProps.getLength();
197 // binaere Suche
198 Property* pProperties = seqProps.getArray();
199 Property* pResult = ::std::lower_bound(pProperties, pProperties + nLen,sPropName, PropertyStringLessFunctor());
201 // gefunden ?
202 if ( pResult && (pResult != pProperties + nLen) && (pResult->Name == sPropName) )
204 pResult->Attributes |= nAddAttrib;
205 pResult->Attributes &= ~nRemoveAttrib;
209 //------------------------------------------------------------------
210 sal_Bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
212 sal_Bool bModified(sal_False);
213 if (_rCurrentValue.getValue() != _rValueToSet.getValue())
215 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
217 _rConvertedValue = Any( NULL, _rExpectedType.getTypeLibType() );
219 if ( !uno_type_assignData(
220 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
221 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
222 reinterpret_cast< uno_QueryInterfaceFunc >(
223 cpp_queryInterface),
224 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
225 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
228 throw starlang::IllegalArgumentException();
230 else
231 _rConvertedValue = _rValueToSet;
233 if ( _rCurrentValue != _rConvertedValue )
235 _rOldValue = _rCurrentValue;
236 bModified = sal_True;
239 return bModified;
242 //.........................................................................
244 //.........................................................................