1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CommonFunctors.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef CHART2_COMMONFUNCTORS_HXX
31 #define CHART2_COMMONFUNCTORS_HXX
35 #include <rtl/math.hxx>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <rtl/ustring.hxx>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include "charttoolsdllapi.hxx"
43 namespace CommonFunctors
46 /** unary function to convert any type T into a ::com::sun::star::uno::Any.
48 <p>uno::makeAny is an inline function. Thus is cannot be taken directly
51 template< typename T
>
52 struct makeAny
: public ::std::unary_function
< T
, ::com::sun::star::uno::Any
>
54 ::com::sun::star::uno::Any
operator() ( const T
& aVal
)
56 return ::com::sun::star::uno::makeAny( aVal
);
60 /** unary function to convert ::com::sun::star::uno::Any into a double number.
62 <p>In case no number can be generated from the Any, NaN (see
63 rtl::math::SetNAN()) is returned.</p>
65 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble
: public ::std::unary_function
< ::com::sun::star::uno::Any
, double >
67 double operator() ( const ::com::sun::star::uno::Any
& rAny
)
70 ::rtl::math::setNan( & fResult
);
72 ::com::sun::star::uno::TypeClass
eClass( rAny
.getValueType().getTypeClass() );
73 if( eClass
== ::com::sun::star::uno::TypeClass_STRING
)
75 rtl_math_ConversionStatus eConversionStatus
;
76 fResult
= ::rtl::math::stringToDouble(
77 * reinterpret_cast< const ::rtl::OUString
* >( rAny
.getValue() ),
78 sal_Char( '.' ), sal_Char( ',' ),
79 & eConversionStatus
, NULL
);
81 if( eConversionStatus
!= rtl_math_ConversionStatus_Ok
)
82 ::rtl::math::setNan( & fResult
);
84 else if( eClass
== ::com::sun::star::uno::TypeClass_DOUBLE
)
86 fResult
= * reinterpret_cast< const double * >( rAny
.getValue() );
93 /** unary function to convert ::com::sun::star::uno::Any into an
96 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString
: public ::std::unary_function
< ::com::sun::star::uno::Any
, ::rtl::OUString
>
98 ::rtl::OUString
operator() ( const ::com::sun::star::uno::Any
& rAny
)
100 ::com::sun::star::uno::TypeClass
eClass( rAny
.getValueType().getTypeClass() );
101 if( eClass
== ::com::sun::star::uno::TypeClass_DOUBLE
)
103 const double* pDouble
= reinterpret_cast< const double * >( rAny
.getValue() );
104 if( ::rtl::math::isNan(*pDouble
) )
105 return ::rtl::OUString();
106 return ::rtl::math::doubleToUString(
108 rtl_math_StringFormat_Automatic
,
109 -1, // use maximum decimal places available
110 sal_Char( '.' ), // decimal separator
111 false // do not erase trailing zeros
114 else if( eClass
== ::com::sun::star::uno::TypeClass_STRING
)
116 return * reinterpret_cast< const ::rtl::OUString
* >( rAny
.getValue() );
119 return ::rtl::OUString();
123 /** unary function to convert an ::rtl::OUString into a double number.
125 <p>For conversion rtl::math::StringToDouble is used.</p>
127 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble
: public ::std::unary_function
< ::rtl::OUString
, double >
129 double operator() ( const ::rtl::OUString
& rStr
)
131 rtl_math_ConversionStatus eConversionStatus
;
132 double fResult
= ::rtl::math::stringToDouble( rStr
, '.', ',', & eConversionStatus
, NULL
);
134 if( eConversionStatus
!= rtl_math_ConversionStatus_Ok
)
135 ::rtl::math::setNan( & fResult
);
141 /** unary function to convert a double number into an ::rtl::OUString.
143 <p>For conversion rtl::math::DoubleToOUString is used.</p>
145 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString
: public ::std::unary_function
< double, ::rtl::OUString
>
147 ::rtl::OUString
operator() ( double fNumber
)
149 return ::rtl::math::doubleToUString(
151 rtl_math_StringFormat_Automatic
,
152 -1, // use maximum number of decimal places
153 static_cast< sal_Char
>( '.' ),
154 false // do not erase trailing zeros
159 // ================================================================================
161 /** can be used to find an element with a specific first element in e.g. a
162 vector of pairs (for searching keys in maps you will of course use map::find)
164 template< typename First
, typename Second
>
165 class FirstOfPairEquals
: public ::std::unary_function
< ::std::pair
< First
, Second
>, bool >
168 FirstOfPairEquals( const First
& aVal
)
169 : m_aValueToCompareWith( aVal
)
171 bool operator() ( const ::std::pair
< First
, Second
> & rElem
)
173 return rElem
.first
== m_aValueToCompareWith
;
177 First m_aValueToCompareWith
;
180 /** can be used to find a certain value in a map
182 ::std::find_if( aMap.begin(), aMap.end(),
183 SecondOfPairEquals< string, int >( 42 ));
185 template< typename Key
, typename Value
>
186 class SecondOfPairEquals
: public ::std::unary_function
< ::std::pair
< Key
, Value
>, bool >
189 SecondOfPairEquals( const Value
& aVal
)
190 : m_aValueToCompareWith( aVal
)
192 bool operator() ( const ::std::pair
< Key
, Value
> & rMapElem
)
194 return rMapElem
.second
== m_aValueToCompareWith
;
198 Value m_aValueToCompareWith
;
201 /** Searches for data in a given map, i.e. not for the key but for the data
202 pointed to by the keys.
204 To find a key (value) you can use rMap.find( rValue )
206 template< class MapType
>
207 inline typename
MapType::const_iterator
208 findValueInMap( const MapType
& rMap
, const typename
MapType::mapped_type
& rData
)
210 return ::std::find_if( rMap
.begin(), rMap
.end(),
211 ::std::compose1( ::std::bind2nd(
212 ::std::equal_to
< typename
MapType::mapped_type
>(),
214 ::std::select2nd
< typename
MapType::value_type
>()));
217 /** Functor that deletes the object behind the given pointer by calling the
220 template< typename T
>
221 struct DeletePtr
: public ::std::unary_function
< T
*, void >
223 void operator() ( T
* pObj
)
227 } // namespace CommonFunctors
230 // CHART2_COMMONFUNCTORS_HXX