Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / comphelper / source / property / propertystatecontainer.cxx
blobe83d4752a57e6a4f28915382ee266c7a20a7c803
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/propertystatecontainer.hxx>
21 #include <rtl/ustrbuf.hxx>
24 namespace comphelper
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::beans;
30 using namespace ::com::sun::star::lang;
32 namespace
34 OUString lcl_getUnknownPropertyErrorMessage( const OUString& _rPropertyName )
36 // TODO: perhaps it's time to think about resources in the comphelper module?
37 // Would be nice to have localized exception strings (a simply resource file containing
38 // strings only would suffice, and could be realized with a UNO service, so we do not
39 // need the dependency to the Tools project)
40 return "The property \"" + _rPropertyName + "\" is unknown.";
44 OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper )
45 :OPropertyContainer( _rBHelper )
50 Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType )
52 Any aReturn = OPropertyContainer::queryInterface( _rType );
53 if ( !aReturn.hasValue() )
54 aReturn = OPropertyStateContainer_TBase::queryInterface( _rType );
55 return aReturn;
59 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase )
62 sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName )
64 // look up the handle for the name
65 ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
66 sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
68 if ( -1 == nHandle )
69 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
71 return nHandle;
75 PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName )
77 return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
81 Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames )
83 sal_Int32 nProperties = _rPropertyNames.getLength();
84 Sequence< PropertyState> aStates( nProperties );
85 if ( !nProperties )
86 return aStates;
88 #ifdef DBG_UTIL
89 // precondition: property sequence is sorted (the algorithm below relies on this)
91 const OUString* pNames = _rPropertyNames.getConstArray();
92 const OUString* pNamesCompare = pNames + 1;
93 const OUString* pNamesEnd = _rPropertyNames.getConstArray() + _rPropertyNames.getLength();
94 for ( ; pNamesCompare != pNamesEnd; ++pNames, ++pNamesCompare )
95 OSL_PRECOND( pNames->compareTo( *pNamesCompare ) < 0,
96 "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" );
98 #endif
100 const OUString* pLookup = _rPropertyNames.getConstArray();
101 const OUString* pLookupEnd = pLookup + nProperties;
102 PropertyState* pStates = aStates.getArray();
104 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
105 Sequence< Property> aAllProperties = rHelper.getProperties();
106 sal_Int32 nAllProperties = aAllProperties.getLength();
107 const Property* pAllProperties = aAllProperties.getConstArray();
108 const Property* pAllPropertiesEnd = pAllProperties + nAllProperties;
110 osl::MutexGuard aGuard( rBHelper.rMutex );
111 for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
113 #ifdef DBG_UTIL
114 if ( pAllProperties < pAllPropertiesEnd - 1 )
115 OSL_ENSURE( pAllProperties->Name.compareTo( (pAllProperties + 1)->Name ) < 0,
116 "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
117 #endif
118 if ( pAllProperties->Name == *pLookup )
120 *pStates++ = getPropertyState( *pLookup );
121 ++pLookup;
125 if ( pLookup != pLookupEnd )
126 // we run out of properties from the IPropertyArrayHelper, but still have properties to lookup
127 // -> we were asked for a nonexistent property
128 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup ), static_cast< XPropertyState* >( this ) );
130 return aStates;
134 void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName )
136 setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
140 Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName )
142 Any aDefault;
143 getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
144 return aDefault;
148 PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle ) const
150 // simply compare the current and the default value
151 Any aCurrentValue;
152 getFastPropertyValue( aCurrentValue, _nHandle );
153 Any aDefaultValue;
154 getPropertyDefaultByHandle( _nHandle, aDefaultValue );
156 bool bEqual = uno_type_equalData(
157 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
158 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
159 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
160 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
162 if ( bEqual )
163 return PropertyState_DEFAULT_VALUE;
164 else
165 return PropertyState_DIRECT_VALUE;
169 void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
171 Any aDefault;
172 getPropertyDefaultByHandle( _nHandle, aDefault );
173 setFastPropertyValue( _nHandle, aDefault );
177 } // namespace comphelper
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */