1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
36 void SvxShapeCollection::release() noexcept
38 uno::Reference
< uno::XInterface
> x( xDelegator
);
41 if (osl_atomic_decrement( &m_refCount
) == 0)
45 uno::Reference
< uno::XInterface
> xHoldAlive( static_cast<uno::XWeak
*>(this) );
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
62 // restore the reference count
63 osl_atomic_increment( &m_refCount
);
65 OWeakAggObject::release();
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
89 // Do not hold the mutex because we are broadcasting
92 // Create an event with this as sender
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
114 // the values bDispose and bInDisposing must set in this order.
115 // No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
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" );
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
);
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
);
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
);
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;
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: */