LibreOffice Module comphelper (master) 1
multiinterfacecontainer3.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#pragma once
20
21#include <sal/config.h>
22
23#include <com/sun/star/lang/EventObject.hpp>
25#include <memory>
26#include <vector>
27
28namespace osl
29{
30class Mutex;
31}
32
33namespace comphelper
34{
43template <class listener, class key, class equalImpl = std::equal_to<key>>
45{
46public:
54 inline OMultiTypeInterfaceContainerHelperVar3(::osl::Mutex& rMutex_)
55 : rMutex(rMutex_)
56 {
57 }
58
62 inline std::vector<key> getContainedTypes() const
63 {
64 ::osl::MutexGuard aGuard(rMutex);
65 std::vector<key> aInterfaceTypes;
66 aInterfaceTypes.reserve(m_aMap.size());
67 for (const auto& rPair : m_aMap)
68 // are interfaces added to this container?
69 if (rPair.second->getLength())
70 // yes, put the type in the array
71 aInterfaceTypes.push_back(rPair.first);
72 return aInterfaceTypes;
73 }
74
75 inline bool hasContainedTypes() const
76 {
77 ::osl::MutexGuard aGuard(rMutex);
78 for (const auto& rPair : m_aMap)
79 // are interfaces added to this container?
80 if (rPair.second->getLength())
81 return true;
82 return false;
83 }
84
92 {
93 ::osl::MutexGuard aGuard(rMutex);
94
95 auto iter = find(rKey);
96 if (iter != m_aMap.end())
97 return (*iter).second.get();
98 return nullptr;
99 }
100
119 inline sal_Int32 addInterface(const key& rKey, const css::uno::Reference<listener>& rListener)
120 {
121 ::osl::MutexGuard aGuard(rMutex);
122 auto iter = find(rKey);
123 if (iter == m_aMap.end())
124 {
126 m_aMap.emplace_back(rKey, pLC);
127 return pLC->addInterface(rListener);
128 }
129 else
130 return (*iter).second->addInterface(rListener);
131 }
132
143 inline sal_Int32 removeInterface(const key& rKey,
144 const css::uno::Reference<listener>& rListener)
145 {
146 ::osl::MutexGuard aGuard(rMutex);
147
148 // search container with id nUik
149 auto iter = find(rKey);
150 // container found?
151 if (iter != m_aMap.end())
152 return (*iter).second->removeInterface(rListener);
153
154 // no container with this id. Always return 0
155 return 0;
156 }
157
163 inline void disposeAndClear(const css::lang::EventObject& rEvt)
164 {
165 // create a copy, because do not fire event in a guarded section
166 InterfaceMap tempMap;
167 {
168 ::osl::MutexGuard aGuard(rMutex);
169 tempMap = std::move(m_aMap);
170 }
171
172 for (auto& rPair : tempMap)
173 rPair.second->disposeAndClear(rEvt);
174 }
175
179 inline void clear()
180 {
181 ::osl::MutexGuard aGuard(rMutex);
182
183 for (const auto& rPair : m_aMap)
184 rPair.second->clear();
185 }
186
187 typedef key keyType;
188
189private:
190 typedef ::std::vector<std::pair<key, std::unique_ptr<OInterfaceContainerHelper3<listener>>>>
193 ::osl::Mutex& rMutex;
194
195 typename InterfaceMap::const_iterator find(const key& rKey) const
196 {
197 auto iter = m_aMap.begin();
198 auto end = m_aMap.end();
199
200 while (iter != end)
201 {
202 equalImpl equal;
203 if (equal(iter->first, rKey))
204 break;
205 ++iter;
206 }
207 return iter;
208 }
209
212 = delete;
213};
214
215} // namespace comphelper
216
217/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A helper class to store interface references of different types.
sal_Int32 addInterface(const key &rKey, const css::uno::Reference< listener > &rListener)
Inserts an element into the container with the specified key.
std::vector< key > getContainedTypes() const
Return all id's under which at least one interface is added.
OMultiTypeInterfaceContainerHelperVar3(::osl::Mutex &rMutex_)
Create a container of interface containers.
void clear()
Remove all elements of all containers.
::std::vector< std::pair< key, std::unique_ptr< OInterfaceContainerHelper3< listener > > > > InterfaceMap
OMultiTypeInterfaceContainerHelperVar3(const OMultiTypeInterfaceContainerHelperVar3 &)=delete
sal_Int32 removeInterface(const key &rKey, const css::uno::Reference< listener > &rListener)
Removes an element from the container with the specified key.
OMultiTypeInterfaceContainerHelperVar3 & operator=(const OMultiTypeInterfaceContainerHelperVar3 &)=delete
InterfaceMap::const_iterator find(const key &rKey) const
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
OInterfaceContainerHelper3< listener > * getContainer(const key &rKey) const
Return the container created under this key.
bool equal(T const &rfValA, T const &rfValB)
end