LibreOffice
LibreOffice 4.3 SDK C/C++ API Reference
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 <osl/mutex.hxx>
27 #include <rtl/alloc.h>
29 #include <com/sun/star/uno/XInterface.hpp>
30 #include <com/sun/star/lang/EventObject.hpp>
31 
32 #include <com/sun/star/lang/DisposedException.hpp>
34  //for docpp
36 namespace cppu
37 {
38 
39 namespace detail {
40 
42  {
44  ::com::sun::star::uno::XInterface * pAsInterface;
45  element_alias() : pAsInterface(0) {}
46  };
47 
48 }
49 
50 
51 class OInterfaceContainerHelper;
60 {
61 public:
76 
81 
83  bool SAL_CALL hasMoreElements() const SAL_THROW(())
84  { return nRemain != 0; }
89  ::com::sun::star::uno::XInterface * SAL_CALL next() SAL_THROW(());
90 
96  void SAL_CALL remove() SAL_THROW(());
97 
98 private:
100  sal_Bool bIsList;
101 
102  detail::element_alias aData;
103 
104  sal_Int32 nRemain;
105 
106  OInterfaceIteratorHelper( const OInterfaceIteratorHelper & ) SAL_THROW(());
107  OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & ) SAL_THROW(());
108 };
109 
110 
118 {
119 public:
120  // these are here to force memory de/allocation to sal lib.
121  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
122  { return ::rtl_allocateMemory( nSize ); }
123  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
124  { ::rtl_freeMemory( pMem ); }
125  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
126  { return pMem; }
127  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
128  {}
129 
137  OInterfaceContainerHelper( ::osl::Mutex & rMutex ) SAL_THROW(());
142  ~OInterfaceContainerHelper() SAL_THROW(());
147  sal_Int32 SAL_CALL getLength() const SAL_THROW(());
148 
152  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > SAL_CALL getElements() const SAL_THROW(());
153 
170  sal_Int32 SAL_CALL addInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace ) SAL_THROW(());
178  sal_Int32 SAL_CALL removeInterface( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace ) SAL_THROW(());
183  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
187  void SAL_CALL clear() SAL_THROW(());
188 
200  template <typename ListenerT, typename FuncT>
201  inline void forEach( FuncT const& func );
202 
224  template< typename ListenerT, typename EventT >
225  inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
226 
227 private:
233  detail::element_alias aData;
234  ::osl::Mutex & rMutex;
236  sal_Bool bInUse;
238  sal_Bool bIsList;
239 
240  OInterfaceContainerHelper( const OInterfaceContainerHelper & ) SAL_THROW(());
241  OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & ) SAL_THROW(());
242 
243  /*
244  Dulicate content of the conaitner and release the old one without destroying.
245  The mutex must be locked and the memberbInUse must be true.
246  */
247  void copyAndResetInUse() SAL_THROW(());
248 
249 private:
250  template< typename ListenerT, typename EventT >
251  class NotifySingleListener
252  {
253  private:
254  typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
255  NotificationMethod m_pMethod;
256  const EventT& m_rEvent;
257  public:
258  NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
259 
260  void operator()( const ::com::sun::star::uno::Reference<ListenerT>& listener ) const
261  {
262  (listener.get()->*m_pMethod)( m_rEvent );
263  }
264  };
265 };
266 
267 template <typename ListenerT, typename FuncT>
268 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
269 {
270  OInterfaceIteratorHelper iter( *this );
271  while (iter.hasMoreElements()) {
274  if (xListener.is()) {
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  }
283  }
284 }
285 
286 template< typename ListenerT, typename EventT >
287 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
288 {
289  forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
290 }
291 
292 
299 template< class key, class hashImpl = void, class equalImpl = std::equal_to<key> >
301 {
302 public:
303  // these are here to force memory de/allocation to sal lib.
304  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
305  { return ::rtl_allocateMemory( nSize ); }
306  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
307  { ::rtl_freeMemory( pMem ); }
308  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
309  { return pMem; }
310  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
311  {}
312 
324  inline ~OMultiTypeInterfaceContainerHelperVar() SAL_THROW(());
325 
329  inline ::com::sun::star::uno::Sequence< key > SAL_CALL getContainedTypes() const SAL_THROW(());
330 
337  inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const SAL_THROW(());
338 
357  inline sal_Int32 SAL_CALL addInterface(
358  const key & rKey,
359  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
360  SAL_THROW(());
361 
372  inline sal_Int32 SAL_CALL removeInterface(
373  const key & rKey,
374  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
375  SAL_THROW(());
376 
382  inline void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
386  inline void SAL_CALL clear() SAL_THROW(());
387 
388  typedef key keyType;
389 private:
390  typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
391  InterfaceMap *m_pMap;
392  ::osl::Mutex & rMutex;
393 
394  inline typename InterfaceMap::iterator find(const key &rKey) const
395  {
396  typename InterfaceMap::iterator iter = m_pMap->begin();
397  typename InterfaceMap::iterator end = m_pMap->end();
398 
399  while( iter != end )
400  {
401  equalImpl equal;
402  if( equal( iter->first, rKey ) )
403  break;
404  iter++;
405  }
406  return iter;
407  }
408 
410  inline OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_THROW(());
411 };
412 
413 
414 
415 
425 template < class container , class keyType >
427 {
431  container aLC;
436 
442  : rMutex( rMutex_ )
443  , aLC( rMutex_ )
444  , bDisposed( sal_False )
445  , bInDispose( sal_False )
446  {}
447 
451  inline void addListener(
452  const keyType &key,
453  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > &r )
454  SAL_THROW(())
455  {
456  ::osl::MutexGuard guard( rMutex );
457  OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
458  OSL_ENSURE( !bDisposed, "object is disposed" );
459  if( ! bInDispose && ! bDisposed )
460  aLC.addInterface( key , r );
461  }
462 
466  inline void removeListener(
467  const keyType &key,
468  const ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > & r )
469  SAL_THROW(())
470  {
471  ::osl::MutexGuard guard( rMutex );
472  OSL_ENSURE( !bDisposed, "object is disposed" );
473  if( ! bInDispose && ! bDisposed )
474  aLC.removeInterface( key , r );
475  }
476 
483  inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const SAL_THROW(())
484  { return aLC.getContainer( key ); }
485 };
486 
487 /*------------------------------------------
488 *
489 * In general, the above templates are used with a Type as key.
490 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
491 *
492 *------------------------------------------*/
493 
494 // helper function call class
496 {
497  size_t operator()(const ::com::sun::star::uno::Type & s) const SAL_THROW(())
498  { return (size_t) s.getTypeName().hashCode(); }
499 };
500 
501 
506 {
507 public:
508  // these are here to force memory de/allocation to sal lib.
509  inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
510  { return ::rtl_allocateMemory( nSize ); }
511  inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
512  { ::rtl_freeMemory( pMem ); }
513  inline static void * SAL_CALL operator new( size_t, void * pMem ) SAL_THROW(())
514  { return pMem; }
515  inline static void SAL_CALL operator delete( void *, void * ) SAL_THROW(())
516  {}
517 
529  ~OMultiTypeInterfaceContainerHelper() SAL_THROW(());
530 
534  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getContainedTypes() const SAL_THROW(());
535 
541  OInterfaceContainerHelper * SAL_CALL getContainer( const ::com::sun::star::uno::Type & rKey ) const SAL_THROW(());
542 
561  sal_Int32 SAL_CALL addInterface(
562  const ::com::sun::star::uno::Type & rKey,
563  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & r )
564  SAL_THROW(());
565 
576  sal_Int32 SAL_CALL removeInterface(
577  const ::com::sun::star::uno::Type & rKey,
578  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rxIFace )
579  SAL_THROW(());
580 
585  void SAL_CALL disposeAndClear( const ::com::sun::star::lang::EventObject & rEvt ) SAL_THROW(());
589  void SAL_CALL clear() SAL_THROW(());
590 
591  typedef ::com::sun::star::uno::Type keyType;
592 private:
593  void *m_pMap;
594  ::osl::Mutex & rMutex;
595 
597  inline OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_THROW(());
598 };
599 
600 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
601 
602 }
603 
604 #endif
605 
606 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OBroadcastHelperVar(::osl::Mutex &rMutex_) SAL_THROW(())
Initialize the structur.
Definition: interfacecontainer.h:441
key keyType
Definition: interfacecontainer.h:388
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:429
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:123
This is the iterator of a InterfaceContainerHelper.
Definition: interfacecontainer.h:59
element_alias()
Definition: interfacecontainer.h:45
A helper class to store interface references of different types.
Definition: interfacecontainer.h:300
unsigned char sal_Bool
Definition: types.h:46
C++ class representing an IDL meta type.
Definition: Type.h:55
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:268
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:433
#define sal_False
Definition: types.h:47
void remove() SAL_THROW(())
Removes the current element (the last one returned by next()) from the underlying container...
void addListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:451
::com::sun::star::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:44
This enum value can be used for implicit interface query.
Definition: Reference.h:138
::com::sun::star::uno::XInterface * next() SAL_THROW(())
Return the next element of the iterator.
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > * pAsSequence
Definition: interfacecontainer.h:43
A container of interfaces.
Definition: interfacecontainer.h:117
Definition: interfacecontainer.h:495
Definition: conditn.hxx:30
#define OSL_ENSURE(c, m)
Definition: diagnose.h:106
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:426
Definition: Enterable.hxx:26
#define SAL_THROW(x)
Exception specification documentation.
Definition: types.h:361
OInterfaceContainerHelper * getContainer(const keyType &key) const SAL_THROW(())
Return the container created under this key.
Definition: interfacecontainer.h:483
void notifyEach(void(ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
Definition: interfacecontainer.h:287
Specialized class for key type com::sun::star::uno::Type, without explicit usage of STL symbols...
Definition: interfacecontainer.h:505
bool hasMoreElements() const SAL_THROW(())
Return true, if there are more elements in the iterator.
Definition: interfacecontainer.h:83
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:431
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:32
void removeListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:466
A mutual exclusion synchronization object.
Definition: mutex.hxx:32
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
Definition: interfacecontainer.h:41
size_t operator()(const ::com::sun::star::uno::Type &s) const SAL_THROW(())
Definition: interfacecontainer.h:497
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:435
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
Definition: types.h:391