1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/config.h>
23 #include <com/sun/star/lang/EventObject.hpp>
24 #include <comphelper/interfacecontainer3.hxx>
36 A helper class to store interface references of different types.
37 This is a copy of the similar class at include/cppuhelper/interfacecontainer.h,
38 but now uses the improved comphelper::InterfaceContainer3.
40 @see OInterfaceIteratorHelper3
41 @see OInterfaceContainerHelper3
43 template <class listener
, class key
, class equalImpl
= std::equal_to
<key
>>
44 class OMultiTypeInterfaceContainerHelperVar3
48 Create a container of interface containers.
50 @param rMutex the mutex to protect multi thread access.
51 The lifetime must be longer than the lifetime
54 inline OMultiTypeInterfaceContainerHelperVar3(::osl::Mutex
& rMutex_
)
60 Return all id's under which at least one interface is added.
62 inline std::vector
<key
> getContainedTypes() const
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
;
75 inline bool hasContainedTypes() const
77 ::osl::MutexGuard
aGuard(rMutex
);
78 for (const auto& rPair
: m_aMap
)
79 // are interfaces added to this container?
80 if (rPair
.second
->getLength())
86 Return the container created under this key.
87 The InterfaceContainerHelper exists until the whole MultiTypeContainer is destroyed.
88 @return the container created under this key. If the container
89 was not created, null was returned.
91 inline OInterfaceContainerHelper3
<listener
>* getContainer(const key
& rKey
) const
93 ::osl::MutexGuard
aGuard(rMutex
);
95 auto iter
= find(rKey
);
96 if (iter
!= m_aMap
.end())
97 return (*iter
).second
.get();
101 /** Inserts an element into the container with the specified key.
102 The position is not specified, thus it is not specified in which order events are fired.
105 If you add the same interface more than once, then it will be added to the elements list
106 more than once and thus if you want to remove that interface from the list, you have to call
107 removeInterface() the same number of times.
108 In the latter case, you will also get events fired more than once (if the interface is a
112 the id of the container
114 interface to be added; it is allowed, to insert null or
115 the same interface more than once
117 the new count of elements in the container
119 inline sal_Int32
addInterface(const key
& rKey
, const css::uno::Reference
<listener
>& rListener
)
121 ::osl::MutexGuard
aGuard(rMutex
);
122 auto iter
= find(rKey
);
123 if (iter
== m_aMap
.end())
125 auto pLC
= new OInterfaceContainerHelper3
<listener
>(rMutex
);
126 m_aMap
.emplace_back(rKey
, pLC
);
127 return pLC
->addInterface(rListener
);
130 return (*iter
).second
->addInterface(rListener
);
133 /** Removes an element from the container with the specified key.
134 It uses interface equality to remove the interface.
137 the id of the container
139 interface to be removed
141 the new count of elements in the container
143 inline sal_Int32
removeInterface(const key
& rKey
,
144 const css::uno::Reference
<listener
>& rListener
)
146 ::osl::MutexGuard
aGuard(rMutex
);
148 // search container with id nUik
149 auto iter
= find(rKey
);
151 if (iter
!= m_aMap
.end())
152 return (*iter
).second
->removeInterface(rListener
);
154 // no container with this id. Always return 0
159 Call disposing on all references in the container, that
160 support XEventListener. Then clears the container.
161 @param rEvt the event object which is passed during disposing() call
163 inline void disposeAndClear(const css::lang::EventObject
& rEvt
)
165 // create a copy, because do not fire event in a guarded section
166 InterfaceMap tempMap
;
168 ::osl::MutexGuard
aGuard(rMutex
);
169 tempMap
= std::move(m_aMap
);
172 for (auto& rPair
: tempMap
)
173 rPair
.second
->disposeAndClear(rEvt
);
177 Remove all elements of all containers. Does not delete the container.
181 ::osl::MutexGuard
aGuard(rMutex
);
183 for (const auto& rPair
: m_aMap
)
184 rPair
.second
->clear();
190 typedef ::std::vector
<std::pair
<key
, std::unique_ptr
<OInterfaceContainerHelper3
<listener
>>>>
193 ::osl::Mutex
& rMutex
;
195 typename
InterfaceMap::const_iterator
find(const key
& rKey
) const
197 auto iter
= m_aMap
.begin();
198 auto end
= m_aMap
.end();
203 if (equal(iter
->first
, rKey
))
210 OMultiTypeInterfaceContainerHelperVar3(const OMultiTypeInterfaceContainerHelperVar3
&) = delete;
211 OMultiTypeInterfaceContainerHelperVar3
& operator=(const OMultiTypeInterfaceContainerHelperVar3
&)
215 } // namespace comphelper
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */