LibreOffice
LibreOffice 4.1 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 _CPPUHELPER_INTERFACECONTAINER_H_
20 #define _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"
30 #include "cppuhelperdllapi.h"
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 #if defined(EXCEPTIONS_OFF)
273  func( xListener );
274 #else
275  try {
276  func( xListener );
277  }
278  catch (::com::sun::star::lang::DisposedException const& exc) {
279  if (exc.Context == xListener)
280  iter.remove();
281  }
282 #endif
283  }
284  }
285 }
286 
287 template< typename ListenerT, typename EventT >
288 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
289 {
290  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
291 }
292 
293 //===================================================================
300 template< class key , class hashImpl , class equalImpl >
302 {
303 public:
304  // these are here to force memory de/allocation to sal lib.
305  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
306  { return ::rtl_allocateMemory( nSize ); }
307  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
308  { ::rtl_freeMemory( pMem ); }
309  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
310  { return pMem; }
311  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
312  {}
313 
325  inline ~OMultiTypeInterfaceContainerHelperVar() SAL_THROW(());
326 
330  inline ::com::sun::star::uno::Sequence< key > SAL_CALL getContainedTypes() const SAL_THROW(());
331 
338  inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const SAL_THROW(());
339 
358  inline sal_Int32 SAL_CALL addInterface(
359  const key & rKey,
360  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
361  SAL_THROW(());
362 
373  inline sal_Int32 SAL_CALL removeInterface(
374  const key & rKey,
375  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
376  SAL_THROW(());
377 
383  inline void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
387  inline void SAL_CALL clear() SAL_THROW(());
388 
389  typedef key keyType;
390 private:
391  typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
392  InterfaceMap *m_pMap;
393  ::osl::Mutex & rMutex;
394 
395  inline typename InterfaceMap::iterator find(const key &rKey) const
396  {
397  typename InterfaceMap::iterator iter = m_pMap->begin();
398  typename InterfaceMap::iterator end = m_pMap->end();
399 
400  while( iter != end )
401  {
402  equalImpl equal;
403  if( equal( iter->first, rKey ) )
404  break;
405  iter++;
406  }
407  return iter;
408  }
409 
411  inline OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_THROW(());
412 };
413 
414 
415 
416 
426 template < class container , class keyType >
428 {
432  container aLC;
437 
442  OBroadcastHelperVar( ::osl::Mutex & rMutex_ ) SAL_THROW(())
443  : rMutex( rMutex_ )
444  , aLC( rMutex_ )
445  , bDisposed( sal_False )
446  , bInDispose( sal_False )
447  {}
448 
452  inline void addListener(
453  const keyType &key,
454  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > &r )
455  SAL_THROW(())
456  {
457  ::osl::MutexGuard guard( rMutex );
458  OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
459  OSL_ENSURE( !bDisposed, "object is disposed" );
460  if( ! bInDispose && ! bDisposed )
461  aLC.addInterface( key , r );
462  }
463 
467  inline void removeListener(
468  const keyType &key,
469  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > & r )
470  SAL_THROW(())
471  {
472  ::osl::MutexGuard guard( rMutex );
473  OSL_ENSURE( !bDisposed, "object is disposed" );
474  if( ! bInDispose && ! bDisposed )
475  aLC.removeInterface( key , r );
476  }
477 
484  inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const SAL_THROW(())
485  { return aLC.getContainer( key ); }
486 };
487 
488 /*------------------------------------------
489 *
490 * In general, the above templates are used with a Type as key.
491 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
492 *
493 *------------------------------------------*/
494 
495 // helper function call class
497 {
498  size_t operator()(const ::com::sun::star::uno::Type & s) const SAL_THROW(())
499  { return (size_t) s.getTypeName().hashCode(); }
500 };
501 
502 
507 {
508 public:
509  // these are here to force memory de/allocation to sal lib.
510  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
511  { return ::rtl_allocateMemory( nSize ); }
512  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
513  { ::rtl_freeMemory( pMem ); }
514  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
515  { return pMem; }
516  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
517  {}
518 
530  ~OMultiTypeInterfaceContainerHelper() SAL_THROW(());
531 
535  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getContainedTypes() const SAL_THROW(());
536 
542  OInterfaceContainerHelper * SAL_CALL getContainer( const ::com::sun::star::uno::Type & rKey ) const SAL_THROW(());
543 
562  sal_Int32 SAL_CALL addInterface(
563  const ::com::sun::star::uno::Type & rKey,
564  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
565  SAL_THROW(());
566 
577  sal_Int32 SAL_CALL removeInterface(
578  const ::com::sun::star::uno::Type & rKey,
579  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
580  SAL_THROW(());
581 
586  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
590  void SAL_CALL clear() SAL_THROW(());
591 
592  typedef ::com::sun::star::uno::Type keyType;
593 private:
594  void *m_pMap;
595  ::osl::Mutex & rMutex;
596 
598  inline OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_THROW(());
599 };
600 
601 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
602 
603 }
604 
605 #endif
606 
607 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */