bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / unodraw / unoshcol.cxx
blob129d0759a57f9ba420f152bc6a3639e982f86dd7
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>
22 #include "svx/unoshcol.hxx"
23 #include <svx/unoprov.hxx>
24 #include <comphelper/serviceinfohelper.hxx>
26 using namespace ::cppu;
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::lang;
30 using namespace ::com::sun::star::container;
31 using namespace ::com::sun::star::drawing;
33 /***********************************************************************
34 * *
35 ***********************************************************************/
36 SvxShapeCollection::SvxShapeCollection() throw()
37 : maShapeContainer( maMutex ), mrBHelper( maMutex )
41 //----------------------------------------------------------------------
42 SvxShapeCollection::~SvxShapeCollection() throw()
47 //----------------------------------------------------------------------
48 Reference< uno::XInterface > SvxShapeCollection_NewInstance() throw()
50 Reference< drawing::XShapes > xShapes( new SvxShapeCollection() );
51 Reference< uno::XInterface > xRef( xShapes, UNO_QUERY );
52 return xRef;
55 // XInterface
56 void SvxShapeCollection::release() throw()
58 uno::Reference< uno::XInterface > x( xDelegator );
59 if (! x.is())
61 if (osl_atomic_decrement( &m_refCount ) == 0)
63 if (! mrBHelper.bDisposed)
65 uno::Reference< uno::XInterface > xHoldAlive( (uno::XWeak*)this );
66 // First dispose
67 try
69 dispose();
71 catch(::com::sun::star::uno::Exception&)
73 // release should not throw exceptions
76 // only the alive ref holds the object
77 OSL_ASSERT( m_refCount == 1 );
78 // destroy the object if xHoldAlive decrement the refcount to 0
79 return;
82 // restore the reference count
83 osl_atomic_increment( &m_refCount );
85 OWeakAggObject::release();
88 // XComponent
89 void SvxShapeCollection::disposing() throw()
91 maShapeContainer.clear();
94 // XComponent
95 void SvxShapeCollection::dispose()
96 throw(::com::sun::star::uno::RuntimeException)
98 // An frequently programming error is to release the last
99 // reference to this object in the disposing message.
100 // Make it rubust, hold a self Reference.
101 uno::Reference< lang::XComponent > xSelf( this );
103 // Guard dispose against multible threading
104 // Remark: It is an error to call dispose more than once
105 bool bDoDispose = false;
107 osl::MutexGuard aGuard( mrBHelper.rMutex );
108 if( !mrBHelper.bDisposed && !mrBHelper.bInDispose )
110 // only one call go into this section
111 mrBHelper.bInDispose = sal_True;
112 bDoDispose = true;
116 // Do not hold the mutex because we are broadcasting
117 if( bDoDispose )
119 // Create an event with this as sender
122 uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( (lang::XComponent *)this ) );
123 document::EventObject aEvt;
124 aEvt.Source = xSource;
125 // inform all listeners to release this object
126 // The listener container are automaticly cleared
127 mrBHelper.aLC.disposeAndClear( aEvt );
128 // notify subclasses to do their dispose
129 disposing();
131 catch(const ::com::sun::star::uno::Exception&)
133 // catch exception and throw again but signal that
134 // the object was disposed. Dispose should be called
135 // only once.
136 mrBHelper.bDisposed = sal_True;
137 mrBHelper.bInDispose = sal_False;
138 throw;
141 // the values bDispose and bInDisposing must set in this order.
142 // No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
143 mrBHelper.bDisposed = sal_True;
144 mrBHelper.bInDispose = sal_False;
146 else
148 // in a multithreaded environment, it can't be avoided, that dispose is called twice.
149 // However this condition is traced, because it MAY indicate an error.
150 OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
154 // XComponent
155 void SAL_CALL SvxShapeCollection::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
157 mrBHelper.addListener( ::getCppuType( &aListener ) , aListener );
160 // XComponent
161 void SAL_CALL SvxShapeCollection::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
163 mrBHelper.removeListener( ::getCppuType( &aListener ) , aListener );
166 // XShapes
167 //----------------------------------------------------------------------
168 void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
170 maShapeContainer.addInterface( xShape );
173 //----------------------------------------------------------------------
174 void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
176 maShapeContainer.removeInterface( xShape );
179 //----------------------------------------------------------------------
180 sal_Int32 SAL_CALL SvxShapeCollection::getCount() throw( uno::RuntimeException )
182 return maShapeContainer.getLength();
185 //----------------------------------------------------------------------
186 uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
187 throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
189 if( Index < 0 || Index >= getCount() )
190 throw lang::IndexOutOfBoundsException();
192 uno::Sequence< Reference< uno::XInterface> > xElements( maShapeContainer.getElements() );
195 return uno::makeAny( Reference< XShape>(static_cast< drawing::XShape* >( xElements.getArray()[Index].get())) );
198 // XElementAccess
200 //----------------------------------------------------------------------
201 uno::Type SAL_CALL SvxShapeCollection::getElementType() throw( uno::RuntimeException )
203 return ::getCppuType(( const Reference< drawing::XShape >*)0);
206 //----------------------------------------------------------------------
207 sal_Bool SAL_CALL SvxShapeCollection::hasElements() throw( uno::RuntimeException )
209 return getCount() != 0;
212 //----------------------------------------------------------------------
213 // XServiceInfo
214 //----------------------------------------------------------------------
215 OUString SAL_CALL SvxShapeCollection::getImplementationName()
216 throw( uno::RuntimeException )
218 return getImplementationName_Static();
221 OUString SvxShapeCollection::getImplementationName_Static()
223 return OUString("com.sun.star.drawing.SvxShapeCollection");
226 sal_Bool SAL_CALL SvxShapeCollection::supportsService( const OUString& ServiceName )
227 throw( uno::RuntimeException )
229 return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
232 uno::Sequence< OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames() throw( uno::RuntimeException )
234 return getSupportedServiceNames_Static();
237 uno::Sequence< OUString > SvxShapeCollection::getSupportedServiceNames_Static()
239 uno::Sequence< OUString > aSeq(2);
240 aSeq.getArray()[0] = OUString("com.sun.star.drawing.Shapes");
241 aSeq.getArray()[1] = OUString("com.sun.star.drawing.ShapeCollection");
242 return aSeq;
245 Reference< XInterface > SAL_CALL SvxShapeCollection_createInstance( const Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
247 return *( new SvxShapeCollection() );
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */