LibreOffice Module svx (master) 1
unoshcol.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/document/EventObject.hpp>
21#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
22
24#include <osl/diagnose.h>
25#include <sal/log.hxx>
26#include <shapecollection.hxx>
27
28using namespace ::com::sun::star;
29using namespace ::com::sun::star::uno;
30
32{
33}
34
35// XInterface
37{
38 uno::Reference< uno::XInterface > x( xDelegator );
39 if (! x.is())
40 {
41 if (osl_atomic_decrement( &m_refCount ) == 0)
42 {
43 if (! bDisposed)
44 {
45 uno::Reference< uno::XInterface > xHoldAlive( getXWeak() );
46 // First dispose
47 try
48 {
49 dispose();
50 }
51 catch(css::uno::Exception&)
52 {
53 // release should not throw exceptions
54 }
55
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;
60 }
61 }
62 // restore the reference count
63 osl_atomic_increment( &m_refCount );
64 }
65 OWeakAggObject::release();
66}
67
68// XComponent
70{
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 );
75
76 // Guard dispose against multiple threading
77 // Remark: It is an error to call dispose more than once
78 bool bDoDispose = false;
79 {
80 std::unique_lock aGuard( m_aMutex );
81 if( !bDisposed && !bInDispose )
82 {
83 // only one call go into this section
84 bInDispose = true;
85 bDoDispose = true;
86 }
87 }
88
89 // Do not hold the mutex because we are broadcasting
90 if( bDoDispose )
91 {
92 // Create an event with this as sender
93 try
94 {
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);
102 maShapeContainer.clear();
103 }
104 catch(const css::uno::Exception&)
105 {
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;
112 }
113
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;
118 }
119 else
120 {
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" );
124 }
125}
126
127// XComponent
128void SAL_CALL SvxShapeCollection::addEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
129{
130 std::unique_lock g(m_aMutex);
131 maEventListeners.addInterface( g, aListener );
132}
133
134// XComponent
135void SAL_CALL SvxShapeCollection::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
136{
137 std::unique_lock g(m_aMutex);
138 maEventListeners.removeInterface( g, aListener );
139}
140
141// XShapes
142
143void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape )
144{
145 std::unique_lock g(m_aMutex);
146 maShapeContainer.push_back( xShape );
147}
148
149
150void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape )
151{
152 std::unique_lock g(m_aMutex);
153 maShapeContainer.erase(std::remove(maShapeContainer.begin(), maShapeContainer.end(), xShape), maShapeContainer.end());
154}
155
156
158{
159 std::unique_lock g(m_aMutex);
160 return maShapeContainer.size();
161}
162
163
165{
167 throw lang::IndexOutOfBoundsException();
168
169 std::unique_lock g(m_aMutex);
170 Reference<drawing::XShape> xShape = maShapeContainer[Index];
171 return uno::Any( xShape );
172}
173
174// XElementAccess
176{
178}
179
181{
182 return getCount() != 0;
183}
184
185// XServiceInfo
187{
188 return "com.sun.star.drawing.SvxShapeCollection";
189}
190
191sal_Bool SAL_CALL SvxShapeCollection::supportsService( const OUString& ServiceName )
192{
193 return cppu::supportsService( this, ServiceName);
194}
195
196uno::Sequence< OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames()
197{
198 return { "com.sun.star.drawing.Shapes", "com.sun.star.drawing.ShapeCollection" };
199}
200
201void SvxShapeCollection::getAllShapes(std::vector<css::uno::Reference<css::drawing::XShape>>& rShapes) const
202{
203 rShapes = maShapeContainer;
204}
205
206extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
208 css::uno::XComponentContext *,
209 css::uno::Sequence<css::uno::Any> const &)
210{
211 return cppu::acquire(new SvxShapeCollection);
212}
213
214/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
void getAllShapes(std::vector< css::uno::Reference< css::drawing::XShape > > &rShapes) const
Definition: unoshcol.cxx:201
virtual void SAL_CALL remove(const css::uno::Reference< css::drawing::XShape > &xShape) override
Definition: unoshcol.cxx:150
std::vector< css::uno::Reference< css::drawing::XShape > > maShapeContainer
SvxShapeCollection() noexcept
Definition: unoshcol.cxx:31
virtual css::uno::Type SAL_CALL getElementType() override
Definition: unoshcol.cxx:175
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
Definition: unoshcol.cxx:135
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: unoshcol.cxx:196
virtual void SAL_CALL add(const css::uno::Reference< css::drawing::XShape > &xShape) override
Definition: unoshcol.cxx:143
virtual sal_Int32 SAL_CALL getCount() override
Definition: unoshcol.cxx:157
virtual void SAL_CALL release() noexcept override
Definition: unoshcol.cxx:36
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: unoshcol.cxx:164
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: unoshcol.cxx:191
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
Definition: unoshcol.cxx:128
virtual void SAL_CALL dispose() override
Definition: unoshcol.cxx:69
virtual OUString SAL_CALL getImplementationName() override
Definition: unoshcol.cxx:186
comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > maEventListeners
virtual sal_Bool SAL_CALL hasElements() override
Definition: unoshcol.cxx:180
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
css::uno::WeakReferenceHelper xDelegator
oslInterlockedCount m_refCount
css::uno::Type const & get()
float x
#define SAL_INFO(area, stream)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_drawing_SvxShapeCollection_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: unoshcol.cxx:207