LCOV - code coverage report
Current view: top level - include/comphelper - anycompare.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 43 58 74.1 %
Date: 2015-06-13 12:38:46 Functions: 40 65 61.5 %
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             : #ifndef INCLUDED_COMPHELPER_ANYCOMPARE_HXX
      21             : #define INCLUDED_COMPHELPER_ANYCOMPARE_HXX
      22             : 
      23             : #include <comphelper/comphelperdllapi.h>
      24             : 
      25             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      26             : #include <com/sun/star/i18n/XCollator.hpp>
      27             : 
      28             : #include <comphelper/extract.hxx>
      29             : 
      30             : #include <functional>
      31             : #include <memory>
      32             : 
      33             : 
      34             : namespace comphelper
      35             : {
      36             : 
      37             : 
      38             : 
      39             :     //= IKeyPredicateLess
      40             : 
      41          40 :     class SAL_NO_VTABLE IKeyPredicateLess
      42             :     {
      43             :     public:
      44             :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const = 0;
      45          40 :         virtual ~IKeyPredicateLess() {}
      46             :     };
      47             : 
      48             : 
      49             :     //= LessPredicateAdapter
      50             : 
      51             :     struct LessPredicateAdapter : public ::std::binary_function< ::com::sun::star::uno::Any, ::com::sun::star::uno::Any, bool >
      52             :     {
      53          34 :         LessPredicateAdapter( const IKeyPredicateLess& _predicate )
      54          34 :             :m_predicate( _predicate )
      55             :         {
      56          34 :         }
      57             : 
      58        1402 :         bool operator()( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
      59             :         {
      60        1402 :             return m_predicate.isLess( _lhs, _rhs );
      61             :         }
      62             : 
      63             :     private:
      64             :         IKeyPredicateLess const &   m_predicate;
      65             : 
      66             :     private:
      67             :         LessPredicateAdapter(); // never implemented
      68             :     };
      69             : 
      70             : 
      71             :     //= ScalarPredicateLess
      72             : 
      73             :     template< typename SCALAR >
      74          93 :     class ScalarPredicateLess : public IKeyPredicateLess
      75             :     {
      76             :     public:
      77        1100 :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
      78             :         {
      79        1100 :             SCALAR lhs(0), rhs(0);
      80        2200 :             if  (   !( _lhs >>= lhs )
      81        1100 :                 ||  !( _rhs >>= rhs )
      82             :                 )
      83           0 :                 throw ::com::sun::star::lang::IllegalArgumentException();
      84        1100 :             return lhs < rhs;
      85             :         }
      86             :     };
      87             : 
      88             : 
      89             :     //= StringPredicateLess
      90             : 
      91           9 :     class StringPredicateLess : public IKeyPredicateLess
      92             :     {
      93             :     public:
      94         211 :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
      95             :         {
      96         422 :             OUString lhs, rhs;
      97         422 :             if  (   !( _lhs >>= lhs )
      98         211 :                 ||  !( _rhs >>= rhs )
      99             :                 )
     100           0 :                 throw ::com::sun::star::lang::IllegalArgumentException();
     101         422 :             return lhs < rhs;
     102             :         }
     103             :     };
     104             : 
     105             : 
     106             :     //= StringCollationPredicateLess
     107             : 
     108           0 :     class StringCollationPredicateLess : public IKeyPredicateLess
     109             :     {
     110             :     public:
     111           0 :         StringCollationPredicateLess( ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator )
     112           0 :             :m_collator( i_collator )
     113             :         {
     114           0 :         }
     115             : 
     116           0 :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
     117             :         {
     118           0 :             OUString lhs, rhs;
     119           0 :             if  (   !( _lhs >>= lhs )
     120           0 :                 ||  !( _rhs >>= rhs )
     121             :                 )
     122           0 :                 throw ::com::sun::star::lang::IllegalArgumentException();
     123           0 :             return m_collator->compareString( lhs, rhs ) < 0;
     124             :         }
     125             : 
     126             :     private:
     127             :         ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const m_collator;
     128             :     };
     129             : 
     130             : 
     131             :     //= TypePredicateLess
     132             : 
     133           6 :     class TypePredicateLess : public IKeyPredicateLess
     134             :     {
     135             :     public:
     136          74 :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
     137             :         {
     138         148 :             ::com::sun::star::uno::Type lhs, rhs;
     139         148 :             if  (   !( _lhs >>= lhs )
     140          74 :                 ||  !( _rhs >>= rhs )
     141             :                 )
     142           0 :                 throw ::com::sun::star::lang::IllegalArgumentException();
     143         148 :             return lhs.getTypeName() < rhs.getTypeName();
     144             :         }
     145             :     };
     146             : 
     147             : 
     148             :     //= EnumPredicateLess
     149             : 
     150           4 :     class EnumPredicateLess : public IKeyPredicateLess
     151             :     {
     152             :     public:
     153           2 :         EnumPredicateLess( ::com::sun::star::uno::Type const & _enumType )
     154           2 :             :m_enumType( _enumType )
     155             :         {
     156           2 :         }
     157             : 
     158          71 :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
     159             :         {
     160          71 :             sal_Int32 lhs(0), rhs(0);
     161         142 :             if  (   !::cppu::enum2int( lhs, _lhs )
     162          71 :                 ||  !::cppu::enum2int( rhs, _rhs )
     163          71 :                 ||  !_lhs.getValueType().equals( m_enumType )
     164         142 :                 ||  !_rhs.getValueType().equals( m_enumType )
     165             :                 )
     166           0 :                 throw ::com::sun::star::lang::IllegalArgumentException();
     167          71 :             return lhs < rhs;
     168             :         }
     169             : 
     170             :     private:
     171             :         ::com::sun::star::uno::Type const   m_enumType;
     172             :     };
     173             : 
     174             : 
     175             :     //= InterfacePredicateLess
     176             : 
     177           6 :     class InterfacePredicateLess : public IKeyPredicateLess
     178             :     {
     179             :     public:
     180         152 :         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
     181             :         {
     182         304 :             if  (   ( _lhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE )
     183         152 :                 ||  ( _rhs.getValueTypeClass() != ::com::sun::star::uno::TypeClass_INTERFACE )
     184             :                 )
     185           0 :                 throw ::com::sun::star::lang::IllegalArgumentException();
     186             : 
     187         152 :             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > lhs( _lhs, ::com::sun::star::uno::UNO_QUERY );
     188         304 :             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > rhs( _rhs, ::com::sun::star::uno::UNO_QUERY );
     189         304 :             return lhs.get() < rhs.get();
     190             :         }
     191             :     };
     192             : 
     193             : 
     194             :     //= getStandardLessPredicate
     195             : 
     196             :     /** creates a default IKeyPredicateLess instance for the given UNO type
     197             :         @param i_type
     198             :             the type for which a predicate instance should be created
     199             :         @param i_collator
     200             :             specifies a collator instance to use, or <NULL/>. If <NULL/>, strings will be compared using the <code>&lt</code>
     201             :             operator, otherwise the collator will be used. The parameter is ignored if <arg>i_type</arg> does not specify
     202             :             the string type.
     203             :         @return
     204             :             a default implementation of IKeyPredicateLess, which is able to compare values of the given type. If no
     205             :             such default implementation is known for the given type, then <NULL/> is returned.
     206             :     */
     207             :     ::std::unique_ptr< IKeyPredicateLess > COMPHELPER_DLLPUBLIC
     208             :         getStandardLessPredicate(
     209             :             ::com::sun::star::uno::Type const & i_type,
     210             :             ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > const & i_collator
     211             :         );
     212             : 
     213             : 
     214             : } // namespace comphelper
     215             : 
     216             : 
     217             : #endif // INCLUDED_COMPHELPER_ANYCOMPARE_HXX
     218             : 
     219             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11