LibreOffice
LibreOffice 5.0 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 <functional>
25 #include <vector>
26 #include <utility>
27 
28 #include <osl/diagnose.h>
29 #include <osl/mutex.hxx>
30 #include <rtl/alloc.h>
32 #include <com/sun/star/uno/XInterface.hpp>
33 #include <com/sun/star/lang/EventObject.hpp>
34 
35 #include <com/sun/star/lang/DisposedException.hpp>
37  //for docpp
39 namespace cppu
40 {
41 
42 namespace detail {
43 
49  {
51  ::com::sun::star::uno::XInterface * pAsInterface;
53  };
54 
55 }
56 
57 
58 class OInterfaceContainerHelper;
67 {
68 public:
83 
88 
90  bool SAL_CALL hasMoreElements() const
91  { return nRemain != 0; }
96  ::com::sun::star::uno::XInterface * SAL_CALL next();
97 
103  void SAL_CALL remove();
104 
105 private:
107  sal_Bool bIsList;
108 
109  detail::element_alias aData;
110 
111  sal_Int32 nRemain;
112 
115  OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & )
116  SAL_DELETED_FUNCTION;
117 };
118 
119 
127 {
128 public:
129  // these are here to force memory de/allocation to sal lib.
130  inline static void * SAL_CALL operator new( size_t nSize )
131  { return ::rtl_allocateMemory( nSize ); }
132  inline static void SAL_CALL operator delete( void * pMem )
133  { ::rtl_freeMemory( pMem ); }
134  inline static void * SAL_CALL operator new( size_t, void * pMem )
135  { return pMem; }
136  inline static void SAL_CALL operator delete( void *, void * )
137  {}
138 
146  OInterfaceContainerHelper( ::osl::Mutex & rMutex );
151  ~OInterfaceContainerHelper();
156  sal_Int32 SAL_CALL getLength() const;
157 
162 
179  sal_Int32 SAL_CALL addInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace );
187  sal_Int32 SAL_CALL removeInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace );
192  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt );
196  void SAL_CALL clear();
197 
209  template <typename ListenerT, typename FuncT>
210  inline void forEach( FuncT const& func );
211 
233  template< typename ListenerT, typename EventT >
234  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
235 
236 private:
242  detail::element_alias aData;
243  ::osl::Mutex & rMutex;
245  sal_Bool bInUse;
247  sal_Bool bIsList;
248 
249  OInterfaceContainerHelper( const OInterfaceContainerHelper & )
250  SAL_DELETED_FUNCTION;
251  OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & )
252  SAL_DELETED_FUNCTION;
253 
254  /*
255  Dulicate content of the conaitner and release the old one without destroying.
256  The mutex must be locked and the memberbInUse must be true.
257  */
258  void copyAndResetInUse();
259 
260 private:
261  template< typename ListenerT, typename EventT >
262  class NotifySingleListener
263  {
264  private:
265  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
266  NotificationMethod m_pMethod;
267  const EventT& m_rEvent;
268  public:
269  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
270 
271  void operator()( const ::com::sun::star::uno::Reference<ListenerT>& listener ) const
272  {
273  (listener.get()->*m_pMethod)( m_rEvent );
274  }
275  };
276 };
277 
278 template <typename ListenerT, typename FuncT>
279 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
280 {
281  OInterfaceIteratorHelper iter( *this );
282  while (iter.hasMoreElements()) {
285  if (xListener.is()) {
286  try {
287  func( xListener );
288  }
289  catch (::com::sun::star::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 ::com::sun::star::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 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r );
371 
382  inline sal_Int32 SAL_CALL removeInterface(
383  const key & rKey,
384  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace );
385 
391  inline void SAL_CALL disposeAndClear( const ::com::sun::star::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( sal_False )
454  , bInDispose( sal_False )
455  {}
456 
460  inline void addListener(
461  const keyType &key,
462  const ::com::sun::star::uno::Reference < ::com::sun::star::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 ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > & r )
477  {
478  ::osl::MutexGuard guard( rMutex );
479  OSL_ENSURE( !bDisposed, "object is disposed" );
480  if( ! bInDispose && ! bDisposed )
481  aLC.removeInterface( key , r );
482  }
483 
490  inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const
491  { return aLC.getContainer( key ); }
492 };
493 
494 /*------------------------------------------
495 *
496 * In general, the above templates are used with a Type as key.
497 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
498 *
499 *------------------------------------------*/
500 
501 // helper function call class
503 {
504  size_t operator()(const ::com::sun::star::uno::Type & s) const
505  { return (size_t) s.getTypeName().hashCode(); }
506 };
507 
508 
513 {
514 public:
515  // these are here to force memory de/allocation to sal lib.
516  inline static void * SAL_CALL operator new( size_t nSize )
517  { return ::rtl_allocateMemory( nSize ); }
518  inline static void SAL_CALL operator delete( void * pMem )
519  { ::rtl_freeMemory( pMem ); }
520  inline static void * SAL_CALL operator new( size_t, void * pMem )
521  { return pMem; }
522  inline static void SAL_CALL operator delete( void *, void * )
523  {}
524 
537 
541  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getContainedTypes() const;
542 
548  OInterfaceContainerHelper * SAL_CALL getContainer( const ::com::sun::star::uno::Type & rKey ) const;
549 
568  sal_Int32 SAL_CALL addInterface(
569  const ::com::sun::star::uno::Type & rKey,
570  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r );
571 
582  sal_Int32 SAL_CALL removeInterface(
583  const ::com::sun::star::uno::Type & rKey,
584  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace );
585 
590  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt );
594  void SAL_CALL clear();
595 
596  typedef ::com::sun::star::uno::Type keyType;
597 private:
598  void *m_pMap;
599  ::osl::Mutex & rMutex;
600 
602  OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
603 };
604 
605 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
606 
607 }
608 
609 #endif
610 
611 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void removeListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:474
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:108
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:440
::com::sun::star::uno::Type keyType
Definition: interfacecontainer.h:596
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:404
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
element_alias()
Definition: interfacecontainer.h:52
This enum value can be used for implicit interface query.
Definition: Reference.h:139
key keyType
Definition: interfacecontainer.h:397
::com::sun::star::uno::XInterface * next()
Return the next element of the iterator.
unsigned char sal_Bool
Definition: types.h:48
::com::sun::star::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:51
::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > * pAsSequence
Definition: interfacecontainer.h:50
Specialized class for key type com::sun::star::uno::Type, without explicit usage of STL symbols...
Definition: interfacecontainer.h:512
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
Definition: interfacecontainer.h:502
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:442
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
Definition: interfacecontainer.h:90
size_t operator()(const ::com::sun::star::uno::Type &s) const
Definition: interfacecontainer.h:504
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structure.
Definition: interfacecontainer.h:450
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:435
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:444
This is the iterator of a InterfaceContainerHelper.
Definition: interfacecontainer.h:66
void addListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:460
A helper class to store interface references of different types.
Definition: interfacecontainer.h:311
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:37
#define sal_False
Definition: types.h:49
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
This is here to optimise space in the common case that there are zero or one listeners.
Definition: interfacecontainer.h:48
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:438
void notifyEach(void(ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
Definition: interfacecontainer.h:298
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:38
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:490
#define OSL_ENSURE(c, m)
Definition: diagnose.h:106
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:279
A container of interfaces.
Definition: interfacecontainer.h:126