Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / CachedDataSequence.cxx
blob23f5add5f85f03ff0f7206b28d47561d430de5ba
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 .
20 #include <CachedDataSequence.hxx>
21 #include <CommonFunctors.hxx>
22 #include <ModifyListenerHelper.hxx>
24 #include <comphelper/sequenceashashmap.hxx>
25 #include <cppuhelper/supportsservice.hxx>
27 #include <algorithm>
29 using namespace ::com::sun::star;
31 using ::com::sun::star::uno::Sequence;
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::Any;
34 using ::osl::MutexGuard;
36 // necessary for MS compiler
37 using ::comphelper::OPropertyContainer;
38 using ::chart::impl::CachedDataSequence_Base;
40 namespace
42 constexpr OUString lcl_aServiceName = u"com.sun.star.comp.chart.CachedDataSequence"_ustr;
44 enum
46 // PROP_SOURCE_IDENTIFIER,
47 PROP_NUMBERFORMAT_KEY,
48 PROP_PROPOSED_ROLE
50 } // anonymous namespace
52 namespace chart
55 CachedDataSequence::CachedDataSequence()
56 : OPropertyContainer( GetBroadcastHelper()),
57 CachedDataSequence_Base( GetMutex()),
58 m_eCurrentDataType( NUMERICAL ),
59 m_xModifyEventForwarder( new ModifyEventForwarder() )
61 registerProperties();
63 CachedDataSequence::CachedDataSequence( const Reference< uno::XComponentContext > & /*xContext*/ )
64 : OPropertyContainer( GetBroadcastHelper()),
65 CachedDataSequence_Base( GetMutex()),
66 m_eCurrentDataType( MIXED ),
67 m_xModifyEventForwarder( new ModifyEventForwarder() )
69 registerProperties();
72 CachedDataSequence::CachedDataSequence( const OUString & rSingleText )
73 : OPropertyContainer( GetBroadcastHelper()),
74 CachedDataSequence_Base( GetMutex()),
75 m_eCurrentDataType( TEXTUAL ),
76 m_aTextualSequence({rSingleText}),
77 m_xModifyEventForwarder( new ModifyEventForwarder() )
79 registerProperties();
82 CachedDataSequence::CachedDataSequence( const CachedDataSequence & rSource )
83 : OPropertyContainer( GetBroadcastHelper()),
84 CachedDataSequence_Base( GetMutex()),
85 m_nNumberFormatKey( rSource.m_nNumberFormatKey ),
86 m_sRole( rSource.m_sRole ),
87 m_eCurrentDataType( rSource.m_eCurrentDataType ),
88 m_xModifyEventForwarder( new ModifyEventForwarder() )
90 switch( m_eCurrentDataType )
92 case TEXTUAL:
93 m_aTextualSequence = rSource.m_aTextualSequence;
94 break;
95 case NUMERICAL:
96 m_aNumericalSequence = rSource.m_aNumericalSequence;
97 break;
98 case MIXED:
99 m_aMixedSequence = rSource.m_aMixedSequence;
100 break;
103 registerProperties();
106 CachedDataSequence::~CachedDataSequence()
109 void CachedDataSequence::registerProperties()
111 registerProperty( "NumberFormatKey",
112 PROP_NUMBERFORMAT_KEY,
113 0, // PropertyAttributes
114 & m_nNumberFormatKey,
115 cppu::UnoType<decltype(m_nNumberFormatKey)>::get() );
117 registerProperty( "Role",
118 PROP_PROPOSED_ROLE,
119 0, // PropertyAttributes
120 & m_sRole,
121 cppu::UnoType<decltype(m_sRole)>::get() );
124 Sequence< double > CachedDataSequence::Impl_getNumericalData() const
126 if( m_eCurrentDataType == NUMERICAL )
127 return m_aNumericalSequence;
129 sal_Int32 nSize = ( m_eCurrentDataType == TEXTUAL )
130 ? m_aTextualSequence.getLength()
131 : m_aMixedSequence.getLength();
133 Sequence< double > aResult( nSize );
134 double * pResultArray = aResult.getArray();
136 if( m_eCurrentDataType == TEXTUAL )
138 const OUString * pTextArray = m_aTextualSequence.getConstArray();
139 std::transform( pTextArray, pTextArray + nSize,
140 pResultArray,
141 CommonFunctors::OUStringToDouble() );
143 else
145 OSL_ASSERT( m_eCurrentDataType == MIXED );
146 const Any * pMixedArray = m_aMixedSequence.getConstArray();
147 std::transform( pMixedArray, pMixedArray + nSize,
148 pResultArray,
149 CommonFunctors::AnyToDouble() );
151 return aResult;
154 Sequence< OUString > CachedDataSequence::Impl_getTextualData() const
156 if( m_eCurrentDataType == TEXTUAL )
157 return m_aTextualSequence;
159 sal_Int32 nSize = ( m_eCurrentDataType == NUMERICAL )
160 ? m_aNumericalSequence.getLength()
161 : m_aMixedSequence.getLength();
163 Sequence< OUString > aResult( nSize );
164 OUString * pResultArray = aResult.getArray();
166 if( m_eCurrentDataType == NUMERICAL )
168 const double * pTextArray = m_aNumericalSequence.getConstArray();
169 std::transform( pTextArray, pTextArray + nSize,
170 pResultArray,
171 CommonFunctors::DoubleToOUString() );
173 else
175 OSL_ASSERT( m_eCurrentDataType == MIXED );
176 const Any * pMixedArray = m_aMixedSequence.getConstArray();
177 std::transform( pMixedArray, pMixedArray + nSize,
178 pResultArray,
179 CommonFunctors::AnyToString() );
182 return aResult;
185 Sequence< Any > CachedDataSequence::Impl_getMixedData() const
187 if( m_eCurrentDataType == MIXED )
188 return m_aMixedSequence;
190 sal_Int32 nSize = ( m_eCurrentDataType == NUMERICAL )
191 ? m_aNumericalSequence.getLength()
192 : m_aTextualSequence.getLength();
194 Sequence< Any > aResult( nSize );
195 Any * pResultArray = aResult.getArray();
197 if( m_eCurrentDataType == NUMERICAL )
199 const double * pTextArray = m_aNumericalSequence.getConstArray();
200 std::transform( pTextArray, pTextArray + nSize,
201 pResultArray,
202 CommonFunctors::makeAny< double >() );
204 else
206 OSL_ASSERT( m_eCurrentDataType == TEXTUAL );
207 const OUString * pMixedArray = m_aTextualSequence.getConstArray();
208 std::transform( pMixedArray, pMixedArray + nSize,
209 pResultArray,
210 CommonFunctors::makeAny< OUString >() );
213 return aResult;
216 IMPLEMENT_FORWARD_XINTERFACE2( CachedDataSequence, CachedDataSequence_Base, OPropertyContainer )
217 IMPLEMENT_FORWARD_XTYPEPROVIDER2( CachedDataSequence, CachedDataSequence_Base, OPropertyContainer )
219 // ____ XPropertySet ____
220 Reference< beans::XPropertySetInfo > SAL_CALL CachedDataSequence::getPropertySetInfo()
222 return createPropertySetInfo( getInfoHelper() );
225 // ____ ::comphelper::OPropertySetHelper ____
226 ::cppu::IPropertyArrayHelper& CachedDataSequence::getInfoHelper()
228 return *getArrayHelper();
231 // ____ ::comphelper::OPropertyArrayHelper ____
232 ::cppu::IPropertyArrayHelper* CachedDataSequence::createArrayHelper() const
234 Sequence< beans::Property > aProps;
235 // describes all properties which have been registered in the ctor
236 describeProperties( aProps );
238 return new ::cppu::OPropertyArrayHelper( aProps );
241 OUString SAL_CALL CachedDataSequence::getImplementationName()
243 return lcl_aServiceName;
246 sal_Bool SAL_CALL CachedDataSequence::supportsService( const OUString& rServiceName )
248 return cppu::supportsService(this, rServiceName);
251 css::uno::Sequence< OUString > SAL_CALL CachedDataSequence::getSupportedServiceNames()
253 return {
254 lcl_aServiceName,
255 "com.sun.star.chart2.data.DataSequence",
256 "com.sun.star.chart2.data.NumericalDataSequence",
257 "com.sun.star.chart2.data.TextualDataSequence"
261 // ________ XNumericalDataSequence ________
262 Sequence< double > SAL_CALL CachedDataSequence::getNumericalData()
264 MutexGuard aGuard( GetMutex() );
266 if( m_eCurrentDataType == NUMERICAL )
267 return m_aNumericalSequence;
268 else
269 return Impl_getNumericalData();
272 // ________ XTextualDataSequence ________
273 Sequence< OUString > SAL_CALL CachedDataSequence::getTextualData()
275 MutexGuard aGuard( GetMutex() );
277 if( m_eCurrentDataType == TEXTUAL )
278 return m_aTextualSequence;
279 else
280 return Impl_getTextualData();
283 // ________ XDataSequence ________
284 Sequence< Any > SAL_CALL CachedDataSequence::getData()
286 MutexGuard aGuard( GetMutex() );
287 return Impl_getMixedData();
290 OUString SAL_CALL CachedDataSequence::getSourceRangeRepresentation()
292 return m_sRole;
295 Sequence< OUString > SAL_CALL CachedDataSequence::generateLabel( chart2::data::LabelOrigin /*eLabelOrigin*/ )
297 // return empty label, as we have no range representations to determine something useful
298 return Sequence< OUString >();
301 ::sal_Int32 SAL_CALL CachedDataSequence::getNumberFormatKeyByIndex( ::sal_Int32 /*nIndex*/ )
303 return 0;
306 Reference< util::XCloneable > SAL_CALL CachedDataSequence::createClone()
308 return new CachedDataSequence( *this );
311 void SAL_CALL CachedDataSequence::addModifyListener( const Reference< util::XModifyListener >& aListener )
313 m_xModifyEventForwarder->addModifyListener( aListener );
316 void SAL_CALL CachedDataSequence::removeModifyListener( const Reference< util::XModifyListener >& aListener )
318 m_xModifyEventForwarder->removeModifyListener( aListener );
321 // lang::XInitialization:
322 void SAL_CALL CachedDataSequence::initialize(const uno::Sequence< uno::Any > & _aArguments)
324 ::comphelper::SequenceAsHashMap aMap(_aArguments);
325 m_aNumericalSequence = aMap.getUnpackedValueOrDefault( "DataSequence" ,m_aNumericalSequence);
326 if ( m_aNumericalSequence.hasElements() )
327 m_eCurrentDataType = NUMERICAL;
328 else
330 m_aTextualSequence = aMap.getUnpackedValueOrDefault( "DataSequence" ,m_aTextualSequence);
331 if ( m_aTextualSequence.hasElements() )
332 m_eCurrentDataType = TEXTUAL;
333 else
335 m_aMixedSequence = aMap.getUnpackedValueOrDefault( "DataSequence" ,m_aMixedSequence);
336 if ( m_aMixedSequence.hasElements() )
337 m_eCurrentDataType = MIXED;
341 } // namespace chart
343 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
344 com_sun_star_comp_chart_CachedDataSequence_get_implementation(css::uno::XComponentContext *context,
345 css::uno::Sequence<css::uno::Any> const &)
347 return cppu::acquire(new ::chart::CachedDataSequence(context));
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */