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 
21 #include <rtl/ustrbuf.hxx>
22 
23 
24 namespace comphelper
25 {
26 
27 
28  using namespace ::com::sun::star::uno;
29  using namespace ::com::sun::star::beans;
30  using namespace ::com::sun::star::lang;
31 
32  namespace
33  {
34  OUString lcl_getUnknownPropertyErrorMessage( const OUString& _rPropertyName )
35  {
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.";
41  }
42  }
43 
45  :OPropertyContainer( _rBHelper )
46  {
47  }
48 
49 
51  {
52  Any aReturn = OPropertyContainer::queryInterface( _rType );
53  if ( !aReturn.hasValue() )
55  return aReturn;
56  }
57 
58 
60 
61 
62  sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName )
63  {
64  // look up the handle for the name
65  ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
66  sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
67 
68  if ( -1 == nHandle )
69  throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
70 
71  return nHandle;
72  }
73 
74 
75  PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName )
76  {
77  return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
78  }
79 
80 
81  Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames )
82  {
83  sal_Int32 nProperties = _rPropertyNames.getLength();
84  Sequence< PropertyState> aStates( nProperties );
85  if ( !nProperties )
86  return aStates;
87 
88 #ifdef DBG_UTIL
89  // precondition: property sequence is sorted (the algorithm below relies on this)
90  {
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!" );
97  }
98 #endif
99 
100  const OUString* pLookup = _rPropertyNames.getConstArray();
101  const OUString* pLookupEnd = pLookup + nProperties;
102  PropertyState* pStates = aStates.getArray();
103 
105  Sequence< Property> aAllProperties = rHelper.getProperties();
106  sal_Int32 nAllProperties = aAllProperties.getLength();
107  const Property* pAllProperties = aAllProperties.getConstArray();
108  const Property* pAllPropertiesEnd = pAllProperties + nAllProperties;
109 
110  osl::MutexGuard aGuard( rBHelper.rMutex );
111  for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
112  {
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 )
119  {
120  *pStates++ = getPropertyState( *pLookup );
121  ++pLookup;
122  }
123  }
124 
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 ) );
129 
130  return aStates;
131  }
132 
133 
134  void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName )
135  {
136  setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
137  }
138 
139 
140  Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName )
141  {
142  Any aDefault;
143  getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
144  return aDefault;
145  }
146 
147 
148  PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle ) const
149  {
150  // simply compare the current and the default value
151  Any aCurrentValue;
152  getFastPropertyValue( aCurrentValue, _nHandle );
153  Any aDefaultValue;
154  getPropertyDefaultByHandle( _nHandle, aDefaultValue );
155 
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)
161  );
162  if ( bEqual )
163  return PropertyState_DEFAULT_VALUE;
164  else
165  return PropertyState_DIRECT_VALUE;
166  }
167 
168 
170  {
171  Any aDefault;
172  getPropertyDefaultByHandle( _nHandle, aDefault );
173  setFastPropertyValue( _nHandle, aDefault );
174  }
175 
176 
177 } // namespace comphelper
178 
179 
180 /* 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