LCOV - code coverage report
Current view: top level - svl/source/items - itemprop.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 175 181 96.7 %
Date: 2015-06-13 12:38:46 Functions: 35 37 94.6 %
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             : 
      21             : #include <svl/itemprop.hxx>
      22             : #include <svl/itempool.hxx>
      23             : #include <svl/itemset.hxx>
      24             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      25             : #include <boost/scoped_ptr.hpp>
      26             : #include <unordered_map>
      27             : /*
      28             :  * UNO III Implementation
      29             :  */
      30             : using namespace com::sun::star;
      31             : using namespace com::sun::star::beans;
      32             : using namespace com::sun::star::lang;
      33             : using namespace com::sun::star::uno;
      34             : 
      35             : struct equalOUString
      36             : {
      37     7538722 :   bool operator()(const OUString& r1, const OUString&  r2) const
      38             :   {
      39     7538722 :     return r1.equals( r2 );
      40             :   }
      41             : };
      42             : 
      43             : typedef std::unordered_map< OUString,
      44             :                             SfxItemPropertySimpleEntry,
      45             :                             OUStringHash,
      46             :                             equalOUString > SfxItemPropertyHashMap_t;
      47             : 
      48       48164 : class SfxItemPropertyMap_Impl : public SfxItemPropertyHashMap_t
      49             : {
      50             : public:
      51             :     mutable uno::Sequence< beans::Property > m_aPropSeq;
      52             : 
      53       46912 :     SfxItemPropertyMap_Impl(){}
      54             :     SfxItemPropertyMap_Impl( const SfxItemPropertyMap_Impl* pSource );
      55             : };
      56             : 
      57        1466 : SfxItemPropertyMap_Impl::SfxItemPropertyMap_Impl( const SfxItemPropertyMap_Impl* pSource )
      58             : {
      59        1466 :     this->SfxItemPropertyHashMap_t::operator=( *pSource );
      60        1466 :     m_aPropSeq = pSource->m_aPropSeq;
      61        1466 : }
      62             : 
      63       46912 : SfxItemPropertyMap::SfxItemPropertyMap( const SfxItemPropertyMapEntry* pEntries ) :
      64       46912 :     m_pImpl( new SfxItemPropertyMap_Impl )
      65             : {
      66     1697202 :     while( !pEntries->aName.isEmpty() )
      67             :     {
      68     1603378 :         (*m_pImpl) [ pEntries->aName ] = pEntries;
      69     1603378 :         ++pEntries;
      70             :     }
      71       46912 : }
      72             : 
      73        1466 : SfxItemPropertyMap::SfxItemPropertyMap( const SfxItemPropertyMap& rSource ) :
      74        1466 :     m_pImpl( new SfxItemPropertyMap_Impl( rSource.m_pImpl ) )
      75             : {
      76        1466 : }
      77             : 
      78       48164 : SfxItemPropertyMap::~SfxItemPropertyMap()
      79             : {
      80       48164 :     delete m_pImpl;
      81       48164 : }
      82             : 
      83     6982147 : const SfxItemPropertySimpleEntry* SfxItemPropertyMap::getByName( const OUString &rName ) const
      84             : {
      85     6982147 :     SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
      86     6982147 :     if( aIter == m_pImpl->end() )
      87      255649 :         return 0;
      88     6726498 :     return &aIter->second;
      89             : }
      90             : 
      91       48448 : uno::Sequence<beans::Property> SfxItemPropertyMap::getProperties() const
      92             : {
      93       48448 :     if( !m_pImpl->m_aPropSeq.getLength() )
      94             :     {
      95         543 :         m_pImpl->m_aPropSeq.realloc( m_pImpl->size() );
      96         543 :         beans::Property* pPropArray = m_pImpl->m_aPropSeq.getArray();
      97         543 :         sal_uInt32 n = 0;
      98         543 :         SfxItemPropertyHashMap_t::const_iterator aIt = m_pImpl->begin();
      99       53009 :         while( aIt != m_pImpl->end() )
     100             :         //for ( const SfxItemPropertyMap *pMap = _pMap; pMap->pName; ++pMap )
     101             :         {
     102       51923 :             const SfxItemPropertySimpleEntry* pEntry = &(*aIt).second;
     103       51923 :             pPropArray[n].Name = (*aIt).first;
     104       51923 :             pPropArray[n].Handle = pEntry->nWID;
     105       51923 :             pPropArray[n].Type = pEntry->aType;
     106       51923 :             pPropArray[n].Attributes =
     107       51923 :                 sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
     108       51923 :             n++;
     109       51923 :             ++aIt;
     110             :         }
     111             :     }
     112             : 
     113       48448 :     return m_pImpl->m_aPropSeq;
     114             : }
     115             : 
     116        1785 : beans::Property SfxItemPropertyMap::getPropertyByName( const OUString & rName ) const
     117             :     throw( beans::UnknownPropertyException )
     118             : {
     119        1785 :     SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
     120        1785 :     if( aIter == m_pImpl->end() )
     121           3 :         throw UnknownPropertyException();
     122        1782 :     const SfxItemPropertySimpleEntry* pEntry = &aIter->second;
     123        1782 :     beans::Property aProp;
     124        1782 :     aProp.Name = rName;
     125        1782 :     aProp.Handle = pEntry->nWID;
     126        1782 :     aProp.Type = pEntry->aType;
     127        1782 :     aProp.Attributes = sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
     128        1782 :     return aProp;
     129             : }
     130             : 
     131     1252534 : bool SfxItemPropertyMap::hasPropertyByName( const OUString& rName ) const
     132             : {
     133     1252534 :     SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
     134     1252534 :     return aIter != m_pImpl->end();
     135             : }
     136             : 
     137       37339 : void SfxItemPropertyMap::mergeProperties( const uno::Sequence< beans::Property >& rPropSeq )
     138             : {
     139       37339 :     const beans::Property* pPropArray = rPropSeq.getConstArray();
     140       37339 :     sal_uInt32 nElements = rPropSeq.getLength();
     141     6297758 :     for( sal_uInt32 nElement = 0; nElement < nElements; ++nElement )
     142             :     {
     143             :         SfxItemPropertySimpleEntry aTemp(
     144     6260419 :             sal::static_int_cast< sal_Int16 >( pPropArray[nElement].Handle ), //nWID
     145     6260419 :             pPropArray[nElement].Type, //aType
     146     6260419 :             pPropArray[nElement].Attributes, //nFlags
     147    18781257 :             0 ); //nMemberId
     148     6260419 :         (*m_pImpl)[pPropArray[nElement].Name] = aTemp;
     149     6260419 :     }
     150       37339 : }
     151             : 
     152       41262 : PropertyEntryVector_t SfxItemPropertyMap::getPropertyEntries() const
     153             : {
     154       41262 :     PropertyEntryVector_t aRet;
     155       41262 :     aRet.reserve(m_pImpl->size());
     156             : 
     157       41262 :     SfxItemPropertyHashMap_t::const_iterator aIt = m_pImpl->begin();
     158     2900266 :     while( aIt != m_pImpl->end() )
     159             :     {
     160     2817742 :         const SfxItemPropertySimpleEntry* pEntry = &(*aIt).second;
     161     2817742 :         aRet.push_back( SfxItemPropertyNamedEntry( (*aIt).first, * pEntry ) );
     162     2817742 :         ++aIt;
     163             :     }
     164       41262 :     return aRet;
     165             : }
     166             : 
     167           0 : sal_uInt32 SfxItemPropertyMap::getSize() const
     168             : {
     169           0 :     return m_pImpl->size();
     170             : }
     171             : 
     172        8157 : SfxItemPropertySet::~SfxItemPropertySet()
     173             : {
     174        8157 : }
     175             : 
     176       73472 : void SfxItemPropertySet::getPropertyValue( const SfxItemPropertySimpleEntry& rEntry,
     177             :             const SfxItemSet& rSet, Any& rAny ) const
     178             :                         throw(RuntimeException)
     179             : {
     180             :     // get the SfxPoolItem
     181       73472 :     const SfxPoolItem* pItem = 0;
     182       73472 :     SfxItemState eState = rSet.GetItemState( rEntry.nWID, true, &pItem );
     183       73472 :     if(SfxItemState::SET != eState && SFX_WHICH_MAX > rEntry.nWID )
     184        7536 :         pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
     185             :     // return item values as uno::Any
     186       73472 :     if(eState >= SfxItemState::DEFAULT && pItem)
     187             :     {
     188       73467 :         pItem->QueryValue( rAny, rEntry.nMemberId );
     189             :     }
     190             :     else
     191             :     {
     192           5 :         SfxItemSet aSet(*rSet.GetPool(), rEntry.nWID, rEntry.nWID);
     193           5 :         if(0 == (rEntry.nFlags & PropertyAttribute::MAYBEVOID))
     194             :             throw RuntimeException(
     195           2 :                     "Property not found in ItemSet but not MAYBEVOID?", 0);
     196             :     }
     197             : 
     198             : 
     199             :     // convert general SfxEnumItem values to specific values
     200       85445 :     if( rEntry.aType.getTypeClass() == TypeClass_ENUM &&
     201       11975 :          rAny.getValueTypeClass() == TypeClass_LONG )
     202             :     {
     203          21 :         sal_Int32 nTmp = *static_cast<sal_Int32 const *>(rAny.getValue());
     204          21 :         rAny.setValue( &nTmp, rEntry.aType );
     205             :     }
     206       73470 : }
     207             : 
     208        1233 : void SfxItemPropertySet::getPropertyValue( const OUString &rName,
     209             :                                            const SfxItemSet& rSet, Any& rAny ) const
     210             :     throw(RuntimeException, UnknownPropertyException)
     211             : {
     212             :     // detect which-id
     213        1233 :     const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
     214        1233 :     if ( !pEntry )
     215           0 :         throw UnknownPropertyException();
     216        1233 :     getPropertyValue( *pEntry,rSet, rAny );
     217        1233 : }
     218             : 
     219          44 : Any SfxItemPropertySet::getPropertyValue( const OUString &rName,
     220             :                                           const SfxItemSet& rSet ) const
     221             :     throw(RuntimeException, UnknownPropertyException)
     222             : {
     223          44 :     Any aVal;
     224          44 :     getPropertyValue( rName,rSet, aVal );
     225          44 :     return aVal;
     226             : }
     227             : 
     228      724743 : void SfxItemPropertySet::setPropertyValue( const SfxItemPropertySimpleEntry& rEntry,
     229             :                                            const Any& aVal,
     230             :                                            SfxItemSet& rSet ) const
     231             :     throw(RuntimeException,
     232             :           IllegalArgumentException)
     233             : {
     234             :     // get the SfxPoolItem
     235      724743 :     const SfxPoolItem* pItem = 0;
     236      724743 :     boost::scoped_ptr<SfxPoolItem> pNewItem;
     237      724743 :     SfxItemState eState = rSet.GetItemState( rEntry.nWID, true, &pItem );
     238      724743 :     if(SfxItemState::SET != eState && SFX_WHICH_MAX > rEntry.nWID )
     239      286669 :         pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
     240             :     //maybe there's another way to find an Item
     241      724743 :     if(eState < SfxItemState::DEFAULT)
     242             :     {
     243           9 :         SfxItemSet aSet(*rSet.GetPool(), rEntry.nWID, rEntry.nWID);
     244             :     }
     245      724743 :     if(!pNewItem && pItem)
     246             :     {
     247      679066 :         pNewItem.reset(pItem->Clone());
     248             :     }
     249      724743 :     if(pNewItem)
     250             :     {
     251      679066 :         if( !pNewItem->PutValue( aVal, rEntry.nMemberId ) )
     252             :         {
     253          78 :             throw IllegalArgumentException();
     254             :         }
     255             :         // apply new item
     256      678988 :         rSet.Put( *pNewItem, rEntry.nWID );
     257      724743 :     }
     258      724665 : }
     259             : 
     260      103075 : void SfxItemPropertySet::setPropertyValue( const OUString &rName,
     261             :                                            const Any& aVal,
     262             :                                            SfxItemSet& rSet ) const
     263             :     throw(RuntimeException,
     264             :           IllegalArgumentException,
     265             :           UnknownPropertyException)
     266             : {
     267      103075 :     const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
     268      103075 :     if ( !pEntry )
     269             :     {
     270           0 :         throw UnknownPropertyException();
     271             :     }
     272      103075 :     setPropertyValue(*pEntry, aVal, rSet);
     273      103075 : }
     274             : 
     275     1948200 : PropertyState SfxItemPropertySet::getPropertyState(const SfxItemPropertySimpleEntry& rEntry, const SfxItemSet& rSet) const
     276             :     throw()
     277             : {
     278     1948200 :     PropertyState eRet = PropertyState_DIRECT_VALUE;
     279     1948200 :     sal_uInt16 nWhich = rEntry.nWID;
     280             : 
     281             :     // Get item state
     282     1948200 :     SfxItemState eState = rSet.GetItemState( nWhich, false );
     283             :     // Return item value as UnoAny
     284     1948200 :     if(eState == SfxItemState::DEFAULT)
     285     1782742 :         eRet = PropertyState_DEFAULT_VALUE;
     286      165458 :     else if(eState < SfxItemState::DEFAULT)
     287       68210 :         eRet = PropertyState_AMBIGUOUS_VALUE;
     288     1948200 :     return eRet;
     289             : }
     290             : 
     291        1900 : PropertyState   SfxItemPropertySet::getPropertyState(const OUString& rName, const SfxItemSet& rSet) const
     292             :     throw(UnknownPropertyException)
     293             : {
     294        1900 :     PropertyState eRet = PropertyState_DIRECT_VALUE;
     295             : 
     296             :     // Get WhichId
     297        1900 :     const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
     298        1900 :     if( !pEntry || !pEntry->nWID )
     299             :     {
     300           0 :         throw UnknownPropertyException();
     301             :     }
     302        1900 :     sal_uInt16 nWhich = pEntry->nWID;
     303             : 
     304             :     // Get item state
     305        1900 :     SfxItemState eState = rSet.GetItemState(nWhich, false);
     306             :     // Return item value as UnoAny
     307        1900 :     if(eState == SfxItemState::DEFAULT)
     308        1824 :         eRet = PropertyState_DEFAULT_VALUE;
     309          76 :     else if(eState < SfxItemState::DEFAULT)
     310           0 :         eRet = PropertyState_AMBIGUOUS_VALUE;
     311        1900 :     return eRet;
     312             : }
     313             : 
     314        8049 : Reference<XPropertySetInfo> SfxItemPropertySet::getPropertySetInfo() const
     315             : {
     316        8049 :     if( !m_xInfo.is() )
     317         838 :         m_xInfo = new SfxItemPropertySetInfo( m_aMap );
     318        8049 :     return m_xInfo;
     319             : }
     320             : 
     321             : struct SfxItemPropertySetInfo_Impl
     322             : {
     323             :     SfxItemPropertyMap*         m_pOwnMap;
     324             : };
     325             : 
     326        1466 : SfxItemPropertySetInfo::SfxItemPropertySetInfo(const SfxItemPropertyMap &rMap )
     327        1466 :     :  m_pImpl( new SfxItemPropertySetInfo_Impl )
     328             : {
     329        1466 :     m_pImpl->m_pOwnMap = new SfxItemPropertyMap( rMap );
     330        1466 : }
     331             : 
     332          77 : SfxItemPropertySetInfo::SfxItemPropertySetInfo(const SfxItemPropertyMapEntry *pEntries )
     333          77 :     : m_pImpl( new SfxItemPropertySetInfo_Impl )
     334             : {
     335          77 :     m_pImpl->m_pOwnMap = new SfxItemPropertyMap( pEntries );
     336          77 : }
     337             : 
     338       48430 : Sequence< Property > SAL_CALL SfxItemPropertySetInfo::getProperties(  )
     339             :     throw(RuntimeException, std::exception)
     340             : {
     341       48430 :     return m_pImpl->m_pOwnMap->getProperties();
     342             : }
     343             : 
     344        4596 : SfxItemPropertySetInfo::~SfxItemPropertySetInfo()
     345             : {
     346        1532 :     delete m_pImpl->m_pOwnMap;
     347        1532 :     delete m_pImpl;
     348        3064 : }
     349             : 
     350        1775 : Property SAL_CALL SfxItemPropertySetInfo::getPropertyByName( const OUString& rName )
     351             :     throw(UnknownPropertyException, RuntimeException, std::exception)
     352             : {
     353        1775 :     return m_pImpl->m_pOwnMap->getPropertyByName( rName );
     354             : }
     355             : 
     356     1138333 : sal_Bool SAL_CALL SfxItemPropertySetInfo::hasPropertyByName( const OUString& rName )
     357             :     throw(RuntimeException, std::exception)
     358             : {
     359     1138333 :     return m_pImpl->m_pOwnMap->hasPropertyByName( rName );
     360             : }
     361             : 
     362       37339 : SfxExtItemPropertySetInfo::SfxExtItemPropertySetInfo( const SfxItemPropertyMapEntry *pMap,
     363             :                                                       const Sequence<Property>& rPropSeq )
     364       37339 :     : aExtMap( pMap )
     365             : {
     366       37339 :     aExtMap.mergeProperties( rPropSeq );
     367       37339 : }
     368             : 
     369       74562 : SfxExtItemPropertySetInfo::~SfxExtItemPropertySetInfo()
     370             : {
     371       74562 : }
     372             : 
     373          18 : Sequence< Property > SAL_CALL SfxExtItemPropertySetInfo::getProperties(  ) throw(RuntimeException, std::exception)
     374             : {
     375          18 :     return aExtMap.getProperties();
     376             : }
     377             : 
     378          10 : Property SAL_CALL SfxExtItemPropertySetInfo::getPropertyByName( const OUString& rPropertyName )
     379             :     throw(UnknownPropertyException, RuntimeException, std::exception)
     380             : {
     381          10 :     return aExtMap.getPropertyByName( rPropertyName );
     382             : }
     383             : 
     384      114201 : sal_Bool SAL_CALL SfxExtItemPropertySetInfo::hasPropertyByName( const OUString& rPropertyName )
     385             :     throw(RuntimeException, std::exception)
     386             : {
     387      114201 :     return aExtMap.hasPropertyByName( rPropertyName );
     388             : }
     389             : 
     390             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11