Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / inc / CommonFunctors.hxx
blob7bb776490cda20d4afbbad050cb1d1ab1f43106e
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 #pragma once
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"
27 #include <limits>
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
35 (via mem_fun_ptr)</p>
37 template< typename T >
38 struct makeAny
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();
55 rAny >>= fResult;
56 return fResult;
60 /** unary function to convert css::uno::Any into an
61 OUString.
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) )
70 return OUString();
71 return ::rtl::math::doubleToUString(
72 * pDouble,
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) )
81 return *s;
84 return OUString();
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();
102 return fResult;
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(
115 fNumber,
116 rtl_math_StringFormat_Automatic,
117 rtl_math_DecimalPlaces_Max, // use maximum decimal places available
118 '.',
119 true
124 } // namespace chart::CommonFunctors
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */