Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / WrappedPropertySet.cxx
blob9eee3f8632e896d933b7de48a2b557a409603c80
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 <WrappedPropertySet.hxx>
21 #include <cppuhelper/propshlp.hxx>
23 #include <comphelper/diagnose_ex.hxx>
24 #include <sal/log.hxx>
26 namespace chart
29 using namespace ::com::sun::star;
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::uno::Sequence;
32 using ::com::sun::star::uno::Any;
34 WrappedPropertySet::WrappedPropertySet()
37 WrappedPropertySet::~WrappedPropertySet()
39 clearWrappedPropertySet();
42 Reference< beans::XPropertyState > WrappedPropertySet::getInnerPropertyState()
44 return Reference< beans::XPropertyState >( getInnerPropertySet(), uno::UNO_QUERY );
47 void WrappedPropertySet::clearWrappedPropertySet()
49 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
51 m_pPropertyArrayHelper.reset();
52 m_pWrappedPropertyMap.reset();
54 m_xInfo = nullptr;
57 //XPropertySet
58 Reference< beans::XPropertySetInfo > SAL_CALL WrappedPropertySet::getPropertySetInfo( )
60 Reference< beans::XPropertySetInfo > xInfo = m_xInfo;
61 if( !xInfo.is() )
63 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
64 xInfo = m_xInfo;
65 if( !xInfo.is() )
67 xInfo = ::cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
68 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
69 m_xInfo = xInfo;
72 else
74 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
76 return m_xInfo;
79 void SAL_CALL WrappedPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
81 try
83 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
84 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
85 Reference< beans::XPropertySet > xInnerPropertySet( getInnerPropertySet() );
86 if( pWrappedProperty )
87 pWrappedProperty->setPropertyValue( rValue, xInnerPropertySet );
88 else if( xInnerPropertySet.is() )
89 xInnerPropertySet->setPropertyValue( rPropertyName, rValue );
90 else
92 SAL_WARN("chart2.tools", "found no inner property set to map to");
95 catch( const beans::UnknownPropertyException& )
97 throw;
99 catch( const beans::PropertyVetoException& )
101 throw;
103 catch( const lang::IllegalArgumentException& )
105 throw;
107 catch( const lang::WrappedTargetException& )
109 throw;
111 catch( const uno::RuntimeException& )
113 throw;
115 catch( const uno::Exception& ex )
117 css::uno::Any anyEx = cppu::getCaughtException();
118 TOOLS_WARN_EXCEPTION( "chart2", "invalid exception caught in WrappedPropertySet::setPropertyValue");
119 throw lang::WrappedTargetException( ex.Message, nullptr, anyEx );
122 Any SAL_CALL WrappedPropertySet::getPropertyValue( const OUString& rPropertyName )
124 Any aRet;
128 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
129 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
130 Reference< beans::XPropertySet > xInnerPropertySet( getInnerPropertySet() );
131 if( pWrappedProperty )
132 aRet = pWrappedProperty->getPropertyValue( xInnerPropertySet );
133 else if( xInnerPropertySet.is() )
134 aRet = xInnerPropertySet->getPropertyValue( rPropertyName );
135 else
137 SAL_WARN("chart2.tools", "found no inner property set to map to");
140 catch( const beans::UnknownPropertyException& )
142 throw;
144 catch( const lang::WrappedTargetException& )
146 throw;
148 catch( const uno::RuntimeException& )
150 throw;
152 catch( const uno::Exception& ex )
154 css::uno::Any anyEx = cppu::getCaughtException();
155 TOOLS_WARN_EXCEPTION( "chart2", "invalid exception caught in WrappedPropertySet::setPropertyValue");
156 throw lang::WrappedTargetException( ex.Message, nullptr, anyEx );
159 return aRet;
162 void SAL_CALL WrappedPropertySet::addPropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
164 Reference< beans::XPropertySet > xInnerPropertySet( getInnerPropertySet() );
165 if( xInnerPropertySet.is() )
167 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
168 if( pWrappedProperty )
169 xInnerPropertySet->addPropertyChangeListener( pWrappedProperty->getInnerName(), xListener );
170 else
171 xInnerPropertySet->addPropertyChangeListener( rPropertyName, xListener );
174 void SAL_CALL WrappedPropertySet::removePropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& aListener )
176 Reference< beans::XPropertySet > xInnerPropertySet( getInnerPropertySet() );
177 if( xInnerPropertySet.is() )
179 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
180 if( pWrappedProperty )
181 xInnerPropertySet->removePropertyChangeListener( pWrappedProperty->getInnerName(), aListener );
182 else
183 xInnerPropertySet->removePropertyChangeListener( rPropertyName, aListener );
186 void SAL_CALL WrappedPropertySet::addVetoableChangeListener( const OUString& rPropertyName, const Reference< beans::XVetoableChangeListener >& aListener )
188 Reference< beans::XPropertySet > xInnerPropertySet( getInnerPropertySet() );
189 if( xInnerPropertySet.is() )
191 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
192 if( pWrappedProperty )
193 xInnerPropertySet->addVetoableChangeListener( pWrappedProperty->getInnerName(), aListener );
194 else
195 xInnerPropertySet->addVetoableChangeListener( rPropertyName, aListener );
198 void SAL_CALL WrappedPropertySet::removeVetoableChangeListener( const OUString& rPropertyName, const Reference< beans::XVetoableChangeListener >& aListener )
200 Reference< beans::XPropertySet > xInnerPropertySet( getInnerPropertySet() );
201 if( xInnerPropertySet.is() )
203 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
204 if( pWrappedProperty )
205 xInnerPropertySet->removeVetoableChangeListener( pWrappedProperty->getInnerName(), aListener );
206 else
207 xInnerPropertySet->removeVetoableChangeListener( rPropertyName, aListener );
211 //XMultiPropertySet
212 void SAL_CALL WrappedPropertySet::setPropertyValues( const Sequence< OUString >& rNameSeq, const Sequence< Any >& rValueSeq )
214 bool bUnknownProperty = false;
215 sal_Int32 nMinCount = std::min( rValueSeq.getLength(), rNameSeq.getLength() );
216 for(sal_Int32 nN=0; nN<nMinCount; nN++)
218 OUString aPropertyName( rNameSeq[nN] );
221 setPropertyValue( aPropertyName, rValueSeq[nN] );
223 catch( const beans::UnknownPropertyException& )
225 DBG_UNHANDLED_EXCEPTION("chart2");
226 bUnknownProperty = true;
229 //todo: store unknown properties elsewhere
230 OSL_ENSURE(!bUnknownProperty,"unknown property");
231 // if( bUnknownProperty )
232 // throw beans::UnknownPropertyException();
234 Sequence< Any > SAL_CALL WrappedPropertySet::getPropertyValues( const Sequence< OUString >& rNameSeq )
236 Sequence< Any > aRetSeq;
237 if( rNameSeq.hasElements() )
239 aRetSeq.realloc( rNameSeq.getLength() );
240 auto pRetSeq = aRetSeq.getArray();
241 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
245 OUString aPropertyName( rNameSeq[nN] );
246 pRetSeq[nN] = getPropertyValue( aPropertyName );
248 catch( const beans::UnknownPropertyException& )
250 DBG_UNHANDLED_EXCEPTION("chart2");
252 catch( const lang::WrappedTargetException& )
254 DBG_UNHANDLED_EXCEPTION("chart2");
258 return aRetSeq;
260 void SAL_CALL WrappedPropertySet::addPropertiesChangeListener( const Sequence< OUString >& /* rNameSeq */, const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
262 OSL_FAIL("not implemented yet");
263 //todo
265 void SAL_CALL WrappedPropertySet::removePropertiesChangeListener( const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
267 OSL_FAIL("not implemented yet");
268 //todo
270 void SAL_CALL WrappedPropertySet::firePropertiesChangeEvent( const Sequence< OUString >& /* rNameSeq */, const Reference< beans::XPropertiesChangeListener >& /* xListener */ )
272 OSL_FAIL("not implemented yet");
273 //todo
276 //XPropertyState
277 beans::PropertyState SAL_CALL WrappedPropertySet::getPropertyState( const OUString& rPropertyName )
279 beans::PropertyState aState( beans::PropertyState_DIRECT_VALUE );
281 Reference< beans::XPropertyState > xInnerPropertyState( getInnerPropertyState() );
282 if( xInnerPropertyState.is() )
284 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
285 if( pWrappedProperty )
286 aState = pWrappedProperty->getPropertyState( xInnerPropertyState );
287 else
288 aState = xInnerPropertyState->getPropertyState( rPropertyName );
290 return aState;
293 const WrappedProperty* WrappedPropertySet::getWrappedProperty( const OUString& rOuterName )
295 sal_Int32 nHandle = getInfoHelper().getHandleByName( rOuterName );
296 return getWrappedProperty( nHandle );
299 const WrappedProperty* WrappedPropertySet::getWrappedProperty( sal_Int32 nHandle )
301 tWrappedPropertyMap::const_iterator aFound( getWrappedPropertyMap().find( nHandle ) );
302 if( aFound != getWrappedPropertyMap().end() )
303 return (*aFound).second.get();
304 return nullptr;
307 Sequence< beans::PropertyState > SAL_CALL WrappedPropertySet::getPropertyStates( const Sequence< OUString >& rNameSeq )
309 Sequence< beans::PropertyState > aRetSeq;
310 if( rNameSeq.hasElements() )
312 aRetSeq.realloc( rNameSeq.getLength() );
313 auto pRetSeq = aRetSeq.getArray();
314 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
316 OUString aPropertyName( rNameSeq[nN] );
317 pRetSeq[nN] = getPropertyState( aPropertyName );
320 return aRetSeq;
323 void SAL_CALL WrappedPropertySet::setPropertyToDefault( const OUString& rPropertyName )
325 Reference< beans::XPropertyState > xInnerPropertyState( getInnerPropertyState() );
326 if( xInnerPropertyState.is() )
328 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
329 if( pWrappedProperty )
330 pWrappedProperty->setPropertyToDefault( xInnerPropertyState );
331 else
332 xInnerPropertyState->setPropertyToDefault( rPropertyName );
335 Any SAL_CALL WrappedPropertySet::getPropertyDefault( const OUString& rPropertyName )
337 Any aRet;
338 Reference< beans::XPropertyState > xInnerPropertyState( getInnerPropertyState() );
339 if( xInnerPropertyState.is() )
341 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
342 if( pWrappedProperty )
343 aRet = pWrappedProperty->getPropertyDefault(xInnerPropertyState);
344 else
345 aRet = xInnerPropertyState->getPropertyDefault( rPropertyName );
347 return aRet;
350 //XMultiPropertyStates
351 void SAL_CALL WrappedPropertySet::setAllPropertiesToDefault( )
353 const Sequence< beans::Property >& rPropSeq = getPropertySequence();
354 for(beans::Property const & prop : rPropSeq)
356 setPropertyToDefault( prop.Name );
359 void SAL_CALL WrappedPropertySet::setPropertiesToDefault( const Sequence< OUString >& rNameSeq )
361 for(OUString const & s : rNameSeq)
363 setPropertyToDefault( s );
366 Sequence< Any > SAL_CALL WrappedPropertySet::getPropertyDefaults( const Sequence< OUString >& rNameSeq )
368 Sequence< Any > aRetSeq;
369 if( rNameSeq.hasElements() )
371 aRetSeq.realloc( rNameSeq.getLength() );
372 auto pRetSeq = aRetSeq.getArray();
373 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
375 OUString aPropertyName( rNameSeq[nN] );
376 pRetSeq[nN] = getPropertyDefault( aPropertyName );
379 return aRetSeq;
382 ::cppu::IPropertyArrayHelper& WrappedPropertySet::getInfoHelper()
384 ::cppu::OPropertyArrayHelper* p = m_pPropertyArrayHelper.get();
385 if(!p)
387 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
388 p = m_pPropertyArrayHelper.get();
389 if(!p)
391 p = new ::cppu::OPropertyArrayHelper( getPropertySequence(), true );
392 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
393 m_pPropertyArrayHelper.reset(p);
396 else
398 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
400 return *m_pPropertyArrayHelper;
403 tWrappedPropertyMap& WrappedPropertySet::getWrappedPropertyMap()
405 tWrappedPropertyMap* p = m_pWrappedPropertyMap.get();
406 if(!p)
408 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );//do not use different mutex than is already used for static property sequence
409 p = m_pWrappedPropertyMap.get();
410 if(!p)
412 std::vector< std::unique_ptr<WrappedProperty> > aPropList( createWrappedProperties() );
413 p = new tWrappedPropertyMap;
415 for (auto & elem : aPropList)
417 sal_Int32 nHandle = getInfoHelper().getHandleByName( elem->getOuterName() );
419 if( nHandle == -1 )
421 OSL_FAIL( "missing property in property list" );
423 else if( p->find( nHandle ) != p->end() )
425 //duplicate Wrapped property
426 OSL_FAIL( "duplicate Wrapped property" );
428 else
429 (*p)[ nHandle ] = std::move(elem);
432 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
433 m_pWrappedPropertyMap.reset(p);
436 else
438 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
440 return *m_pWrappedPropertyMap;
443 } //namespace chart
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */