LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/svl/source/items - aeitem.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 53 92 57.6 %
Date: 2013-07-09 Functions: 19 28 67.9 %
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 <tools/string.hxx>
      22             : 
      23             : #include <svl/aeitem.hxx>
      24             : #include <vector>
      25             : 
      26             : // STATIC DATA -----------------------------------------------------------
      27             : 
      28             : DBG_NAME(SfxAllEnumItem)
      29             : 
      30       12305 : TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
      31             : 
      32             : // -----------------------------------------------------------------------
      33             : 
      34        5073 : struct SfxAllEnumValue_Impl
      35             : {
      36             :     sal_uInt16 nValue;
      37             :     OUString aText;
      38             : };
      39             : 
      40        2539 : class SfxAllEnumValueArr : public std::vector<SfxAllEnumValue_Impl*>
      41             : {
      42             : public:
      43        2534 :     ~SfxAllEnumValueArr()
      44        2534 :     {
      45        5068 :         for( const_iterator it = begin(); it != end(); ++it )
      46        2534 :             delete *it;
      47        2534 :     }
      48             : };
      49             : 
      50             : // -----------------------------------------------------------------------
      51             : 
      52         752 : SfxAllEnumItem::SfxAllEnumItem() :
      53             :     SfxEnumItem(),
      54             :     pValues( 0 ),
      55         752 :     pDisabledValues( 0 )
      56             : {
      57         752 : }
      58             : 
      59             : // -----------------------------------------------------------------------
      60             : 
      61         775 : SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
      62             :     SfxEnumItem(which, nVal),
      63             :     pValues( 0 ),
      64         775 :     pDisabledValues( 0 )
      65             : {
      66             :     DBG_CTOR(SfxAllEnumItem, 0);
      67         775 :     InsertValue( nVal );
      68         775 : }
      69             : 
      70             : // -----------------------------------------------------------------------
      71             : 
      72           0 : SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
      73             :     SfxEnumItem(which, rStream),
      74             :     pValues( 0 ),
      75           0 :     pDisabledValues( 0 )
      76             : {
      77             :     DBG_CTOR(SfxAllEnumItem, 0);
      78           0 :     InsertValue( GetValue() );
      79           0 : }
      80             : 
      81             : // -----------------------------------------------------------------------
      82             : 
      83             : 
      84         610 : SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
      85             :     SfxEnumItem(which, 0),
      86             :     pValues( 0 ),
      87         610 :     pDisabledValues( 0 )
      88             : {
      89             :     DBG_CTOR(SfxAllEnumItem, 0);
      90         610 : }
      91             : 
      92             : 
      93             : // -----------------------------------------------------------------------
      94             : 
      95        1764 : SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
      96             :     SfxEnumItem(rCopy),
      97             :     pValues(0),
      98        1764 :     pDisabledValues( 0 )
      99             : {
     100             :     DBG_CTOR(SfxAllEnumItem, 0);
     101        1764 :     if ( !rCopy.pValues )
     102        1764 :         return;
     103             : 
     104        1764 :     pValues = new SfxAllEnumValueArr;
     105             : 
     106        3528 :     for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->size(); ++nPos )
     107             :     {
     108        1764 :         SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
     109        1764 :         pVal->nValue = (*rCopy.pValues)[nPos]->nValue;
     110        1764 :         pVal->aText = (*rCopy.pValues)[nPos]->aText;
     111        1764 :         pValues->insert( pValues->begin() + nPos, pVal );
     112             :     }
     113             : 
     114        1764 :     if( rCopy.pDisabledValues )
     115             :     {
     116        1764 :         pDisabledValues = new std::vector<sal_uInt16>( *(rCopy.pDisabledValues) );
     117             :     }
     118             : }
     119             : 
     120             : // -----------------------------------------------------------------------
     121             : 
     122       10520 : SfxAllEnumItem::~SfxAllEnumItem()
     123             : {
     124             :     DBG_DTOR(SfxAllEnumItem, 0);
     125        3765 :     delete pValues;
     126        3765 :     delete pDisabledValues;
     127        6755 : }
     128             : 
     129             : // -----------------------------------------------------------------------
     130             : 
     131           0 : sal_uInt16 SfxAllEnumItem::GetValueCount() const
     132             : {
     133             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     134           0 :     return pValues ? pValues->size() : 0;
     135             : }
     136             : 
     137             : // -----------------------------------------------------------------------
     138             : 
     139           0 : OUString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
     140             : {
     141             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     142             :     DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
     143           0 :     return (*pValues)[nPos]->aText;
     144             : }
     145             : 
     146             : // -----------------------------------------------------------------------
     147             : 
     148           0 : sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
     149             : {
     150             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     151             :     DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
     152           0 :     return (*pValues)[nPos]->nValue;
     153             : }
     154             : 
     155             : // -----------------------------------------------------------------------
     156             : 
     157        1764 : SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
     158             : {
     159             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     160        1764 :     return new SfxAllEnumItem(*this);
     161             : }
     162             : 
     163             : // -----------------------------------------------------------------------
     164             : 
     165           0 : SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
     166             : {
     167             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     168           0 :     return new SfxAllEnumItem( Which(), rStream );
     169             : }
     170             : 
     171             : 
     172             : // -----------------------------------------------------------------------
     173             : 
     174         775 : sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
     175             : 
     176             : /*  [Beschreibung]
     177             : 
     178             :     Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const> liefert
     179             :     diese interne Methode bei nicht vorhandenen Values die Position,
     180             :     an der der Wert liegen w"urde.
     181             : */
     182             : 
     183             : {
     184             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     185             : 
     186         775 :     if ( !pValues )
     187           0 :         return 0;
     188             : 
     189             :     //!O: binaere Suche oder SortArray verwenden
     190             :     sal_uInt16 nPos;
     191         775 :     for ( nPos = 0; nPos < pValues->size(); ++nPos )
     192           0 :         if ( (*pValues)[nPos]->nValue >= nVal )
     193           0 :             return nPos;
     194         775 :     return nPos;
     195             : }
     196             : 
     197             : // -----------------------------------------------------------------------
     198             : 
     199           0 : sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
     200             : 
     201             : /*  [Beschreibung]
     202             : 
     203             :     Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const>
     204             :     immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
     205             :     Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
     206             : */
     207             : 
     208             : {
     209             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     210             : 
     211           0 :     if ( !pValues || pValues->empty() )
     212           0 :         return nValue;
     213             : 
     214           0 :     return SfxEnumItem::GetPosByValue( nValue );
     215             : }
     216             : 
     217             : // -----------------------------------------------------------------------
     218             : 
     219           0 : void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const OUString &rValue )
     220             : {
     221             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     222           0 :     SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
     223           0 :     pVal->nValue = nValue;
     224           0 :     pVal->aText = rValue;
     225           0 :     if ( !pValues )
     226           0 :         pValues = new SfxAllEnumValueArr;
     227           0 :     else if ( GetPosByValue( nValue ) != USHRT_MAX )
     228             :         // remove when exists
     229           0 :         RemoveValue( nValue );
     230             :     // then insert
     231           0 :     pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
     232           0 : }
     233             : 
     234             : // -----------------------------------------------------------------------
     235             : 
     236         775 : void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
     237             : {
     238             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     239         775 :     SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
     240         775 :     pVal->nValue = nValue;
     241         775 :     pVal->aText = OUString::number(nValue);
     242         775 :     if ( !pValues )
     243         775 :         pValues = new SfxAllEnumValueArr;
     244             : 
     245         775 :     pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
     246         775 : }
     247             : 
     248        1550 : void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
     249             : {
     250             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     251        1550 :     if ( !pDisabledValues )
     252         775 :         pDisabledValues = new std::vector<sal_uInt16>;
     253             : 
     254        1550 :     pDisabledValues->push_back( nValue );
     255        1550 : }
     256             : 
     257           0 : sal_Bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
     258             : {
     259           0 :     if ( pDisabledValues )
     260             :     {
     261           0 :         for ( size_t i=0; i<pDisabledValues->size(); i++ )
     262           0 :             if ( (*pDisabledValues)[i] == nValue )
     263           0 :                 return sal_False;
     264             :     }
     265             : 
     266           0 :     return sal_True;
     267             : }
     268             : 
     269             : // -----------------------------------------------------------------------
     270             : 
     271           0 : void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
     272             : {
     273             :     DBG_CHKTHIS(SfxAllEnumItem, 0);
     274           0 :     sal_uInt16 nPos = GetPosByValue(nValue);
     275             :     DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
     276           0 :     pValues->erase( pValues->begin() + nPos );
     277           0 : }
     278             : 
     279             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10