LibreOffice Module comphelper (master)  1
interfacecontainer4.hxx
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 #ifndef INCLUDED_COMPHELPER_INTERFACECONTAINER3_H
20 #define INCLUDED_COMPHELPER_INTERFACECONTAINER3_H
21 
22 #include <sal/config.h>
23 
24 #include <com/sun/star/lang/EventObject.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <o3tl/cow_wrapper.hxx>
27 #include <mutex>
28 #include <vector>
29 
30 namespace com::sun::star::uno
31 {
32 class XInterface;
33 }
34  //for docpp
36 namespace comphelper
37 {
38 template <class ListenerT> class OInterfaceContainerHelper4;
47 template <class ListenerT> class OInterfaceIteratorHelper4
48 {
49 public:
63  : rCont(rCont_)
65  , nRemain(maData->size())
66  {
67  }
68 
70  bool hasMoreElements() const { return nRemain != 0; }
74  css::uno::Reference<ListenerT> const& next();
75 
81  void remove();
82 
83 private:
86  sal_Int32 nRemain;
87 
90 };
91 
92 template <class ListenerT>
93 const css::uno::Reference<ListenerT>& OInterfaceIteratorHelper4<ListenerT>::next()
94 {
95  nRemain--;
96  return (*maData)[nRemain];
97 }
98 
99 template <class ListenerT> void OInterfaceIteratorHelper4<ListenerT>::remove()
100 {
101  rCont.removeInterface((*maData)[nRemain]);
102 }
103 
116 template <class ListenerT> class OInterfaceContainerHelper4
117 {
118 public:
124  sal_Int32 getLength() const;
125 
129  std::vector<css::uno::Reference<ListenerT>> getElements() const;
130 
147  sal_Int32 addInterface(const css::uno::Reference<ListenerT>& rxIFace);
155  sal_Int32 removeInterface(const css::uno::Reference<ListenerT>& rxIFace);
161  void disposeAndClear(::std::unique_lock<::std::mutex>& rGuard,
162  const css::lang::EventObject& rEvt);
166  void clear();
167 
178  template <typename FuncT> inline void forEach(FuncT const& func);
179 
200  template <typename EventT>
201  inline void notifyEach(void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&),
202  const EventT& Event);
203 
204 private:
205  friend class OInterfaceIteratorHelper4<ListenerT>;
209 
210 private:
211  template <typename EventT> class NotifySingleListener
212  {
213  private:
214  typedef void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&);
215  NotificationMethod const m_pMethod;
216  const EventT& m_rEvent;
217 
218  public:
219  NotifySingleListener(NotificationMethod method, const EventT& event)
220  : m_pMethod(method)
221  , m_rEvent(event)
222  {
223  }
224 
225  void operator()(const css::uno::Reference<ListenerT>& listener) const
226  {
227  (listener.get()->*m_pMethod)(m_rEvent);
228  }
229  };
230 };
231 
232 template <class T>
233 template <typename FuncT>
234 inline void OInterfaceContainerHelper4<T>::forEach(FuncT const& func)
235 {
236  OInterfaceIteratorHelper4<T> iter(*this);
237  while (iter.hasMoreElements())
238  {
239  auto xListener = iter.next();
240  try
241  {
242  func(xListener);
243  }
244  catch (css::lang::DisposedException const& exc)
245  {
246  if (exc.Context == xListener)
247  iter.remove();
248  }
249  }
250 }
251 
252 template <class ListenerT>
253 template <typename EventT>
255  void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&), const EventT& Event)
256 {
257  forEach<NotifySingleListener<EventT>>(NotifySingleListener<EventT>(NotificationMethod, Event));
258 }
259 
260 template <class ListenerT> sal_Int32 OInterfaceContainerHelper4<ListenerT>::getLength() const
261 {
262  return maData->size();
263 }
264 
265 template <class ListenerT>
266 std::vector<css::uno::Reference<ListenerT>>
268 {
269  return *maData;
270 }
271 
272 template <class ListenerT>
273 sal_Int32
274 OInterfaceContainerHelper4<ListenerT>::addInterface(const css::uno::Reference<ListenerT>& rListener)
275 {
276  assert(rListener.is());
277  maData->push_back(rListener);
278  return maData->size();
279 }
280 
281 template <class ListenerT>
283  const css::uno::Reference<ListenerT>& rListener)
284 {
285  assert(rListener.is());
286 
287  // It is not valid to compare the pointer directly, but it's faster.
288  auto it = std::find_if(maData->begin(), maData->end(),
289  [&rListener](const css::uno::Reference<css::uno::XInterface>& rItem) {
290  return rItem.get() == rListener.get();
291  });
292 
293  // interface not found, use the correct compare method
294  if (it == maData->end())
295  it = std::find(maData->begin(), maData->end(), rListener);
296 
297  if (it != maData->end())
298  maData->erase(it);
299 
300  return maData->size();
301 }
302 
303 template <class ListenerT>
304 void OInterfaceContainerHelper4<ListenerT>::disposeAndClear(std::unique_lock<std::mutex>& rGuard,
305  const css::lang::EventObject& rEvt)
306 {
308  maData->clear();
309  rGuard.unlock();
310  while (aIt.hasMoreElements())
311  {
312  try
313  {
314  aIt.next()->disposing(rEvt);
315  }
316  catch (css::uno::RuntimeException&)
317  {
318  // be robust, if e.g. a remote bridge has disposed already.
319  // there is no way to delegate the error to the caller :o(.
320  }
321  }
322 }
323 
324 template <class ListenerT> void OInterfaceContainerHelper4<ListenerT>::clear() { maData->clear(); }
325 }
326 #endif
327 
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
sal_Int32 getLength() const
Return the number of Elements in the container.
std::vector< sal_Int8 > maData
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
typedef void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
Removes an element from the container.
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
css::uno::Reference< ListenerT > const & next()
Return the next element of the iterator.
o3tl::cow_wrapper< std::vector< css::uno::Reference< ListenerT > > > maData
NotifySingleListener(NotificationMethod method, const EventT &event)
OInterfaceContainerHelper4< ListenerT > & rCont
OInterfaceContainerHelper4 & operator=(const OInterfaceContainerHelper4 &)=delete
size
void operator()(const css::uno::Reference< ListenerT > &listener) const
o3tl::cow_wrapper< std::vector< css::uno::Reference< ListenerT > > > maData
This is the iterator of an OInterfaceContainerHelper4.
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
Inserts an element into the container.
std::vector< css::uno::Reference< ListenerT > > getElements() const
Return all interfaces added to this container.
static PropertyMapEntry const * find(const rtl::Reference< PropertySetInfo > &mxInfo, const OUString &aName) noexcept
void clear()
Clears the container without calling disposing().
OInterfaceIteratorHelper4(OInterfaceContainerHelper4< ListenerT > &rCont_)
Create an iterator over the elements of the container.
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
OInterfaceIteratorHelper4 & operator=(const OInterfaceIteratorHelper4 &)=delete