LibreOffice Module comphelper (master)  1
propertystatecontainer.cxx
Go to the documentation of this file.
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 <sal/config.h>
21 
22 #include <string_view>
23 
25 
26 
27 namespace comphelper
28 {
29 
30 
31  using namespace ::com::sun::star::uno;
32  using namespace ::com::sun::star::beans;
33  using namespace ::com::sun::star::lang;
34 
35  namespace
36  {
37  OUString lcl_getUnknownPropertyErrorMessage( std::u16string_view _rPropertyName )
38  {
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.";
44  }
45  }
46 
48  :OPropertyContainer( _rBHelper )
49  {
50  }
51 
52 
54  {
55  Any aReturn = OPropertyContainer::queryInterface( _rType );
56  if ( !aReturn.hasValue() )
58  return aReturn;
59  }
60 
61 
63 
64 
65  sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName )
66  {
67  // look up the handle for the name
68  ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
69  sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
70 
71  if ( -1 == nHandle )
72  throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
73 
74  return nHandle;
75  }
76 
77 
78  PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName )
79  {
80  return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
81  }
82 
83 
84  Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames )
85  {
86  sal_Int32 nProperties = _rPropertyNames.getLength();
87  Sequence< PropertyState> aStates( nProperties );
88  if ( !nProperties )
89  return aStates;
90 
91 #ifdef DBG_UTIL
92  // precondition: property sequence is sorted (the algorithm below relies on this)
93  {
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!" );
100  }
101 #endif
102 
103  const OUString* pLookup = _rPropertyNames.getConstArray();
104  const OUString* pLookupEnd = pLookup + nProperties;
105  PropertyState* pStates = aStates.getArray();
106 
108  Sequence< Property> aAllProperties = rHelper.getProperties();
109  sal_Int32 nAllProperties = aAllProperties.getLength();
110  const Property* pAllProperties = aAllProperties.getConstArray();
111  const Property* pAllPropertiesEnd = pAllProperties + nAllProperties;
112 
113  osl::MutexGuard aGuard( rBHelper.rMutex );
114  for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
115  {
116 #ifdef DBG_UTIL
117  if ( pAllProperties < pAllPropertiesEnd - 1 )
118  OSL_ENSURE( pAllProperties->Name.compareTo( (pAllProperties + 1)->Name ) < 0,
119  "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
120 #endif
121  if ( pAllProperties->Name == *pLookup )
122  {
123  *pStates++ = getPropertyState( *pLookup );
124  ++pLookup;
125  }
126  }
127 
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 ) );
132 
133  return aStates;
134  }
135 
136 
137  void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName )
138  {
139  setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
140  }
141 
142 
143  Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName )
144  {
145  Any aDefault;
146  getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
147  return aDefault;
148  }
149 
150 
151  PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle ) const
152  {
153  // simply compare the current and the default value
154  Any aCurrentValue;
155  getFastPropertyValue( aCurrentValue, _nHandle );
156  Any aDefaultValue;
157  getPropertyDefaultByHandle( _nHandle, aDefaultValue );
158 
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)
164  );
165  if ( bEqual )
166  return PropertyState_DEFAULT_VALUE;
167  else
168  return PropertyState_DIRECT_VALUE;
169  }
170 
171 
173  {
174  Any aDefault;
175  getPropertyDefaultByHandle( _nHandle, aDefault );
176  setFastPropertyValue( _nHandle, aDefault );
177  }
178 
179 
180 } // namespace comphelper
181 
182 
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Bool SAL_CALL uno_type_equalData(void *pVal1, typelib_TypeDescriptionReference *pVal1Type, void *pVal2, typelib_TypeDescriptionReference *pVal2Type, uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Type
virtual void SAL_CALL setPropertyToDefault(const OUString &PropertyName) override
virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties()=0
virtual void SAL_CALL setFastPropertyValue(sal_Int32 nHandle, const css::uno::Any &rValue) SAL_OVERRIDE
sal_Int32 nProperties
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &_rType) override
#define IMPLEMENT_FORWARD_XTYPEPROVIDER2(classname, baseclass1, baseclass2)
Definition: uno3.hxx:136
virtual css::beans::PropertyState SAL_CALL getPropertyState(const OUString &PropertyName) override
an OPropertySetHelper implementation which is just a simple container for properties represented by c...
OPropertyStateContainer(::cppu::OBroadcastHelper &_rBHelper)
ctor
virtual void getPropertyDefaultByHandle(sal_Int32 _nHandle, css::uno::Any &_rDefault) const =0
get the default value for the property denoted by the given handle
void setPropertyToDefaultByHandle(sal_Int32 _nHandle)
set the property denoted by the given handle to its default value
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
sal_Int32 nHandle
sal_Int32 getHandleForName(const OUString &_rPropertyName)
returns the handle for the given name
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) SAL_OVERRIDE
css::beans::PropertyState getPropertyStateByHandle(sal_Int32 _nHandle) const
get the PropertyState of the property denoted by the given handle
virtual IPropertyArrayHelper &SAL_CALL getInfoHelper()=0
OBroadcastHelper & rBHelper
virtual css::uno::Any SAL_CALL getPropertyDefault(const OUString &aPropertyName) override
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &rType) SAL_OVERRIDE
virtual css::uno::Sequence< css::beans::PropertyState > SAL_CALL getPropertyStates(const css::uno::Sequence< OUString > &aPropertyName) override
helper implementation for components which have properties with a default
virtual sal_Int32 SAL_CALL getHandleByName(const ::rtl::OUString &rPropertyName)=0