LibreOffice: cppuhelper/interfacecontainer.h Source File

LibreOffice LibreOffice 5.1 SDK C/C++ API Reference

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

- File List
- File Members

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 <cstddef>
25 #include <functional>
26 #include <vector>
27 #include <utility>
28
29 #include <osl/diagnose.h>
30 #include <osl/mutex.hxx>
31 #include <rtl/alloc.h>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <com/sun/star/uno/XInterface.hpp>
34 #include <com/sun/star/lang/EventObject.hpp>
35
36 #include <com/sun/star/lang/DisposedException.hpp>
37 #include <cppuhelper/cppuhelperdllapi.h>
38 //for docpp
40 namespace cppu
41 {
42
43 namespace detail {
44
49 union element_alias
50 {
51 css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > *pAsSequence;
52 css::uno::XInterface * pAsInterface;
53 element_alias() : pAsInterface(NULL) {}
54 };
55
56 }
57
58
59 class OInterfaceContainerHelper;
67 class CPPUHELPER_DLLPUBLIC OInterfaceIteratorHelper
68 {
69 public:
83 OInterfaceIteratorHelper( OInterfaceContainerHelper & rCont );
84
88 ~OInterfaceIteratorHelper();
89
91 bool SAL_CALL hasMoreElements() const
92 { return nRemain != 0; }
97 css::uno::XInterface * SAL_CALL next();
98
104 void SAL_CALL remove();
105
106 private:
107 OInterfaceContainerHelper & rCont;
108 sal_Bool bIsList;
109
110 detail::element_alias aData;
111
112 sal_Int32 nRemain;
113
114 OInterfaceIteratorHelper( const OInterfaceIteratorHelper & )
115 SAL_DELETED_FUNCTION;
116 OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & )
117 SAL_DELETED_FUNCTION;
118 };
119
120
127 class CPPUHELPER_DLLPUBLIC OInterfaceContainerHelper
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 )
132 { return ::rtl_allocateMemory( nSize ); }
133 inline static void SAL_CALL operator delete( void * pMem )
134 { ::rtl_freeMemory( pMem ); }
135 inline static void * SAL_CALL operator new( size_t, void * pMem )
136 { return pMem; }
137 inline static void SAL_CALL operator delete( void *, void * )
138 {}
139
147 OInterfaceContainerHelper( ::osl::Mutex & rMutex );
152 ~OInterfaceContainerHelper();
157 sal_Int32 SAL_CALL getLength() const;
158
162 css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getElements() const;
163
180 sal_Int32 SAL_CALL addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
188 sal_Int32 SAL_CALL removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
193 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
197 void SAL_CALL clear();
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:
238 friend class OInterfaceIteratorHelper;
243 detail::element_alias aData;
244 ::osl::Mutex & rMutex;
246 sal_Bool bInUse;
248 sal_Bool bIsList;
249
250 OInterfaceContainerHelper( const OInterfaceContainerHelper & )
251 SAL_DELETED_FUNCTION;
252 OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & )
253 SAL_DELETED_FUNCTION;
254
255 /*
256 Duplicate content of the conaitner and release the old one without destroying.
257 The mutex must be locked and the memberbInUse must be true.
258 */
259 void copyAndResetInUse();
260
261 private:
262 template< typename ListenerT, typename EventT >
263 class NotifySingleListener
264 {
265 private:
266 typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
267 NotificationMethod m_pMethod;
268 const EventT& m_rEvent;
269 public:
270 NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
271
272 void operator()( const css::uno::Reference<ListenerT>& listener ) const
273 {
274 (listener.get()->*m_pMethod)( m_rEvent );
275 }
276 };
277 };
278
279 template <typename ListenerT, typename FuncT>
280 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
281 {
282 OInterfaceIteratorHelper iter( *this );
283 while (iter.hasMoreElements()) {
284 css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
285 if (xListener.is()) {
286 try {
287 func( xListener );
288 }
289 catch (css::lang::DisposedException const& exc) {
290 if (exc.Context == xListener)
291 iter.remove();
292 }
293 }
294 }
295 }
296
297 template< typename ListenerT, typename EventT >
298 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
299 {
300 forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
301 }
302
303
310 template< class key, class hashImpl = void, class equalImpl = std::equal_to<key> >
311 class OMultiTypeInterfaceContainerHelperVar
312 {
313 public:
314 // these are here to force memory de/allocation to sal lib.
315 inline static void * SAL_CALL operator new( size_t nSize )
316 { return ::rtl_allocateMemory( nSize ); }
317 inline static void SAL_CALL operator delete( void * pMem )
318 { ::rtl_freeMemory( pMem ); }
319 inline static void * SAL_CALL operator new( size_t, void * pMem )
320 { return pMem; }
321 inline static void SAL_CALL operator delete( void *, void * )
322 {}
323
331 inline OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex & rMutex );
335 inline ~OMultiTypeInterfaceContainerHelperVar();
336
340 inline css::uno::Sequence< key > SAL_CALL getContainedTypes() const;
341
348 inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const;
349
368 inline sal_Int32 SAL_CALL addInterface(
369 const key & rKey,
370 const css::uno::Reference< css::uno::XInterface > & r );
371
382 inline sal_Int32 SAL_CALL removeInterface(
383 const key & rKey,
384 const css::uno::Reference< css::uno::XInterface > & rxIFace );
385
391 inline void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
395 inline void SAL_CALL clear();
396
397 typedef key keyType;
398 private:
399 typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
400 InterfaceMap *m_pMap;
401 ::osl::Mutex & rMutex;
402
403 inline typename InterfaceMap::iterator find(const key &rKey) const
404 {
405 typename InterfaceMap::iterator iter = m_pMap->begin();
406 typename InterfaceMap::iterator end = m_pMap->end();
407
408 while( iter != end )
409 {
410 equalImpl equal;
411 if( equal( iter->first, rKey ) )
412 break;
413 iter++;
414 }
415 return iter;
416 }
417
418 OMultiTypeInterfaceContainerHelperVar( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
419 OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
420 };
421
422
423
424
434 template < class container , class keyType >
435 struct OBroadcastHelperVar
436 {
438 ::osl::Mutex & rMutex;
440 container aLC;
442 sal_Bool bDisposed;
444 sal_Bool bInDispose;
445
450 OBroadcastHelperVar( ::osl::Mutex & rMutex_ )
451 : rMutex( rMutex_ )
452 , aLC( rMutex_ )
453 , bDisposed( sal_False )
454 , bInDispose( sal_False )
455 {}
456
460 inline void addListener(
461 const keyType &key,
462 const css::uno::Reference < css::uno::XInterface > &r )
463 {
464 ::osl::MutexGuard guard( rMutex );
465 OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
466 OSL_ENSURE( !bDisposed, "object is disposed" );
467 if( ! bInDispose && ! bDisposed )
468 aLC.addInterface( key , r );
469 }
470
474 inline void removeListener(
475 const keyType &key,
476 const css::uno::Reference < css::uno::XInterface > & r )
477 {
478 ::osl::MutexGuard guard( rMutex );
479 OSL_ENSURE( !bDisposed, "object is disposed" );
480 if( ! bInDispose && ! bDisposed )
481 aLC.removeInterface( key , r );
482 }
483
490 inline OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const
491 { return aLC.getContainer( key ); }
492 };
493
494 /*------------------------------------------
495 *
496 * In general, the above templates are used with a Type as key.
497 * Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
498 *
499 *------------------------------------------*/
500
501 // helper function call class
502 struct hashType_Impl
503 {
504 size_t operator()(const css::uno::Type & s) const
505 { return (size_t) s.getTypeName().hashCode(); }
506 };
507
508
512 class CPPUHELPER_DLLPUBLIC OMultiTypeInterfaceContainerHelper
513 {
514 public:
515 // these are here to force memory de/allocation to sal lib.
516 inline static void * SAL_CALL operator new( size_t nSize )
517 { return ::rtl_allocateMemory( nSize ); }
518 inline static void SAL_CALL operator delete( void * pMem )
519 { ::rtl_freeMemory( pMem ); }
520 inline static void * SAL_CALL operator new( size_t, void * pMem )
521 { return pMem; }
522 inline static void SAL_CALL operator delete( void *, void * )
523 {}
524
532 OMultiTypeInterfaceContainerHelper( ::osl::Mutex & rMutex );
536 ~OMultiTypeInterfaceContainerHelper();
537
541 css::uno::Sequence< css::uno::Type > SAL_CALL getContainedTypes() const;
542
548 OInterfaceContainerHelper * SAL_CALL getContainer( const css::uno::Type & rKey ) const;
549
568 sal_Int32 SAL_CALL addInterface(
569 const css::uno::Type & rKey,
570 const css::uno::Reference< css::uno::XInterface > & r );
571
582 sal_Int32 SAL_CALL removeInterface(
583 const css::uno::Type & rKey,
584 const css::uno::Reference< css::uno::XInterface > & rxIFace );
585
590 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
594 void SAL_CALL clear();
595
596 typedef css::uno::Type keyType;
597 private:
598 void * m_pMap;
599 ::osl::Mutex & rMutex;
600
601 OMultiTypeInterfaceContainerHelper( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
602 OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
603 };
604
605 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
606
607 }
608
609 #endif
610
611 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
cppu::OInterfaceContainerHelper::forEach
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:280
com::sun::star::uno::UNO_QUERY
This enum value can be used for implicit interface query.
Definition: Reference.h:140
cppu::OInterfaceIteratorHelper::remove
void remove()
Removes the current element (the last one returned by next()) from the underlying container...
cppu::OMultiTypeInterfaceContainerHelper
Specialized class for key type css::uno::Type, without explicit usage of STL symbols.
Definition: interfacecontainer.h:512
cppu::OBroadcastHelperVar::bInDispose
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:444
cppu
Definition: Enterable.hxx:26
config.h
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:408
alloc.h
cppu::detail::element_alias::pAsSequence
css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > * pAsSequence
Definition: interfacecontainer.h:51
cppu::OMultiTypeInterfaceContainerHelper::keyType
css::uno::Type keyType
Definition: interfacecontainer.h:596
cppu::OMultiTypeInterfaceContainerHelperVar::keyType
key keyType
Definition: interfacecontainer.h:397
cppu::hashType_Impl
Definition: interfacecontainer.h:502
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
cppu::OBroadcastHelperVar::aLC
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:440
cppu::OBroadcastHelperVar::rMutex
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:438
cppu::OMultiTypeInterfaceContainerHelperVar
A helper class to store interface references of different types.
Definition: interfacecontainer.h:311
cppu::detail::element_alias::element_alias
element_alias()
Definition: interfacecontainer.h:53
sal_Bool
unsigned char sal_Bool
Definition: types.h:48
diagnose.h
cppu::OInterfaceIteratorHelper
This is the iterator of a InterfaceContainerHelper.
Definition: interfacecontainer.h:67
cppuhelperdllapi.h
sal_False
#define sal_False
Definition: types.h:49
cppu::OBroadcastHelperVar::removeListener
void removeListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:474
cppu::detail::element_alias
This is here to optimise space in the common case that there are zero or one listeners.
Definition: interfacecontainer.h:49
cppu::OBroadcastHelperVar::OBroadcastHelperVar
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structure.
Definition: interfacecontainer.h:450
CPPUHELPER_DLLPUBLIC
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
cppu::OInterfaceIteratorHelper::hasMoreElements
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
Definition: interfacecontainer.h:91
cppu::OInterfaceContainerHelper
A container of interfaces.
Definition: interfacecontainer.h:127
cppu::OBroadcastHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:490
OSL_ENSURE
#define OSL_ENSURE(c, m)
Definition: diagnose.h:101
cppu::OInterfaceIteratorHelper::next
css::uno::XInterface * next()
Return the next element of the iterator.
cppu::hashType_Impl::operator()
size_t operator()(const css::uno::Type &s) const
Definition: interfacecontainer.h:504
cppu::OBroadcastHelperVar
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:435
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:298
rtl_allocateMemory
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
osl::Guard
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:108
cppu::detail::element_alias::pAsInterface
css::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:52
mutex.hxx
Sequence.hxx
cppu::OBroadcastHelperVar::bDisposed
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:442
cppu::OBroadcastHelperVar::addListener
void addListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:460