1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include <sal/config.h>
#include <com/sun/star/lang/EventObject.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <o3tl/cow_wrapper.hxx>
#include <mutex>
#include <vector>
namespace com::sun::star::uno
{
class XInterface;
}
namespace comphelper
{
template <class ListenerT> class OInterfaceContainerHelper4;
/**
This is the iterator of an OInterfaceContainerHelper4. Typically
one constructs an instance on the stack for one firing session.
It is not allowed to assign or copy an instance of this class.
@tparam ListenerT UNO event listener type
@see OInterfaceContainerHelper4
*/
template <class ListenerT> class OInterfaceIteratorHelper4
{
public:
/**
Create an iterator over the elements of the container. The iterator
copies the elements of the container. A change to the container
during the lifetime of an iterator is allowed and does not
affect the iterator-instance. The iterator and the container take cares
themself for concurrent access, no additional guarding is necessary.
Remark: The copy is on demand. The iterator copy the elements only if the container
change the contents...
@param rCont the container of the elements.
@param rGuard
this parameter only here to make that this container is accessed while locked
*/
OInterfaceIteratorHelper4(std::unique_lock<std::mutex>& /*rGuard*/,
OInterfaceContainerHelper4<ListenerT>& rCont_)
: rCont(rCont_)
, maData(rCont.maData)
// const_cast so we don't trigger make_unique via o3tl::cow_wrapper::operator->
, nRemain(std::as_const(maData)->size())
{
}
/** Return true, if there are more elements in the iterator. */
bool hasMoreElements() const { return nRemain != 0; }
/** Return the next element of the iterator. Calling this method if
hasMoreElements() has returned false, is an error.
*/
css::uno::Reference<ListenerT> const& next();
/** Removes the current element (the last one returned by next())
from the underlying container. Calling this method before
next() has been called or calling it twice with no next()
in between is an error.
@param rGuard
this parameter only here to make that this container is accessed while locked
*/
void remove(::std::unique_lock<::std::mutex>& rGuard);
private:
OInterfaceContainerHelper4<ListenerT>& rCont;
o3tl::cow_wrapper<std::vector<css::uno::Reference<ListenerT>>,
o3tl::ThreadSafeRefCountingPolicy>
maData;
sal_Int32 nRemain;
OInterfaceIteratorHelper4(const OInterfaceIteratorHelper4&) = delete;
OInterfaceIteratorHelper4& operator=(const OInterfaceIteratorHelper4&) = delete;
};
template <class ListenerT>
const css::uno::Reference<ListenerT>& OInterfaceIteratorHelper4<ListenerT>::next()
{
nRemain--;
return (*std::as_const(maData))[nRemain];
}
template <class ListenerT>
void OInterfaceIteratorHelper4<ListenerT>::remove(::std::unique_lock<::std::mutex>& rGuard)
{
rCont.removeInterface(rGuard, (*std::as_const(maData))[nRemain]);
}
/**
A container of interfaces. To access the elements use an iterator.
This implementation is thread-safe.
This is a copy of the code at include/comphelper/interfacecontainer3.hxx,
except that it (a) uses std::mutex instead of osl::Mutex and (b) does not
store a reference to the mutex, but relies on the calling class to take
a lock around using it.
@tparam ListenerT UNO event listener type
@see OInterfaceIteratorHelper
*/
template <class ListenerT> class OInterfaceContainerHelper4
{
public:
OInterfaceContainerHelper4();
/**
Return the number of Elements in the container. Only useful if you have acquired
the mutex.
@param rGuard
this parameter only here to make that this container is accessed while locked
*/
sal_Int32 getLength(std::unique_lock<std::mutex>& rGuard) const;
/**
Return all interfaces added to this container.
@param rGuard
this parameter only here to make that this container is accessed while locked
**/
std::vector<css::uno::Reference<ListenerT>>
getElements(std::unique_lock<std::mutex>& rGuard) const;
/** Inserts an element into the container. The position is not specified, thus it is not
specified in which order events are fired.
@attention
If you add the same interface more than once, then it will be added to the elements list
more than once and thus if you want to remove that interface from the list, you have to call
removeInterface() the same number of times.
In the latter case, you will also get events fired more than once (if the interface is a
listener interface).
@param rxIFace
interface to be added; it is allowed to insert
the same interface more than once
@param rGuard
this parameter only here to make that this container is accessed while locked
@return
the new count of elements in the container
*/
sal_Int32 addInterface(std::unique_lock<std::mutex>& rGuard,
const css::uno::Reference<ListenerT>& rxIFace);
/** Removes an element from the container. It uses interface equality to remove the interface.
@param rxIFace
interface to be removed
@param rGuard
this parameter only here to make that this container is accessed while locked
@return
the new count of elements in the container
*/
sal_Int32 removeInterface(std::unique_lock<std::mutex>& rGuard,
const css::uno::Reference<ListenerT>& rxIFace);
/**
Call disposing on all object in the container that
support XEventListener. Then clear the container.
The guard is unlock()'ed before calling the listeners.
*/
void disposeAndClear(::std::unique_lock<::std::mutex>& rGuard,
const css::lang::EventObject& rEvt);
/**
Clears the container without calling disposing().
@param rGuard
this parameter only here to make that this container is accessed while locked
*/
void clear(::std::unique_lock<::std::mutex>& rGuard);
/** Executes a functor for each contained listener of specified type, e.g.
<code>forEach<awt::XPaintListener>(...</code>.
If a css::lang::DisposedException occurs which relates to
the called listener, then that listener is removed from the container.
@tparam FuncT unary functor type, let your compiler deduce this for you
@param func unary functor object expecting an argument of type
css::uno::Reference<ListenerT>
@param rGuard
this parameter only here to make that this container is accessed while locked
*/
template <typename FuncT>
inline void forEach(std::unique_lock<std::mutex>& rGuard, FuncT const& func);
/** Calls a UNO listener method for each contained listener.
The listener method must take a single argument of type EventT,
and return <code>void</code>.
If a css::lang::DisposedException occurs which relates to
the called listener, then that listener is removed from the container.
@tparam EventT event type, let your compiler deduce this for you
@param NotificationMethod
Pointer to a method of a ListenerT interface.
@param Event
Event to notify to all contained listeners
@param rGuard
this parameter only here to make that this container is accessed while locked
Example:
@code
awt::PaintEvent aEvent( static_cast< cppu::OWeakObject* >( this ), ... );
listeners.notifyEach( &XPaintListener::windowPaint, aEvent );
@endcode
*/
template <typename EventT>
inline void notifyEach(std::unique_lock<std::mutex>& rGuard,
void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&),
const EventT& Event);
private:
friend class OInterfaceIteratorHelper4<ListenerT>;
o3tl::cow_wrapper<std::vector<css::uno::Reference<ListenerT>>,
o3tl::ThreadSafeRefCountingPolicy>
maData;
OInterfaceContainerHelper4(const OInterfaceContainerHelper4&) = delete;
OInterfaceContainerHelper4& operator=(const OInterfaceContainerHelper4&) = delete;
static o3tl::cow_wrapper<std::vector<css::uno::Reference<ListenerT>>,
o3tl::ThreadSafeRefCountingPolicy>&
DEFAULT()
{
static o3tl::cow_wrapper<std::vector<css::uno::Reference<ListenerT>>,
o3tl::ThreadSafeRefCountingPolicy>
SINGLETON;
return SINGLETON;
}
private:
template <typename EventT> class NotifySingleListener
{
private:
typedef void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&);
NotificationMethod const m_pMethod;
const EventT& m_rEvent;
public:
NotifySingleListener(NotificationMethod method, const EventT& event)
: m_pMethod(method)
, m_rEvent(event)
{
}
void operator()(const css::uno::Reference<ListenerT>& listener) const
{
(listener.get()->*m_pMethod)(m_rEvent);
}
};
};
template <class T>
inline OInterfaceContainerHelper4<T>::OInterfaceContainerHelper4()
: maData(OInterfaceContainerHelper4<T>::DEFAULT())
{
}
template <class T>
template <typename FuncT>
inline void OInterfaceContainerHelper4<T>::forEach(std::unique_lock<std::mutex>& rGuard,
FuncT const& func)
{
if (std::as_const(maData)->size() == 0)
{
return;
}
maData.make_unique(); // so we can iterate over the data without holding the lock
OInterfaceIteratorHelper4<T> iter(rGuard, *this);
rGuard.unlock();
while (iter.hasMoreElements())
{
auto xListener = iter.next();
try
{
func(xListener);
}
catch (css::lang::DisposedException const& exc)
{
if (exc.Context == xListener)
{
rGuard.lock();
iter.remove(rGuard);
rGuard.unlock();
}
}
}
rGuard.lock();
}
template <class ListenerT>
template <typename EventT>
inline void OInterfaceContainerHelper4<ListenerT>::notifyEach(
std::unique_lock<std::mutex>& rGuard,
void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&), const EventT& Event)
{
forEach<NotifySingleListener<EventT>>(rGuard,
NotifySingleListener<EventT>(NotificationMethod, Event));
}
template <class ListenerT>
sal_Int32
OInterfaceContainerHelper4<ListenerT>::getLength(std::unique_lock<std::mutex>& /*rGuard*/) const
{
return maData->size();
}
template <class ListenerT>
std::vector<css::uno::Reference<ListenerT>>
OInterfaceContainerHelper4<ListenerT>::getElements(std::unique_lock<std::mutex>& /*rGuard*/) const
{
return *maData;
}
template <class ListenerT>
sal_Int32
OInterfaceContainerHelper4<ListenerT>::addInterface(std::unique_lock<std::mutex>& /*rGuard*/,
const css::uno::Reference<ListenerT>& rListener)
{
assert(rListener.is());
maData->push_back(rListener);
return maData->size();
}
template <class ListenerT>
sal_Int32 OInterfaceContainerHelper4<ListenerT>::removeInterface(
std::unique_lock<std::mutex>& /*rGuard*/, const css::uno::Reference<ListenerT>& rListener)
{
assert(rListener.is());
// It is not valid to compare the pointer directly, but it's faster.
auto it = std::find_if(maData->begin(), maData->end(),
[&rListener](const css::uno::Reference<css::uno::XInterface>& rItem) {
return rItem.get() == rListener.get();
});
// interface not found, use the correct compare method
if (it == maData->end())
it = std::find(maData->begin(), maData->end(), rListener);
if (it != maData->end())
maData->erase(it);
return maData->size();
}
template <class ListenerT>
void OInterfaceContainerHelper4<ListenerT>::disposeAndClear(std::unique_lock<std::mutex>& rGuard,
const css::lang::EventObject& rEvt)
{
{
OInterfaceIteratorHelper4<ListenerT> aIt(rGuard, *this);
maData
= DEFAULT(); // cheaper than calling maData->clear() because it doesn't allocate a new vector
rGuard.unlock();
// unlock followed by iterating is only safe because we are not going to call remove() on the iterator
while (aIt.hasMoreElements())
{
try
{
aIt.next()->disposing(rEvt);
}
catch (css::uno::RuntimeException&)
{
// be robust, if e.g. a remote bridge has disposed already.
// there is no way to delegate the error to the caller :o(.
}
}
}
// tdf#152077 need to destruct the OInterfaceIteratorHelper4 before we take the lock again
// because there is a vague chance that destructing it will trigger a call back into something
// that wants to take the lock.
rGuard.lock();
}
template <class ListenerT>
void OInterfaceContainerHelper4<ListenerT>::clear(::std::unique_lock<::std::mutex>& /*rGuard*/)
{
maData->clear();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|