| 1 | // Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTINPUTSUPPORT_DEVICEHANDLERLIST_P_H |
| 5 | #define QTINPUTSUPPORT_DEVICEHANDLERLIST_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QString> |
| 19 | #include <private/qglobal_p.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | #include <memory> |
| 23 | |
| 24 | namespace QtInputSupport { |
| 25 | |
| 26 | template <typename Handler> |
| 27 | class DeviceHandlerList { |
| 28 | public: |
| 29 | struct Device { |
| 30 | QString deviceNode; |
| 31 | std::unique_ptr<Handler> handler; |
| 32 | }; |
| 33 | |
| 34 | void add(const QString &deviceNode, std::unique_ptr<Handler> handler) |
| 35 | { |
| 36 | v.push_back({deviceNode, std::move(handler)}); |
| 37 | } |
| 38 | |
| 39 | bool remove(const QString &deviceNode) |
| 40 | { |
| 41 | const auto deviceNodeMatches = [&] (const Device &d) { return d.deviceNode == deviceNode; }; |
| 42 | const auto it = std::find_if(v.cbegin(), v.cend(), deviceNodeMatches); |
| 43 | if (it == v.cend()) |
| 44 | return false; |
| 45 | v.erase(it); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | int count() const noexcept { return static_cast<int>(v.size()); } |
| 50 | |
| 51 | typename std::vector<Device>::const_iterator begin() const noexcept { return v.begin(); } |
| 52 | typename std::vector<Device>::const_iterator end() const noexcept { return v.end(); } |
| 53 | |
| 54 | private: |
| 55 | std::vector<Device> v; |
| 56 | }; |
| 57 | |
| 58 | } // QtInputSupport |
| 59 | |
| 60 | #endif // QTINPUTSUPPORT_DEVICEHANDLERLIST_P_H |
| 61 | |