cid#1607171 Data race condition
[LibreOffice.git] / reportdesign / source / core / api / Functions.cxx
blob4ac080febf1f8c8844c4006ff574d1df315cd171
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 #include <Functions.hxx>
20 #include <Function.hxx>
21 #include <core_resource.hxx>
22 #include <strings.hrc>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <com/sun/star/lang/NoSupportException.hpp>
25 #include <o3tl/safeint.hxx>
26 #include <utility>
28 namespace reportdesign
31 using namespace com::sun::star;
33 OFunctions::OFunctions(const uno::Reference< report::XFunctionsSupplier >& _xParent,uno::Reference< uno::XComponentContext > context)
34 :FunctionsBase(m_aMutex)
35 ,m_aContainerListeners(m_aMutex)
36 ,m_xContext(std::move(context))
37 ,m_xParent(_xParent)
41 // TODO: VirtualFunctionFinder: This is virtual function!
43 OFunctions::~OFunctions()
47 void SAL_CALL OFunctions::dispose()
49 cppu::WeakComponentImplHelperBase::dispose();
52 // TODO: VirtualFunctionFinder: This is virtual function!
54 void SAL_CALL OFunctions::disposing()
56 for (auto& rFunction : m_aFunctions)
57 rFunction->dispose();
58 m_aFunctions.clear();
59 lang::EventObject aDisposeEvent( getXWeak() );
60 m_aContainerListeners.disposeAndClear( aDisposeEvent );
61 m_xContext.clear();
64 // XFunctionsSupplier
66 uno::Reference< report::XFunction > SAL_CALL OFunctions::createFunction( )
68 return new OFunction(m_xContext);
71 // XIndexContainer
72 void SAL_CALL OFunctions::insertByIndex( ::sal_Int32 Index, const uno::Any& aElement )
75 ::osl::MutexGuard aGuard(m_aMutex);
76 bool bAdd = (Index == static_cast<sal_Int32>(m_aFunctions.size()));
77 if ( !bAdd )
78 checkIndex(Index);
79 uno::Reference< report::XFunction > xFunction(aElement,uno::UNO_QUERY);
80 if ( !xFunction.is() )
81 throw lang::IllegalArgumentException(RptResId(RID_STR_ARGUMENT_IS_NULL),*this,2);
83 if ( bAdd )
84 m_aFunctions.push_back(xFunction);
85 else
87 TFunctions::iterator aPos = m_aFunctions.begin();
88 ::std::advance(aPos,Index);
89 m_aFunctions.insert(aPos, xFunction);
91 xFunction->setParent(*this);
93 // notify our container listeners
94 container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::Any(Index), aElement, uno::Any());
95 m_aContainerListeners.notifyEach(&container::XContainerListener::elementInserted,aEvent);
99 void SAL_CALL OFunctions::removeByIndex( ::sal_Int32 Index )
101 uno::Reference< report::XFunction > xFunction;
103 ::osl::MutexGuard aGuard(m_aMutex);
104 checkIndex(Index);
105 TFunctions::iterator aPos = m_aFunctions.begin();
106 ::std::advance(aPos,Index);
107 xFunction = *aPos;
108 m_aFunctions.erase(aPos);
109 xFunction->setParent(nullptr);
111 container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::Any(Index), uno::Any(xFunction), uno::Any());
112 m_aContainerListeners.notifyEach(&container::XContainerListener::elementRemoved,aEvent);
115 // XIndexReplace
116 void SAL_CALL OFunctions::replaceByIndex( ::sal_Int32 Index, const uno::Any& Element )
118 uno::Any aOldElement;
120 ::osl::MutexGuard aGuard(m_aMutex);
121 checkIndex(Index);
122 uno::Reference< report::XFunction > xFunction(Element,uno::UNO_QUERY);
123 if ( !xFunction.is() )
124 throw lang::IllegalArgumentException(RptResId(RID_STR_ARGUMENT_IS_NULL),*this,2);
125 TFunctions::iterator aPos = m_aFunctions.begin();
126 ::std::advance(aPos,Index);
127 aOldElement <<= *aPos;
128 *aPos = std::move(xFunction);
131 container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::Any(Index), Element, aOldElement);
132 m_aContainerListeners.notifyEach(&container::XContainerListener::elementReplaced,aEvent);
135 // XIndexAccess
136 ::sal_Int32 SAL_CALL OFunctions::getCount( )
138 ::osl::MutexGuard aGuard(m_aMutex);
139 return m_aFunctions.size();
142 uno::Any SAL_CALL OFunctions::getByIndex( ::sal_Int32 Index )
144 ::osl::MutexGuard aGuard(m_aMutex);
145 checkIndex(Index);
146 TFunctions::const_iterator aPos = m_aFunctions.begin();
147 ::std::advance(aPos,Index);
148 return uno::Any(*aPos);
151 // XElementAccess
152 uno::Type SAL_CALL OFunctions::getElementType( )
154 return cppu::UnoType<report::XFunction>::get();
157 sal_Bool SAL_CALL OFunctions::hasElements( )
159 ::osl::MutexGuard aGuard(m_aMutex);
160 return !m_aFunctions.empty();
163 // XChild
164 uno::Reference< uno::XInterface > SAL_CALL OFunctions::getParent( )
166 return m_xParent;
169 void SAL_CALL OFunctions::setParent( const uno::Reference< uno::XInterface >& /*Parent*/ )
171 throw lang::NoSupportException();
174 // XContainer
175 void SAL_CALL OFunctions::addContainerListener( const uno::Reference< container::XContainerListener >& xListener )
177 m_aContainerListeners.addInterface(xListener);
180 void SAL_CALL OFunctions::removeContainerListener( const uno::Reference< container::XContainerListener >& xListener )
182 m_aContainerListeners.removeInterface(xListener);
185 void OFunctions::checkIndex(sal_Int32 _nIndex)
187 if ( _nIndex < 0 || m_aFunctions.size() <= o3tl::make_unsigned(_nIndex) )
188 throw lang::IndexOutOfBoundsException();
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */