LibreOffice
LibreOffice 4.2 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 <vector>
23 #include <osl/mutex.hxx>
24 #include <rtl/alloc.h>
26 #include <com/sun/star/uno/XInterface.hpp>
27 #include <com/sun/star/lang/EventObject.hpp>
28 
29 #include <com/sun/star/lang/DisposedException.hpp>
31  //for docpp
33 namespace cppu
34 {
35 
36 namespace detail {
37 
39  {
41  ::com::sun::star::uno::XInterface * pAsInterface;
43  };
44 
45 }
46 
47 //===================================================================
48 class OInterfaceContainerHelper;
57 {
58 public:
73 
78 
80  sal_Bool SAL_CALL hasMoreElements() const SAL_THROW(())
81  { return nRemain != 0; }
86  ::com::sun::star::uno::XInterface * SAL_CALL next() SAL_THROW(());
87 
93  void SAL_CALL remove() SAL_THROW(());
94 
95 private:
97  sal_Bool bIsList;
98 
99  detail::element_alias aData;
100 
101  sal_Int32 nRemain;
102 
103  OInterfaceIteratorHelper( const OInterfaceIteratorHelper & ) SAL_THROW(());
104  OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & ) SAL_THROW(());
105 };
106 
107 //===================================================================
115 {
116 public:
117  // these are here to force memory de/allocation to sal lib.
118  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
119  { return ::rtl_allocateMemory( nSize ); }
120  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
121  { ::rtl_freeMemory( pMem ); }
122  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
123  { return pMem; }
124  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
125  {}
126 
134  OInterfaceContainerHelper( ::osl::Mutex & rMutex ) SAL_THROW(());
139  ~OInterfaceContainerHelper() SAL_THROW(());
144  sal_Int32 SAL_CALL getLength() const SAL_THROW(());
145 
149  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > SAL_CALL getElements() const SAL_THROW(());
150 
167  sal_Int32 SAL_CALL addInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace ) SAL_THROW(());
175  sal_Int32 SAL_CALL removeInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace ) SAL_THROW(());
180  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
184  void SAL_CALL clear() SAL_THROW(());
185 
197  template <typename ListenerT, typename FuncT>
198  inline void forEach( FuncT const& func );
199 
221  template< typename ListenerT, typename EventT >
222  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
223 
224 private:
230  detail::element_alias aData;
231  ::osl::Mutex & rMutex;
233  sal_Bool bInUse;
235  sal_Bool bIsList;
236 
237  OInterfaceContainerHelper( const OInterfaceContainerHelper & ) SAL_THROW(());
238  OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & ) SAL_THROW(());
239 
240  /*
241  Dulicate content of the conaitner and release the old one without destroying.
242  The mutex must be locked and the memberbInUse must be true.
243  */
244  void copyAndResetInUse() SAL_THROW(());
245 
246 private:
247  template< typename ListenerT, typename EventT >
248  class NotifySingleListener
249  {
250  private:
251  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
252  NotificationMethod m_pMethod;
253  const EventT& m_rEvent;
254  public:
255  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
256 
257  void operator()( const ::com::sun::star::uno::Reference<ListenerT>& listener ) const
258  {
259  (listener.get()->*m_pMethod)( m_rEvent );
260  }
261  };
262 };
263 
264 template <typename ListenerT, typename FuncT>
265 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
266 {
267  OInterfaceIteratorHelper iter( *this );
268  while (iter.hasMoreElements()) {
271  if (xListener.is()) {
272  try {
273  func( xListener );
274  }
275  catch (::com::sun::star::lang::DisposedException const& exc) {
276  if (exc.Context == xListener)
277  iter.remove();
278  }
279  }
280  }
281 }
282 
283 template< typename ListenerT, typename EventT >
284 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
285 {
286  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
287 }
288 
289 //===================================================================
296 template< class key , class hashImpl , class equalImpl >
298 {
299 public:
300  // these are here to force memory de/allocation to sal lib.
301  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
302  { return ::rtl_allocateMemory( nSize ); }
303  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
304  { ::rtl_freeMemory( pMem ); }
305  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
306  { return pMem; }
307  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
308  {}
309 
321  inline ~OMultiTypeInterfaceContainerHelperVar() SAL_THROW(());
322 
326  inline ::com::sun::star::uno::Sequence< key > SAL_CALL getContainedTypes() const SAL_THROW(());
327 
334  inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const SAL_THROW(());
335 
354  inline sal_Int32 SAL_CALL addInterface(
355  const key & rKey,
356  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
357  SAL_THROW(());
358 
369  inline sal_Int32 SAL_CALL removeInterface(
370  const key & rKey,
371  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
372  SAL_THROW(());
373 
379  inline void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
383  inline void SAL_CALL clear() SAL_THROW(());
384 
385  typedef key keyType;
386 private:
387  typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
388  InterfaceMap *m_pMap;
389  ::osl::Mutex & rMutex;
390 
391  inline typename InterfaceMap::iterator find(const key &rKey) const
392  {
393  typename InterfaceMap::iterator iter = m_pMap->begin();
394  typename InterfaceMap::iterator end = m_pMap->end();
395 
396  while( iter != end )
397  {
398  equalImpl equal;
399  if( equal( iter->first, rKey ) )
400  break;
401  iter++;
402  }
403  return iter;
404  }
405 
407  inline OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_THROW(());
408 };
409 
410 
411 
412 
422 template < class container , class keyType >
424 {
428  container aLC;
433 
439  : rMutex( rMutex_ )
440  , aLC( rMutex_ )
441  , bDisposed( sal_False )
442  , bInDispose( sal_False )
443  {}
444 
448  inline void addListener(
449  const keyType &key,
450  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > &r )
451  SAL_THROW(())
452  {
453  ::osl::MutexGuard guard( rMutex );
454  OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
455  OSL_ENSURE( !bDisposed, "object is disposed" );
456  if( ! bInDispose && ! bDisposed )
457  aLC.addInterface( key , r );
458  }
459 
463  inline void removeListener(
464  const keyType &key,
465  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > & r )
466  SAL_THROW(())
467  {
468  ::osl::MutexGuard guard( rMutex );
469  OSL_ENSURE( !bDisposed, "object is disposed" );
470  if( ! bInDispose && ! bDisposed )
471  aLC.removeInterface( key , r );
472  }
473 
480  inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const SAL_THROW(())
481  { return aLC.getContainer( key ); }
482 };
483 
484 /*------------------------------------------
485 *
486 * In general, the above templates are used with a Type as key.
487 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
488 *
489 *------------------------------------------*/
490 
491 // helper function call class
493 {
494  size_t operator()(const ::com::sun::star::uno::Type & s) const SAL_THROW(())
495  { return (size_t) s.getTypeName().hashCode(); }
496 };
497 
498 
503 {
504 public:
505  // these are here to force memory de/allocation to sal lib.
506  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
507  { return ::rtl_allocateMemory( nSize ); }
508  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
509  { ::rtl_freeMemory( pMem ); }
510  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
511  { return pMem; }
512  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
513  {}
514 
526  ~OMultiTypeInterfaceContainerHelper() SAL_THROW(());
527 
531  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getContainedTypes() const SAL_THROW(());
532 
538  OInterfaceContainerHelper * SAL_CALL getContainer( const ::com::sun::star::uno::Type & rKey ) const SAL_THROW(());
539 
558  sal_Int32 SAL_CALL addInterface(
559  const ::com::sun::star::uno::Type & rKey,
560  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
561  SAL_THROW(());
562 
573  sal_Int32 SAL_CALL removeInterface(
574  const ::com::sun::star::uno::Type & rKey,
575  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
576  SAL_THROW(());
577 
582  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
586  void SAL_CALL clear() SAL_THROW(());
587 
588  typedef ::com::sun::star::uno::Type keyType;
589 private:
590  void *m_pMap;
591  ::osl::Mutex & rMutex;
592 
594  inline OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_THROW(());
595 };
596 
597 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
598 
599 }
600 
601 #endif
602 
603 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:480
::com::sun::star::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:41
size_t operator()(const ::com::sun::star::uno::Type &s) const
Definition: interfacecontainer.h:494
This is the iterator of a InterfaceContainerHelper.
Definition: interfacecontainer.h:56
C++ class representing an IDL meta type.
Definition: Type.h:55
void removeListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:463
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
void notifyEach(void(ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
Definition: interfacecontainer.h:284
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structur.
Definition: interfacecontainer.h:438
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:123
Specialized class for key type com::sun::star::uno::Type, without explicit usage of STL symbols...
Definition: interfacecontainer.h:502
A container of interfaces.
Definition: interfacecontainer.h:114
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
#define OSL_ENSURE(c, m)
Definition: diagnose.h:154
key keyType
Definition: interfacecontainer.h:385
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:430
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:265
::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > * pAsSequence
Definition: interfacecontainer.h:40
::com::sun::star::uno::XInterface * next()
Return the next element of the iterator.
element_alias()
Definition: interfacecontainer.h:42
void addListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:448
A mutual exclusion synchronization object.
Definition: mutex.hxx:32
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:32
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:423
sal_Bool hasMoreElements() const
Return sal_True, if there are more elements in the iterator.
Definition: interfacecontainer.h:80
This enum value can be used for implicit interface query.
Definition: Reference.h:138
Definition: interfacecontainer.h:492
unsigned char sal_Bool
Definition: types.h:46
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:432
A helper class to store interface references of different types.
Definition: interfacecontainer.h:297
#define sal_False
Definition: types.h:47
Definition: interfacecontainer.h:38
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:428
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:426
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:358