LibreOffice: cppuhelper/interfacecontainer.h Source File

LibreOffice LibreOffice 7.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
20/*
21 * This file is part of LibreOffice published API.
22 */
23#ifndef INCLUDED_CPPUHELPER_INTERFACECONTAINER_H
24#define INCLUDED_CPPUHELPER_INTERFACECONTAINER_H
25
26#include "sal/config.h"
27
28#include <cstddef>
29#include <functional>
30#include <vector>
31#include <utility>
32
33#include "osl/diagnose.h"
34#include "osl/mutex.hxx"
35#include "rtl/alloc.h"
36#include "com/sun/star/uno/Sequence.hxx"
37#include "com/sun/star/lang/EventObject.hpp"
38
39#include "com/sun/star/lang/DisposedException.hpp"
40#include "cppuhelper/cppuhelperdllapi.h"
41
42namespace com { namespace sun { namespace star { namespace uno { class XInterface; } } } }
43 //for docpp
45namespace cppu
46{
47
48namespace detail {
49
54 union element_alias
55 {
56 std::vector< css::uno::Reference< css::uno::XInterface > > *pAsVector;
57 css::uno::XInterface * pAsInterface;
58 element_alias() : pAsInterface(NULL) {}
59 };
60
61}
62
63
64class OInterfaceContainerHelper;
72 class CPPUHELPER_DLLPUBLIC OInterfaceIteratorHelper
73{
74public:
88 OInterfaceIteratorHelper( OInterfaceContainerHelper & rCont );
89
93 ~OInterfaceIteratorHelper();
94
96 bool SAL_CALL hasMoreElements() const
97 { return nRemain != 0; }
102 css::uno::XInterface * SAL_CALL next();
103
109 void SAL_CALL remove();
110
111private:
112 OInterfaceContainerHelper & rCont;
113 sal_Bool bIsList;
114
115 detail::element_alias aData;
116
117 sal_Int32 nRemain;
118
119 OInterfaceIteratorHelper( const OInterfaceIteratorHelper & )
120 SAL_DELETED_FUNCTION;
121 OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & )
122 SAL_DELETED_FUNCTION;
123};
124
125
132 class SAL_WARN_UNUSED CPPUHELPER_DLLPUBLIC OInterfaceContainerHelper
133{
134public:
135 // these are here to force memory de/allocation to sal lib.
136 static void * SAL_CALL operator new( size_t nSize )
137 { return ::rtl_allocateMemory( nSize ); }
138 static void SAL_CALL operator delete( void * pMem )
139 { ::rtl_freeMemory( pMem ); }
140 static void * SAL_CALL operator new( size_t, void * pMem )
141 { return pMem; }
142 static void SAL_CALL operator delete( void *, void * )
143 {}
144
152 OInterfaceContainerHelper( ::osl::Mutex & rMutex );
157 ~OInterfaceContainerHelper();
162 sal_Int32 SAL_CALL getLength() const;
163
167 css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getElements() const;
168
185 sal_Int32 SAL_CALL addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
193 sal_Int32 SAL_CALL removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
198 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
202 void SAL_CALL clear();
203
215 template <typename ListenerT, typename FuncT>
216 inline void forEach( FuncT const& func );
217
239 template< typename ListenerT, typename EventT >
240 inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
241
242private:
243 friend class OInterfaceIteratorHelper;
248 detail::element_alias aData;
249 ::osl::Mutex & rMutex;
251 sal_Bool bInUse;
253 sal_Bool bIsList;
254
255 OInterfaceContainerHelper( const OInterfaceContainerHelper & )
256 SAL_DELETED_FUNCTION;
257 OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & )
258 SAL_DELETED_FUNCTION;
259
260 /*
261 Duplicate content of the container and release the old one without destroying.
262 The mutex must be locked and the memberbInUse must be true.
263 */
264 void copyAndResetInUse();
265
266private:
267 template< typename ListenerT, typename EventT >
268 class NotifySingleListener
269 {
270 private:
271 typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
272 NotificationMethod m_pMethod;
273 const EventT& m_rEvent;
274 public:
275 NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
276
277 void operator()( const css::uno::Reference<ListenerT>& listener ) const
278 {
279 (listener.get()->*m_pMethod)( m_rEvent );
280 }
281 };
282};
283
284template <typename ListenerT, typename FuncT>
285 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
286{
287 OInterfaceIteratorHelper iter( *this );
288 while (iter.hasMoreElements()) {
289 css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
290 if (xListener.is()) {
291 try {
292 func( xListener );
293 }
294 catch (css::lang::DisposedException const& exc) {
295 if (exc.Context == xListener)
296 iter.remove();
297 }
298 }
299 }
300}
301
302template< typename ListenerT, typename EventT >
303 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
304{
305 forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
306}
307
308
315template< class key, class hashImpl = void, class equalImpl = std::equal_to<key> >
316 class OMultiTypeInterfaceContainerHelperVar
317{
318public:
319 // these are here to force memory de/allocation to sal lib.
320 static void * SAL_CALL operator new( size_t nSize )
321 { return ::rtl_allocateMemory( nSize ); }
322 static void SAL_CALL operator delete( void * pMem )
323 { ::rtl_freeMemory( pMem ); }
324 static void * SAL_CALL operator new( size_t, void * pMem )
325 { return pMem; }
326 static void SAL_CALL operator delete( void *, void * )
327 {}
328
336 inline OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex & rMutex );
340 inline ~OMultiTypeInterfaceContainerHelperVar();
341
345 inline css::uno::Sequence< key > SAL_CALL getContainedTypes() const;
346
353 inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const;
354
373 inline sal_Int32 SAL_CALL addInterface(
374 const key & rKey,
375 const css::uno::Reference< css::uno::XInterface > & r );
376
387 inline sal_Int32 SAL_CALL removeInterface(
388 const key & rKey,
389 const css::uno::Reference< css::uno::XInterface > & rxIFace );
390
396 inline void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
400 inline void SAL_CALL clear();
401
402 typedef key keyType;
403private:
404 typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
405 InterfaceMap *m_pMap;
406 ::osl::Mutex & rMutex;
407
408 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
423 OMultiTypeInterfaceContainerHelperVar( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
424 OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
425};
426
427
428
429
439template < class container , class keyType >
440 struct SAL_WARN_UNUSED OBroadcastHelperVar
441{
443 ::osl::Mutex & rMutex;
445 container aLC;
447 sal_Bool bDisposed;
449 sal_Bool bInDispose;
450
455 OBroadcastHelperVar( ::osl::Mutex & rMutex_ )
456 : rMutex( rMutex_ )
457 , aLC( rMutex_ )
458 , bDisposed( false )
459 , bInDispose( false )
460 {}
461
465 void addListener(
466 const keyType &key,
467 const css::uno::Reference < css::uno::XInterface > &r )
468 {
469 ::osl::MutexGuard guard( rMutex );
470 OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
471 OSL_ENSURE( !bDisposed, "object is disposed" );
472 if( ! bInDispose && ! bDisposed )
473 aLC.addInterface( key , r );
474 }
475
479 void removeListener(
480 const keyType &key,
481 const css::uno::Reference < css::uno::XInterface > & r )
482 {
483 ::osl::MutexGuard guard( rMutex );
484 if( ! bInDispose && ! bDisposed )
485 aLC.removeInterface( key , r );
486 }
487
494 OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const
495 { return aLC.getContainer( key ); }
496};
497
498/*------------------------------------------
499*
500* In general, the above templates are used with a Type as key.
501* Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
502*
503*------------------------------------------*/
504
505// helper function call class
506 struct hashType_Impl
507{
508 size_t operator()(const css::uno::Type & s) const
509 { return static_cast<size_t>(s.getTypeName().hashCode()); }
510};
511
512
516 class CPPUHELPER_DLLPUBLIC OMultiTypeInterfaceContainerHelper
517{
518public:
519 // these are here to force memory de/allocation to sal lib.
520 static void * SAL_CALL operator new( size_t nSize )
521 { return ::rtl_allocateMemory( nSize ); }
522 static void SAL_CALL operator delete( void * pMem )
523 { ::rtl_freeMemory( pMem ); }
524 static void * SAL_CALL operator new( size_t, void * pMem )
525 { return pMem; }
526 static void SAL_CALL operator delete( void *, void * )
527 {}
528
536 OMultiTypeInterfaceContainerHelper( ::osl::Mutex & rMutex );
540 ~OMultiTypeInterfaceContainerHelper();
541
545 css::uno::Sequence< css::uno::Type > SAL_CALL getContainedTypes() const;
546
552 OInterfaceContainerHelper * SAL_CALL getContainer( const css::uno::Type & rKey ) const;
553
572 sal_Int32 SAL_CALL addInterface(
573 const css::uno::Type & rKey,
574 const css::uno::Reference< css::uno::XInterface > & r );
575
586 sal_Int32 SAL_CALL removeInterface(
587 const css::uno::Type & rKey,
588 const css::uno::Reference< css::uno::XInterface > & rxIFace );
589
594 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
598 void SAL_CALL clear();
599
600 typedef css::uno::Type keyType;
601private:
602 void * m_pMap;
603 ::osl::Mutex & rMutex;
604
605 OMultiTypeInterfaceContainerHelper( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
606 OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
607};
608
609 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
610
611}
612
613#endif
614
615/* 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:378
sal_Bool
unsigned char sal_Bool
Definition: types.h:38
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
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.
diagnose.h
Provides simple diagnostic support.
mutex.hxx
cppuhelperdllapi.h
CPPUHELPER_DLLPUBLIC
#define CPPUHELPER_DLLPUBLIC
Definition: cppuhelperdllapi.h:32
Sequence.hxx
OSL_ENSURE
#define OSL_ENSURE(c, m)
If cond is false, reports an error with message msg.
Definition: diagnose.h:87
com
Definition: types.h:359
com::sun::star::uno::UNO_QUERY
@ UNO_QUERY
This enum value can be used for implicit interface query.
Definition: Reference.h:157
cppu
Definition: Enterable.hxx:31
cppu::OBroadcastHelper
OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper
Definition: interfacecontainer.h:609
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:35
osl::Guard
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:122
cppu::detail::element_alias
This is here to optimise space in the common case that there are zero or one listeners.
Definition: interfacecontainer.h:55
cppu::detail::element_alias::pAsVector
std::vector< css::uno::Reference< css::uno::XInterface > > * pAsVector
Definition: interfacecontainer.h:56
cppu::detail::element_alias::pAsInterface
css::uno::XInterface * pAsInterface
Definition: interfacecontainer.h:57
cppu::detail::element_alias::element_alias
element_alias()
Definition: interfacecontainer.h:58
cppu::OInterfaceIteratorHelper
This is the iterator of an InterfaceContainerHelper.
Definition: interfacecontainer.h:73
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:96
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:133
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:303
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:285
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:317
cppu::OMultiTypeInterfaceContainerHelperVar::~OMultiTypeInterfaceContainerHelperVar
~OMultiTypeInterfaceContainerHelperVar()
Deletes all containers.
Definition: interfacecontainer.hxx:45
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:106
cppu::OMultiTypeInterfaceContainerHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const key &) const
Return the container created under this key.
Definition: interfacecontainer.hxx:93
cppu::OMultiTypeInterfaceContainerHelperVar::clear
void clear()
Remove all elements of all containers.
Definition: interfacecontainer.hxx:179
cppu::OMultiTypeInterfaceContainerHelperVar::disposeAndClear
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
Definition: interfacecontainer.hxx:142
cppu::OMultiTypeInterfaceContainerHelperVar::keyType
key keyType
Definition: interfacecontainer.h:402
cppu::OMultiTypeInterfaceContainerHelperVar::OMultiTypeInterfaceContainerHelperVar
OMultiTypeInterfaceContainerHelperVar(::osl::Mutex &rMutex)
Create a container of interface containers.
Definition: interfacecontainer.hxx:37
cppu::OMultiTypeInterfaceContainerHelperVar::getContainedTypes
css::uno::Sequence< key > getContainedTypes() const
Return all id's under which at least one interface is added.
Definition: interfacecontainer.hxx:61
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:124
cppu::OBroadcastHelperVar
This struct contains the standard variables of a broadcaster.
Definition: interfacecontainer.h:441
cppu::OBroadcastHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition: interfacecontainer.h:494
cppu::OBroadcastHelperVar::addListener
void addListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
adds a listener threadsafe.
Definition: interfacecontainer.h:465
cppu::OBroadcastHelperVar::aLC
container aLC
ListenerContainer class is thread safe.
Definition: interfacecontainer.h:445
cppu::OBroadcastHelperVar::rMutex
::osl::Mutex & rMutex
The shared mutex.
Definition: interfacecontainer.h:443
cppu::OBroadcastHelperVar::bInDispose
sal_Bool bInDispose
In dispose call.
Definition: interfacecontainer.h:449
cppu::OBroadcastHelperVar::removeListener
void removeListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
removes a listener threadsafe
Definition: interfacecontainer.h:479
cppu::OBroadcastHelperVar::bDisposed
sal_Bool bDisposed
Dispose call ready.
Definition: interfacecontainer.h:447
cppu::OBroadcastHelperVar::OBroadcastHelperVar
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structure.
Definition: interfacecontainer.h:455
cppu::hashType_Impl
Definition: interfacecontainer.h:507
cppu::hashType_Impl::operator()
size_t operator()(const css::uno::Type &s) const
Definition: interfacecontainer.h:508
cppu::OMultiTypeInterfaceContainerHelper
Specialized class for key type css::uno::Type, without explicit usage of STL symbols.
Definition: interfacecontainer.h:517
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:600