LibreOffice Module comphelper (master) 1
property.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
22#include <osl/diagnose.h>
23#include <sal/log.hxx>
24
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>
35
36
37namespace comphelper
38{
39
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;
55
56 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
57
58
59void copyProperties(const Reference<XPropertySet>& _rxSource,
60 const Reference<XPropertySet>& _rxDest)
61{
62 if (!_rxSource.is() || !_rxDest.is())
63 {
64 OSL_FAIL("copyProperties: invalid arguments !");
65 return;
66 }
67
68 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
69 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
70
71 const Sequence< Property > aSourceProps = xSourceProps->getProperties();
72 Property aDestProp;
73 for (const Property& rSourceProp : aSourceProps)
74 {
75 if ( xDestProps->hasPropertyByName(rSourceProp.Name) )
76 {
77 try
78 {
79 aDestProp = xDestProps->getPropertyByName(rSourceProp.Name);
80 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
81 {
82 const Any aSourceValue = _rxSource->getPropertyValue(rSourceProp.Name);
83 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
84 _rxDest->setPropertyValue(rSourceProp.Name, aSourceValue);
85 }
86 }
87 catch (Exception&)
88 {
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 '" );
94
95 Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
96 if ( xSI.is() )
97 {
98 aBuffer.append( xSI->getImplementationName() );
99 }
100 else
101 {
102 aBuffer.append( OUString::createFromAscii(typeid( *_rxDest ).name()) );
103 }
104 aBuffer.append( "' implementation).\n" );
105
106 Any aException( ::cppu::getCaughtException() );
107 aBuffer.append( "Caught an exception of type '" );
108 aBuffer.append( aException.getValueTypeName() );
109 aBuffer.append( "'" );
110
111 Exception aBaseException;
112 if ( ( aException >>= aBaseException ) && !aBaseException.Message.isEmpty() )
113 {
114 aBuffer.append( ", saying '" );
115 aBuffer.append( aBaseException.Message );
116 aBuffer.append( "'" );
117 }
118 aBuffer.append( "." );
119
120 SAL_WARN( "comphelper", aBuffer.makeStringAndClear() );
121#endif
122 }
123 }
124 }
125}
126
127
128bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
129{
130 if (_rxSet.is())
131 {
132 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
133 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
134 }
135 return false;
136}
137
138
139void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
140{
141 // binary search
142 Property aNameProp(_rPropName, 0, Type(), 0);
143 const Property* pResult = std::lower_bound(std::cbegin(_rProps), std::cend(_rProps), aNameProp, PropertyCompareByName());
144
145 if ( pResult != std::cend(_rProps) && pResult->Name == _rPropName)
146 {
147 removeElementAt(_rProps, pResult - std::cbegin(_rProps));
148 }
149}
150
151
152void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
153{
154 // binary search
155 auto [begin, end] = asNonConstRange(seqProps);
156 Property aNameProp(sPropName, 0, Type(), 0);
157 Property* pResult = std::lower_bound(begin, end, aNameProp, PropertyCompareByName());
158
159 if ( (pResult != end) && (pResult->Name == sPropName) )
160 {
161 pResult->Attributes |= nAddAttrib;
162 pResult->Attributes &= ~nRemoveAttrib;
163 }
164}
165
166
167bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
168{
169 bool bModified(false);
170 if (_rCurrentValue.getValue() != _rValueToSet.getValue())
171 {
172 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
173 {
174 _rConvertedValue = Any( nullptr, _rExpectedType.getTypeLibType() );
175
177 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
178 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
179 reinterpret_cast< uno_QueryInterfaceFunc >(
180 cpp_queryInterface),
181 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
182 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
183 )
184 )
185 throw css::lang::IllegalArgumentException();
186 }
187 else
188 _rConvertedValue = _rValueToSet;
189
190 if ( _rCurrentValue != _rConvertedValue )
191 {
192 _rOldValue = _rCurrentValue;
193 bModified = true;
194 }
195 }
196 return bModified;
197}
198
199
200}
201
202
203/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Bool SAL_CALL uno_type_assignData(void *pDest, typelib_TypeDescriptionReference *pDestType, void *pSource, typelib_TypeDescriptionReference *pSourceType, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
#define SAL_WARN(area, stream)
@ Exception
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
Definition: property.cxx:128
bool tryPropertyValue(Any &_rConvertedValue, Any &_rOldValue, const Any &_rValueToSet, const Any &_rCurrentValue, const Type &_rExpectedType)
Definition: property.cxx:167
void ModifyPropertyAttributes(Sequence< Property > &seqProps, const OUString &sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
Definition: property.cxx:152
void removeElementAt(css::uno::Sequence< T > &_rSeq, sal_Int32 _nPos)
remove a specified element from a sequences
Definition: sequence.hxx:88
void copyProperties(const Reference< XPropertySet > &_rxSource, const Reference< XPropertySet > &_rxDest)
Definition: property.cxx:59
void RemoveProperty(Sequence< Property > &_rProps, const OUString &_rPropName)
Definition: property.cxx:139
Type
enumrange< T >::Iterator begin(enumrange< T >)
end
std::unique_ptr< char[]> aBuffer