bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / inc / CommonFunctors.hxx
blob458d29d7177f2a5892e76ef130bd8e2eb90dde64
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef CHART2_COMMONFUNCTORS_HXX
20 #define CHART2_COMMONFUNCTORS_HXX
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"
31 namespace chart
33 namespace CommonFunctors
36 /** unary function to convert any type T into a ::com::sun::star::uno::Any.
38 <p>uno::makeAny is an inline function. Thus is cannot be taken directly
39 (via mem_fun_ptr)</p>
41 template< typename T >
42 struct makeAny : public ::std::unary_function< T, ::com::sun::star::uno::Any >
44 ::com::sun::star::uno::Any operator() ( const T & aVal )
46 return ::com::sun::star::uno::makeAny( aVal );
50 /** unary function to convert ::com::sun::star::uno::Any into a double number.
52 <p>In case no number can be generated from the Any, NaN (see
53 rtl::math::SetNAN()) is returned.</p>
55 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble : public ::std::unary_function< ::com::sun::star::uno::Any, double >
57 double operator() ( const ::com::sun::star::uno::Any & rAny )
59 double fResult;
60 ::rtl::math::setNan( & fResult );
62 ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
63 if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
65 fResult = * reinterpret_cast< const double * >( rAny.getValue() );
68 return fResult;
72 /** unary function to convert ::com::sun::star::uno::Any into an
73 OUString.
75 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString : public ::std::unary_function< ::com::sun::star::uno::Any, OUString >
77 OUString operator() ( const ::com::sun::star::uno::Any & rAny )
79 ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
80 if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
82 const double* pDouble = reinterpret_cast< const double * >( rAny.getValue() );
83 if( ::rtl::math::isNan(*pDouble) )
84 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
93 else if( eClass == ::com::sun::star::uno::TypeClass_STRING )
95 return * reinterpret_cast< const OUString * >( rAny.getValue() );
98 return OUString();
102 /** unary function to convert an OUString into a double number.
104 <p>For conversion rtl::math::StringToDouble is used.</p>
106 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble : public ::std::unary_function< OUString, double >
108 double operator() ( const OUString & rStr )
110 rtl_math_ConversionStatus eConversionStatus;
111 double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus, NULL );
113 if( eConversionStatus != rtl_math_ConversionStatus_Ok )
114 ::rtl::math::setNan( & fResult );
116 return fResult;
120 /** unary function to convert a double number into an OUString.
122 <p>For conversion rtl::math::DoubleToOUString is used.</p>
124 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString : public ::std::unary_function< double, OUString >
126 OUString operator() ( double fNumber )
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
138 // ================================================================================
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)
143 template< typename First, typename Second >
144 class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool >
146 public:
147 FirstOfPairEquals( const First & aVal )
148 : m_aValueToCompareWith( aVal )
150 bool operator() ( const ::std::pair< First, Second > & rElem )
152 return rElem.first == m_aValueToCompareWith;
155 private:
156 First m_aValueToCompareWith;
159 /** can be used to find a certain value in a map
161 ::std::find_if( aMap.begin(), aMap.end(),
162 SecondOfPairEquals< string, int >( 42 ));
164 template< typename Key, typename Value >
165 class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool >
167 public:
168 SecondOfPairEquals( const Value & aVal )
169 : m_aValueToCompareWith( aVal )
171 bool operator() ( const ::std::pair< Key, Value > & rMapElem )
173 return rMapElem.second == m_aValueToCompareWith;
176 private:
177 Value m_aValueToCompareWith;
180 /** Searches for data in a given map, i.e. not for the key but for the data
181 pointed to by the keys.
183 To find a key (value) you can use rMap.find( rValue )
185 template< class MapType >
186 inline typename MapType::const_iterator
187 findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData )
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 >()));
196 /** Functor that deletes the object behind the given pointer by calling the
197 delete operator
199 template< typename T >
200 struct DeletePtr : public ::std::unary_function< T *, void >
202 void operator() ( T * pObj )
203 { delete pObj; }
206 } // namespace CommonFunctors
207 } // namespace chart
209 // CHART2_COMMONFUNCTORS_HXX
210 #endif
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */