fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / inc / CommonFunctors.hxx
blobc7bcff16c897bd50af2cfa14532a84e21867d261
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 INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
20 #define INCLUDED_CHART2_SOURCE_INC_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 = * static_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 = static_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 * static_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 /** 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)
141 template< typename First, typename Second >
142 class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool >
144 public:
145 FirstOfPairEquals( const First & aVal )
146 : m_aValueToCompareWith( aVal )
148 bool operator() ( const ::std::pair< First, Second > & rElem )
150 return rElem.first == m_aValueToCompareWith;
153 private:
154 First m_aValueToCompareWith;
157 /** can be used to find a certain value in a map
159 ::std::find_if( aMap.begin(), aMap.end(),
160 SecondOfPairEquals< string, int >( 42 ));
162 template< typename Key, typename Value >
163 class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool >
165 public:
166 SecondOfPairEquals( const Value & aVal )
167 : m_aValueToCompareWith( aVal )
169 bool operator() ( const ::std::pair< Key, Value > & rMapElem )
171 return rMapElem.second == m_aValueToCompareWith;
174 private:
175 Value m_aValueToCompareWith;
178 /** Searches for data in a given map, i.e. not for the key but for the data
179 pointed to by the keys.
181 To find a key (value) you can use rMap.find( rValue )
183 template< class MapType >
184 inline typename MapType::const_iterator
185 findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData )
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 >()));
194 } // namespace CommonFunctors
195 } // namespace chart
197 // INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
198 #endif
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */