Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / fwe / helper / propertysetcontainer.cxx
blobc766b40db56bfb6f283288c3465c6868a2b0b7d9
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 <helper/propertysetcontainer.hxx>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/queryinterface.hxx>
24 #include <vcl/svapp.hxx>
26 constexpr OUString WRONG_TYPE_EXCEPTION = u"Only XPropertSet allowed!"_ustr;
28 using namespace cppu;
29 using namespace com::sun::star::uno;
30 using namespace com::sun::star::container;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::beans;
34 namespace framework
37 PropertySetContainer::PropertySetContainer()
41 PropertySetContainer::~PropertySetContainer()
45 // XInterface
46 void SAL_CALL PropertySetContainer::acquire() noexcept
48 OWeakObject::acquire();
51 void SAL_CALL PropertySetContainer::release() noexcept
53 OWeakObject::release();
56 Any SAL_CALL PropertySetContainer::queryInterface( const Type& rType )
58 Any a = ::cppu::queryInterface(
59 rType ,
60 static_cast< XIndexContainer* >(this),
61 static_cast< XIndexReplace* >(this),
62 static_cast< XIndexAccess* >(this),
63 static_cast< XElementAccess* >(this) );
65 if( a.hasValue() )
67 return a;
70 return OWeakObject::queryInterface( rType );
73 // XIndexContainer
74 void SAL_CALL PropertySetContainer::insertByIndex( sal_Int32 Index, const css::uno::Any& Element )
76 std::unique_lock g(m_aMutex);
78 sal_Int32 nSize = m_aPropertySetVector.size();
80 if ( nSize < Index )
81 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
83 Reference< XPropertySet > aPropertySetElement;
85 if ( !(Element >>= aPropertySetElement) )
87 throw IllegalArgumentException(
88 WRONG_TYPE_EXCEPTION,
89 static_cast<OWeakObject *>(this), 2 );
92 if ( nSize == Index )
93 m_aPropertySetVector.push_back( aPropertySetElement );
94 else
96 PropertySetVector::iterator aIter = m_aPropertySetVector.begin();
97 aIter += Index;
98 m_aPropertySetVector.insert( aIter, aPropertySetElement );
102 void SAL_CALL PropertySetContainer::removeByIndex( sal_Int32 nIndex )
104 std::unique_lock g(m_aMutex);
106 if ( static_cast<sal_Int32>(m_aPropertySetVector.size()) <= nIndex )
107 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
109 m_aPropertySetVector.erase(m_aPropertySetVector.begin() + nIndex);
112 // XIndexReplace
113 void SAL_CALL PropertySetContainer::replaceByIndex( sal_Int32 Index, const css::uno::Any& Element )
115 std::unique_lock g(m_aMutex);
117 if ( static_cast<sal_Int32>(m_aPropertySetVector.size()) <= Index )
118 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
120 Reference< XPropertySet > aPropertySetElement;
122 if ( !(Element >>= aPropertySetElement) )
124 throw IllegalArgumentException(
125 WRONG_TYPE_EXCEPTION,
126 static_cast<OWeakObject *>(this), 2 );
129 m_aPropertySetVector[ Index ] = aPropertySetElement;
132 // XIndexAccess
133 sal_Int32 SAL_CALL PropertySetContainer::getCount()
135 std::unique_lock g(m_aMutex);
137 return m_aPropertySetVector.size();
140 Any SAL_CALL PropertySetContainer::getByIndex( sal_Int32 Index )
142 std::unique_lock g(m_aMutex);
144 if ( static_cast<sal_Int32>(m_aPropertySetVector.size()) <= Index )
145 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
147 return Any(m_aPropertySetVector[ Index ]);
150 // XElementAccess
151 sal_Bool SAL_CALL PropertySetContainer::hasElements()
153 std::unique_lock g(m_aMutex);
155 return !( m_aPropertySetVector.empty() );
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */