Simplify a bit
[LibreOffice.git] / comphelper / source / property / propstate.cxx
blob20deab1963eeed4ae4bb4d869d88cd6b4c9e8038
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/propstate.hxx>
21 #include <cppuhelper/queryinterface.hxx>
22 #include <comphelper/sequence.hxx>
24 namespace comphelper
28 using ::com::sun::star::uno::Type;
29 using ::com::sun::star::uno::Sequence;
30 using ::com::sun::star::lang::XTypeProvider;
31 using ::com::sun::star::uno::Any;
32 using ::com::sun::star::uno::cpp_queryInterface;
33 using ::com::sun::star::uno::cpp_release;
34 using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE;
35 using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
38 // OPropertyStateHelper
41 css::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const css::uno::Type& _rType)
43 css::uno::Any aReturn = OPropertySetHelper2::queryInterface(_rType);
44 // our own ifaces
45 if ( !aReturn.hasValue() )
46 aReturn = ::cppu::queryInterface(_rType, static_cast< css::beans::XPropertyState*>(this));
48 return aReturn;
52 css::uno::Sequence<css::uno::Type> OPropertyStateHelper::getTypes()
54 return {
55 cppu::UnoType<css::beans::XPropertySet>::get(),
56 cppu::UnoType<css::beans::XMultiPropertySet>::get(),
57 cppu::UnoType<css::beans::XFastPropertySet>::get(),
58 cppu::UnoType<css::beans::XPropertySetOption>::get(),
59 cppu::UnoType<css::beans::XPropertyState>::get()};
62 OPropertyStateHelper::OPropertyStateHelper(
63 ::cppu::OBroadcastHelper& rBHlp,
64 ::cppu::IEventNotificationHook *i_pFireEvents)
65 : ::cppu::OPropertySetHelper2(rBHlp, i_pFireEvents) { }
67 OPropertyStateHelper::~OPropertyStateHelper() {}
70 void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const css::uno::Any& aNewValue, const css::uno::Any& aOldValue)
72 fire(&nHandle, &aNewValue, &aOldValue, 1, false);
75 // XPropertyState
77 css::beans::PropertyState SAL_CALL OPropertyStateHelper::getPropertyState(const OUString& _rsName)
79 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
80 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
82 if (nHandle == -1)
83 throw css::beans::UnknownPropertyException(_rsName);
85 return getPropertyStateByHandle(nHandle);
89 void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const OUString& _rsName)
91 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
92 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
94 if (nHandle == -1)
95 throw css::beans::UnknownPropertyException(_rsName);
97 setPropertyToDefaultByHandle(nHandle);
101 css::uno::Any SAL_CALL OPropertyStateHelper::getPropertyDefault(const OUString& _rsName)
103 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
104 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
106 if (nHandle == -1)
107 throw css::beans::UnknownPropertyException(_rsName);
109 return getPropertyDefaultByHandle(nHandle);
113 css::uno::Sequence< css::beans::PropertyState> SAL_CALL OPropertyStateHelper::getPropertyStates(const css::uno::Sequence< OUString >& _rPropertyNames)
115 sal_Int32 nLen = _rPropertyNames.getLength();
116 css::uno::Sequence< css::beans::PropertyState> aRet(nLen);
117 css::beans::PropertyState* pValues = aRet.getArray();
119 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
121 css::uno::Sequence< css::beans::Property> aProps = rHelper.getProperties();
122 auto it = aProps.begin();
123 const auto end = aProps.end();
125 osl::MutexGuard aGuard(rBHelper.rMutex);
126 // Assumption is that both _rPropertyNames and aProps are sorted
127 for (auto& propName : _rPropertyNames)
129 // get the values only for valid properties
130 it = std::find_if(it, end, [&propName](auto& prop) { return prop.Name == propName; });
131 if (it == end)
132 break;
133 *pValues++ = getPropertyStateByHandle(it->Handle);
136 return aRet;
140 css::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
142 // simply compare the current and the default value
143 Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
144 Any aDefaultValue;
145 getFastPropertyValue( aDefaultValue, _nHandle );
147 bool bEqual = uno_type_equalData(
148 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
149 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
150 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
151 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
153 return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
157 void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
159 setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
163 css::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
165 return css::uno::Any();
169 // OStatefulPropertySet
172 OStatefulPropertySet::OStatefulPropertySet()
173 :OPropertyStateHelper( GetBroadcastHelper() )
178 OStatefulPropertySet::~OStatefulPropertySet()
183 Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes()
185 return concatSequences(
186 Sequence {
187 cppu::UnoType<XWeak>::get(),
188 cppu::UnoType<XTypeProvider>::get() },
189 OPropertyStateHelper::getTypes()
193 Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId()
195 return css::uno::Sequence<sal_Int8>();
199 Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType )
201 Any aReturn = OWeakObject::queryInterface( _rType );
202 if ( !aReturn.hasValue() )
203 aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
204 if ( !aReturn.hasValue() )
205 aReturn = OPropertyStateHelper::queryInterface( _rType );
206 return aReturn;
210 void SAL_CALL OStatefulPropertySet::acquire() noexcept
212 ::cppu::OWeakObject::acquire();
216 void SAL_CALL OStatefulPropertySet::release() noexcept
218 ::cppu::OWeakObject::release();
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */