LibreOffice: cppuhelper/interfacecontainer.h Source File

LibreOffice LibreOffice 7.1 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 <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/lang/EventObject.hpp"
34
35#include "com/sun/star/lang/DisposedException.hpp"
36#include "cppuhelper/cppuhelperdllapi.h"
37
38namespace com { namespace sun { namespace star { namespace uno { class XInterface; } } } }
39 //for docpp
41namespace cppu
42{
43
44namespace detail {
45
50 union element_alias
51 {
52 std::vector< css::uno::Reference< css::uno::XInterface > > *pAsVector;
53 css::uno::XInterface * pAsInterface;
54 element_alias() : pAsInterface(NULL) {}
55 };
56
57}
58
59
60class OInterfaceContainerHelper;
68 class CPPUHELPER_DLLPUBLIC OInterfaceIteratorHelper
69{
70public:
84 OInterfaceIteratorHelper( OInterfaceContainerHelper & rCont );
85
89 ~OInterfaceIteratorHelper();
90
92 bool SAL_CALL hasMoreElements() const
93 { return nRemain != 0; }
98 css::uno::XInterface * SAL_CALL next();
99
105 void SAL_CALL remove();
106
107private:
108 OInterfaceContainerHelper & rCont;
109 sal_Bool bIsList;
110
111 detail::element_alias aData;
112
113 sal_Int32 nRemain;
114
115 OInterfaceIteratorHelper( const OInterfaceIteratorHelper & )
116 SAL_DELETED_FUNCTION;
117 OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & )
118 SAL_DELETED_FUNCTION;
119};
120
121
128 class SAL_WARN_UNUSED CPPUHELPER_DLLPUBLIC OInterfaceContainerHelper
129{
130public:
131 // these are here to force memory de/allocation to sal lib.
132 static void * SAL_CALL operator new( size_t nSize )
133 { return ::rtl_allocateMemory( nSize ); }
134 static void SAL_CALL operator delete( void * pMem )
135 { ::rtl_freeMemory( pMem ); }
136 static void * SAL_CALL operator new( size_t, void * pMem )
137 { return pMem; }
138 static void SAL_CALL operator delete( void *, void * )
139 {}
140
148 OInterfaceContainerHelper( ::osl::Mutex & rMutex );
153 ~OInterfaceContainerHelper();
158 sal_Int32 SAL_CALL getLength() const;
159
163 css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getElements() const;
164
181 sal_Int32 SAL_CALL addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
189 sal_Int32 SAL_CALL removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
194 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
198 void SAL_CALL clear();
199
211 template <typename ListenerT, typename FuncT>
212 inline void forEach( FuncT const& func );
213
235 template< typename ListenerT, typename EventT >
236 inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
237
238private:
239 friend class OInterfaceIteratorHelper;
244 detail::element_alias aData;
245 ::osl::Mutex & rMutex;
247 sal_Bool bInUse;
249 sal_Bool bIsList;
250
251 OInterfaceContainerHelper( const OInterfaceContainerHelper & )
252 SAL_DELETED_FUNCTION;
253 OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & )
254 SAL_DELETED_FUNCTION;
255
256 /*
257 Duplicate content of the container and release the old one without destroying.
258 The mutex must be locked and the memberbInUse must be true.
259 */
260 void copyAndResetInUse();
261
262private:
263 template< typename ListenerT, typename EventT >
264 class NotifySingleListener
265 {
266 private:
267 typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
268 NotificationMethod m_pMethod;
269 const EventT& m_rEvent;
270 public:
271 NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
272
273 void operator()( const css::uno::Reference<ListenerT>& listener ) const
274 {
275 (listener.get()->*m_pMethod)( m_rEvent );
276 }
277 };
278};
279
280template <typename ListenerT, typename FuncT>
281 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
282{
283 OInterfaceIteratorHelper iter( *this );
284 while (iter.hasMoreElements()) {
285 css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
286 if (xListener.is()) {
287 try {
288 func( xListener );
289 }
290 catch (css::lang::DisposedException const& exc) {
291 if (exc.Context == xListener)
292 iter.remove();
293 }
294 }
295 }
296}
297
298template< typename ListenerT, typename EventT >
299 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
300{
301 forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
302}
303
304
311template< class key, class hashImpl = void, class equalImpl = std::equal_to<key> >
312 class OMultiTypeInterfaceContainerHelperVar
313{
314public:
315 // these are here to force memory de/allocation to sal lib.
316 static void * SAL_CALL operator new( size_t nSize )
317 { return ::rtl_allocateMemory( nSize ); }
318 static void SAL_CALL operator delete( void * pMem )
319 { ::rtl_freeMemory( pMem ); }
320 static void * SAL_CALL operator new( size_t, void * pMem )
321 { return pMem; }
322 static void SAL_CALL operator delete( void *, void * )
323 {}
324
332 inline OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex & rMutex );
336 inline ~OMultiTypeInterfaceContainerHelperVar();
337
341 inline css::uno::Sequence< key > SAL_CALL getContainedTypes() const;
342
349 inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const;
350
369 inline sal_Int32 SAL_CALL addInterface(
370 const key & rKey,
371 const css::uno::Reference< css::uno::XInterface > & r );
372
383 inline sal_Int32 SAL_CALL removeInterface(
384 const key & rKey,
385 const css::uno::Reference< css::uno::XInterface > & rxIFace );
386
392 inline void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
396 inline void SAL_CALL clear();
397
398 typedef key keyType;
399private:
400 typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
401 InterfaceMap *m_pMap;
402 ::osl::Mutex & rMutex;
403
404 typename InterfaceMap::iterator find(const key &rKey) const
405 {
406 typename InterfaceMap::iterator iter = m_pMap->begin();
407 typename InterfaceMap::iterator end = m_pMap->end();
408
409 while( iter != end )
410 {
411 equalImpl equal;
412 if( equal( iter->first, rKey ) )
413 break;
414 ++iter;
415 }
416 return iter;
417 }
418
419 OMultiTypeInterfaceContainerHelperVar( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
420 OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
421};
422
423
424
425
435template < class container , class keyType >
436 struct SAL_WARN_UNUSED OBroadcastHelperVar
437{
439 ::osl::Mutex & rMutex;
441 container aLC;
443 sal_Bool bDisposed;
445 sal_Bool bInDispose;
446
451 OBroadcastHelperVar( ::osl::Mutex & rMutex_ )
452 : rMutex( rMutex_ )
453 , aLC( rMutex_ )
454 , bDisposed( false )
455 , bInDispose( false )
456 {}
457
461 void addListener(
462 const keyType &key,
463 const css::uno::Reference < css::uno::XInterface > &r )
464 {
465 ::osl::MutexGuard guard( rMutex );
466 OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
467 OSL_ENSURE( !bDisposed, "object is disposed" );
468 if( ! bInDispose && ! bDisposed )
469 aLC.addInterface( key , r );
470 }
471
475 void removeListener(
476 const keyType &key,
477 const css::uno::Reference < css::uno::XInterface > & r )
478 {
479 ::osl::MutexGuard guard( rMutex );
480 if( ! bInDispose && ! bDisposed )
481 aLC.removeInterface( key , r );
482 }
483
490 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 static_cast<size_t>(s.getTypeName().hashCode()); }
506};
507
508
512 class CPPUHELPER_DLLPUBLIC OMultiTypeInterfaceContainerHelper
513{
514public:
515 // these are here to force memory de/allocation to sal lib.
516 static void * SAL_CALL operator new( size_t nSize )
517 { return ::rtl_allocateMemory( nSize ); }
518 static void SAL_CALL operator delete( void * pMem )
519 { ::rtl_freeMemory( pMem ); }
520 static void * SAL_CALL operator new( size_t, void * pMem )
521 { return pMem; }
522 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;
597private:
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: */
config.h
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:374
sal_Bool
unsigned char sal_Bool
Definition: types.h:34
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:558
alloc.h
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
rtl_allocateMemory
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
mutex.hxx
diagnose.h
Provides simple diagnostic support.
cppuhelperdllapi.h
CPPUHELPER_DLLPUBLIC
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:28
Sequence.hxx
OSL_ENSURE
#define OSL_ENSURE(c, m)
If cond is false, reports an error with message msg.
Definition: diagnose.h:84
cppu
Definition: Enterable.hxx:27
cppu::OBroadcastHelper
OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper
Definition: interfacecontainer.h:605
com
Definition: unotype.hxx:35
com::sun::star::uno::UNO_QUERY
@ UNO_QUERY
This enum value can be used for implicit interface query.
Definition: Reference.h:153
cppu::detail::element_alias
This is here to optimise space in the common case that there are zero or one listeners.
Definition: interfacecontainer.h:51
cppu::detail::element_alias::pAsVector
std::vector< css::uno::Reference< css::uno::XInterface > > * pAsVector
Definition: interfacecontainer.h:52
cppu::detail::element_alias::pAsInterface
css::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:53
cppu::detail::element_alias::element_alias
element_alias()
Definition: interfacecontainer.h:54
cppu::OInterfaceIteratorHelper
This is the iterator of an InterfaceContainerHelper.
Definition: interfacecontainer.h:69
cppu::OInterfaceIteratorHelper::~OInterfaceIteratorHelper
~OInterfaceIteratorHelper()
Releases the connection to the container.
cppu::OInterfaceIteratorHelper::remove
void remove()
Removes the current element (the last one returned by next()) from the underlying container.
cppu::OInterfaceIteratorHelper::hasMoreElements
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
Definition: interfacecontainer.h:92
cppu::OInterfaceIteratorHelper::OInterfaceIteratorHelper
OInterfaceIteratorHelper(OInterfaceContainerHelper &rCont)
Create an iterator over the elements of the container.
cppu::OInterfaceIteratorHelper::next
css::uno::XInterface * next()
Return the next element of the iterator.
cppu::OInterfaceContainerHelper
A container of interfaces.
Definition: interfacecontainer.h:129
cppu::OInterfaceContainerHelper::getLength
sal_Int32 getLength() const
Return the number of Elements in the container.
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:299
cppu::OInterfaceContainerHelper::disposeAndClear
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
cppu::OInterfaceContainerHelper::removeInterface
sal_Int32 removeInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
Removes an element from the container.
cppu::OInterfaceContainerHelper::forEach
void forEach(FuncT const &func)
Executes a functor for each contained listener of specified type, e.g.
Definition: interfacecontainer.h:281
cppu::OInterfaceContainerHelper::~OInterfaceContainerHelper
~OInterfaceContainerHelper()
Release all interfaces.
cppu::OInterfaceContainerHelper::addInterface
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
Inserts an element into the container.
cppu::OInterfaceContainerHelper::clear
void clear()
Clears the container without calling disposing().
cppu::OInterfaceContainerHelper::OInterfaceContainerHelper
OInterfaceContainerHelper(::osl::Mutex &rMutex)
Create an interface container.
cppu::OInterfaceContainerHelper::getElements
css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > getElements() const
Return all interfaces added to this container.
cppu::OMultiTypeInterfaceContainerHelperVar
A helper class to store interface references of different types.
Definition: interfacecontainer.h:313
cppu::OMultiTypeInterfaceContainerHelperVar::~OMultiTypeInterfaceContainerHelperVar
~OMultiTypeInterfaceContainerHelperVar()
Deletes all containers.
Definition: interfacecontainer.hxx:41
cppu::OMultiTypeInterfaceContainerHelperVar::addInterface
sal_Int32 addInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &r)
Inserts an element into the container with the specified key.
Definition: interfacecontainer.hxx:102
cppu::OMultiTypeInterfaceContainerHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const key &) const
Return the container created under this key.
Definition: interfacecontainer.hxx:89
cppu::OMultiTypeInterfaceContainerHelperVar::clear
void clear()
Remove all elements of all containers.
Definition: interfacecontainer.hxx:175
cppu::OMultiTypeInterfaceContainerHelperVar::disposeAndClear
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
Definition: interfacecontainer.hxx:138
cppu::OMultiTypeInterfaceContainerHelperVar::keyType
key keyType
Definition: interfacecontainer.h:398
cppu::OMultiTypeInterfaceContainerHelperVar::OMultiTypeInterfaceContainerHelperVar
OMultiTypeInterfaceContainerHelperVar(::osl::Mutex &rMutex)
Create a container of interface containers.
Definition: interfacecontainer.hxx:33
cppu::OMultiTypeInterfaceContainerHelperVar::getContainedTypes
css::uno::Sequence< key > getContainedTypes() const
Return all id's under which at least one interface is added.
Definition: interfacecontainer.hxx:57
cppu::OMultiTypeInterfaceContainerHelperVar::removeInterface
sal_Int32 removeInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
Removes an element from the container with the specified key.
Definition: interfacecontainer.hxx:120
cppu::OBroadcastHelperVar
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:437
cppu::OBroadcastHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:490
cppu::OBroadcastHelperVar::addListener
void addListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:461
cppu::OBroadcastHelperVar::aLC
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:441
cppu::OBroadcastHelperVar::rMutex
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:439
cppu::OBroadcastHelperVar::bInDispose
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:445
cppu::OBroadcastHelperVar::removeListener
void removeListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:475
cppu::OBroadcastHelperVar::bDisposed
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:443
cppu::OBroadcastHelperVar::OBroadcastHelperVar
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structure.
Definition: interfacecontainer.h:451
cppu::hashType_Impl
Definition: interfacecontainer.h:503
cppu::hashType_Impl::operator()
size_t operator()(const css::uno::Type &s) const
Definition: interfacecontainer.h:504
cppu::OMultiTypeInterfaceContainerHelper
Specialized class for key type css::uno::Type, without explicit usage of STL symbols.
Definition: interfacecontainer.h:513
cppu::OMultiTypeInterfaceContainerHelper::disposeAndClear
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
cppu::OMultiTypeInterfaceContainerHelper::OMultiTypeInterfaceContainerHelper
OMultiTypeInterfaceContainerHelper(::osl::Mutex &rMutex)
Create a container of interface containers.
cppu::OMultiTypeInterfaceContainerHelper::clear
void clear()
Remove all elements of all containers.
cppu::OMultiTypeInterfaceContainerHelper::~OMultiTypeInterfaceContainerHelper
~OMultiTypeInterfaceContainerHelper()
Delete all containers.
cppu::OMultiTypeInterfaceContainerHelper::getContainedTypes
css::uno::Sequence< css::uno::Type > getContainedTypes() const
Return all id's under which at least one interface is added.
cppu::OMultiTypeInterfaceContainerHelper::removeInterface
sal_Int32 removeInterface(const css::uno::Type &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
Removes an element from the container with the specified key.
cppu::OMultiTypeInterfaceContainerHelper::getContainer
OInterfaceContainerHelper * getContainer(const css::uno::Type &rKey) const
Return the container created under this key.
cppu::OMultiTypeInterfaceContainerHelper::addInterface
sal_Int32 addInterface(const css::uno::Type &rKey, const css::uno::Reference< css::uno::XInterface > &r)
Inserts an element into the container with the specified key.
cppu::OMultiTypeInterfaceContainerHelper::keyType
css::uno::Type keyType
Definition: interfacecontainer.h:596
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:31
osl::Guard
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:115