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 
20 #include <comphelper/property.hxx>
21 #include <comphelper/sequence.hxx>
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 
37 namespace 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 
59 void 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 
128 bool 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 
139 void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
140 {
141  sal_Int32 nLen = _rProps.getLength();
142 
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());
147 
148  if ( pResult != _rProps.end() && pResult->Name == _rPropName )
149  {
150  OSL_ENSURE(pResult->Name == _rPropName, "::RemoveProperty Properties not sorted");
151  removeElementAt(_rProps, pResult - pProperties);
152  }
153 }
154 
155 
156 void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
157 {
158  sal_Int32 nLen = seqProps.getLength();
159 
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());
164 
165  if ( (pResult != seqProps.end()) && (pResult->Name == sPropName) )
166  {
167  pResult->Attributes |= nAddAttrib;
168  pResult->Attributes &= ~nRemoveAttrib;
169  }
170 }
171 
172 
173 bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
174 {
175  bool bModified(false);
176  if (_rCurrentValue.getValue() != _rValueToSet.getValue())
177  {
178  if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
179  {
180  _rConvertedValue = Any( nullptr, _rExpectedType.getTypeLibType() );
181 
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)
189  )
190  )
191  throw css::lang::IllegalArgumentException();
192  }
193  else
194  _rConvertedValue = _rValueToSet;
195 
196  if ( _rCurrentValue != _rConvertedValue )
197  {
198  _rOldValue = _rCurrentValue;
199  bModified = true;
200  }
201  }
202  return bModified;
203 }
204 
205 
206 }
207 
208 
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void ModifyPropertyAttributes(Sequence< Property > &seqProps, const OUString &sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
Definition: property.cxx:156
Type
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()
void RemoveProperty(Sequence< Property > &_rProps, const OUString &_rPropName)
Definition: property.cxx:139
Any SAL_CALL getCaughtException()
void removeElementAt(css::uno::Sequence< T > &_rSeq, sal_Int32 _nPos)
remove a specified element from a sequences
Definition: sequence.hxx:87
bool tryPropertyValue(Any &_rConvertedValue, Any &_rOldValue, const Any &_rValueToSet, const Any &_rCurrentValue, const Type &_rExpectedType)
Definition: property.cxx:173
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
Definition: property.cxx:128
#define SAL_WARN(area, stream)
void copyProperties(const Reference< XPropertySet > &_rxSource, const Reference< XPropertySet > &_rxDest)
Definition: property.cxx:59