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>
25
26#if OSL_DEBUG_LEVEL > 0
27 #include <cppuhelper/exc_hlp.hxx>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <typeinfo>
30#endif
31#include <com/sun/star/beans/PropertyAttribute.hpp>
32#include <com/sun/star/beans/XPropertySet.hpp>
33#include <com/sun/star/lang/IllegalArgumentException.hpp>
34#include <rtl/ustrbuf.hxx>
35#include <algorithm>
36
37
38namespace comphelper
39{
40
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::beans::XPropertySet;
43 using ::com::sun::star::beans::XPropertySetInfo;
44 using ::com::sun::star::beans::Property;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::uno::Type;
49 using ::com::sun::star::uno::cpp_queryInterface;
50 using ::com::sun::star::uno::cpp_acquire;
51 using ::com::sun::star::uno::cpp_release;
52#if OSL_DEBUG_LEVEL > 0
53 using ::com::sun::star::lang::XServiceInfo;
54#endif
55 using ::com::sun::star::uno::UNO_QUERY;
56
57 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
58
59
60void copyProperties(const Reference<XPropertySet>& _rxSource,
61 const Reference<XPropertySet>& _rxDest)
62{
63 if (!_rxSource.is() || !_rxDest.is())
64 {
65 OSL_FAIL("copyProperties: invalid arguments !");
66 return;
67 }
68
69 Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
70 Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
71
72 const Sequence< Property > aSourceProps = xSourceProps->getProperties();
73 Property aDestProp;
74 for (const Property& rSourceProp : aSourceProps)
75 {
76 if ( xDestProps->hasPropertyByName(rSourceProp.Name) )
77 {
78 try
79 {
80 aDestProp = xDestProps->getPropertyByName(rSourceProp.Name);
81 if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
82 {
83 const Any aSourceValue = _rxSource->getPropertyValue(rSourceProp.Name);
84 if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
85 _rxDest->setPropertyValue(rSourceProp.Name, aSourceValue);
86 }
87 }
88 catch (Exception&)
89 {
90#if OSL_DEBUG_LEVEL > 0
91 TOOLS_WARN_EXCEPTION("comphelper", "Caught exception copying properties");
92
93 OUStringBuffer aBuffer(
94 "::comphelper::copyProperties: could not copy property '"
95 + rSourceProp.Name
96 + "' to the destination set (a '" );
97
98 Reference< XServiceInfo > xSI( _rxDest, UNO_QUERY );
99 if ( xSI.is() )
100 {
101 aBuffer.append( xSI->getImplementationName() );
102 }
103 else
104 {
105 aBuffer.appendAscii( typeid( *_rxDest ).name() );
106 }
107 aBuffer.append( "' implementation).\n" );
108
109 Any aException( ::cppu::getCaughtException() );
110 aBuffer.append( "Caught an exception of type '"
111 + aException.getValueTypeName()
112 + "'" );
113
114 Exception aBaseException;
115 if ( ( aException >>= aBaseException ) && !aBaseException.Message.isEmpty() )
116 {
117 aBuffer.append( ", saying '"
118 + aBaseException.Message
119 + "'" );
120 }
121 aBuffer.append( "." );
122
123 SAL_WARN( "comphelper", aBuffer.makeStringAndClear() );
124#endif
125 }
126 }
127 }
128}
129
130
131bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
132{
133 if (_rxSet.is())
134 {
135 // XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
136 return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
137 }
138 return false;
139}
140
141
142void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
143{
144 // binary search
145 Property aNameProp(_rPropName, 0, Type(), 0);
146 const Property* pResult = std::lower_bound(std::cbegin(_rProps), std::cend(_rProps), aNameProp, PropertyCompareByName());
147
148 if ( pResult != std::cend(_rProps) && pResult->Name == _rPropName)
149 {
150 removeElementAt(_rProps, pResult - std::cbegin(_rProps));
151 }
152}
153
154
155void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
156{
157 // binary search
158 auto [begin, end] = asNonConstRange(seqProps);
159 Property aNameProp(sPropName, 0, Type(), 0);
160 Property* pResult = std::lower_bound(begin, end, aNameProp, PropertyCompareByName());
161
162 if ( (pResult != end) && (pResult->Name == sPropName) )
163 {
164 pResult->Attributes |= nAddAttrib;
165 pResult->Attributes &= ~nRemoveAttrib;
166 }
167}
168
169
170bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
171{
172 bool bModified(false);
173 if (_rCurrentValue.getValue() != _rValueToSet.getValue())
174 {
175 if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
176 {
177 _rConvertedValue = Any( nullptr, _rExpectedType.getTypeLibType() );
178
180 const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
181 const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
182 reinterpret_cast< uno_QueryInterfaceFunc >(
183 cpp_queryInterface),
184 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
185 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
186 )
187 )
188 throw css::lang::IllegalArgumentException();
189 }
190 else
191 _rConvertedValue = _rValueToSet;
192
193 if ( _rCurrentValue != _rConvertedValue )
194 {
195 _rOldValue = _rCurrentValue;
196 bModified = true;
197 }
198 }
199 return bModified;
200}
201
202
203}
204
205
206/* 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 TOOLS_WARN_EXCEPTION(area, stream)
Logs an message along with a nicely formatted version of the current exception.
#define SAL_WARN(area, stream)
@ Exception
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
Definition: property.cxx:131
bool tryPropertyValue(Any &_rConvertedValue, Any &_rOldValue, const Any &_rValueToSet, const Any &_rCurrentValue, const Type &_rExpectedType)
Definition: property.cxx:170
void ModifyPropertyAttributes(Sequence< Property > &seqProps, const OUString &sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
Definition: property.cxx:155
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:60
void RemoveProperty(Sequence< Property > &_rProps, const OUString &_rPropName)
Definition: property.cxx:142
Type
enumrange< T >::Iterator begin(enumrange< T >)
end
std::unique_ptr< char[]> aBuffer