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