Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / unodraw / unoshcol.cxx
blobc7fadc62014713b6cdf4444b1081a909d04651e9
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 <com/sun/star/document/EventObject.hpp>
21 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <osl/diagnose.h>
25 #include <sal/log.hxx>
26 #include <shapecollection.hxx>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
31 SvxShapeCollection::SvxShapeCollection() noexcept
35 // XInterface
36 void SvxShapeCollection::release() noexcept
38 uno::Reference< uno::XInterface > x( xDelegator );
39 if (! x.is())
41 if (osl_atomic_decrement( &m_refCount ) == 0)
43 if (! bDisposed)
45 uno::Reference< uno::XInterface > xHoldAlive( static_cast<uno::XWeak*>(this) );
46 // First dispose
47 try
49 dispose();
51 catch(css::uno::Exception&)
53 // release should not throw exceptions
56 // only the alive ref holds the object
57 OSL_ASSERT( m_refCount == 1 );
58 // destroy the object if xHoldAlive decrement the refcount to 0
59 return;
62 // restore the reference count
63 osl_atomic_increment( &m_refCount );
65 OWeakAggObject::release();
68 // XComponent
69 void SvxShapeCollection::dispose()
71 // An frequently programming error is to release the last
72 // reference to this object in the disposing message.
73 // Make it robust, hold a self Reference.
74 uno::Reference< lang::XComponent > xSelf( this );
76 // Guard dispose against multiple threading
77 // Remark: It is an error to call dispose more than once
78 bool bDoDispose = false;
80 std::unique_lock aGuard( m_aMutex );
81 if( !bDisposed && !bInDispose )
83 // only one call go into this section
84 bInDispose = true;
85 bDoDispose = true;
89 // Do not hold the mutex because we are broadcasting
90 if( bDoDispose )
92 // Create an event with this as sender
93 try
95 uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( static_cast<lang::XComponent *>(this) ) );
96 document::EventObject aEvt;
97 aEvt.Source = xSource;
98 // inform all listeners to release this object
99 // The listener container are automatically cleared
100 std::unique_lock g(m_aMutex);
101 maEventListeners.disposeAndClear( g, aEvt );
102 maShapeContainer.clear();
104 catch(const css::uno::Exception&)
106 // catch exception and throw again but signal that
107 // the object was disposed. Dispose should be called
108 // only once.
109 bDisposed = true;
110 bInDispose = false;
111 throw;
114 // the values bDispose and bInDisposing must set in this order.
115 // No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
116 bDisposed = true;
117 bInDispose = false;
119 else
121 // in a multithreaded environment, it can't be avoided, that dispose is called twice.
122 // However this condition is traced, because it MAY indicate an error.
123 SAL_INFO("svx", "dispose called twice" );
127 // XComponent
128 void SAL_CALL SvxShapeCollection::addEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
130 std::unique_lock g(m_aMutex);
131 maEventListeners.addInterface( g, aListener );
134 // XComponent
135 void SAL_CALL SvxShapeCollection::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
137 std::unique_lock g(m_aMutex);
138 maEventListeners.removeInterface( g, aListener );
141 // XShapes
143 void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape )
145 std::unique_lock g(m_aMutex);
146 maShapeContainer.push_back( xShape );
150 void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape )
152 std::unique_lock g(m_aMutex);
153 maShapeContainer.erase(std::remove(maShapeContainer.begin(), maShapeContainer.end(), xShape), maShapeContainer.end());
157 sal_Int32 SAL_CALL SvxShapeCollection::getCount()
159 std::unique_lock g(m_aMutex);
160 return maShapeContainer.size();
164 uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
166 if( Index < 0 || Index >= getCount() )
167 throw lang::IndexOutOfBoundsException();
169 std::unique_lock g(m_aMutex);
170 Reference<drawing::XShape> xShape = maShapeContainer[Index];
171 return uno::Any( xShape );
174 // XElementAccess
175 uno::Type SAL_CALL SvxShapeCollection::getElementType()
177 return cppu::UnoType<drawing::XShape>::get();
180 sal_Bool SAL_CALL SvxShapeCollection::hasElements()
182 return getCount() != 0;
185 // XServiceInfo
186 OUString SAL_CALL SvxShapeCollection::getImplementationName()
188 return "com.sun.star.drawing.SvxShapeCollection";
191 sal_Bool SAL_CALL SvxShapeCollection::supportsService( const OUString& ServiceName )
193 return cppu::supportsService( this, ServiceName);
196 uno::Sequence< OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames()
198 return { "com.sun.star.drawing.Shapes", "com.sun.star.drawing.ShapeCollection" };
201 void SvxShapeCollection::getAllShapes(std::vector<css::uno::Reference<css::drawing::XShape>>& rShapes) const
203 rShapes = maShapeContainer;
206 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
207 com_sun_star_drawing_SvxShapeCollection_get_implementation(
208 css::uno::XComponentContext *,
209 css::uno::Sequence<css::uno::Any> const &)
211 return cppu::acquire(new SvxShapeCollection);
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */