Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/comphelper/source/property/property.cxx
Line
Count
Source
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
#include <comphelper/diagnose_ex.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
38
namespace 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
60
void copyProperties(const Reference<XPropertySet>& _rxSource,
61
                    const Reference<XPropertySet>& _rxDest)
62
0
{
63
0
    if (!_rxSource.is() || !_rxDest.is())
64
0
    {
65
0
        OSL_FAIL("copyProperties: invalid arguments !");
66
0
        return;
67
0
    }
68
69
0
    Reference< XPropertySetInfo > xSourceProps = _rxSource->getPropertySetInfo();
70
0
    Reference< XPropertySetInfo > xDestProps = _rxDest->getPropertySetInfo();
71
72
0
    const Sequence< Property > aSourceProps = xSourceProps->getProperties();
73
0
    Property aDestProp;
74
0
    for (const Property& rSourceProp : aSourceProps)
75
0
    {
76
0
        if ( xDestProps->hasPropertyByName(rSourceProp.Name) )
77
0
        {
78
0
            try
79
0
            {
80
0
                aDestProp = xDestProps->getPropertyByName(rSourceProp.Name);
81
0
                if (0 == (aDestProp.Attributes & PropertyAttribute::READONLY) )
82
0
                {
83
0
                    const Any aSourceValue = _rxSource->getPropertyValue(rSourceProp.Name);
84
0
                    if ( 0 != (aDestProp.Attributes & PropertyAttribute::MAYBEVOID) || aSourceValue.hasValue() )
85
0
                        _rxDest->setPropertyValue(rSourceProp.Name, aSourceValue);
86
0
                }
87
0
            }
88
0
            catch (Exception&)
89
0
            {
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
0
            }
126
0
        }
127
0
    }
128
0
}
129
130
131
bool hasProperty(const OUString& _rName, const Reference<XPropertySet>& _rxSet)
132
0
{
133
0
    if (_rxSet.is())
134
0
    {
135
        //  XPropertySetInfoRef xInfo(rxSet->getPropertySetInfo());
136
0
        return _rxSet->getPropertySetInfo()->hasPropertyByName(_rName);
137
0
    }
138
0
    return false;
139
0
}
140
141
142
void RemoveProperty(Sequence<Property>& _rProps, const OUString& _rPropName)
143
0
{
144
    // binary search
145
0
    Property aNameProp(_rPropName, 0, Type(), 0);
146
0
    const Property* pResult = std::lower_bound(std::cbegin(_rProps), std::cend(_rProps), aNameProp, PropertyCompareByName());
147
148
0
    if ( pResult != std::cend(_rProps) && pResult->Name == _rPropName)
149
0
    {
150
0
        removeElementAt(_rProps, pResult - std::cbegin(_rProps));
151
0
    }
152
0
}
153
154
155
void ModifyPropertyAttributes(Sequence<Property>& seqProps, const OUString& sPropName, sal_Int16 nAddAttrib, sal_Int16 nRemoveAttrib)
156
0
{
157
    // binary search
158
0
    auto [begin, end] = asNonConstRange(seqProps);
159
0
    Property aNameProp(sPropName, 0, Type(), 0);
160
0
    Property* pResult = std::lower_bound(begin, end, aNameProp, PropertyCompareByName());
161
162
0
    if ( (pResult != end) && (pResult->Name == sPropName) )
163
0
    {
164
0
        pResult->Attributes |= nAddAttrib;
165
0
        pResult->Attributes &= ~nRemoveAttrib;
166
0
    }
167
0
}
168
169
170
bool tryPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rValueToSet, const Any& _rCurrentValue, const Type& _rExpectedType)
171
0
{
172
0
    bool bModified(false);
173
0
    if (_rCurrentValue.getValue() != _rValueToSet.getValue())
174
0
    {
175
0
        if ( _rValueToSet.hasValue() && ( !_rExpectedType.equals( _rValueToSet.getValueType() ) ) )
176
0
        {
177
0
            _rConvertedValue = Any( nullptr, _rExpectedType.getTypeLibType() );
178
179
0
            if  ( !uno_type_assignData(
180
0
                    const_cast< void* >( _rConvertedValue.getValue() ), _rConvertedValue.getValueType().getTypeLibType(),
181
0
                    const_cast< void* >( _rValueToSet.getValue() ), _rValueToSet.getValueType().getTypeLibType(),
182
0
                    reinterpret_cast< uno_QueryInterfaceFunc >(
183
0
                        cpp_queryInterface),
184
0
                    reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
185
0
                    reinterpret_cast< uno_ReleaseFunc >(cpp_release)
186
0
                  )
187
0
                )
188
0
                throw css::lang::IllegalArgumentException();
189
0
        }
190
0
        else
191
0
            _rConvertedValue = _rValueToSet;
192
193
0
        if ( _rCurrentValue != _rConvertedValue )
194
0
        {
195
0
            _rOldValue = _rCurrentValue;
196
0
            bModified = true;
197
0
        }
198
0
    }
199
0
    return bModified;
200
0
}
201
202
203
}
204
205
206
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */