Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / inc / CommonFunctors.hxx
blobe8818c67c972cef386457696a9c4fd99974ab1ae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
20 #define INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
22 #include <o3tl/any.hxx>
23 #include <rtl/math.hxx>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <rtl/ustring.hxx>
26 #include "charttoolsdllapi.hxx"
28 namespace chart
30 namespace CommonFunctors
33 /** unary function to convert any type T into a css::uno::Any.
35 <p>uno::makeAny is an inline function. Thus is cannot be taken directly
36 (via mem_fun_ptr)</p>
38 template< typename T >
39 struct makeAny
41 css::uno::Any operator() ( const T & aVal )
43 return css::uno::makeAny( aVal );
47 /** unary function to convert css::uno::Any into a double number.
49 <p>In case no number can be generated from the Any, NaN (see
50 rtl::math::SetNAN()) is returned.</p>
52 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble
54 double operator() ( const css::uno::Any & rAny )
56 double fResult;
57 ::rtl::math::setNan( & fResult );
58 rAny >>= fResult;
59 return fResult;
63 /** unary function to convert css::uno::Any into an
64 OUString.
66 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString
68 OUString operator() ( const css::uno::Any & rAny )
70 if( auto pDouble = o3tl::tryAccess<double>(rAny) )
72 if( ::rtl::math::isNan(*pDouble) )
73 return OUString();
74 return ::rtl::math::doubleToUString(
75 * pDouble,
76 rtl_math_StringFormat_Automatic,
77 rtl_math_DecimalPlaces_Max, // use maximum decimal places available
78 sal_Char( '.' ), // decimal separator
79 true // remove trailing zeros
82 else if( auto s = o3tl::tryAccess<OUString>(rAny) )
84 return *s;
87 return OUString();
91 /** unary function to convert an OUString into a double number.
93 <p>For conversion rtl::math::StringToDouble is used.</p>
95 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble
97 double operator() ( const OUString & rStr )
99 rtl_math_ConversionStatus eConversionStatus;
100 double fResult = ::rtl::math::stringToDouble( rStr, '.', ',', & eConversionStatus );
102 if( eConversionStatus != rtl_math_ConversionStatus_Ok )
103 ::rtl::math::setNan( & fResult );
105 return fResult;
109 /** unary function to convert a double number into an OUString.
111 <p>For conversion rtl::math::DoubleToOUString is used.</p>
113 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString
115 OUString operator() ( double fNumber )
117 return ::rtl::math::doubleToUString(
118 fNumber,
119 rtl_math_StringFormat_Automatic,
120 rtl_math_DecimalPlaces_Max, // use maximum decimal places available
121 static_cast< sal_Char >( '.' ),
122 true
127 } // namespace CommonFunctors
128 } // namespace chart
130 // INCLUDED_CHART2_SOURCE_INC_COMMONFUNCTORS_HXX
131 #endif
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */