1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::beans
;
33 using namespace ::com::sun::star::lang
;
37 OUString
lcl_getUnknownPropertyErrorMessage( std::u16string_view _rPropertyName
)
39 // TODO: perhaps it's time to think about resources in the comphelper module?
40 // Would be nice to have localized exception strings (a simply resource file containing
41 // strings only would suffice, and could be realized with a UNO service, so we do not
42 // need the dependency to the Tools project)
43 return OUString::Concat("The property \"") + _rPropertyName
+ "\" is unknown.";
47 OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper
& _rBHelper
)
48 :OPropertyContainer( _rBHelper
)
53 Any SAL_CALL
OPropertyStateContainer::queryInterface( const Type
& _rType
)
55 Any aReturn
= OPropertyContainer::queryInterface( _rType
);
56 if ( !aReturn
.hasValue() )
57 aReturn
= OPropertyStateContainer_TBase::queryInterface( _rType
);
62 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer
, OPropertyContainer
, OPropertyStateContainer_TBase
)
65 sal_Int32
OPropertyStateContainer::getHandleForName( const OUString
& _rPropertyName
)
67 // look up the handle for the name
68 ::cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
69 sal_Int32 nHandle
= rPH
.getHandleByName( _rPropertyName
);
72 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName
), static_cast< XPropertyState
* >( this ) );
78 PropertyState SAL_CALL
OPropertyStateContainer::getPropertyState( const OUString
& _rPropertyName
)
80 return getPropertyStateByHandle( getHandleForName( _rPropertyName
) );
84 Sequence
< PropertyState
> SAL_CALL
OPropertyStateContainer::getPropertyStates( const Sequence
< OUString
>& _rPropertyNames
)
86 sal_Int32 nProperties
= _rPropertyNames
.getLength();
87 Sequence
< PropertyState
> aStates( nProperties
);
92 // precondition: property sequence is sorted (the algorithm below relies on this)
94 const OUString
* pNames
= _rPropertyNames
.getConstArray();
95 const OUString
* pNamesCompare
= pNames
+ 1;
96 const OUString
* pNamesEnd
= _rPropertyNames
.getConstArray() + _rPropertyNames
.getLength();
97 for ( ; pNamesCompare
!= pNamesEnd
; ++pNames
, ++pNamesCompare
)
98 OSL_PRECOND( pNames
->compareTo( *pNamesCompare
) < 0,
99 "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" );
103 const OUString
* pLookup
= _rPropertyNames
.getConstArray();
104 const OUString
* pLookupEnd
= pLookup
+ nProperties
;
105 PropertyState
* pStates
= aStates
.getArray();
107 cppu::IPropertyArrayHelper
& rHelper
= getInfoHelper();
108 Sequence
< Property
> aAllProperties
= rHelper
.getProperties();
109 sal_Int32 nAllProperties
= aAllProperties
.getLength();
110 const Property
* pAllProperties
= aAllProperties
.getConstArray();
111 const Property
* pAllPropertiesEnd
= pAllProperties
+ nAllProperties
;
113 osl::MutexGuard
aGuard( rBHelper
.rMutex
);
114 for ( ; ( pAllProperties
!= pAllPropertiesEnd
) && ( pLookup
!= pLookupEnd
); ++pAllProperties
)
117 if ( pAllProperties
< pAllPropertiesEnd
- 1 )
118 OSL_ENSURE( pAllProperties
->Name
.compareTo( (pAllProperties
+ 1)->Name
) < 0,
119 "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
121 if ( pAllProperties
->Name
== *pLookup
)
123 *pStates
++ = getPropertyState( *pLookup
);
128 if ( pLookup
!= pLookupEnd
)
129 // we run out of properties from the IPropertyArrayHelper, but still have properties to lookup
130 // -> we were asked for a nonexistent property
131 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup
), static_cast< XPropertyState
* >( this ) );
137 void SAL_CALL
OPropertyStateContainer::setPropertyToDefault( const OUString
& _rPropertyName
)
139 setPropertyToDefaultByHandle( getHandleForName( _rPropertyName
) );
143 Any SAL_CALL
OPropertyStateContainer::getPropertyDefault( const OUString
& _rPropertyName
)
146 getPropertyDefaultByHandle( getHandleForName( _rPropertyName
), aDefault
);
151 PropertyState
OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle
) const
153 // simply compare the current and the default value
155 getFastPropertyValue( aCurrentValue
, _nHandle
);
157 getPropertyDefaultByHandle( _nHandle
, aDefaultValue
);
159 bool bEqual
= uno_type_equalData(
160 const_cast< void* >( aCurrentValue
.getValue() ), aCurrentValue
.getValueType().getTypeLibType(),
161 const_cast< void* >( aDefaultValue
.getValue() ), aDefaultValue
.getValueType().getTypeLibType(),
162 reinterpret_cast< uno_QueryInterfaceFunc
>(cpp_queryInterface
),
163 reinterpret_cast< uno_ReleaseFunc
>(cpp_release
)
166 return PropertyState_DEFAULT_VALUE
;
168 return PropertyState_DIRECT_VALUE
;
172 void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle
)
175 getPropertyDefaultByHandle( _nHandle
, aDefault
);
176 setFastPropertyValue( _nHandle
, aDefault
);
180 } // namespace comphelper
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */