My Project
UDK 3.2.7 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  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 #ifndef _CPPUHELPER_INTERFACECONTAINER_H_
29 #define _CPPUHELPER_INTERFACECONTAINER_H_
30 
31 #include <vector>
32 #include <osl/mutex.hxx>
33 #include <rtl/alloc.h>
35 #include <com/sun/star/uno/XInterface.hpp>
36 #ifndef _COM_SUN_STAR_LANG_EVENTOBJECT_HXX_
37 #include <com/sun/star/lang/EventObject.hpp>
38 #endif
39 
40 #ifndef _COM_SUN_STAR_LANG_DISPOSEDEXCEPTION_HXX_
41 #include "com/sun/star/lang/DisposedException.hpp"
42 #endif
43 #include "cppuhelperdllapi.h"
44  //for docpp
46 namespace cppu
47 {
48 
49 namespace detail {
50 
52  {
54  ::com::sun::star::uno::XInterface * pAsInterface;
56  };
57 
58 }
59 
60 //===================================================================
61 class OInterfaceContainerHelper;
70 {
71 public:
86 
91 
93  sal_Bool SAL_CALL hasMoreElements() const SAL_THROW(())
94  { return nRemain != 0; }
99  ::com::sun::star::uno::XInterface * SAL_CALL next() SAL_THROW(());
100 
106  void SAL_CALL remove() SAL_THROW(());
107 
108 private:
110  sal_Bool bIsList;
111 
112  detail::element_alias aData;
113 
114  sal_Int32 nRemain;
115 
116  OInterfaceIteratorHelper( const OInterfaceIteratorHelper & ) SAL_THROW(());
117  OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & ) SAL_THROW(());
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 ) SAL_THROW(())
132  { return ::rtl_allocateMemory( nSize ); }
133  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
134  { ::rtl_freeMemory( pMem ); }
135  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
136  { return pMem; }
137  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
138  {}
139 
147  OInterfaceContainerHelper( ::osl::Mutex & rMutex ) SAL_THROW(());
152  ~OInterfaceContainerHelper() SAL_THROW(());
157  sal_Int32 SAL_CALL getLength() const SAL_THROW(());
158 
162  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > SAL_CALL getElements() const SAL_THROW(());
163 
180  sal_Int32 SAL_CALL addInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace ) SAL_THROW(());
188  sal_Int32 SAL_CALL removeInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace ) SAL_THROW(());
193  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
197  void SAL_CALL clear() SAL_THROW(());
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 & ) SAL_THROW(());
251  OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & ) SAL_THROW(());
252 
253  /*
254  Dulicate content of the conaitner and release the old one without destroying.
255  The mutex must be locked and the memberbInUse must be true.
256  */
257  void copyAndResetInUse() SAL_THROW(());
258 
259 private:
260  template< typename ListenerT, typename EventT >
261  class NotifySingleListener
262  {
263  private:
264  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
265  NotificationMethod m_pMethod;
266  const EventT& m_rEvent;
267  public:
268  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
269 
270  void operator()( const ::com::sun::star::uno::Reference<ListenerT>& listener ) const
271  {
272  (listener.get()->*m_pMethod)( m_rEvent );
273  }
274  };
275 };
276 
277 template <typename ListenerT, typename FuncT>
278 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
279 {
280  OInterfaceIteratorHelper iter( *this );
281  while (iter.hasMoreElements()) {
284  if (xListener.is()) {
285 #if defined(EXCEPTIONS_OFF)
286  func( xListener );
287 #else
288  try {
289  func( xListener );
290  }
291  catch (::com::sun::star::lang::DisposedException const& exc) {
292  if (exc.Context == xListener)
293  iter.remove();
294  }
295 #endif
296  }
297  }
298 }
299 
300 template< typename ListenerT, typename EventT >
301 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
302 {
303  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
304 }
305 
306 //===================================================================
313 template< class key , class hashImpl , class equalImpl >
315 {
316 public:
317  // these are here to force memory de/allocation to sal lib.
318  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
319  { return ::rtl_allocateMemory( nSize ); }
320  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
321  { ::rtl_freeMemory( pMem ); }
322  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
323  { return pMem; }
324  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
325  {}
326 
338  inline ~OMultiTypeInterfaceContainerHelperVar() SAL_THROW(());
339 
343  inline ::com::sun::star::uno::Sequence< key > SAL_CALL getContainedTypes() const SAL_THROW(());
344 
351  inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const SAL_THROW(());
352 
371  inline sal_Int32 SAL_CALL addInterface(
372  const key & rKey,
373  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
374  SAL_THROW(());
375 
386  inline sal_Int32 SAL_CALL removeInterface(
387  const key & rKey,
388  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
389  SAL_THROW(());
390 
396  inline void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
400  inline void SAL_CALL clear() SAL_THROW(());
401 
402  typedef key keyType;
403 private:
404  typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
405  InterfaceMap *m_pMap;
406  ::osl::Mutex & rMutex;
407 
408  inline typename InterfaceMap::iterator find(const key &rKey) const
409  {
410  typename InterfaceMap::iterator iter = m_pMap->begin();
411  typename InterfaceMap::iterator end = m_pMap->end();
412 
413  while( iter != end )
414  {
415  equalImpl equal;
416  if( equal( iter->first, rKey ) )
417  break;
418  iter++;
419  }
420  return iter;
421  }
422 
424  inline OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_THROW(());
425 };
426 
427 
428 
429 
439 template < class container , class keyType >
441 {
445  container aLC;
450 
455  OBroadcastHelperVar( ::osl::Mutex & rMutex_ ) SAL_THROW(())
456  : rMutex( rMutex_ )
457  , aLC( rMutex_ )
458  , bDisposed( sal_False )
459  , bInDispose( sal_False )
460  {}
461 
465  inline void addListener(
466  const keyType &key,
467  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > &r )
468  SAL_THROW(())
469  {
470  ::osl::MutexGuard guard( rMutex );
471  OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
472  OSL_ENSURE( !bDisposed, "object is disposed" );
473  if( ! bInDispose && ! bDisposed )
474  aLC.addInterface( key , r );
475  }
476 
480  inline void removeListener(
481  const keyType &key,
482  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > & r )
483  SAL_THROW(())
484  {
485  ::osl::MutexGuard guard( rMutex );
486  OSL_ENSURE( !bDisposed, "object is disposed" );
487  if( ! bInDispose && ! bDisposed )
488  aLC.removeInterface( key , r );
489  }
490 
497  inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const SAL_THROW(())
498  { return aLC.getContainer( key ); }
499 };
500 
501 /*------------------------------------------
502 *
503 * In general, the above templates are used with a Type as key.
504 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
505 *
506 *------------------------------------------*/
507 
508 // helper function call class
510 {
511  size_t operator()(const ::com::sun::star::uno::Type & s) const SAL_THROW(())
512  { return (size_t) s.getTypeName().hashCode(); }
513 };
514 
515 
520 {
521 public:
522  // these are here to force memory de/allocation to sal lib.
523  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
524  { return ::rtl_allocateMemory( nSize ); }
525  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
526  { ::rtl_freeMemory( pMem ); }
527  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
528  { return pMem; }
529  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
530  {}
531 
543  ~OMultiTypeInterfaceContainerHelper() SAL_THROW(());
544 
548  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getContainedTypes() const SAL_THROW(());
549 
555  OInterfaceContainerHelper * SAL_CALL getContainer( const ::com::sun::star::uno::Type & rKey ) const SAL_THROW(());
556 
575  sal_Int32 SAL_CALL addInterface(
576  const ::com::sun::star::uno::Type & rKey,
577  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
578  SAL_THROW(());
579 
590  sal_Int32 SAL_CALL removeInterface(
591  const ::com::sun::star::uno::Type & rKey,
592  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
593  SAL_THROW(());
594 
599  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
603  void SAL_CALL clear() SAL_THROW(());
604 
605  typedef ::com::sun::star::uno::Type keyType;
606 private:
607  void *m_pMap;
608  ::osl::Mutex & rMutex;
609 
611  inline OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_THROW(());
612 };
613 
614 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
615 
616 }
617 
618 #endif
619 
620 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */