LibreOffice
LibreOffice 7.1 SDK C/C++ API Reference
interfacecontainer.hxx
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_HXX
20#define INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
21
22#include "sal/config.h"
23
24#include <cstddef>
25
27
28
29namespace cppu
30{
31
32template< class key , class hashImpl , class equalImpl >
34 : rMutex( rMutex_ )
35{
36 m_pMap = new InterfaceMap;
37}
38
39
40template< class key , class hashImpl , class equalImpl >
42{
43 typename InterfaceMap::iterator iter = m_pMap->begin();
44 typename InterfaceMap::iterator end = m_pMap->end();
45
46 while( iter != end )
47 {
48 delete static_cast<OInterfaceContainerHelper*>((*iter).second);
49 (*iter).second = 0;
50 ++iter;
51 }
52 delete m_pMap;
53}
54
55
56template< class key , class hashImpl , class equalImpl >
58{
59 ::osl::MutexGuard aGuard( rMutex );
60 typename InterfaceMap::size_type nSize = m_pMap->size();
61 if( nSize != 0 )
62 {
63 css::uno::Sequence< key > aInterfaceTypes( nSize );
64 key * pArray = aInterfaceTypes.getArray();
65
66 typename InterfaceMap::iterator iter = m_pMap->begin();
67 typename InterfaceMap::iterator end = m_pMap->end();
68
69 sal_uInt32 i = 0;
70 while( iter != end )
71 {
72 // are interfaces added to this container?
73 if( static_cast<OInterfaceContainerHelper*>((*iter).second)->getLength() )
74 // yes, put the type in the array
75 pArray[i++] = (*iter).first;
76 ++iter;
77 }
78 if( i != nSize ) {
79 // may be empty container, reduce the sequence to the right size
80 aInterfaceTypes = css::uno::Sequence<key>( pArray, i );
81 }
82 return aInterfaceTypes;
83 }
84 return css::uno::Sequence<key>();
85}
86
87
88template< class key , class hashImpl , class equalImpl >
90 const key & rKey ) const
91{
92 ::osl::MutexGuard aGuard( rMutex );
93
94 typename InterfaceMap::iterator iter = find( rKey );
95 if( iter != m_pMap->end() )
96 return static_cast<OInterfaceContainerHelper*>( (*iter).second );
97 return NULL;
98}
99
100
101template< class key , class hashImpl , class equalImpl >
103 const key & rKey,
104 const css::uno::Reference< css::uno::XInterface > & rListener )
105{
106 ::osl::MutexGuard aGuard( rMutex );
107 typename InterfaceMap::iterator iter = find( rKey );
108 if( iter == m_pMap->end() )
109 {
111 m_pMap->push_back(std::pair<key, void*>(rKey, pLC));
112 return pLC->addInterface( rListener );
113 }
114 else
115 return static_cast<OInterfaceContainerHelper*>((*iter).second)->addInterface( rListener );
116}
117
118
119template< class key , class hashImpl , class equalImpl >
121 const key & rKey,
122 const css::uno::Reference< css::uno::XInterface > & rListener )
123{
124 ::osl::MutexGuard aGuard( rMutex );
125
126 // search container with id nUik
127 typename InterfaceMap::iterator iter = find( rKey );
128 // container found?
129 if( iter != m_pMap->end() )
130 return static_cast<OInterfaceContainerHelper*>((*iter).second)->removeInterface( rListener );
131
132 // no container with this id. Always return 0
133 return 0;
134}
135
136
137template< class key , class hashImpl , class equalImpl >
139 const css::lang::EventObject & rEvt )
140{
141 typename InterfaceMap::size_type nSize = 0;
142 OInterfaceContainerHelper ** ppListenerContainers = NULL;
143 {
144 ::osl::MutexGuard aGuard( rMutex );
145 nSize = m_pMap->size();
146 if( nSize )
147 {
148 typedef OInterfaceContainerHelper* ppp;
149 ppListenerContainers = new ppp[nSize];
150
151 typename InterfaceMap::iterator iter = m_pMap->begin();
152 typename InterfaceMap::iterator end = m_pMap->end();
153
154 typename InterfaceMap::size_type i = 0;
155 while( iter != end )
156 {
157 ppListenerContainers[i++] = static_cast<OInterfaceContainerHelper*>((*iter).second);
158 ++iter;
159 }
160 }
161 }
162
163 // create a copy, because do not fire event in a guarded section
164 for( typename InterfaceMap::size_type i = 0; i < nSize; i++ )
165 {
166 if( ppListenerContainers[i] )
167 ppListenerContainers[i]->disposeAndClear( rEvt );
168 }
169
170 delete [] ppListenerContainers;
171}
172
173
174template< class key , class hashImpl , class equalImpl >
176{
177 ::osl::MutexGuard aGuard( rMutex );
178 typename InterfaceMap::iterator iter = m_pMap->begin();
179 typename InterfaceMap::iterator end = m_pMap->end();
180
181 while( iter != end )
182 {
183 static_cast<OInterfaceContainerHelper*>((*iter).second)->clear();
184 ++iter;
185 }
186}
187
188
189}
190
191#endif
192
193/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: Enterable.hxx:27
A container of interfaces.
Definition: interfacecontainer.h:129
sal_Int32 getLength() const
Return the number of Elements in the container.
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
Inserts an element into the container.
~OMultiTypeInterfaceContainerHelperVar()
Deletes all containers.
Definition: interfacecontainer.hxx:41
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
OInterfaceContainerHelper * getContainer(const key &) const
Return the container created under this key.
Definition: interfacecontainer.hxx:89
void clear()
Remove all elements of all containers.
Definition: interfacecontainer.hxx:175
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
Definition: interfacecontainer.hxx:138
OMultiTypeInterfaceContainerHelperVar(::osl::Mutex &rMutex)
Create a container of interface containers.
Definition: interfacecontainer.hxx:33
css::uno::Sequence< key > getContainedTypes() const
Return all id's under which at least one interface is added.
Definition: interfacecontainer.hxx:57
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
A mutual exclusion synchronization object.
Definition: mutex.hxx:31
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:115