Simplify a bit
[LibreOffice.git] / comphelper / source / property / propertystatecontainer.cxx
blob9ab7f534dc02133daab5a9ca8404b10e72488260
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 <sal/config.h>
22 #include <string_view>
24 #include <comphelper/propertystatecontainer.hxx>
27 namespace comphelper
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::beans;
34 namespace
36 OUString lcl_getUnknownPropertyErrorMessage( std::u16string_view _rPropertyName )
38 // TODO: perhaps it's time to think about resources in the comphelper module?
39 // Would be nice to have localized exception strings (a simply resource file containing
40 // strings only would suffice, and could be realized with a UNO service, so we do not
41 // need the dependency to the Tools project)
42 return OUString::Concat("The property \"") + _rPropertyName + "\" is unknown.";
46 OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper )
47 :OPropertyContainer( _rBHelper )
52 Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType )
54 Any aReturn = OPropertyContainer::queryInterface( _rType );
55 if ( !aReturn.hasValue() )
56 aReturn = OPropertyStateContainer_TBase::queryInterface( _rType );
57 return aReturn;
61 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase )
64 sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName )
66 // look up the handle for the name
67 ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
68 sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
70 if ( -1 == nHandle )
71 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
73 return nHandle;
77 PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName )
79 return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
83 Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames )
85 sal_Int32 nProperties = _rPropertyNames.getLength();
86 Sequence< PropertyState> aStates( nProperties );
87 if ( !nProperties )
88 return aStates;
90 #ifdef DBG_UTIL
91 // precondition: property sequence is sorted (the algorithm below relies on this)
92 OSL_PRECOND(std::is_sorted(_rPropertyNames.begin(), _rPropertyNames.end(),
93 [](auto& lhs, auto& rhs) { return lhs.compareTo(rhs) < 0; }),
94 "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" );
95 #endif
97 PropertyState* pStates = aStates.getArray();
99 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
100 Sequence< Property> aAllProperties = rHelper.getProperties();
101 #ifdef DBG_UTIL
102 OSL_ENSURE(std::is_sorted(aAllProperties.begin(), aAllProperties.end(),
103 [](auto& lhs, auto& rhs)
104 { return lhs.Name.compareTo(rhs.Name) < 0; }),
105 "OPropertyStateContainer::getPropertyStates: all-properties not sorted!");
106 #endif
108 auto it = aAllProperties.begin();
109 const auto end = aAllProperties.end();
110 osl::MutexGuard aGuard( rBHelper.rMutex );
111 for (auto& propName : _rPropertyNames)
113 it = std::find_if(it, end, [&propName](auto& prop) { return prop.Name == propName; });
114 if (it == end)
115 throw UnknownPropertyException(lcl_getUnknownPropertyErrorMessage(propName),
116 static_cast<XPropertyState*>(this));
117 *pStates++ = getPropertyStateByHandle(it->Handle);
120 return aStates;
124 void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName )
126 setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
130 Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName )
132 Any aDefault;
133 getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
134 return aDefault;
138 PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle ) const
140 // simply compare the current and the default value
141 Any aCurrentValue;
142 getFastPropertyValue( aCurrentValue, _nHandle );
143 Any aDefaultValue;
144 getPropertyDefaultByHandle( _nHandle, aDefaultValue );
146 bool bEqual = uno_type_equalData(
147 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
148 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
149 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
150 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
152 if ( bEqual )
153 return PropertyState_DEFAULT_VALUE;
154 else
155 return PropertyState_DIRECT_VALUE;
159 void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
161 Any aDefault;
162 getPropertyDefaultByHandle( _nHandle, aDefault );
163 setFastPropertyValue( _nHandle, aDefault );
167 } // namespace comphelper
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */