1*de7b3f82SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*de7b3f82SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*de7b3f82SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*de7b3f82SAndrew Rist * distributed with this work for additional information 6*de7b3f82SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*de7b3f82SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*de7b3f82SAndrew Rist * "License"); you may not use this file except in compliance 9*de7b3f82SAndrew Rist * with the License. You may obtain a copy of the License at 10*de7b3f82SAndrew Rist * 11*de7b3f82SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*de7b3f82SAndrew Rist * 13*de7b3f82SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*de7b3f82SAndrew Rist * software distributed under the License is distributed on an 15*de7b3f82SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*de7b3f82SAndrew Rist * KIND, either express or implied. See the License for the 17*de7b3f82SAndrew Rist * specific language governing permissions and limitations 18*de7b3f82SAndrew Rist * under the License. 19*de7b3f82SAndrew Rist * 20*de7b3f82SAndrew Rist *************************************************************/ 21*de7b3f82SAndrew Rist 22*de7b3f82SAndrew Rist 23cdf0e10cSrcweir #ifndef CHART2_COMMONFUNCTORS_HXX 24cdf0e10cSrcweir #define CHART2_COMMONFUNCTORS_HXX 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <algorithm> 27cdf0e10cSrcweir #include <functional> 28cdf0e10cSrcweir #include <rtl/math.hxx> 29cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx> 30cdf0e10cSrcweir #include <rtl/ustring.hxx> 31cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx> 32cdf0e10cSrcweir #include "charttoolsdllapi.hxx" 33cdf0e10cSrcweir 34cdf0e10cSrcweir namespace chart 35cdf0e10cSrcweir { 36cdf0e10cSrcweir namespace CommonFunctors 37cdf0e10cSrcweir { 38cdf0e10cSrcweir 39cdf0e10cSrcweir /** unary function to convert any type T into a ::com::sun::star::uno::Any. 40cdf0e10cSrcweir 41cdf0e10cSrcweir <p>uno::makeAny is an inline function. Thus is cannot be taken directly 42cdf0e10cSrcweir (via mem_fun_ptr)</p> 43cdf0e10cSrcweir */ 44cdf0e10cSrcweir template< typename T > 45cdf0e10cSrcweir struct makeAny : public ::std::unary_function< T, ::com::sun::star::uno::Any > 46cdf0e10cSrcweir { operator ()chart::CommonFunctors::makeAny47cdf0e10cSrcweir ::com::sun::star::uno::Any operator() ( const T & aVal ) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir return ::com::sun::star::uno::makeAny( aVal ); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir }; 52cdf0e10cSrcweir 53cdf0e10cSrcweir /** unary function to convert ::com::sun::star::uno::Any into a double number. 54cdf0e10cSrcweir 55cdf0e10cSrcweir <p>In case no number can be generated from the Any, NaN (see 56cdf0e10cSrcweir rtl::math::SetNAN()) is returned.</p> 57cdf0e10cSrcweir */ 58cdf0e10cSrcweir struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble : public ::std::unary_function< ::com::sun::star::uno::Any, double > 59cdf0e10cSrcweir { operator ()chart::CommonFunctors::AnyToDouble60cdf0e10cSrcweir double operator() ( const ::com::sun::star::uno::Any & rAny ) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir double fResult; 63cdf0e10cSrcweir ::rtl::math::setNan( & fResult ); 64cdf0e10cSrcweir 65cdf0e10cSrcweir ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() ); 66cdf0e10cSrcweir if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE ) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir fResult = * reinterpret_cast< const double * >( rAny.getValue() ); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir return fResult; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir }; 74cdf0e10cSrcweir 75cdf0e10cSrcweir /** unary function to convert ::com::sun::star::uno::Any into an 76cdf0e10cSrcweir ::rtl::OUString. 77cdf0e10cSrcweir */ 78cdf0e10cSrcweir struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString : public ::std::unary_function< ::com::sun::star::uno::Any, ::rtl::OUString > 79cdf0e10cSrcweir { operator ()chart::CommonFunctors::AnyToString80cdf0e10cSrcweir ::rtl::OUString operator() ( const ::com::sun::star::uno::Any & rAny ) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() ); 83cdf0e10cSrcweir if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE ) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir const double* pDouble = reinterpret_cast< const double * >( rAny.getValue() ); 86cdf0e10cSrcweir if( ::rtl::math::isNan(*pDouble) ) 87cdf0e10cSrcweir return ::rtl::OUString(); 88cdf0e10cSrcweir return ::rtl::math::doubleToUString( 89cdf0e10cSrcweir * pDouble, 90cdf0e10cSrcweir rtl_math_StringFormat_Automatic, 91cdf0e10cSrcweir -1, // use maximum decimal places available 92cdf0e10cSrcweir sal_Char( '.' ), // decimal separator 93cdf0e10cSrcweir false // do not erase trailing zeros 94cdf0e10cSrcweir ); 95cdf0e10cSrcweir } 96cdf0e10cSrcweir else if( eClass == ::com::sun::star::uno::TypeClass_STRING ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir return * reinterpret_cast< const ::rtl::OUString * >( rAny.getValue() ); 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir return ::rtl::OUString(); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir }; 104cdf0e10cSrcweir 105cdf0e10cSrcweir /** unary function to convert an ::rtl::OUString into a double number. 106cdf0e10cSrcweir 107cdf0e10cSrcweir <p>For conversion rtl::math::StringToDouble is used.</p> 108cdf0e10cSrcweir */ 109cdf0e10cSrcweir struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble : public ::std::unary_function< ::rtl::OUString, double > 110cdf0e10cSrcweir { operator ()chart::CommonFunctors::OUStringToDouble111cdf0e10cSrcweir double operator() ( const ::rtl::OUString & rStr ) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir rtl_math_ConversionStatus eConversionStatus; 114cdf0e10cSrcweir double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus, NULL ); 115cdf0e10cSrcweir 116cdf0e10cSrcweir if( eConversionStatus != rtl_math_ConversionStatus_Ok ) 117cdf0e10cSrcweir ::rtl::math::setNan( & fResult ); 118cdf0e10cSrcweir 119cdf0e10cSrcweir return fResult; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir }; 122cdf0e10cSrcweir 123cdf0e10cSrcweir /** unary function to convert a double number into an ::rtl::OUString. 124cdf0e10cSrcweir 125cdf0e10cSrcweir <p>For conversion rtl::math::DoubleToOUString is used.</p> 126cdf0e10cSrcweir */ 127cdf0e10cSrcweir struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString : public ::std::unary_function< double, ::rtl::OUString > 128cdf0e10cSrcweir { operator ()chart::CommonFunctors::DoubleToOUString129cdf0e10cSrcweir ::rtl::OUString operator() ( double fNumber ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir return ::rtl::math::doubleToUString( 132cdf0e10cSrcweir fNumber, 133cdf0e10cSrcweir rtl_math_StringFormat_Automatic, 134cdf0e10cSrcweir -1, // use maximum number of decimal places 135cdf0e10cSrcweir static_cast< sal_Char >( '.' ), 136cdf0e10cSrcweir false // do not erase trailing zeros 137cdf0e10cSrcweir ); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir }; 140cdf0e10cSrcweir 141cdf0e10cSrcweir // ================================================================================ 142cdf0e10cSrcweir 143cdf0e10cSrcweir /** can be used to find an element with a specific first element in e.g. a 144cdf0e10cSrcweir vector of pairs (for searching keys in maps you will of course use map::find) 145cdf0e10cSrcweir */ 146cdf0e10cSrcweir template< typename First, typename Second > 147cdf0e10cSrcweir class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool > 148cdf0e10cSrcweir { 149cdf0e10cSrcweir public: FirstOfPairEquals(const First & aVal)150cdf0e10cSrcweir FirstOfPairEquals( const First & aVal ) 151cdf0e10cSrcweir : m_aValueToCompareWith( aVal ) 152cdf0e10cSrcweir {} operator ()(const::std::pair<First,Second> & rElem)153cdf0e10cSrcweir bool operator() ( const ::std::pair< First, Second > & rElem ) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir return rElem.first == m_aValueToCompareWith; 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir private: 159cdf0e10cSrcweir First m_aValueToCompareWith; 160cdf0e10cSrcweir }; 161cdf0e10cSrcweir 162cdf0e10cSrcweir /** can be used to find a certain value in a map 163cdf0e10cSrcweir 164cdf0e10cSrcweir ::std::find_if( aMap.begin(), aMap.end(), 165cdf0e10cSrcweir SecondOfPairEquals< string, int >( 42 )); 166cdf0e10cSrcweir */ 167cdf0e10cSrcweir template< typename Key, typename Value > 168cdf0e10cSrcweir class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool > 169cdf0e10cSrcweir { 170cdf0e10cSrcweir public: SecondOfPairEquals(const Value & aVal)171cdf0e10cSrcweir SecondOfPairEquals( const Value & aVal ) 172cdf0e10cSrcweir : m_aValueToCompareWith( aVal ) 173cdf0e10cSrcweir {} operator ()(const::std::pair<Key,Value> & rMapElem)174cdf0e10cSrcweir bool operator() ( const ::std::pair< Key, Value > & rMapElem ) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir return rMapElem.second == m_aValueToCompareWith; 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir private: 180cdf0e10cSrcweir Value m_aValueToCompareWith; 181cdf0e10cSrcweir }; 182cdf0e10cSrcweir 183cdf0e10cSrcweir /** Searches for data in a given map, i.e. not for the key but for the data 184cdf0e10cSrcweir pointed to by the keys. 185cdf0e10cSrcweir 186cdf0e10cSrcweir To find a key (value) you can use rMap.find( rValue ) 187cdf0e10cSrcweir */ 188cdf0e10cSrcweir template< class MapType > 189cdf0e10cSrcweir inline typename MapType::const_iterator findValueInMap(const MapType & rMap,const typename MapType::mapped_type & rData)190cdf0e10cSrcweir findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData ) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir return ::std::find_if( rMap.begin(), rMap.end(), 193cdf0e10cSrcweir ::std::compose1( ::std::bind2nd( 194cdf0e10cSrcweir ::std::equal_to< typename MapType::mapped_type >(), 195cdf0e10cSrcweir rData ), 196cdf0e10cSrcweir ::std::select2nd< typename MapType::value_type >())); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir /** Functor that deletes the object behind the given pointer by calling the 200cdf0e10cSrcweir delete operator 201cdf0e10cSrcweir */ 202cdf0e10cSrcweir template< typename T > 203cdf0e10cSrcweir struct DeletePtr : public ::std::unary_function< T *, void > 204cdf0e10cSrcweir { operator ()chart::CommonFunctors::DeletePtr205cdf0e10cSrcweir void operator() ( T * pObj ) 206cdf0e10cSrcweir { delete pObj; } 207cdf0e10cSrcweir }; 208cdf0e10cSrcweir 209cdf0e10cSrcweir } // namespace CommonFunctors 210cdf0e10cSrcweir } // namespace chart 211cdf0e10cSrcweir 212cdf0e10cSrcweir // CHART2_COMMONFUNCTORS_HXX 213cdf0e10cSrcweir #endif 214