LibreOffice Module comphelper (master) 1
multiinterfacecontainer4.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#include <sal/config.h>
21#include <com/sun/star/lang/EventObject.hpp>
23#include <memory>
24#include <mutex>
25#include <vector> //for docpp
27namespace comphelper
28{
38template <class key, class listener, class equalImpl = std::equal_to<key>>
40{
41public:
46 inline std::vector<key> getContainedTypes(std::unique_lock<std::mutex>& rGuard) const
47 {
48 std::vector<key> aInterfaceTypes;
49 aInterfaceTypes.reserve(m_aMap.size());
50 for (const auto& rPair : m_aMap)
51 // are interfaces added to this container?
52 if (rPair.second->getLength(rGuard))
53 // yes, put the type in the array
54 aInterfaceTypes.push_back(rPair.first);
55 return aInterfaceTypes;
56 }
57 inline bool hasContainedTypes() const
58 {
59 for (const auto& rPair : m_aMap)
60 // are interfaces added to this container?
61 if (rPair.second->getLength())
62 return true;
63 return false;
64 }
72 {
73 auto iter = find(rKey);
74 if (iter != m_aMap.end())
75 return (*iter).second.get();
76 return nullptr;
77 }
94 inline sal_Int32 addInterface(::std::unique_lock<::std::mutex>& rGuard, const key& rKey,
95 const css::uno::Reference<listener>& rListener)
96 {
97 auto iter = find(rKey);
98 if (iter == m_aMap.end())
99 {
101 m_aMap.emplace_back(rKey, pLC);
102 return pLC->addInterface(rGuard, rListener);
103 }
104 else
105 return (*iter).second->addInterface(rGuard, rListener);
106 }
116 inline sal_Int32 removeInterface(::std::unique_lock<::std::mutex>& rGuard, const key& rKey,
117 const css::uno::Reference<listener>& rListener)
118 {
119 // search container with id nUik
120 auto iter = find(rKey);
121 // container found?
122 if (iter != m_aMap.end())
123 return (*iter).second->removeInterface(rGuard, rListener);
124 // no container with this id. Always return 0
125 return 0;
126 }
132 inline void disposeAndClear(std::unique_lock<std::mutex>& rGuard,
133 const css::lang::EventObject& rEvt)
134 {
135 // create a copy, because do not fire event in a guarded section
136 InterfaceMap tempMap;
137 {
138 tempMap = std::move(m_aMap);
139 }
140 rGuard.unlock();
141 for (auto& rPair : tempMap)
142 {
143 OInterfaceIteratorHelper4<listener> aIt(rGuard, *rPair.second);
144 while (aIt.hasMoreElements())
145 {
146 try
147 {
148 aIt.next()->disposing(rEvt);
149 }
150 catch (css::uno::RuntimeException&)
151 {
152 // be robust, if e.g. a remote bridge has disposed already.
153 // there is no way to delegate the error to the caller :o(.
154 }
155 }
156 }
157 }
161 inline void clear()
162 {
163 for (const auto& rPair : m_aMap)
164 rPair.second->clear();
165 }
166 typedef key keyType;
167
168private:
169 typedef ::std::vector<std::pair<key, std::unique_ptr<OInterfaceContainerHelper4<listener>>>>
172 typename InterfaceMap::const_iterator find(const key& rKey) const
173 {
174 auto iter = m_aMap.begin();
175 auto end = m_aMap.end();
176 while (iter != end)
177 {
178 equalImpl equal;
179 if (equal(iter->first, rKey))
180 break;
181 ++iter;
182 }
183 return iter;
184 }
187 = delete;
188};
189} // namespace comphelper
190/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This is the iterator of an OInterfaceContainerHelper4.
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
css::uno::Reference< ListenerT > const & next()
Return the next element of the iterator.
A helper class to store interface references of different types.
::std::vector< std::pair< key, std::unique_ptr< OInterfaceContainerHelper4< listener > > > > InterfaceMap
OMultiTypeInterfaceContainerHelperVar4 & operator=(const OMultiTypeInterfaceContainerHelperVar4 &)=delete
void clear()
Remove all elements of all containers.
sal_Int32 removeInterface(::std::unique_lock<::std::mutex > &rGuard, const key &rKey, const css::uno::Reference< listener > &rListener)
Removes an element from the container with the specified key.
InterfaceMap::const_iterator find(const key &rKey) const
OMultiTypeInterfaceContainerHelperVar4(const OMultiTypeInterfaceContainerHelperVar4 &)=delete
std::vector< key > getContainedTypes(std::unique_lock< std::mutex > &rGuard) const
Return all id's under which at least one interface is added.
sal_Int32 addInterface(::std::unique_lock<::std::mutex > &rGuard, const key &rKey, const css::uno::Reference< listener > &rListener)
Inserts an element into the container with the specified key.
void disposeAndClear(std::unique_lock< std::mutex > &rGuard, const css::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
OInterfaceContainerHelper4< listener > * getContainer(const key &rKey) const
Return the container created under this key.
bool equal(T const &rfValA, T const &rfValB)
end