1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <o3tl/any.hxx>
22 #include <rtl/math.hxx>
23 #include <com/sun/star/uno/Any.hxx>
24 #include <rtl/ustring.hxx>
25 #include "charttoolsdllapi.hxx"
29 namespace chart::CommonFunctors
32 /** unary function to convert any type T into a css::uno::Any.
34 <p>uno::makeAny is an inline function. Thus is cannot be taken directly
37 template< typename T
>
40 css::uno::Any
operator() ( const T
& aVal
)
42 return css::uno::Any( aVal
);
46 /** unary function to convert css::uno::Any into a double number.
48 <p>In case no number can be generated from the Any, NaN is returned.</p>
50 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToDouble
52 double operator() ( const css::uno::Any
& rAny
)
54 double fResult
= std::numeric_limits
<double>::quiet_NaN();
60 /** unary function to convert css::uno::Any into an
63 struct OOO_DLLPUBLIC_CHARTTOOLS AnyToString
65 OUString
operator() ( const css::uno::Any
& rAny
)
67 if( auto pDouble
= o3tl::tryAccess
<double>(rAny
) )
69 if( std::isnan(*pDouble
) )
71 return ::rtl::math::doubleToUString(
73 rtl_math_StringFormat_Automatic
,
74 rtl_math_DecimalPlaces_Max
, // use maximum decimal places available
75 '.', // decimal separator
76 true // remove trailing zeros
79 else if( auto s
= o3tl::tryAccess
<OUString
>(rAny
) )
88 /** unary function to convert an OUString into a double number.
90 <p>For conversion rtl::math::StringToDouble is used.</p>
92 struct OOO_DLLPUBLIC_CHARTTOOLS OUStringToDouble
94 double operator() ( std::u16string_view rStr
)
96 rtl_math_ConversionStatus eConversionStatus
;
97 double fResult
= ::rtl::math::stringToDouble( rStr
, '.', ',', & eConversionStatus
);
99 if( eConversionStatus
!= rtl_math_ConversionStatus_Ok
)
100 return std::numeric_limits
<double>::quiet_NaN();
106 /** unary function to convert a double number into an OUString.
108 <p>For conversion rtl::math::DoubleToOUString is used.</p>
110 struct OOO_DLLPUBLIC_CHARTTOOLS DoubleToOUString
112 OUString
operator() ( double fNumber
)
114 return ::rtl::math::doubleToUString(
116 rtl_math_StringFormat_Automatic
,
117 rtl_math_DecimalPlaces_Max
, // use maximum decimal places available
124 } // namespace chart::CommonFunctors
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */