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 : public ::std::unary_function< T, ::com::sun::star::uno::Any >
46 {
operator ()chart::CommonFunctors::makeAny47     ::com::sun::star::uno::Any operator() ( const T & aVal )
48     {
49         return ::com::sun::star::uno::makeAny( aVal );
50     }
51 };
52 
53 /** unary function to convert ::com::sun::star::uno::Any into a double number.
54 
55     <p>In case no number can be generated from the Any, NaN (see
56     rtl::math::SetNAN()) is returned.</p>
57 */
58 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble : public ::std::unary_function< ::com::sun::star::uno::Any, double >
59 {
operator ()chart::CommonFunctors::AnyToDouble60     double operator() ( const ::com::sun::star::uno::Any & rAny )
61     {
62         double fResult;
63         ::rtl::math::setNan( & fResult );
64 
65         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
66         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
67         {
68             fResult = * reinterpret_cast< const double * >( rAny.getValue() );
69         }
70 
71         return fResult;
72     }
73 };
74 
75 /** unary function to convert ::com::sun::star::uno::Any into an
76     ::rtl::OUString.
77 */
78 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString : public ::std::unary_function< ::com::sun::star::uno::Any,  ::rtl::OUString >
79 {
operator ()chart::CommonFunctors::AnyToString80     ::rtl::OUString operator() ( const ::com::sun::star::uno::Any & rAny )
81     {
82         ::com::sun::star::uno::TypeClass eClass( rAny.getValueType().getTypeClass() );
83         if( eClass == ::com::sun::star::uno::TypeClass_DOUBLE )
84         {
85             const double* pDouble = reinterpret_cast< const double * >( rAny.getValue() );
86             if( ::rtl::math::isNan(*pDouble) )
87                 return ::rtl::OUString();
88             return ::rtl::math::doubleToUString(
89                 * pDouble,
90                 rtl_math_StringFormat_Automatic,
91                 -1, // use maximum decimal places available
92                 sal_Char( '.' ), // decimal separator
93                 false // do not erase trailing zeros
94                 );
95         }
96         else if( eClass == ::com::sun::star::uno::TypeClass_STRING )
97         {
98             return * reinterpret_cast< const ::rtl::OUString * >( rAny.getValue() );
99         }
100 
101         return ::rtl::OUString();
102     }
103 };
104 
105 /** unary function to convert an ::rtl::OUString into a double number.
106 
107     <p>For conversion rtl::math::StringToDouble is used.</p>
108  */
109 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble : public ::std::unary_function< ::rtl::OUString, double >
110 {
operator ()chart::CommonFunctors::OUStringToDouble111     double operator() ( const ::rtl::OUString & rStr )
112     {
113         rtl_math_ConversionStatus eConversionStatus;
114         double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus, NULL );
115 
116         if( eConversionStatus != rtl_math_ConversionStatus_Ok )
117             ::rtl::math::setNan( & fResult );
118 
119         return fResult;
120     }
121 };
122 
123 /** unary function to convert a double number into an ::rtl::OUString.
124 
125     <p>For conversion rtl::math::DoubleToOUString is used.</p>
126  */
127 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString : public ::std::unary_function< double, ::rtl::OUString >
128 {
operator ()chart::CommonFunctors::DoubleToOUString129     ::rtl::OUString operator() ( double fNumber )
130     {
131         return ::rtl::math::doubleToUString(
132             fNumber,
133             rtl_math_StringFormat_Automatic,
134             -1, // use maximum number of decimal places
135             static_cast< sal_Char >( '.' ),
136             false // do not erase trailing zeros
137             );
138     }
139 };
140 
141 // ================================================================================
142 
143 /** can be used to find an element with a specific first element in e.g. a
144     vector of pairs (for searching keys in maps you will of course use map::find)
145  */
146 template< typename First, typename Second >
147     class FirstOfPairEquals : public ::std::unary_function< ::std::pair< First, Second >, bool >
148 {
149 public:
FirstOfPairEquals(const First & aVal)150     FirstOfPairEquals( const First & aVal )
151             : m_aValueToCompareWith( aVal )
152     {}
operator ()(const::std::pair<First,Second> & rElem)153     bool operator() ( const ::std::pair< First, Second > & rElem )
154     {
155         return rElem.first == m_aValueToCompareWith;
156     }
157 
158 private:
159     First m_aValueToCompareWith;
160 };
161 
162 /** can be used to find a certain value in a map
163 
164     ::std::find_if( aMap.begin(), aMap.end(),
165                     SecondOfPairEquals< string, int >( 42 ));
166  */
167 template< typename Key, typename Value >
168     class SecondOfPairEquals : public ::std::unary_function< ::std::pair< Key, Value >, bool >
169 {
170 public:
SecondOfPairEquals(const Value & aVal)171     SecondOfPairEquals( const Value & aVal )
172             : m_aValueToCompareWith( aVal )
173     {}
operator ()(const::std::pair<Key,Value> & rMapElem)174     bool operator() ( const ::std::pair< Key, Value > & rMapElem )
175     {
176         return rMapElem.second == m_aValueToCompareWith;
177     }
178 
179 private:
180     Value m_aValueToCompareWith;
181 };
182 
183 /** Searches for data in a given map, i.e. not for the key but for the data
184     pointed to by the keys.
185 
186     To find a key (value) you can use rMap.find( rValue )
187  */
188 template< class MapType >
189     inline typename MapType::const_iterator
findValueInMap(const MapType & rMap,const typename MapType::mapped_type & rData)190     findValueInMap( const MapType & rMap, const typename MapType::mapped_type & rData )
191 {
192     return ::std::find_if( rMap.begin(), rMap.end(),
193                            ::std::compose1( ::std::bind2nd(
194                                                 ::std::equal_to< typename MapType::mapped_type >(),
195                                                 rData ),
196                                             ::std::select2nd< typename MapType::value_type >()));
197 }
198 
199 /** Functor that deletes the object behind the given pointer by calling the
200     delete operator
201  */
202 template< typename T >
203     struct DeletePtr : public ::std::unary_function< T *, void >
204 {
operator ()chart::CommonFunctors::DeletePtr205     void operator() ( T * pObj )
206     { delete pObj; }
207 };
208 
209 } //  namespace CommonFunctors
210 } //  namespace chart
211 
212 // CHART2_COMMONFUNCTORS_HXX
213 #endif
214