LCOV - code coverage report
Current view: top level - libreoffice/comphelper/source/misc - namedvaluecollection.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 82 143 57.3 %
Date: 2012-12-27 Functions: 17 28 60.7 %
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             :  *
       4             :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5             :  *
       6             :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7             :  *
       8             :  * OpenOffice.org - a multi-platform office productivity suite
       9             :  *
      10             :  * This file is part of OpenOffice.org.
      11             :  *
      12             :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13             :  * it under the terms of the GNU Lesser General Public License version 3
      14             :  * only, as published by the Free Software Foundation.
      15             :  *
      16             :  * OpenOffice.org is distributed in the hope that it will be useful,
      17             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :  * GNU Lesser General Public License version 3 for more details
      20             :  * (a copy is included in the LICENSE file that accompanied this code).
      21             :  *
      22             :  * You should have received a copy of the GNU Lesser General Public License
      23             :  * version 3 along with OpenOffice.org.  If not, see
      24             :  * <http://www.openoffice.org/license.html>
      25             :  * for a copy of the LGPLv3 License.
      26             :  *
      27             :  ************************************************************************/
      28             : 
      29             : #include <comphelper/namedvaluecollection.hxx>
      30             : 
      31             : #include <com/sun/star/beans/NamedValue.hpp>
      32             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      33             : #include <com/sun/star/beans/PropertyState.hpp>
      34             : 
      35             : #include <rtl/ustrbuf.hxx>
      36             : #include <rtl/instance.hxx>
      37             : 
      38             : #include <boost/unordered_map.hpp>
      39             : #include <functional>
      40             : #include <algorithm>
      41             : 
      42             : //........................................................................
      43             : namespace comphelper
      44             : {
      45             : //........................................................................
      46             : 
      47             :     /** === begin UNO using === **/
      48             :     using ::com::sun::star::uno::Any;
      49             :     using ::com::sun::star::uno::Sequence;
      50             :     using ::com::sun::star::beans::PropertyValue;
      51             :     using ::com::sun::star::beans::NamedValue;
      52             :     using ::com::sun::star::uno::Type;
      53             :     using ::com::sun::star::uno::cpp_acquire;
      54             :     using ::com::sun::star::uno::cpp_release;
      55             :     using ::com::sun::star::uno::cpp_queryInterface;
      56             :     using ::com::sun::star::lang::IllegalArgumentException;
      57             :     using ::com::sun::star::beans::NamedValue;
      58             :     using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
      59             :     /** === end UNO using === **/
      60             : 
      61             :     //====================================================================
      62             :     //= NamedValueCollection_Impl
      63             :     //====================================================================
      64             :     typedef ::boost::unordered_map< ::rtl::OUString, Any, ::rtl::OUStringHash >    NamedValueRepository;
      65             : 
      66        5884 :     struct NamedValueCollection_Impl
      67             :     {
      68             :         NamedValueRepository    aValues;
      69             :     };
      70             : 
      71             :     //====================================================================
      72             :     //= NamedValueCollection
      73             :     //====================================================================
      74             :     //--------------------------------------------------------------------
      75         346 :     NamedValueCollection::NamedValueCollection()
      76         346 :         :m_pImpl( new NamedValueCollection_Impl )
      77             :     {
      78         346 :     }
      79             : 
      80             :     //--------------------------------------------------------------------
      81           0 :     NamedValueCollection::NamedValueCollection( const NamedValueCollection& _rCopySource )
      82           0 :         :m_pImpl( new NamedValueCollection_Impl )
      83             :     {
      84           0 :         *this = _rCopySource;
      85           0 :     }
      86             : 
      87             :     //--------------------------------------------------------------------
      88           0 :     NamedValueCollection& NamedValueCollection::operator=( const NamedValueCollection& i_rCopySource )
      89             :     {
      90           0 :         m_pImpl->aValues = i_rCopySource.m_pImpl->aValues;
      91           0 :         return *this;
      92             :     }
      93             : 
      94             :     //--------------------------------------------------------------------
      95         178 :     NamedValueCollection::NamedValueCollection( const Any& _rElements )
      96         178 :         :m_pImpl( new NamedValueCollection_Impl )
      97             :     {
      98         178 :         impl_assign( _rElements );
      99         178 :     }
     100             : 
     101             :     //--------------------------------------------------------------------
     102         450 :     NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
     103         450 :         :m_pImpl( new NamedValueCollection_Impl )
     104             :     {
     105         450 :         impl_assign( _rArguments );
     106         450 :     }
     107             : 
     108             :     //--------------------------------------------------------------------
     109        1968 :     NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
     110        1968 :         :m_pImpl( new NamedValueCollection_Impl )
     111             :     {
     112        1968 :         impl_assign( _rArguments );
     113        1968 :     }
     114             : 
     115             :     //--------------------------------------------------------------------
     116           0 :     NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
     117           0 :         :m_pImpl( new NamedValueCollection_Impl )
     118             :     {
     119           0 :         impl_assign( _rArguments );
     120           0 :     }
     121             : 
     122             :     //--------------------------------------------------------------------
     123        2942 :     NamedValueCollection::~NamedValueCollection()
     124             :     {
     125        2942 :     }
     126             : 
     127             :     //--------------------------------------------------------------------
     128           0 :     bool NamedValueCollection::canExtractFrom( ::com::sun::star::uno::Any const & i_value )
     129             :     {
     130           0 :         Type const & aValueType = i_value.getValueType();
     131           0 :         if  (   aValueType.equals( ::cppu::UnoType< PropertyValue >::get() )
     132           0 :             ||  aValueType.equals( ::cppu::UnoType< NamedValue >::get() )
     133           0 :             ||  aValueType.equals( ::cppu::UnoType< Sequence< PropertyValue > >::get() )
     134           0 :             ||  aValueType.equals( ::cppu::UnoType< Sequence< NamedValue > >::get() )
     135             :             )
     136           0 :             return true;
     137           0 :         return false;
     138             :     }
     139             : 
     140             :    //--------------------------------------------------------------------
     141           0 :     NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
     142             :     {
     143           0 :         for (   NamedValueRepository::const_iterator namedValue = _rAdditionalValues.m_pImpl->aValues.begin();
     144           0 :                 namedValue != _rAdditionalValues.m_pImpl->aValues.end();
     145             :                 ++namedValue
     146             :             )
     147             :         {
     148           0 :             if ( _bOverwriteExisting || !impl_has( namedValue->first ) )
     149           0 :                 impl_put( namedValue->first, namedValue->second );
     150             :         }
     151             : 
     152           0 :         return *this;
     153             :     }
     154             : 
     155             :     //--------------------------------------------------------------------
     156           0 :     size_t NamedValueCollection::size() const
     157             :     {
     158           0 :         return m_pImpl->aValues.size();
     159             :     }
     160             : 
     161             :     //--------------------------------------------------------------------
     162           0 :     bool NamedValueCollection::empty() const
     163             :     {
     164           0 :         return m_pImpl->aValues.empty();
     165             :     }
     166             : 
     167             :     //--------------------------------------------------------------------
     168           0 :     ::std::vector< ::rtl::OUString > NamedValueCollection::getNames() const
     169             :     {
     170           0 :         ::std::vector< ::rtl::OUString > aNames;
     171           0 :         for ( NamedValueRepository::const_iterator it = m_pImpl->aValues.begin(), end = m_pImpl->aValues.end(); it != end; ++it )
     172             :         {
     173           0 :             aNames.push_back( it->first );
     174             :         }
     175           0 :         return aNames;
     176             :     }
     177             : 
     178             :     //--------------------------------------------------------------------
     179         178 :     void NamedValueCollection::impl_assign( const Any& i_rWrappedElements )
     180             :     {
     181         178 :         Sequence< NamedValue > aNamedValues;
     182         178 :         Sequence< PropertyValue > aPropertyValues;
     183         178 :         NamedValue aNamedValue;
     184         178 :         PropertyValue aPropertyValue;
     185             : 
     186         178 :         if ( i_rWrappedElements >>= aNamedValues )
     187           0 :             impl_assign( aNamedValues );
     188         178 :         else if ( i_rWrappedElements >>= aPropertyValues )
     189         178 :             impl_assign( aPropertyValues );
     190           0 :         else if ( i_rWrappedElements >>= aNamedValue )
     191           0 :             impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
     192           0 :         else if ( i_rWrappedElements >>= aPropertyValue )
     193           0 :             impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
     194             :         else
     195         178 :             SAL_WARN_IF( i_rWrappedElements.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
     196         178 :     }
     197             : 
     198             :     //--------------------------------------------------------------------
     199         450 :     void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
     200             :     {
     201             :         {
     202         450 :             NamedValueRepository aEmpty;
     203         450 :             m_pImpl->aValues.swap( aEmpty );
     204             :         }
     205             : 
     206         450 :         PropertyValue aPropertyValue;
     207         450 :         NamedValue aNamedValue;
     208             : 
     209         450 :         const Any* pArgument = _rArguments.getConstArray();
     210         450 :         const Any* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
     211         971 :         for ( ; pArgument != pArgumentEnd; ++pArgument )
     212             :         {
     213         521 :             if ( *pArgument >>= aPropertyValue )
     214         255 :                 m_pImpl->aValues[ aPropertyValue.Name ] = aPropertyValue.Value;
     215         266 :             else if ( *pArgument >>= aNamedValue )
     216         266 :                 m_pImpl->aValues[ aNamedValue.Name ] = aNamedValue.Value;
     217             :             else
     218             :             {
     219             :                 SAL_WARN_IF(
     220             :                     pArgument->hasValue(), "comphelper",
     221             :                     ("NamedValueCollection::impl_assign: encountered a value"
     222             :                      " type which I cannot handle: "
     223             :                      + pArgument->getValueTypeName()));
     224             :             }
     225         450 :         }
     226         450 :     }
     227             : 
     228             :     //--------------------------------------------------------------------
     229        2146 :     void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
     230             :     {
     231             :         {
     232        2146 :             NamedValueRepository aEmpty;
     233        2146 :             m_pImpl->aValues.swap( aEmpty );
     234             :         }
     235             : 
     236        2146 :         const PropertyValue* pArgument = _rArguments.getConstArray();
     237        2146 :         const PropertyValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
     238       14917 :         for ( ; pArgument != pArgumentEnd; ++pArgument )
     239       12771 :             m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
     240        2146 :     }
     241             : 
     242             :     //--------------------------------------------------------------------
     243           0 :     void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
     244             :     {
     245             :         {
     246           0 :             NamedValueRepository aEmpty;
     247           0 :             m_pImpl->aValues.swap( aEmpty );
     248             :         }
     249             : 
     250           0 :         const NamedValue* pArgument = _rArguments.getConstArray();
     251           0 :         const NamedValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
     252           0 :         for ( ; pArgument != pArgumentEnd; ++pArgument )
     253           0 :             m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
     254           0 :     }
     255             : 
     256             :     //--------------------------------------------------------------------
     257        4739 :     bool NamedValueCollection::get_ensureType( const ::rtl::OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
     258             :     {
     259        4739 :         NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
     260        4739 :         if ( pos != m_pImpl->aValues.end() )
     261             :         {
     262        1705 :             if ( uno_type_assignData(
     263             :                     _pValueLocation, _rExpectedValueType.getTypeLibType(),
     264        3410 :                     const_cast< void* >( pos->second.getValue() ), pos->second.getValueType().getTypeLibType(),
     265             :                     reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
     266             :                     reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
     267             :                     reinterpret_cast< uno_ReleaseFunc >( cpp_release )
     268        3410 :                 ) )
     269             :                 // argument exists, and could be extracted
     270        1705 :                 return true;
     271             : 
     272             :             // argument exists, but is of wrong type
     273           0 :             ::rtl::OUStringBuffer aBuffer;
     274           0 :             aBuffer.appendAscii( "Invalid value type for '" );
     275           0 :             aBuffer.append     ( _rValueName );
     276           0 :             aBuffer.appendAscii( "'.\nExpected: " );
     277           0 :             aBuffer.append     ( _rExpectedValueType.getTypeName() );
     278           0 :             aBuffer.appendAscii( "\nFound: " );
     279           0 :             aBuffer.append     ( pos->second.getValueType().getTypeName() );
     280           0 :             throw IllegalArgumentException( aBuffer.makeStringAndClear(), NULL, 0 );
     281             :         }
     282             : 
     283             :         // argument does not exist
     284        3034 :         return false;
     285             :     }
     286             : 
     287             :     namespace
     288             :     {
     289             :         class theEmptyDefault : public rtl::Static<Any, theEmptyDefault> {};
     290             :     }
     291             : 
     292             :     //--------------------------------------------------------------------
     293        1636 :     const Any& NamedValueCollection::impl_get( const ::rtl::OUString& _rValueName ) const
     294             :     {
     295        1636 :         NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
     296        1636 :         if ( pos != m_pImpl->aValues.end() )
     297         198 :             return pos->second;
     298             : 
     299        1438 :         return theEmptyDefault::get();
     300             :     }
     301             : 
     302             :     //--------------------------------------------------------------------
     303        1585 :     bool NamedValueCollection::impl_has( const ::rtl::OUString& _rValueName ) const
     304             :     {
     305        1585 :         NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
     306        1585 :         return ( pos != m_pImpl->aValues.end() );
     307             :     }
     308             : 
     309             :     //--------------------------------------------------------------------
     310        1220 :     bool NamedValueCollection::impl_put( const ::rtl::OUString& _rValueName, const Any& _rValue )
     311             :     {
     312        1220 :         bool bHas = impl_has( _rValueName );
     313        1220 :         m_pImpl->aValues[ _rValueName ] = _rValue;
     314        1220 :         return bHas;
     315             :     }
     316             : 
     317             :     //--------------------------------------------------------------------
     318        5824 :     bool NamedValueCollection::impl_remove( const ::rtl::OUString& _rValueName )
     319             :     {
     320        5824 :         NamedValueRepository::iterator pos = m_pImpl->aValues.find( _rValueName );
     321        5824 :         if ( pos == m_pImpl->aValues.end() )
     322        5340 :             return false;
     323         484 :         m_pImpl->aValues.erase( pos );
     324         484 :         return true;
     325             :     }
     326             : 
     327             :     //--------------------------------------------------------------------
     328             :     namespace
     329             :     {
     330             :         struct Value2PropertyValue : public ::std::unary_function< NamedValueRepository::value_type, PropertyValue >
     331             :         {
     332        4874 :             PropertyValue operator()( const NamedValueRepository::value_type& _rValue )
     333             :             {
     334             :                 return PropertyValue(
     335        4874 :                     _rValue.first, 0, _rValue.second, PropertyState_DIRECT_VALUE );
     336             :             }
     337             :         };
     338             : 
     339             :         struct Value2NamedValue : public ::std::unary_function< NamedValueRepository::value_type, NamedValue >
     340             :         {
     341           0 :             NamedValue operator()( const NamedValueRepository::value_type& _rValue )
     342             :             {
     343           0 :                 return NamedValue( _rValue.first, _rValue.second );
     344             :             }
     345             :         };
     346             :     }
     347             : 
     348             :     //--------------------------------------------------------------------
     349        1259 :     sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
     350             :     {
     351        1259 :         _out_rValues.realloc( m_pImpl->aValues.size() );
     352        1259 :         ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2PropertyValue() );
     353        1259 :         return _out_rValues.getLength();
     354             :     }
     355             : 
     356             :     //--------------------------------------------------------------------
     357           0 :     sal_Int32 NamedValueCollection::operator >>= ( Sequence< NamedValue >& _out_rValues ) const
     358             :     {
     359           0 :         _out_rValues.realloc( m_pImpl->aValues.size() );
     360           0 :         ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2NamedValue() );
     361           0 :         return _out_rValues.getLength();
     362             :     }
     363             : 
     364             : //........................................................................
     365             : } // namespace comphelper
     366             : //........................................................................
     367             : 
     368             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10