1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
27 #ifndef CHART2_COMMONFUNCTORS_HXX
28 #define CHART2_COMMONFUNCTORS_HXX
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"
40 namespace CommonFunctors
43 /** unary function to convert any type T into a ::com::sun::star::uno::Any.
45 <p>uno::makeAny is an inline function. Thus is cannot be taken directly
48 template< typename T
>
49 struct makeAny
: public ::std::unary_function
< T
, ::com::sun::star::uno::Any
>
51 ::com::sun::star::uno::Any
operator() ( const T
& aVal
)
53 return ::com::sun::star::uno::makeAny( aVal
);
57 /** unary function to convert ::com::sun::star::uno::Any into a double number.
59 <p>In case no number can be generated from the Any, NaN (see
60 rtl::math::SetNAN()) is returned.</p>
62 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble
: public ::std::unary_function
< ::com::sun::star::uno::Any
, double >
64 double operator() ( const ::com::sun::star::uno::Any
& rAny
)
67 ::rtl::math::setNan( & fResult
);
69 ::com::sun::star::uno::TypeClass
eClass( rAny
.getValueType().getTypeClass() );
70 if( eClass
== ::com::sun::star::uno::TypeClass_STRING
)
72 rtl_math_ConversionStatus eConversionStatus
;
73 fResult
= ::rtl::math::stringToDouble(
74 * reinterpret_cast< const ::rtl::OUString
* >( rAny
.getValue() ),
75 sal_Char( '.' ), sal_Char( ',' ),
76 & eConversionStatus
, NULL
);
78 if( eConversionStatus
!= rtl_math_ConversionStatus_Ok
)
79 ::rtl::math::setNan( & fResult
);
81 else if( eClass
== ::com::sun::star::uno::TypeClass_DOUBLE
)
83 fResult
= * reinterpret_cast< const double * >( rAny
.getValue() );
90 /** unary function to convert ::com::sun::star::uno::Any into an
93 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString
: public ::std::unary_function
< ::com::sun::star::uno::Any
, ::rtl::OUString
>
95 ::rtl::OUString
operator() ( const ::com::sun::star::uno::Any
& rAny
)
97 ::com::sun::star::uno::TypeClass
eClass( rAny
.getValueType().getTypeClass() );
98 if( eClass
== ::com::sun::star::uno::TypeClass_DOUBLE
)
100 const double* pDouble
= reinterpret_cast< const double * >( rAny
.getValue() );
101 if( ::rtl::math::isNan(*pDouble
) )
102 return ::rtl::OUString();
103 return ::rtl::math::doubleToUString(
105 rtl_math_StringFormat_Automatic
,
106 -1, // use maximum decimal places available
107 sal_Char( '.' ), // decimal separator
108 false // do not erase trailing zeros
111 else if( eClass
== ::com::sun::star::uno::TypeClass_STRING
)
113 return * reinterpret_cast< const ::rtl::OUString
* >( rAny
.getValue() );
116 return ::rtl::OUString();
120 /** unary function to convert an ::rtl::OUString into a double number.
122 <p>For conversion rtl::math::StringToDouble is used.</p>
124 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble
: public ::std::unary_function
< ::rtl::OUString
, double >
126 double operator() ( const ::rtl::OUString
& rStr
)
128 rtl_math_ConversionStatus eConversionStatus
;
129 double fResult
= ::rtl::math::stringToDouble( rStr
, '.', ',', & eConversionStatus
, NULL
);
131 if( eConversionStatus
!= rtl_math_ConversionStatus_Ok
)
132 ::rtl::math::setNan( & fResult
);
138 /** unary function to convert a double number into an ::rtl::OUString.
140 <p>For conversion rtl::math::DoubleToOUString is used.</p>
142 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString
: public ::std::unary_function
< double, ::rtl::OUString
>
144 ::rtl::OUString
operator() ( double fNumber
)
146 return ::rtl::math::doubleToUString(
148 rtl_math_StringFormat_Automatic
,
149 -1, // use maximum number of decimal places
150 static_cast< sal_Char
>( '.' ),
151 false // do not erase trailing zeros
156 // ================================================================================
158 /** can be used to find an element with a specific first element in e.g. a
159 vector of pairs (for searching keys in maps you will of course use map::find)
161 template< typename First
, typename Second
>
162 class FirstOfPairEquals
: public ::std::unary_function
< ::std::pair
< First
, Second
>, bool >
165 FirstOfPairEquals( const First
& aVal
)
166 : m_aValueToCompareWith( aVal
)
168 bool operator() ( const ::std::pair
< First
, Second
> & rElem
)
170 return rElem
.first
== m_aValueToCompareWith
;
174 First m_aValueToCompareWith
;
177 /** can be used to find a certain value in a map
179 ::std::find_if( aMap.begin(), aMap.end(),
180 SecondOfPairEquals< string, int >( 42 ));
182 template< typename Key
, typename Value
>
183 class SecondOfPairEquals
: public ::std::unary_function
< ::std::pair
< Key
, Value
>, bool >
186 SecondOfPairEquals( const Value
& aVal
)
187 : m_aValueToCompareWith( aVal
)
189 bool operator() ( const ::std::pair
< Key
, Value
> & rMapElem
)
191 return rMapElem
.second
== m_aValueToCompareWith
;
195 Value m_aValueToCompareWith
;
198 /** Searches for data in a given map, i.e. not for the key but for the data
199 pointed to by the keys.
201 To find a key (value) you can use rMap.find( rValue )
203 template< class MapType
>
204 inline typename
MapType::const_iterator
205 findValueInMap( const MapType
& rMap
, const typename
MapType::mapped_type
& rData
)
207 return ::std::find_if( rMap
.begin(), rMap
.end(),
208 ::std::compose1( ::std::bind2nd(
209 ::std::equal_to
< typename
MapType::mapped_type
>(),
211 ::std::select2nd
< typename
MapType::value_type
>()));
214 /** Functor that deletes the object behind the given pointer by calling the
217 template< typename T
>
218 struct DeletePtr
: public ::std::unary_function
< T
*, void >
220 void operator() ( T
* pObj
)
224 } // namespace CommonFunctors
227 // CHART2_COMMONFUNCTORS_HXX