Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / unodraw / unoshcol.cxx
blobb1f3a4f4a10ec9fe568bd418dfb0ed8569a9f7df
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/drawing/XShapes.hpp>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <com/sun/star/lang/XComponent.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <cppuhelper/implbase3.hxx>
28 #include <cppuhelper/interfacecontainer.hxx>
29 #include <comphelper/interfacecontainer2.hxx>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <osl/mutex.hxx>
32 #include <sal/log.hxx>
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
37 namespace {
39 class SvxShapeCollectionMutex
41 public:
42 ::osl::Mutex maMutex;
45 class SvxShapeCollection :
46 public cppu::WeakAggImplHelper3<drawing::XShapes, lang::XServiceInfo, lang::XComponent>,
47 public SvxShapeCollectionMutex
49 private:
50 comphelper::OInterfaceContainerHelper2 maShapeContainer;
52 cppu::OBroadcastHelper mrBHelper;
54 public:
55 SvxShapeCollection() throw();
57 // XInterface
58 virtual void SAL_CALL release() throw() override;
60 // XComponent
61 virtual void SAL_CALL dispose() override;
62 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
63 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
65 // XIndexAccess
66 virtual sal_Int32 SAL_CALL getCount() override ;
67 virtual css::uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override;
69 // XElementAccess
70 virtual css::uno::Type SAL_CALL getElementType() override;
71 virtual sal_Bool SAL_CALL hasElements() override;
73 // XShapes
74 virtual void SAL_CALL add( const css::uno::Reference< css::drawing::XShape >& xShape ) override;
75 virtual void SAL_CALL remove( const css::uno::Reference< css::drawing::XShape >& xShape ) override;
77 // XServiceInfo
78 virtual OUString SAL_CALL getImplementationName() override;
79 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
80 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
83 SvxShapeCollection::SvxShapeCollection() throw()
84 : maShapeContainer( maMutex ), mrBHelper( maMutex )
88 // XInterface
89 void SvxShapeCollection::release() throw()
91 uno::Reference< uno::XInterface > x( xDelegator );
92 if (! x.is())
94 if (osl_atomic_decrement( &m_refCount ) == 0)
96 if (! mrBHelper.bDisposed)
98 uno::Reference< uno::XInterface > xHoldAlive( static_cast<uno::XWeak*>(this) );
99 // First dispose
102 dispose();
104 catch(css::uno::Exception&)
106 // release should not throw exceptions
109 // only the alive ref holds the object
110 OSL_ASSERT( m_refCount == 1 );
111 // destroy the object if xHoldAlive decrement the refcount to 0
112 return;
115 // restore the reference count
116 osl_atomic_increment( &m_refCount );
118 OWeakAggObject::release();
121 // XComponent
122 void SvxShapeCollection::dispose()
124 // An frequently programming error is to release the last
125 // reference to this object in the disposing message.
126 // Make it robust, hold a self Reference.
127 uno::Reference< lang::XComponent > xSelf( this );
129 // Guard dispose against multiple threading
130 // Remark: It is an error to call dispose more than once
131 bool bDoDispose = false;
133 osl::MutexGuard aGuard( mrBHelper.rMutex );
134 if( !mrBHelper.bDisposed && !mrBHelper.bInDispose )
136 // only one call go into this section
137 mrBHelper.bInDispose = true;
138 bDoDispose = true;
142 // Do not hold the mutex because we are broadcasting
143 if( bDoDispose )
145 // Create an event with this as sender
148 uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( static_cast<lang::XComponent *>(this) ) );
149 document::EventObject aEvt;
150 aEvt.Source = xSource;
151 // inform all listeners to release this object
152 // The listener container are automatically cleared
153 mrBHelper.aLC.disposeAndClear( aEvt );
154 maShapeContainer.clear();
156 catch(const css::uno::Exception&)
158 // catch exception and throw again but signal that
159 // the object was disposed. Dispose should be called
160 // only once.
161 mrBHelper.bDisposed = true;
162 mrBHelper.bInDispose = false;
163 throw;
166 // the values bDispose and bInDisposing must set in this order.
167 // No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
168 mrBHelper.bDisposed = true;
169 mrBHelper.bInDispose = false;
171 else
173 // in a multithreaded environment, it can't be avoided, that dispose is called twice.
174 // However this condition is traced, because it MAY indicate an error.
175 SAL_INFO("svx", "dispose called twice" );
179 // XComponent
180 void SAL_CALL SvxShapeCollection::addEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
182 mrBHelper.addListener( cppu::UnoType<decltype(aListener)>::get() , aListener );
185 // XComponent
186 void SAL_CALL SvxShapeCollection::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
188 mrBHelper.removeListener( cppu::UnoType<decltype(aListener)>::get() , aListener );
191 // XShapes
193 void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape )
195 maShapeContainer.addInterface( xShape );
199 void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape )
201 maShapeContainer.removeInterface( xShape );
205 sal_Int32 SAL_CALL SvxShapeCollection::getCount()
207 return maShapeContainer.getLength();
211 uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
213 if( Index < 0 || Index >= getCount() )
214 throw lang::IndexOutOfBoundsException();
216 std::vector< Reference< uno::XInterface> > aElements( maShapeContainer.getElements() );
219 return uno::makeAny( Reference< drawing::XShape>(static_cast< drawing::XShape* >( aElements[Index].get())) );
222 // XElementAccess
223 uno::Type SAL_CALL SvxShapeCollection::getElementType()
225 return cppu::UnoType<drawing::XShape>::get();
228 sal_Bool SAL_CALL SvxShapeCollection::hasElements()
230 return getCount() != 0;
233 // XServiceInfo
234 OUString SAL_CALL SvxShapeCollection::getImplementationName()
236 return "com.sun.star.drawing.SvxShapeCollection";
239 sal_Bool SAL_CALL SvxShapeCollection::supportsService( const OUString& ServiceName )
241 return cppu::supportsService( this, ServiceName);
244 uno::Sequence< OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames()
246 return { "com.sun.star.drawing.Shapes", "com.sun.star.drawing.ShapeCollection" };
251 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
252 com_sun_star_drawing_SvxShapeCollection_get_implementation(
253 css::uno::XComponentContext *,
254 css::uno::Sequence<css::uno::Any> const &)
256 return cppu::acquire(new SvxShapeCollection);
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */