LCOV - code coverage report
Current view: top level - chart2/source/inc - CommonFunctors.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 13 25 52.0 %
Date: 2015-06-13 12:38:46 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 INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
      20             : #define INCLUDED_CHART2_SOURCE_INC_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     1198321 :     ::com::sun::star::uno::Any operator() ( const T & aVal )
      45             :     {
      46     1198321 :         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       14391 :     double operator() ( const ::com::sun::star::uno::Any & rAny )
      58             :     {
      59             :         double fResult;
      60       14391 :         ::rtl::math::setNan( & fResult );
      61             : 
      62       14391 :         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
      63       14391 :         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
      64             :         {
      65       14379 :             fResult = * static_cast< const double * >( rAny.getValue() );
      66             :         }
      67             : 
      68       14391 :         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        7201 :     OUString operator() ( const ::com::sun::star::uno::Any & rAny )
      78             :     {
      79        7201 :         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
      80        7201 :         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
      81             :         {
      82           0 :             const double* pDouble = static_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        7201 :         else if( eClass == ::com::sun::star::uno::TypeClass_STRING )
      94             :         {
      95        7201 :             return * static_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             : /** can be used to find an element with a specific first element in e.g. a
     139             :     vector of pairs (for searching keys in maps you will of course use map::find)
     140             :  */
     141             : template< typename First, typename Second >
     142             :     class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool >
     143             : {
     144             : public:
     145             :     FirstOfPairEquals( const First & aVal )
     146             :             : m_aValueToCompareWith( aVal )
     147             :     {}
     148             :     bool operator() ( const ::std::pair< First, Second > & rElem )
     149             :     {
     150             :         return rElem.first == m_aValueToCompareWith;
     151             :     }
     152             : 
     153             : private:
     154             :     First m_aValueToCompareWith;
     155             : };
     156             : 
     157             : /** can be used to find a certain value in a map
     158             : 
     159             :     ::std::find_if( aMap.begin(), aMap.end(),
     160             :                     SecondOfPairEquals< string, int >( 42 ));
     161             :  */
     162             : template< typename Key, typename Value >
     163             :     class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool >
     164             : {
     165             : public:
     166             :     SecondOfPairEquals( const Value & aVal )
     167             :             : m_aValueToCompareWith( aVal )
     168             :     {}
     169             :     bool operator() ( const ::std::pair< Key, Value > & rMapElem )
     170             :     {
     171             :         return rMapElem.second == m_aValueToCompareWith;
     172             :     }
     173             : 
     174             : private:
     175             :     Value m_aValueToCompareWith;
     176             : };
     177             : 
     178             : /** Searches for data in a given map, i.e. not for the key but for the data
     179             :     pointed to by the keys.
     180             : 
     181             :     To find a key (value) you can use rMap.find( rValue )
     182             :  */
     183             : template< class MapType >
     184             :     inline typename MapType::const_iterator
     185             :     findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData )
     186             : {
     187             :     return ::std::find_if( rMap.begin(), rMap.end(),
     188             :                            ::o3tl::compose1( ::std::bind2nd(
     189             :                                                 ::std::equal_to< typename MapType::mapped_type >(),
     190             :                                                 rData ),
     191             :                                             ::o3tl::select2nd< typename MapType::value_type >()));
     192             : }
     193             : 
     194             : } //  namespace CommonFunctors
     195             : } //  namespace chart
     196             : 
     197             : // INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
     198             : #endif
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11