1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef CHART2_COMMONFUNCTORS_HXX 28 #define CHART2_COMMONFUNCTORS_HXX 29 30 #include <algorithm> 31 #include <functional> 32 #include <rtl/math.hxx> 33 #include <com/sun/star/uno/Any.hxx> 34 #include <rtl/ustring.hxx> 35 #include <com/sun/star/uno/Sequence.hxx> 36 #include "charttoolsdllapi.hxx" 37 38 namespace chart 39 { 40 namespace CommonFunctors 41 { 42 43 /** unary function to convert any type T into a ::com::sun::star::uno::Any. 44 45 <p>uno::makeAny is an inline function. Thus is cannot be taken directly 46 (via mem_fun_ptr)</p> 47 */ 48 template< typename T > 49 struct makeAny : public ::std::unary_function< T, ::com::sun::star::uno::Any > 50 { 51 ::com::sun::star::uno::Any operator() ( const T & aVal ) 52 { 53 return ::com::sun::star::uno::makeAny( aVal ); 54 } 55 }; 56 57 /** unary function to convert ::com::sun::star::uno::Any into a double number. 58 59 <p>In case no number can be generated from the Any, NaN (see 60 rtl::math::SetNAN()) is returned.</p> 61 */ 62 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble : public ::std::unary_function< ::com::sun::star::uno::Any, double > 63 { 64 double operator() ( const ::com::sun::star::uno::Any & rAny ) 65 { 66 double fResult; 67 ::rtl::math::setNan( & fResult ); 68 69 ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() ); 70 if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE ) 71 { 72 fResult = * reinterpret_cast< const double * >( rAny.getValue() ); 73 } 74 75 return fResult; 76 } 77 }; 78 79 /** unary function to convert ::com::sun::star::uno::Any into an 80 ::rtl::OUString. 81 */ 82 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString : public ::std::unary_function< ::com::sun::star::uno::Any, ::rtl::OUString > 83 { 84 ::rtl::OUString operator() ( const ::com::sun::star::uno::Any & rAny ) 85 { 86 ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() ); 87 if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE ) 88 { 89 const double* pDouble = reinterpret_cast< const double * >( rAny.getValue() ); 90 if( ::rtl::math::isNan(*pDouble) ) 91 return ::rtl::OUString(); 92 return ::rtl::math::doubleToUString( 93 * pDouble, 94 rtl_math_StringFormat_Automatic, 95 -1, // use maximum decimal places available 96 sal_Char( '.' ), // decimal separator 97 false // do not erase trailing zeros 98 ); 99 } 100 else if( eClass == ::com::sun::star::uno::TypeClass_STRING ) 101 { 102 return * reinterpret_cast< const ::rtl::OUString * >( rAny.getValue() ); 103 } 104 105 return ::rtl::OUString(); 106 } 107 }; 108 109 /** unary function to convert an ::rtl::OUString into a double number. 110 111 <p>For conversion rtl::math::StringToDouble is used.</p> 112 */ 113 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble : public ::std::unary_function< ::rtl::OUString, double > 114 { 115 double operator() ( const ::rtl::OUString & rStr ) 116 { 117 rtl_math_ConversionStatus eConversionStatus; 118 double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus, NULL ); 119 120 if( eConversionStatus != rtl_math_ConversionStatus_Ok ) 121 ::rtl::math::setNan( & fResult ); 122 123 return fResult; 124 } 125 }; 126 127 /** unary function to convert a double number into an ::rtl::OUString. 128 129 <p>For conversion rtl::math::DoubleToOUString is used.</p> 130 */ 131 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString : public ::std::unary_function< double, ::rtl::OUString > 132 { 133 ::rtl::OUString operator() ( double fNumber ) 134 { 135 return ::rtl::math::doubleToUString( 136 fNumber, 137 rtl_math_StringFormat_Automatic, 138 -1, // use maximum number of decimal places 139 static_cast< sal_Char >( '.' ), 140 false // do not erase trailing zeros 141 ); 142 } 143 }; 144 145 // ================================================================================ 146 147 /** can be used to find an element with a specific first element in e.g. a 148 vector of pairs (for searching keys in maps you will of course use map::find) 149 */ 150 template< typename First, typename Second > 151 class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool > 152 { 153 public: 154 FirstOfPairEquals( const First & aVal ) 155 : m_aValueToCompareWith( aVal ) 156 {} 157 bool operator() ( const ::std::pair< First, Second > & rElem ) 158 { 159 return rElem.first == m_aValueToCompareWith; 160 } 161 162 private: 163 First m_aValueToCompareWith; 164 }; 165 166 /** can be used to find a certain value in a map 167 168 ::std::find_if( aMap.begin(), aMap.end(), 169 SecondOfPairEquals< string, int >( 42 )); 170 */ 171 template< typename Key, typename Value > 172 class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool > 173 { 174 public: 175 SecondOfPairEquals( const Value & aVal ) 176 : m_aValueToCompareWith( aVal ) 177 {} 178 bool operator() ( const ::std::pair< Key, Value > & rMapElem ) 179 { 180 return rMapElem.second == m_aValueToCompareWith; 181 } 182 183 private: 184 Value m_aValueToCompareWith; 185 }; 186 187 /** Searches for data in a given map, i.e. not for the key but for the data 188 pointed to by the keys. 189 190 To find a key (value) you can use rMap.find( rValue ) 191 */ 192 template< class MapType > 193 inline typename MapType::const_iterator 194 findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData ) 195 { 196 return ::std::find_if( rMap.begin(), rMap.end(), 197 ::std::compose1( ::std::bind2nd( 198 ::std::equal_to< typename MapType::mapped_type >(), 199 rData ), 200 ::std::select2nd< typename MapType::value_type >())); 201 } 202 203 /** Functor that deletes the object behind the given pointer by calling the 204 delete operator 205 */ 206 template< typename T > 207 struct DeletePtr : public ::std::unary_function< T *, void > 208 { 209 void operator() ( T * pObj ) 210 { delete pObj; } 211 }; 212 213 } // namespace CommonFunctors 214 } // namespace chart 215 216 // CHART2_COMMONFUNCTORS_HXX 217 #endif 218