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>
22 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
27 #include <shapecollection.hxx>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::uno
;
32 SvxShapeCollection::SvxShapeCollection() noexcept
37 void SvxShapeCollection::release() noexcept
39 uno::Reference
< uno::XInterface
> x( xDelegator
);
42 if (osl_atomic_decrement( &m_refCount
) == 0)
46 uno::Reference
< uno::XInterface
> xHoldAlive( getXWeak() );
52 catch(css::uno::Exception
&)
54 // release should not throw exceptions
57 // only the alive ref holds the object
58 OSL_ASSERT( m_refCount
== 1 );
59 // destroy the object if xHoldAlive decrement the refcount to 0
63 // restore the reference count
64 osl_atomic_increment( &m_refCount
);
66 OWeakAggObject::release();
70 void SvxShapeCollection::dispose()
72 // An frequently programming error is to release the last
73 // reference to this object in the disposing message.
74 // Make it robust, hold a self Reference.
75 uno::Reference
< lang::XComponent
> xSelf( this );
77 // Guard dispose against multiple threading
78 // Remark: It is an error to call dispose more than once
80 std::unique_lock
aGuard( m_aMutex
);
81 if( bDisposed
|| bInDispose
)
83 // only one call go into this section
87 // Do not hold the mutex because we are broadcasting
88 // Create an event with this as sender
91 document::EventObject aEvt
;
92 aEvt
.Source
= uno::Reference
< uno::XInterface
>::query( static_cast<lang::XComponent
*>(this) );
93 // inform all listeners to release this object
94 // The listener container are automatically cleared
95 std::unique_lock
g(m_aMutex
);
96 maEventListeners
.disposeAndClear( g
, aEvt
);
97 maShapeContainer
.clear();
99 // bDisposed and bInDispose must be set in this order.
103 catch(const css::uno::Exception
&)
105 // catch exception and throw again but signal that
106 // the object was disposed. Dispose should be called
115 void SAL_CALL
SvxShapeCollection::addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& aListener
)
117 std::unique_lock
g(m_aMutex
);
118 maEventListeners
.addInterface( g
, aListener
);
122 void SAL_CALL
SvxShapeCollection::removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& aListener
)
124 std::unique_lock
g(m_aMutex
);
125 maEventListeners
.removeInterface( g
, aListener
);
130 void SAL_CALL
SvxShapeCollection::add( const Reference
< drawing::XShape
>& xShape
)
132 std::unique_lock
g(m_aMutex
);
133 maShapeContainer
.push_back( xShape
);
137 void SAL_CALL
SvxShapeCollection::remove( const uno::Reference
< drawing::XShape
>& xShape
)
139 std::unique_lock
g(m_aMutex
);
140 std::erase(maShapeContainer
, xShape
);
144 sal_Int32 SAL_CALL
SvxShapeCollection::getCount()
146 std::unique_lock
g(m_aMutex
);
147 return maShapeContainer
.size();
150 uno::Any SAL_CALL
SvxShapeCollection::getByIndex( sal_Int32 Index
)
152 if( Index
< 0 || Index
>= getCount() )
153 throw lang::IndexOutOfBoundsException();
155 std::unique_lock
g(m_aMutex
);
156 Reference
<drawing::XShape
> xShape
= maShapeContainer
[Index
];
157 return uno::Any( xShape
);
160 std::vector
<css::uno::Reference
<css::drawing::XShape
>> SvxShapeCollection::getAllShapes() const
162 std::unique_lock
g(m_aMutex
);
163 return maShapeContainer
;
167 uno::Type SAL_CALL
SvxShapeCollection::getElementType()
169 return cppu::UnoType
<drawing::XShape
>::get();
172 sal_Bool SAL_CALL
SvxShapeCollection::hasElements()
174 return getCount() != 0;
178 OUString SAL_CALL
SvxShapeCollection::getImplementationName()
180 return u
"com.sun.star.drawing.SvxShapeCollection"_ustr
;
183 sal_Bool SAL_CALL
SvxShapeCollection::supportsService( const OUString
& ServiceName
)
185 return cppu::supportsService( this, ServiceName
);
188 uno::Sequence
< OUString
> SAL_CALL
SvxShapeCollection::getSupportedServiceNames()
190 return { u
"com.sun.star.drawing.Shapes"_ustr
, u
"com.sun.star.drawing.ShapeCollection"_ustr
};
193 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
194 com_sun_star_drawing_SvxShapeCollection_get_implementation(
195 css::uno::XComponentContext
*,
196 css::uno::Sequence
<css::uno::Any
> const &)
198 return cppu::acquire(new SvxShapeCollection
);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */