LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/chart2/source/inc - CommonFunctors.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 13 25 52.0 %
Date: 2013-07-09 Functions: 4 6 66.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             :  * 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             : #ifndef CHART2_COMMONFUNCTORS_HXX
      20             : #define CHART2_COMMONFUNCTORS_HXX
      21             : 
      22             : #include <algorithm>
      23             : #include <functional>
      24             : #include <o3tl/compat_functional.hxx>
      25             : #include <rtl/math.hxx>
      26             : #include <com/sun/star/uno/Any.hxx>
      27             : #include <rtl/ustring.hxx>
      28             : #include <com/sun/star/uno/Sequence.hxx>
      29             : #include "charttoolsdllapi.hxx"
      30             : 
      31             : namespace chart
      32             : {
      33             : namespace CommonFunctors
      34             : {
      35             : 
      36             : /** unary function to convert any type T into a ::com::sun::star::uno::Any.
      37             : 
      38             :     <p>uno::makeAny is an inline function.  Thus is cannot be taken directly
      39             :     (via mem_fun_ptr)</p>
      40             : */
      41             : template< typename T >
      42             :     struct makeAny : public ::std::unary_function< T, ::com::sun::star::uno::Any >
      43             : {
      44       14025 :     ::com::sun::star::uno::Any operator() ( const T & aVal )
      45             :     {
      46       14025 :         return ::com::sun::star::uno::makeAny( aVal );
      47             :     }
      48             : };
      49             : 
      50             : /** unary function to convert ::com::sun::star::uno::Any into a double number.
      51             : 
      52             :     <p>In case no number can be generated from the Any, NaN (see
      53             :     rtl::math::SetNAN()) is returned.</p>
      54             : */
      55             : struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble : public ::std::unary_function< ::com::sun::star::uno::Any, double >
      56             : {
      57        5820 :     double operator() ( const ::com::sun::star::uno::Any & rAny )
      58             :     {
      59             :         double fResult;
      60        5820 :         ::rtl::math::setNan( & fResult );
      61             : 
      62        5820 :         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
      63        5820 :         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
      64             :         {
      65        5808 :             fResult = * reinterpret_cast< const double * >( rAny.getValue() );
      66             :         }
      67             : 
      68        5820 :         return fResult;
      69             :     }
      70             : };
      71             : 
      72             : /** unary function to convert ::com::sun::star::uno::Any into an
      73             :     OUString.
      74             : */
      75             : struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString : public ::std::unary_function< ::com::sun::star::uno::Any,  OUString >
      76             : {
      77        3757 :     OUString operator() ( const ::com::sun::star::uno::Any & rAny )
      78             :     {
      79        3757 :         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
      80        3757 :         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
      81             :         {
      82           0 :             const double* pDouble = reinterpret_cast< const double * >( rAny.getValue() );
      83           0 :             if( ::rtl::math::isNan(*pDouble) )
      84           0 :                 return OUString();
      85             :             return ::rtl::math::doubleToUString(
      86             :                 * pDouble,
      87             :                 rtl_math_StringFormat_Automatic,
      88             :                 -1, // use maximum decimal places available
      89             :                 sal_Char( '.' ), // decimal separator
      90             :                 false // do not erase trailing zeros
      91           0 :                 );
      92             :         }
      93        3757 :         else if( eClass == ::com::sun::star::uno::TypeClass_STRING )
      94             :         {
      95        3757 :             return * reinterpret_cast< const OUString * >( rAny.getValue() );
      96             :         }
      97             : 
      98           0 :         return OUString();
      99             :     }
     100             : };
     101             : 
     102             : /** unary function to convert an OUString into a double number.
     103             : 
     104             :     <p>For conversion rtl::math::StringToDouble is used.</p>
     105             :  */
     106             : struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble : public ::std::unary_function< OUString, double >
     107             : {
     108           0 :     double operator() ( const OUString & rStr )
     109             :     {
     110             :         rtl_math_ConversionStatus eConversionStatus;
     111           0 :         double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus, NULL );
     112             : 
     113           0 :         if( eConversionStatus != rtl_math_ConversionStatus_Ok )
     114           0 :             ::rtl::math::setNan( & fResult );
     115             : 
     116           0 :         return fResult;
     117             :     }
     118             : };
     119             : 
     120             : /** unary function to convert a double number into an OUString.
     121             : 
     122             :     <p>For conversion rtl::math::DoubleToOUString is used.</p>
     123             :  */
     124             : struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString : public ::std::unary_function< double, OUString >
     125             : {
     126           0 :     OUString operator() ( double fNumber )
     127             :     {
     128             :         return ::rtl::math::doubleToUString(
     129             :             fNumber,
     130             :             rtl_math_StringFormat_Automatic,
     131             :             -1, // use maximum number of decimal places
     132             :             static_cast< sal_Char >( '.' ),
     133             :             false // do not erase trailing zeros
     134           0 :             );
     135             :     }
     136             : };
     137             : 
     138             : // ================================================================================
     139             : 
     140             : /** can be used to find an element with a specific first element in e.g. a
     141             :     vector of pairs (for searching keys in maps you will of course use map::find)
     142             :  */
     143             : template< typename First, typename Second >
     144             :     class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool >
     145             : {
     146             : public:
     147             :     FirstOfPairEquals( const First & aVal )
     148             :             : m_aValueToCompareWith( aVal )
     149             :     {}
     150             :     bool operator() ( const ::std::pair< First, Second > & rElem )
     151             :     {
     152             :         return rElem.first == m_aValueToCompareWith;
     153             :     }
     154             : 
     155             : private:
     156             :     First m_aValueToCompareWith;
     157             : };
     158             : 
     159             : /** can be used to find a certain value in a map
     160             : 
     161             :     ::std::find_if( aMap.begin(), aMap.end(),
     162             :                     SecondOfPairEquals< string, int >( 42 ));
     163             :  */
     164             : template< typename Key, typename Value >
     165             :     class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool >
     166             : {
     167             : public:
     168             :     SecondOfPairEquals( const Value & aVal )
     169             :             : m_aValueToCompareWith( aVal )
     170             :     {}
     171             :     bool operator() ( const ::std::pair< Key, Value > & rMapElem )
     172             :     {
     173             :         return rMapElem.second == m_aValueToCompareWith;
     174             :     }
     175             : 
     176             : private:
     177             :     Value m_aValueToCompareWith;
     178             : };
     179             : 
     180             : /** Searches for data in a given map, i.e. not for the key but for the data
     181             :     pointed to by the keys.
     182             : 
     183             :     To find a key (value) you can use rMap.find( rValue )
     184             :  */
     185             : template< class MapType >
     186             :     inline typename MapType::const_iterator
     187             :     findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData )
     188             : {
     189             :     return ::std::find_if( rMap.begin(), rMap.end(),
     190             :                            ::o3tl::compose1( ::std::bind2nd(
     191             :                                                 ::std::equal_to< typename MapType::mapped_type >(),
     192             :                                                 rData ),
     193             :                                             ::o3tl::select2nd< typename MapType::value_type >()));
     194             : }
     195             : 
     196             : /** Functor that deletes the object behind the given pointer by calling the
     197             :     delete operator
     198             :  */
     199             : template< typename T >
     200             :     struct DeletePtr : public ::std::unary_function< T *, void >
     201             : {
     202             :     void operator() ( T * pObj )
     203             :     { delete pObj; }
     204             : };
     205             : 
     206             : } //  namespace CommonFunctors
     207             : } //  namespace chart
     208             : 
     209             : // CHART2_COMMONFUNCTORS_HXX
     210             : #endif
     211             : 
     212             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10