xref: /trunk/main/chart2/source/inc/CommonFunctors.hxx (revision e32ec1a966a7222d04c4ed2904dd376185cdeba0)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef CHART2_COMMONFUNCTORS_HXX
24 #define CHART2_COMMONFUNCTORS_HXX
25 
26 #include <algorithm>
27 #include <functional>
28 #include <rtl/math.hxx>
29 #include <com/sun/star/uno/Any.hxx>
30 #include <rtl/ustring.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include "charttoolsdllapi.hxx"
33 
34 namespace chart
35 {
36 namespace CommonFunctors
37 {
38 
39 /** unary function to convert any type T into a ::com::sun::star::uno::Any.
40 
41     <p>uno::makeAny is an inline function.  Thus is cannot be taken directly
42     (via mem_fun_ptr)</p>
43 */
44 template< typename T >
45     struct makeAny
46 {
47     typedef T argument_type;
48     typedef ::com::sun::star::uno::Any result_type;
operator ()chart::CommonFunctors::makeAny49     ::com::sun::star::uno::Any operator() ( const T & aVal )
50     {
51         return ::com::sun::star::uno::makeAny( aVal );
52     }
53 };
54 
55 /** unary function to convert ::com::sun::star::uno::Any into a double number.
56 
57     <p>In case no number can be generated from the Any, NaN (see
58     rtl::math::SetNAN()) is returned.</p>
59 */
60 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble
61 {
62     typedef ::com::sun::star::uno::Any argument_type;
63     typedef double result_type;
operator ()chart::CommonFunctors::AnyToDouble64     double operator() ( const ::com::sun::star::uno::Any & rAny )
65     {
66         double fResult;
67         ::rtl::math::setNan( & fResult );
68 
69         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
70         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
71         {
72             fResult = * reinterpret_cast< const double * >( rAny.getValue() );
73         }
74 
75         return fResult;
76     }
77 };
78 
79 /** unary function to convert ::com::sun::star::uno::Any into an
80     ::rtl::OUString.
81 */
82 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString
83 {
84     typedef ::com::sun::star::uno::Any argument_type;
85     typedef ::rtl::OUString result_type;
operator ()chart::CommonFunctors::AnyToString86     ::rtl::OUString operator() ( const ::com::sun::star::uno::Any & rAny )
87     {
88         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
89         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
90         {
91             const double* pDouble = reinterpret_cast< const double * >( rAny.getValue() );
92             if( ::rtl::math::isNan(*pDouble) )
93                 return ::rtl::OUString();
94             return ::rtl::math::doubleToUString(
95                 * pDouble,
96                 rtl_math_StringFormat_Automatic,
97                 -1, // use maximum decimal places available
98                 sal_Char( '.' ), // decimal separator
99                 false // do not erase trailing zeros
100                 );
101         }
102         else if( eClass == ::com::sun::star::uno::TypeClass_STRING )
103         {
104             return * reinterpret_cast< const ::rtl::OUString * >( rAny.getValue() );
105         }
106 
107         return ::rtl::OUString();
108     }
109 };
110 
111 /** unary function to convert an ::rtl::OUString into a double number.
112 
113     <p>For conversion rtl::math::StringToDouble is used.</p>
114  */
115 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble
116 {
117     typedef ::rtl::OUString argument_type;
118     typedef double result_type;
operator ()chart::CommonFunctors::OUStringToDouble119     double operator() ( const ::rtl::OUString & rStr )
120     {
121         rtl_math_ConversionStatus eConversionStatus;
122         double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus, NULL );
123 
124         if( eConversionStatus != rtl_math_ConversionStatus_Ok )
125             ::rtl::math::setNan( & fResult );
126 
127         return fResult;
128     }
129 };
130 
131 /** unary function to convert a double number into an ::rtl::OUString.
132 
133     <p>For conversion rtl::math::DoubleToOUString is used.</p>
134  */
135 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString
136 {
137     typedef double argument_type;
138     typedef ::rtl::OUString result_type;
operator ()chart::CommonFunctors::DoubleToOUString139     ::rtl::OUString operator() ( double fNumber )
140     {
141         return ::rtl::math::doubleToUString(
142             fNumber,
143             rtl_math_StringFormat_Automatic,
144             -1, // use maximum number of decimal places
145             static_cast< sal_Char >( '.' ),
146             false // do not erase trailing zeros
147             );
148     }
149 };
150 
151 // ================================================================================
152 
153 /** can be used to find an element with a specific first element in e.g. a
154     vector of pairs (for searching keys in maps you will of course use map::find)
155  */
156 template< typename First, typename Second >
157     class FirstOfPairEquals
158 {
159 public:
160     typedef ::std::pair< First, Second > argument_type;
161     typedef bool result_type;
FirstOfPairEquals(const First & aVal)162     FirstOfPairEquals( const First & aVal )
163             : m_aValueToCompareWith( aVal )
164     {}
operator ()(const::std::pair<First,Second> & rElem)165     bool operator() ( const ::std::pair< First, Second > & rElem )
166     {
167         return rElem.first == m_aValueToCompareWith;
168     }
169 
170 private:
171     First m_aValueToCompareWith;
172 };
173 
174 /** can be used to find a certain value in a map
175 
176     ::std::find_if( aMap.begin(), aMap.end(),
177                     SecondOfPairEquals< string, int >( 42 ));
178  */
179 template< typename Key, typename Value >
180     class SecondOfPairEquals
181 {
182 public:
183     typedef ::std::pair< Key, Value > argument_type;
184     typedef bool result_type;
SecondOfPairEquals(const Value & aVal)185     SecondOfPairEquals( const Value & aVal )
186             : m_aValueToCompareWith( aVal )
187     {}
operator ()(const::std::pair<Key,Value> & rMapElem)188     bool operator() ( const ::std::pair< Key, Value > & rMapElem )
189     {
190         return rMapElem.second == m_aValueToCompareWith;
191     }
192 
193 private:
194     Value m_aValueToCompareWith;
195 };
196 
197 /** Searches for data in a given map, i.e. not for the key but for the data
198     pointed to by the keys.
199 
200     To find a key (value) you can use rMap.find( rValue )
201  */
202 template< class MapType >
203     inline typename MapType::const_iterator
findValueInMap(const MapType & rMap,const typename MapType::mapped_type & rData)204     findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData )
205 {
206     return ::std::find_if( rMap.begin(), rMap.end(),
207                            ::std::compose1( ::std::bind(
208                                                 ::std::equal_to< typename MapType::mapped_type >(), ::std::placeholders::_1,
209                                                 rData ),
210                                             ::std::select2nd< typename MapType::value_type >()));
211 }
212 
213 /** Functor that deletes the object behind the given pointer by calling the
214     delete operator
215  */
216 template< typename T >
217     struct DeletePtr
218 {
219     typedef T * argument_type;
220     typedef void result_type;
operator ()chart::CommonFunctors::DeletePtr221     void operator() ( T * pObj )
222     { delete pObj; }
223 };
224 
225 } //  namespace CommonFunctors
226 } //  namespace chart
227 
228 // CHART2_COMMONFUNCTORS_HXX
229 #endif
230