LibreOffice
LibreOffice 5.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
interfacecontainer.h
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_CPPUHELPER_INTERFACECONTAINER_H
20 #define INCLUDED_CPPUHELPER_INTERFACECONTAINER_H
21 
22 #include <sal/config.h>
23 
24 #include <cstddef>
25 #include <functional>
26 #include <vector>
27 #include <utility>
28 
29 #include <osl/diagnose.h>
30 #include <osl/mutex.hxx>
31 #include <rtl/alloc.h>
33 #include <com/sun/star/uno/XInterface.hpp>
34 #include <com/sun/star/lang/EventObject.hpp>
35 
36 #include <com/sun/star/lang/DisposedException.hpp>
38  //for docpp
40 namespace cppu
41 {
42 
43 namespace detail {
44 
50  {
51  css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > *pAsSequence;
52  css::uno::XInterface * pAsInterface;
54  };
55 
56 }
57 
58 
59 class OInterfaceContainerHelper;
68 {
69 public:
84 
89 
91  bool SAL_CALL hasMoreElements() const
92  { return nRemain != 0; }
97  css::uno::XInterface * SAL_CALL next();
98 
104  void SAL_CALL remove();
105 
106 private:
108  sal_Bool bIsList;
109 
110  detail::element_alias aData;
111 
112  sal_Int32 nRemain;
113 
116  OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & )
117  SAL_DELETED_FUNCTION;
118 };
119 
120 
128 {
129 public:
130  // these are here to force memory de/allocation to sal lib.
131  inline static void * SAL_CALL operator new( size_t nSize )
132  { return ::rtl_allocateMemory( nSize ); }
133  inline static void SAL_CALL operator delete( void * pMem )
134  { ::rtl_freeMemory( pMem ); }
135  inline static void * SAL_CALL operator new( size_t, void * pMem )
136  { return pMem; }
137  inline static void SAL_CALL operator delete( void *, void * )
138  {}
139 
147  OInterfaceContainerHelper( ::osl::Mutex & rMutex );
152  ~OInterfaceContainerHelper();
157  sal_Int32 SAL_CALL getLength() const;
158 
162  css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getElements() const;
163 
180  sal_Int32 SAL_CALL addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
188  sal_Int32 SAL_CALL removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
193  void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
197  void SAL_CALL clear();
198 
210  template <typename ListenerT, typename FuncT>
211  inline void forEach( FuncT const& func );
212 
234  template< typename ListenerT, typename EventT >
235  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
236 
237 private:
243  detail::element_alias aData;
244  ::osl::Mutex & rMutex;
246  sal_Bool bInUse;
248  sal_Bool bIsList;
249 
250  OInterfaceContainerHelper( const OInterfaceContainerHelper & )
251  SAL_DELETED_FUNCTION;
252  OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & )
253  SAL_DELETED_FUNCTION;
254 
255  /*
256  Duplicate content of the container and release the old one without destroying.
257  The mutex must be locked and the memberbInUse must be true.
258  */
259  void copyAndResetInUse();
260 
261 private:
262  template< typename ListenerT, typename EventT >
263  class NotifySingleListener
264  {
265  private:
266  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
267  NotificationMethod m_pMethod;
268  const EventT& m_rEvent;
269  public:
270  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
271 
272  void operator()( const css::uno::Reference<ListenerT>& listener ) const
273  {
274  (listener.get()->*m_pMethod)( m_rEvent );
275  }
276  };
277 };
278 
279 template <typename ListenerT, typename FuncT>
280 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
281 {
282  OInterfaceIteratorHelper iter( *this );
283  while (iter.hasMoreElements()) {
284  css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
285  if (xListener.is()) {
286  try {
287  func( xListener );
288  }
289  catch (css::lang::DisposedException const& exc) {
290  if (exc.Context == xListener)
291  iter.remove();
292  }
293  }
294  }
295 }
296 
297 template< typename ListenerT, typename EventT >
298 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
299 {
300  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
301 }
302 
303 
310 template< class key, class hashImpl = void, class equalImpl = std::equal_to<key> >
312 {
313 public:
314  // these are here to force memory de/allocation to sal lib.
315  inline static void * SAL_CALL operator new( size_t nSize )
316  { return ::rtl_allocateMemory( nSize ); }
317  inline static void SAL_CALL operator delete( void * pMem )
318  { ::rtl_freeMemory( pMem ); }
319  inline static void * SAL_CALL operator new( size_t, void * pMem )
320  { return pMem; }
321  inline static void SAL_CALL operator delete( void *, void * )
322  {}
323 
336 
340  inline css::uno::Sequence< key > SAL_CALL getContainedTypes() const;
341 
348  inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const;
349 
368  inline sal_Int32 SAL_CALL addInterface(
369  const key & rKey,
370  const css::uno::Reference< css::uno::XInterface > & r );
371 
382  inline sal_Int32 SAL_CALL removeInterface(
383  const key & rKey,
384  const css::uno::Reference< css::uno::XInterface > & rxIFace );
385 
391  inline void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
395  inline void SAL_CALL clear();
396 
397  typedef key keyType;
398 private:
399  typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
400  InterfaceMap *m_pMap;
401  ::osl::Mutex & rMutex;
402 
403  inline typename InterfaceMap::iterator find(const key &rKey) const
404  {
405  typename InterfaceMap::iterator iter = m_pMap->begin();
406  typename InterfaceMap::iterator end = m_pMap->end();
407 
408  while( iter != end )
409  {
410  equalImpl equal;
411  if( equal( iter->first, rKey ) )
412  break;
413  iter++;
414  }
415  return iter;
416  }
417 
419  OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
420 };
421 
422 
423 
424 
434 template < class container , class keyType >
436 {
440  container aLC;
445 
451  : rMutex( rMutex_ )
452  , aLC( rMutex_ )
453  , bDisposed( false )
454  , bInDispose( false )
455  {}
456 
460  inline void addListener(
461  const keyType &key,
462  const css::uno::Reference < css::uno::XInterface > &r )
463  {
464  ::osl::MutexGuard guard( rMutex );
465  OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
466  OSL_ENSURE( !bDisposed, "object is disposed" );
467  if( ! bInDispose && ! bDisposed )
468  aLC.addInterface( key , r );
469  }
470 
474  inline void removeListener(
475  const keyType &key,
476  const css::uno::Reference < css::uno::XInterface > & r )
477  {
478  ::osl::MutexGuard guard( rMutex );
479  if( ! bInDispose && ! bDisposed )
480  aLC.removeInterface( key , r );
481  }
482 
489  inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const
490  { return aLC.getContainer( key ); }
491 };
492 
493 /*------------------------------------------
494 *
495 * In general, the above templates are used with a Type as key.
496 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
497 *
498 *------------------------------------------*/
499 
500 // helper function call class
502 {
503  size_t operator()(const css::uno::Type & s) const
504  { return (size_t) s.getTypeName().hashCode(); }
505 };
506 
507 
512 {
513 public:
514  // these are here to force memory de/allocation to sal lib.
515  inline static void * SAL_CALL operator new( size_t nSize )
516  { return ::rtl_allocateMemory( nSize ); }
517  inline static void SAL_CALL operator delete( void * pMem )
518  { ::rtl_freeMemory( pMem ); }
519  inline static void * SAL_CALL operator new( size_t, void * pMem )
520  { return pMem; }
521  inline static void SAL_CALL operator delete( void *, void * )
522  {}
523 
536 
540  css::uno::Sequence< css::uno::Type > SAL_CALL getContainedTypes() const;
541 
547  OInterfaceContainerHelper * SAL_CALL getContainer( const css::uno::Type & rKey ) const;
548 
567  sal_Int32 SAL_CALL addInterface(
568  const css::uno::Type & rKey,
569  const css::uno::Reference< css::uno::XInterface > & r );
570 
581  sal_Int32 SAL_CALL removeInterface(
582  const css::uno::Type & rKey,
583  const css::uno::Reference< css::uno::XInterface > & rxIFace );
584 
589  void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
593  void SAL_CALL clear();
594 
595  typedef css::uno::Type keyType;
596 private:
597  void * m_pMap;
598  ::osl::Mutex & rMutex;
599 
601  OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
602 };
603 
604 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
605 
606 }
607 
608 #endif
609 
610 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:280
This enum value can be used for implicit interface query.
Definition: Reference.h:140
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
Specialized class for key type css::uno::Type, without explicit usage of STL symbols.
Definition: interfacecontainer.h:511
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:444
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:410
css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > * pAsSequence
Definition: interfacecontainer.h:51
css::uno::Type keyType
Definition: interfacecontainer.h:595
key keyType
Definition: interfacecontainer.h:397
Definition: interfacecontainer.h:501
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:440
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:438
A helper class to store interface references of different types.
Definition: interfacecontainer.h:311
element_alias()
Definition: interfacecontainer.h:53
unsigned char sal_Bool
Definition: types.h:48
This is the iterator of a InterfaceContainerHelper.
Definition: interfacecontainer.h:67
void removeListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:474
This is here to optimise space in the common case that there are zero or one listeners.
Definition: interfacecontainer.h:49
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structure.
Definition: interfacecontainer.h:450
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
Definition: interfacecontainer.h:91
A container of interfaces.
Definition: interfacecontainer.h:127
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:489
#define OSL_ENSURE(c, m)
Definition: diagnose.h:101
css::uno::XInterface * next()
Return the next element of the iterator.
size_t operator()(const css::uno::Type &s) const
Definition: interfacecontainer.h:503
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:435
void notifyEach(void(ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
Definition: interfacecontainer.h:298
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:108
css::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:52
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:442
void addListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:460