summaryrefslogtreecommitdiffstats
path: root/include/comphelper/multiinterfacecontainer4.hxx
blob: eeccd20920ca0f9ad80ae2eb40608fa6927187cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#pragma once
#include <sal/config.h>
#include <com/sun/star/lang/EventObject.hpp>
#include <comphelper/interfacecontainer4.hxx>
#include <memory>
#include <mutex>
#include <vector>
/** */ //for docpp
namespace comphelper
{
/**
  A helper class to store interface references of different types.
  This is a copy of the code at include/comphelper/multiinterfacecontainer3.hxx,
  except that it (a) uses std::mutex instead of osl::Mutex and (b) does not
  store a reference to the mutex, but relies on the calling class to take
  a lock around using it.
  @see OInterfaceIteratorHelper3
  @see OInterfaceContainerHelper3
 */
template <class key, class listener, class equalImpl = std::equal_to<key>>
class OMultiTypeInterfaceContainerHelperVar4
{
public:
    OMultiTypeInterfaceContainerHelperVar4() {}
    /**
      Return all id's under which at least one interface is added.
     */
    inline std::vector<key> getContainedTypes(std::unique_lock<std::mutex>& rGuard) const
    {
        std::vector<key> aInterfaceTypes;
        aInterfaceTypes.reserve(m_aMap.size());
        for (const auto& rPair : m_aMap)
            // are interfaces added to this container?
            if (rPair.second->getLength(rGuard))
                // yes, put the type in the array
                aInterfaceTypes.push_back(rPair.first);
        return aInterfaceTypes;
    }
    inline bool hasContainedTypes() const
    {
        for (const auto& rPair : m_aMap)
            // are interfaces added to this container?
            if (rPair.second->getLength())
                return true;
        return false;
    }
    /**
      Return the container created under this key.
      The InterfaceContainerHelper exists until the whole MultiTypeContainer is destroyed.
      @return the container created under this key. If the container
                 was not created, null was returned.
     */
    inline OInterfaceContainerHelper4<listener>* getContainer(const key& rKey) const
    {
        auto iter = find(rKey);
        if (iter != m_aMap.end())
            return (*iter).second.get();
        return nullptr;
    }
    /** Inserts an element into the container with the specified key.
        The position is not specified, thus it is not specified in which order events are fired.
        @attention
        If you add the same interface more than once, then it will be added to the elements list
        more than once and thus if you want to remove that interface from the list, you have to call
        removeInterface() the same number of times.
        In the latter case, you will also get events fired more than once (if the interface is a
        listener interface).
        @param rKey
               the id of the container
        @param r
               interface to be added; it is allowed, to insert null or
               the same interface more than once
        @return
                the new count of elements in the container
    */
    inline sal_Int32 addInterface(::std::unique_lock<::std::mutex>& rGuard, const key& rKey,
                                  const css::uno::Reference<listener>& rListener)
    {
        auto iter = find(rKey);
        if (iter == m_aMap.end())
        {
            auto pLC = new OInterfaceContainerHelper4<listener>();
            m_aMap.emplace_back(rKey, pLC);
            return pLC->addInterface(rGuard, rListener);
        }
        else
            return (*iter).second->addInterface(rGuard, rListener);
    }
    /** Removes an element from the container with the specified key.
        It uses interface equality to remove the interface.
        @param rKey
               the id of the container
        @param rxIFace
               interface to be removed
        @return
                the new count of elements in the container
    */
    inline sal_Int32 removeInterface(::std::unique_lock<::std::mutex>& rGuard, const key& rKey,
                                     const css::uno::Reference<listener>& rListener)
    {
        // search container with id nUik
        auto iter = find(rKey);
        // container found?
        if (iter != m_aMap.end())
            return (*iter).second->removeInterface(rGuard, rListener);
        // no container with this id. Always return 0
        return 0;
    }
    /**
      Call disposing on all references in the container, that
      support XEventListener. Then clears the container.
      @param rEvt the event object which is passed during disposing() call
     */
    inline void disposeAndClear(std::unique_lock<std::mutex>& rGuard,
                                const css::lang::EventObject& rEvt)
    {
        // create a copy, because do not fire event in a guarded section
        InterfaceMap tempMap;
        {
            tempMap = std::move(m_aMap);
        }
        rGuard.unlock();
        for (auto& rPair : tempMap)
        {
            OInterfaceIteratorHelper4<listener> aIt(rGuard, *rPair.second);
            while (aIt.hasMoreElements())
            {
                try
                {
                    aIt.next()->disposing(rEvt);
                }
                catch (css::uno::RuntimeException&)
                {
                    // be robust, if e.g. a remote bridge has disposed already.
                    // there is no way to delegate the error to the caller :o(.
                }
            }
        }
    }
    /**
      Remove all elements of all containers. Does not delete the container.
     */
    inline void clear()
    {
        for (const auto& rPair : m_aMap)
            rPair.second->clear();
    }
    typedef key keyType;

private:
    typedef ::std::vector<std::pair<key, std::unique_ptr<OInterfaceContainerHelper4<listener>>>>
        InterfaceMap;
    InterfaceMap m_aMap;
    typename InterfaceMap::const_iterator find(const key& rKey) const
    {
        auto iter = m_aMap.begin();
        auto end = m_aMap.end();
        while (iter != end)
        {
            equalImpl equal;
            if (equal(iter->first, rKey))
                break;
            ++iter;
        }
        return iter;
    }
    OMultiTypeInterfaceContainerHelperVar4(const OMultiTypeInterfaceContainerHelperVar4&) = delete;
    OMultiTypeInterfaceContainerHelperVar4& operator=(const OMultiTypeInterfaceContainerHelperVar4&)
        = delete;
};
} // namespace comphelper
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */