LibreOffice: cppuhelper/interfacecontainer.h Source File

LibreOffice LibreOffice 26.2 SDK C/C++ API Reference

Loading...
Searching...
No Matches
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
44namespace cppu
45{
46
47namespace detail {
48
53 union element_alias
54 {
55 std::vector< css::uno::Reference< css::uno::XInterface > > *pAsVector;
56 css::uno::XInterface * pAsInterface;
57 element_alias() : pAsInterface(NULL) {}
58 };
59
60}
61
62
63class OInterfaceContainerHelper;
71 class CPPUHELPER_DLLPUBLIC OInterfaceIteratorHelper
72{
73public:
87 OInterfaceIteratorHelper( OInterfaceContainerHelper & rCont );
88
92 ~OInterfaceIteratorHelper();
93
95 bool SAL_CALL hasMoreElements() const
96 { return nRemain != 0; }
97
101 css::uno::XInterface * SAL_CALL next();
102
108 void SAL_CALL remove();
109
110private:
111 OInterfaceContainerHelper & rCont;
112 sal_Bool bIsList;
113
114 detail::element_alias aData;
115
116 sal_Int32 nRemain;
117
118 OInterfaceIteratorHelper( const OInterfaceIteratorHelper & )
119 SAL_DELETED_FUNCTION;
120 OInterfaceIteratorHelper & operator = ( const OInterfaceIteratorHelper & )
121 SAL_DELETED_FUNCTION;
122};
123
124
131 class SAL_WARN_UNUSED CPPUHELPER_DLLPUBLIC OInterfaceContainerHelper
132{
133public:
134 // these are here to force memory de/allocation to sal lib.
135 static void * SAL_CALL operator new( size_t nSize )
136 { return ::rtl_allocateMemory( nSize ); }
137 static void SAL_CALL operator delete( void * pMem )
138 { ::rtl_freeMemory( pMem ); }
139 static void * SAL_CALL operator new( size_t, void * pMem )
140 { return pMem; }
141 static void SAL_CALL operator delete( void *, void * )
142 {}
143
151 OInterfaceContainerHelper( ::osl::Mutex & rMutex );
156 ~OInterfaceContainerHelper();
161 sal_Int32 SAL_CALL getLength() const;
162
166 css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getElements() const;
167
184 sal_Int32 SAL_CALL addInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
192 sal_Int32 SAL_CALL removeInterface( const css::uno::Reference< css::uno::XInterface > & rxIFace );
197 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
201 void SAL_CALL clear();
202
214 template <typename ListenerT, typename FuncT>
215 inline void forEach( FuncT const& func );
216
238 template< typename ListenerT, typename EventT >
239 inline void notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event );
240
241private:
242 friend class OInterfaceIteratorHelper;
247 detail::element_alias aData;
248 ::osl::Mutex & rMutex;
250 sal_Bool bInUse;
252 sal_Bool bIsList;
253
254 OInterfaceContainerHelper( const OInterfaceContainerHelper & )
255 SAL_DELETED_FUNCTION;
256 OInterfaceContainerHelper & operator = ( const OInterfaceContainerHelper & )
257 SAL_DELETED_FUNCTION;
258
259 /*
260 Duplicate content of the container and release the old one without destroying.
261 The mutex must be locked and the memberbInUse must be true.
262 */
263 void copyAndResetInUse();
264
265private:
266 template< typename ListenerT, typename EventT >
267 class NotifySingleListener
268 {
269 private:
270 typedef void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& );
271 NotificationMethod m_pMethod;
272 const EventT& m_rEvent;
273 public:
274 NotifySingleListener( NotificationMethod method, const EventT& event ) : m_pMethod( method ), m_rEvent( event ) { }
275
276 void operator()( const css::uno::Reference<ListenerT>& listener ) const
277 {
278 (listener.get()->*m_pMethod)( m_rEvent );
279 }
280 };
281};
282
283template <typename ListenerT, typename FuncT>
284 inline void OInterfaceContainerHelper::forEach( FuncT const& func )
285{
286 OInterfaceIteratorHelper iter( *this );
287 while (iter.hasMoreElements()) {
288 css::uno::Reference<ListenerT> const xListener( iter.next(), css::uno::UNO_QUERY );
289 if (xListener.is()) {
290 try {
291 func( xListener );
292 }
293 catch (css::lang::DisposedException const& exc) {
294 if (exc.Context == xListener)
295 iter.remove();
296 }
297 }
298 }
299}
300
301template< typename ListenerT, typename EventT >
302 inline void OInterfaceContainerHelper::notifyEach( void ( SAL_CALL ListenerT::*NotificationMethod )( const EventT& ), const EventT& Event )
303{
304 forEach< ListenerT, NotifySingleListener< ListenerT, EventT > >( NotifySingleListener< ListenerT, EventT >( NotificationMethod, Event ) );
305}
306
307
314template< class key, class hashImpl = void, class equalImpl = std::equal_to<key> >
315 class OMultiTypeInterfaceContainerHelperVar
316{
317public:
318 // these are here to force memory de/allocation to sal lib.
319 static void * SAL_CALL operator new( size_t nSize )
320 { return ::rtl_allocateMemory( nSize ); }
321 static void SAL_CALL operator delete( void * pMem )
322 { ::rtl_freeMemory( pMem ); }
323 static void * SAL_CALL operator new( size_t, void * pMem )
324 { return pMem; }
325 static void SAL_CALL operator delete( void *, void * )
326 {}
327
335 inline OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex & rMutex );
339 inline ~OMultiTypeInterfaceContainerHelperVar();
340
344 inline css::uno::Sequence< key > SAL_CALL getContainedTypes() const;
345
352 inline OInterfaceContainerHelper * SAL_CALL getContainer( const key & ) const;
353
372 inline sal_Int32 SAL_CALL addInterface(
373 const key & rKey,
374 const css::uno::Reference< css::uno::XInterface > & r );
375
386 inline sal_Int32 SAL_CALL removeInterface(
387 const key & rKey,
388 const css::uno::Reference< css::uno::XInterface > & rxIFace );
389
395 inline void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
399 inline void SAL_CALL clear();
400
401 typedef key keyType;
402private:
403 typedef ::std::vector< std::pair < key , void* > > InterfaceMap;
404 InterfaceMap *m_pMap;
405 ::osl::Mutex & rMutex;
406
407 typename InterfaceMap::iterator find(const key &rKey) const
408 {
409 typename InterfaceMap::iterator iter = m_pMap->begin();
410 typename InterfaceMap::iterator end = m_pMap->end();
411
412 while( iter != end )
413 {
414 equalImpl equal;
415 if( equal( iter->first, rKey ) )
416 break;
417 ++iter;
418 }
419 return iter;
420 }
421
422 OMultiTypeInterfaceContainerHelperVar( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
423 OMultiTypeInterfaceContainerHelperVar & operator = ( const OMultiTypeInterfaceContainerHelperVar & ) SAL_DELETED_FUNCTION;
424};
425
426
427
428
438template < class container , class keyType >
439 struct SAL_WARN_UNUSED OBroadcastHelperVar
440{
442 ::osl::Mutex & rMutex;
444 container aLC;
446 sal_Bool bDisposed;
448 sal_Bool bInDispose;
449
454 OBroadcastHelperVar( ::osl::Mutex & rMutex_ )
455 : rMutex( rMutex_ )
456 , aLC( rMutex_ )
457 , bDisposed( false )
458 , bInDispose( false )
459 {}
460
464 void addListener(
465 const keyType &key,
466 const css::uno::Reference < css::uno::XInterface > &r )
467 {
468 ::osl::MutexGuard guard( rMutex );
469 OSL_ENSURE( !bInDispose, "do not add listeners in the dispose call" );
470 OSL_ENSURE( !bDisposed, "object is disposed" );
471 if( ! bInDispose && ! bDisposed )
472 aLC.addInterface( key , r );
473 }
474
478 void removeListener(
479 const keyType &key,
480 const css::uno::Reference < css::uno::XInterface > & r )
481 {
482 ::osl::MutexGuard guard( rMutex );
483 if( ! bInDispose && ! bDisposed )
484 aLC.removeInterface( key , r );
485 }
486
493 OInterfaceContainerHelper * SAL_CALL getContainer( const keyType &key ) const
494 { return aLC.getContainer( key ); }
495};
496
497/*------------------------------------------
498*
499* In general, the above templates are used with a Type as key.
500* Therefore a default declaration is given ( OMultiTypeInterfaceContainerHelper and OBroadcastHelper )
501*
502*------------------------------------------*/
503
504// helper function call class
505 struct hashType_Impl
506{
507 size_t operator()(const css::uno::Type & s) const
508 { return static_cast<size_t>(s.getTypeName().hashCode()); }
509};
510
511
515 class CPPUHELPER_DLLPUBLIC OMultiTypeInterfaceContainerHelper
516{
517public:
518 // these are here to force memory de/allocation to sal lib.
519 static void * SAL_CALL operator new( size_t nSize )
520 { return ::rtl_allocateMemory( nSize ); }
521 static void SAL_CALL operator delete( void * pMem )
522 { ::rtl_freeMemory( pMem ); }
523 static void * SAL_CALL operator new( size_t, void * pMem )
524 { return pMem; }
525 static void SAL_CALL operator delete( void *, void * )
526 {}
527
535 OMultiTypeInterfaceContainerHelper( ::osl::Mutex & rMutex );
539 ~OMultiTypeInterfaceContainerHelper();
540
544 css::uno::Sequence< css::uno::Type > SAL_CALL getContainedTypes() const;
545
551 OInterfaceContainerHelper * SAL_CALL getContainer( const css::uno::Type & rKey ) const;
552
571 sal_Int32 SAL_CALL addInterface(
572 const css::uno::Type & rKey,
573 const css::uno::Reference< css::uno::XInterface > & r );
574
585 sal_Int32 SAL_CALL removeInterface(
586 const css::uno::Type & rKey,
587 const css::uno::Reference< css::uno::XInterface > & rxIFace );
588
593 void SAL_CALL disposeAndClear( const css::lang::EventObject & rEvt );
597 void SAL_CALL clear();
598
599 typedef css::uno::Type keyType;
600private:
601 void * m_pMap;
602 ::osl::Mutex & rMutex;
603
604 OMultiTypeInterfaceContainerHelper( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
605 OMultiTypeInterfaceContainerHelper & operator = ( const OMultiTypeInterfaceContainerHelper & ) SAL_DELETED_FUNCTION;
606};
607
608 typedef OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper , OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper;
609
610}
611
612#endif
613
614/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition types.h:396
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:611
config.h
alloc.h
rtl_freeMemory
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
mutex.hxx
diagnose.h
Provides simple diagnostic support.
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:377
com::sun
Definition types.h:377
com::sun::star
Definition types.h:377
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition mutex.hxx:273
com::sun::star::uno
Definition typedescription.hxx:43
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:608
cppu::detail
Definition unotype.hxx:103
osl::Mutex
A mutual exclusion synchronization object.
Definition mutex.hxx:35
com::sun::star::uno::Reference< css::uno::XInterface >
com::sun::star::uno::Reference::get
interface_type * get() const
Gets interface pointer.
Definition Reference.h:405
com::sun::star::uno::Sequence
Template C++ class representing an IDL sequence.
Definition Sequence.h:61
cppu::detail::element_alias
This is here to optimise space in the common case that there are zero or one listeners.
Definition interfacecontainer.h:54
cppu::detail::element_alias::pAsVector
std::vector< css::uno::Reference< css::uno::XInterface > > * pAsVector
Definition interfacecontainer.h:55
cppu::detail::element_alias::pAsInterface
css::uno::XInterface * pAsInterface
Definition interfacecontainer.h:56
cppu::detail::element_alias::element_alias
element_alias()
Definition interfacecontainer.h:57
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:95
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:132
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:302
cppu::OInterfaceContainerHelper::disposeAndClear
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
cppu::OInterfaceContainerHelper::OInterfaceIteratorHelper
friend class OInterfaceIteratorHelper
Definition interfacecontainer.h:242
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:284
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:316
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:401
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:440
cppu::OBroadcastHelperVar::getContainer
OInterfaceContainerHelper * getContainer(const keyType &key) const
Return the container created under this key.
Definition interfacecontainer.h:493
cppu::OBroadcastHelperVar::addListener
void addListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
adds a listener threadsafe.
Definition interfacecontainer.h:464
cppu::OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType >::aLC
OMultiTypeInterfaceContainerHelper aLC
Definition interfacecontainer.h:444
cppu::OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType >::rMutex
::osl::Mutex & rMutex
Definition interfacecontainer.h:442
cppu::OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType >::bInDispose
sal_Bool bInDispose
Definition interfacecontainer.h:448
cppu::OBroadcastHelperVar::removeListener
void removeListener(const keyType &key, const css::uno::Reference< css::uno::XInterface > &r)
removes a listener threadsafe
Definition interfacecontainer.h:478
cppu::OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType >::bDisposed
sal_Bool bDisposed
Definition interfacecontainer.h:446
cppu::OBroadcastHelperVar::OBroadcastHelperVar
OBroadcastHelperVar(::osl::Mutex &rMutex_)
Initialize the structure.
Definition interfacecontainer.h:454
cppu::hashType_Impl
Definition interfacecontainer.h:506
cppu::hashType_Impl::operator()
size_t operator()(const css::uno::Type &s) const
Definition interfacecontainer.h:507
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:599
com::sun::star::uno::BaseReference::is
bool is() const
Checks if reference is null.
Definition Reference.h:101
com::sun::star::uno::Type
C++ class representing an IDL meta type.
Definition Type.h:59
com::sun::star::uno::Type::getTypeName
inline ::rtl::OUString getTypeName() const
Gets the name of the set type.
Definition Type.hxx:85