LCOV - code coverage report
Current view: top level - comphelper/source/property - propertystatecontainer.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 44 65 67.7 %
Date: 2014-11-03 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <comphelper/propertystatecontainer.hxx>
      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           0 :         static 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 an UNO service, so we do not
      39             :             // need the dependency to the Tools project)
      40           0 :             OUStringBuffer sMessage;
      41           0 :             sMessage.appendAscii( "The property \"" );
      42           0 :             sMessage.append( _rPropertyName );
      43           0 :             sMessage.appendAscii( "\" is unknown." );
      44           0 :             return sMessage.makeStringAndClear();
      45             :         }
      46             :     }
      47             : 
      48             : 
      49             :     //= OPropertyStateContainer
      50             : 
      51             : 
      52         924 :     OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper )
      53         924 :         :OPropertyContainer( _rBHelper )
      54             :     {
      55         924 :     }
      56             : 
      57             : 
      58        6682 :     Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
      59             :     {
      60        6682 :         Any aReturn = OPropertyContainer::queryInterface( _rType );
      61        6682 :         if ( !aReturn.hasValue() )
      62        4576 :             aReturn = OPropertyStateContainer_TBase::queryInterface( _rType );
      63        6682 :         return aReturn;
      64             :     }
      65             : 
      66             : 
      67           4 :     IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase )
      68             : 
      69             : 
      70         216 :     sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName )
      71             :     {
      72             :         // look up the handle for the name
      73         216 :         ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
      74         216 :         sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
      75             : 
      76         216 :         if ( -1 == nHandle )
      77           0 :             throw  UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
      78             : 
      79         216 :         return nHandle;
      80             :     }
      81             : 
      82             : 
      83         216 :     PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
      84             :     {
      85         216 :         return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
      86             :     }
      87             : 
      88             : 
      89          24 :     Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames ) throw (UnknownPropertyException, RuntimeException, std::exception)
      90             :     {
      91          24 :         sal_Int32 nProperties = _rPropertyNames.getLength();
      92          24 :         Sequence< PropertyState> aStates( nProperties );
      93          24 :         if ( !nProperties )
      94           0 :             return aStates;
      95             : 
      96             : #ifdef _DEBUG
      97             :         // precondition: property sequence is sorted (the algorithm below relies on this)
      98             :         {
      99             :             const OUString* pNames = _rPropertyNames.getConstArray();
     100             :             const OUString* pNamesCompare = pNames + 1;
     101             :             const OUString* pNamesEnd = _rPropertyNames.getConstArray() + _rPropertyNames.getLength();
     102             :             for ( ; pNamesCompare != pNamesEnd; ++pNames, ++pNamesCompare )
     103             :                 OSL_PRECOND( pNames->compareTo( *pNamesCompare ) < 0,
     104             :                     "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" );
     105             :         }
     106             : #endif
     107             : 
     108          24 :         const OUString* pLookup = _rPropertyNames.getConstArray();
     109          24 :         const OUString* pLookupEnd = pLookup + nProperties;
     110          24 :         PropertyState* pStates = aStates.getArray();
     111             : 
     112          24 :         cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
     113          48 :         Sequence< Property> aAllProperties  = rHelper.getProperties();
     114          24 :         sal_Int32 nAllProperties            = aAllProperties.getLength();
     115          24 :         const  Property* pAllProperties     = aAllProperties.getConstArray();
     116          24 :         const  Property* pAllPropertiesEnd  = pAllProperties + nAllProperties;
     117             : 
     118          48 :         osl::MutexGuard aGuard( rBHelper.rMutex );
     119         702 :         for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
     120             :         {
     121             : #ifdef _DEBUG
     122             :             if ( pAllProperties < pAllPropertiesEnd - 1 )
     123             :                 OSL_ENSURE( pAllProperties->Name.compareTo( (pAllProperties + 1)->Name ) < 0,
     124             :                     "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
     125             : #endif
     126         678 :             if ( pAllProperties->Name.equals( *pLookup ) )
     127             :             {
     128         216 :                 *pStates++ = getPropertyState( *pLookup );
     129         216 :                 ++pLookup;
     130             :             }
     131             :         }
     132             : 
     133          24 :         if ( pLookup != pLookupEnd )
     134             :             // we run out of properties from the IPropertyArrayHelper, but still have properties to lookup
     135             :             // -> we were asked for a nonexistent property
     136           0 :             throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup ), static_cast< XPropertyState* >( this ) );
     137             : 
     138          24 :         return aStates;
     139             :     }
     140             : 
     141             : 
     142           0 :     void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
     143             :     {
     144           0 :         setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
     145           0 :     }
     146             : 
     147             : 
     148           0 :     Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
     149             :     {
     150           0 :         Any aDefault;
     151           0 :         getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
     152           0 :         return aDefault;
     153             :     }
     154             : 
     155             : 
     156         216 :     PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle )
     157             :     {
     158             :         // simply compare the current and the default value
     159         216 :         Any aCurrentValue; getFastPropertyValue( aCurrentValue, _nHandle );
     160         432 :         Any aDefaultValue; getPropertyDefaultByHandle( _nHandle, aDefaultValue );
     161             : 
     162             :         bool bEqual = uno_type_equalData(
     163         432 :                 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
     164         432 :                 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
     165             :                 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
     166             :                 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
     167         648 :             );
     168         216 :         if ( bEqual )
     169         216 :             return PropertyState_DEFAULT_VALUE;
     170             :         else
     171         216 :             return PropertyState_DIRECT_VALUE;
     172             :     }
     173             : 
     174             : 
     175           0 :     void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
     176             :     {
     177           0 :         Any aDefault;
     178           0 :         getPropertyDefaultByHandle( _nHandle, aDefault );
     179           0 :         setFastPropertyValue( _nHandle, aDefault );
     180           0 :     }
     181             : 
     182             : 
     183             : }   // namespace comphelper
     184             : 
     185             : 
     186             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10