tdf#125181 maxY is 50000 in prstGeom for star24 and star32
[LibreOffice.git] / comphelper / source / property / property.cxx
blob6b7787725d43db707b9ff0fc730c89131d0fa67a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/property.hxx>
21 #include <comphelper/sequence.hxx>
22 #include <osl/diagnose.h>
23 #include <sal/log.hxx>
25 #if OSL_DEBUG_LEVEL > 0
26 #include <cppuhelper/exc_hlp.hxx>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <typeinfo>
29 #endif
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include <rtl/ustrbuf.hxx>
34 #include <algorithm>
37 namespace comphelper
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::beans::XPropertySetInfo;
43 using ::com::sun::star::beans::Property;
44 using ::com::sun::star::uno::Sequence;
45 using ::com::sun::star::uno::Exception;
46 using ::com::sun::star::uno::Any;
47 using ::com::sun::star::uno::Type;
48 using ::com::sun::star::uno::cpp_queryInterface;
49 using ::com::sun::star::uno::cpp_acquire;
50 using ::com::sun::star::uno::cpp_release;
51 #if OSL_DEBUG_LEVEL > 0
52 using ::com::sun::star::lang::XServiceInfo;
53 #endif
54 using ::com::sun::star::uno::UNO_QUERY;
56 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
59 void copyProperties(const Reference<XPropertySet>& _rxSource,
60 const Reference<XPropertySet>& _rxDest)
62 if (!_rxSource.is() || !_rxDest.is())
64 OSL_FAIL("copyProperties: invalid arguments !");
65 return;
68 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
69 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
71 Sequence< Property > aSourceProps = xSourceProps->getProperties();
72 Property aDestProp;
73 for (const Property& rSourceProp : aSourceProps)
75 if ( xDestProps->hasPropertyByName(rSourceProp.Name) )
77 try
79 aDestProp = xDestProps->getPropertyByName(rSourceProp.Name);
80 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
82 const Any aSourceValue = _rxSource->getPropertyValue(rSourceProp.Name);
83 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
84 _rxDest->setPropertyValue(rSourceProp.Name, aSourceValue);
87 catch (Exception&)
89 #if OSL_DEBUG_LEVEL > 0
90 OUStringBuffer aBuffer;
91 aBuffer.append( "::comphelper::copyProperties: could not copy property '" );
92 aBuffer.append(rSourceProp.Name );
93 aBuffer.append( "' to the destination set (a '" );
95 Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
96 if ( xSI.is() )
98 aBuffer.append( xSI->getImplementationName() );
100 else
102 aBuffer.append( OUString::createFromAscii(typeid( *_rxDest.get() ).name()) );
104 aBuffer.append( "' implementation).\n" );
106 Any aException( ::cppu::getCaughtException() );
107 aBuffer.append( "Caught an exception of type '" );
108 aBuffer.append( aException.getValueTypeName() );
109 aBuffer.append( "'" );
111 Exception aBaseException;
112 if ( ( aException >>= aBaseException ) && !aBaseException.Message.isEmpty() )
114 aBuffer.append( ", saying '" );
115 aBuffer.append( aBaseException.Message );
116 aBuffer.append( "'" );
118 aBuffer.append( "." );
120 SAL_WARN( "comphelper", aBuffer.makeStringAndClear() );
121 #endif
128 bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
130 if (_rxSet.is())
132 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
133 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
135 return false;
139 void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
141 sal_Int32 nLen = _rProps.getLength();
143 // binary search
144 const Property* pProperties = _rProps.getConstArray();
145 Property aNameProp(_rPropName, 0, Type(), 0);
146 const Property* pResult = std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
148 if ( pResult != _rProps.end() && pResult->Name == _rPropName )
150 OSL_ENSURE(pResult->Name == _rPropName, "::RemoveProperty Properties not sorted");
151 removeElementAt(_rProps, pResult - pProperties);
156 void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
158 sal_Int32 nLen = seqProps.getLength();
160 // binary search
161 Property* pProperties = seqProps.getArray();
162 Property aNameProp(sPropName, 0, Type(), 0);
163 Property* pResult = std::lower_bound(pProperties, pProperties + nLen, aNameProp, PropertyCompareByName());
165 if ( (pResult != seqProps.end()) && (pResult->Name == sPropName) )
167 pResult->Attributes |= nAddAttrib;
168 pResult->Attributes &= ~nRemoveAttrib;
173 bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
175 bool bModified(false);
176 if (_rCurrentValue.getValue() != _rValueToSet.getValue())
178 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
180 _rConvertedValue = Any( nullptr, _rExpectedType.getTypeLibType() );
182 if ( !uno_type_assignData(
183 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
184 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
185 reinterpret_cast< uno_QueryInterfaceFunc >(
186 cpp_queryInterface),
187 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
188 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
191 throw css::lang::IllegalArgumentException();
193 else
194 _rConvertedValue = _rValueToSet;
196 if ( _rCurrentValue != _rConvertedValue )
198 _rOldValue = _rCurrentValue;
199 bModified = true;
202 return bModified;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */