LibreOffice LibreOffice 4.1 SDK C/C++ API Reference

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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>
25 #include <com/sun/star/uno/Sequence.hxx>
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
38 union element_alias
39 {
40 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > *pAsSequence;
41 ::com::sun::star::uno::XInterface * pAsInterface;
42 element_alias() : pAsInterface(0) {}
43 };
44
45 }
46
47 //===================================================================
48 class OInterfaceContainerHelper;
56 class CPPUHELPER_DLLPUBLIC OInterfaceIteratorHelper
57 {
58 public:
72 OInterfaceIteratorHelper( OInterfaceContainerHelper & rCont ) SAL_THROW(());
73
77 ~OInterfaceIteratorHelper() SAL_THROW(());
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:
96 OInterfaceContainerHelper & rCont;
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 //===================================================================
114 class CPPUHELPER_DLLPUBLIC OInterfaceContainerHelper
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:
225 friend class OInterfaceIteratorHelper;
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()) {
269 ::com::sun::star::uno::Reference<ListenerT> const xListener(
270 iter.next(), ::com::sun::star::uno::UNO_QUERY );
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 >
301 class OMultiTypeInterfaceContainerHelperVar
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
321 inline OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex & rMutex ) SAL_THROW(());
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
410 inline OMultiTypeInterfaceContainerHelperVar( const OMultiTypeInterfaceContainerHelperVar & ) SAL_THROW(());
411 inline OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_THROW(());
412 };
413
414
415
416
426 template < class container , class keyType >
427 struct OBroadcastHelperVar
428 {
430 ::osl::Mutex & rMutex;
432 container aLC;
434 sal_Bool bDisposed;
436 sal_Bool bInDispose;
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
496 struct hashType_Impl
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
506 class CPPUHELPER_DLLPUBLIC OMultiTypeInterfaceContainerHelper
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
526 OMultiTypeInterfaceContainerHelper( ::osl::Mutex & rMutex ) SAL_THROW(());
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
597 inline OMultiTypeInterfaceContainerHelper( const OMultiTypeInterfaceContainerHelper & ) SAL_THROW(());
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: */
cppu::OInterfaceIteratorHelper::remove
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
cppu::OBroadcastHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:484
cppu::detail::element_alias::pAsInterface
::com::sun::star::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:41
cppu::hashType_Impl::operator()
size_t operator()(const ::com::sun::star::uno::Type &s) const
Definition: interfacecontainer.h:498
cppu::OInterfaceIteratorHelper
This is the iterator of a InterfaceContainerHelper.
Definition: interfacecontainer.h:56
com::sun::star::uno::Type
C++ class representing an IDL meta type.
Definition: Type.h:55
cppu::OBroadcastHelperVar::removeListener
void removeListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:467
SAL_THROW
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:356
rtl_allocateMemory
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
cppu::OInterfaceContainerHelper::notifyEach
void notifyEach(void(ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Calls a UNO listener method for each contained listener.
Definition: interfacecontainer.h:288
cppu::OBroadcastHelperVar::OBroadcastHelperVar
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structur.
Definition: interfacecontainer.h:442
osl::Guard
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:123
cppu::OMultiTypeInterfaceContainerHelper
Specialized class for key type com::sun::star::uno::Type, without explicit usage of STL symbols...
Definition: interfacecontainer.h:506
cppu::OInterfaceContainerHelper
A container of interfaces.
Definition: interfacecontainer.h:114
CPPUHELPER_DLLPUBLIC
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
OSL_ENSURE
#define OSL_ENSURE(c, m)
Definition: diagnose.h:154
cppu::OMultiTypeInterfaceContainerHelperVar::keyType
key keyType
Definition: interfacecontainer.h:389
cppu::OBroadcastHelperVar::bDisposed
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:434
cppu::OInterfaceContainerHelper::forEach
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:265
cppu::detail::element_alias::pAsSequence
::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > * pAsSequence
Definition: interfacecontainer.h:40
cppu::OInterfaceIteratorHelper::next
::com::sun::star::uno::XInterface * next()
Return the next element of the iterator.
cppu::detail::element_alias::element_alias
element_alias()
Definition: interfacecontainer.h:42
cppu::OBroadcastHelperVar::addListener
void addListener(const keyType &key, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:452
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:32
com::sun::star::uno::Reference
Template reference class for interface type derived from BaseReference.
Definition: unotype.hxx:32
cppu::OBroadcastHelperVar
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:427
mutex.hxx
cppu::OInterfaceIteratorHelper::hasMoreElements
sal_Bool hasMoreElements() const
Return sal_True, if there are more elements in the iterator.
Definition: interfacecontainer.h:80
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
com::sun::star::uno::UNO_QUERY
This enum value can be used for implicit interface query.
Definition: Reference.h:140
cppu::hashType_Impl
Definition: interfacecontainer.h:496
cppu::OBroadcastHelperVar::bInDispose
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:436
Sequence.hxx
cppu::OMultiTypeInterfaceContainerHelperVar
A helper class to store interface references of different types.
Definition: interfacecontainer.h:301
sal_Bool
unsigned char sal_Bool
Definition: types.h:37
sal_False
#define sal_False
Definition: types.h:38
cppu::detail::element_alias
Definition: interfacecontainer.h:38
cppuhelperdllapi.h
cppu::OBroadcastHelperVar::aLC
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:432
com::sun::star::uno::Sequence
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
cppu::OBroadcastHelperVar::rMutex
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:430
alloc.h