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 <com/sun/star/uno/Sequence.hxx>
25 #include <rtl/ustring.hxx>
26 #include "charttoolsdllapi.hxx"
31 namespace chart::CommonFunctors
34 /** unary function to convert any type T into a css::uno::Any.
36 <p>uno::makeAny is an inline function. Thus is cannot be taken directly
41 template< typename T
>
42 css::uno::Any
operator() ( const T
& aVal
)
44 return css::uno::Any( aVal
);
48 /** unary function to convert an OUString or css::uno::Any into a double number.
50 <p>For conversion of OUString, rtl::math::StringToDouble is used.</p>
51 <p>In case no number can be generated from the Any, NaN is returned.</p>
53 struct OOO_DLLPUBLIC_CHARTTOOLS ToDouble
55 double operator() ( const css::uno::Any
& rAny
)
57 double fResult
= std::numeric_limits
<double>::quiet_NaN();
62 double operator() ( std::u16string_view rStr
)
64 rtl_math_ConversionStatus eConversionStatus
;
65 double fResult
= ::rtl::math::stringToDouble( rStr
, '.', ',', & eConversionStatus
);
67 if( eConversionStatus
!= rtl_math_ConversionStatus_Ok
)
68 return std::numeric_limits
<double>::quiet_NaN();
74 /** unary function to convert a double number or css::uno::Any into an OUString.
76 <p>For conversion of doubles, rtl::math::DoubleToOUString is used.</p>
78 struct OOO_DLLPUBLIC_CHARTTOOLS ToString
80 OUString
operator() ( const css::uno::Any
& rAny
)
82 if( auto pDouble
= o3tl::tryAccess
<double>(rAny
) )
84 if( std::isnan(*pDouble
) )
86 return ::rtl::math::doubleToUString(
88 rtl_math_StringFormat_Automatic
,
89 rtl_math_DecimalPlaces_Max
, // use maximum decimal places available
90 '.', // decimal separator
91 true // remove trailing zeros
94 else if( auto s
= o3tl::tryAccess
<OUString
>(rAny
) )
102 OUString
operator() ( double fNumber
)
104 return ::rtl::math::doubleToUString(
106 rtl_math_StringFormat_Automatic
,
107 rtl_math_DecimalPlaces_Max
, // use maximum decimal places available
114 template <class Container
, class Func
> auto convertToSequence(const Container
& container
, Func f
)
116 css::uno::Sequence
<decltype(f(container
[0]))> result(container
.size());
117 std::transform(container
.begin(), container
.end(), result
.getArray(), f
);
121 } // namespace chart::CommonFunctors
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */