PulseAudio Qt Bindings

maps.h
1/*
2 SPDX-FileCopyrightText: 2014-2015 Harald Sitter <sitter@kde.org>
3 SPDX-FileCopyrightText: 2018 David Rosca <nowrep@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#pragma once
9
10#include <QHash>
11#include <QList>
12#include <QObject>
13#include <QSet>
14
15#include <pulse/ext-stream-restore.h>
16#include <pulse/pulseaudio.h>
17
18#include "card_p.h"
19#include "client_p.h"
20#include "module_p.h"
21#include "sink_p.h"
22#include "sinkinput_p.h"
23#include "source_p.h"
24#include "sourceoutput_p.h"
25#include "streamrestore_p.h"
26
27namespace PulseAudioQt
28{
29// Used for typedefs.
30class Card;
31class Client;
32class Sink;
33class SinkInput;
34class Source;
35class SourceOutput;
36class StreamRestore;
37class Module;
38
39/**
40 * @see MapBase
41 * This class is nothing more than the QObject base since moc cannot handle
42 * templates.
43 */
44class MapBaseQObject : public QObject
45{
47
48public:
49 virtual int count() const = 0;
50 virtual QObject *objectAt(int index) const = 0;
51 virtual int indexOfObject(QObject *object) const = 0;
52
54 void aboutToBeAdded(int index);
55 void added(int index, QObject *object);
56 void aboutToBeRemoved(int index);
57 void removed(int index, QObject *object);
58};
59
60/**
61 * Maps a specific index to a specific object pointer.
62 * This is used to give the unique arbitrary PulseAudio index of a PulseObject a
63 * serialized list index. Namely it enables us to translate a discrete list
64 * index to a pulse index to an object, and any permutation thereof.
65 */
66template<typename Type, typename PAInfo>
67class MapBase : public MapBaseQObject
68{
69public:
70 ~MapBase() override = default;
71
72 const QList<Type *> &data() const
73 {
74 return m_data;
75 }
76
77 int count() const override
78 {
79 return m_data.count();
80 }
81
82 int indexOfObject(QObject *object) const override
83 {
84 return m_data.indexOf(static_cast<Type *>(object));
85 }
86
87 QObject *objectAt(int index) const override
88 {
89 return m_data.at(index);
90 }
91
92 void reset()
93 {
94 while (!m_hash.isEmpty()) {
95 removeEntry(m_data.at(m_data.count() - 1)->index());
96 }
97 m_pendingRemovals.clear();
98 }
99
100 void insert(Type *object)
101 {
102 Q_ASSERT(!m_data.contains(object));
103
104 const int modelIndex = m_data.count();
105
106 Q_EMIT aboutToBeAdded(modelIndex);
107 m_data.append(object);
108 m_hash[object->index()] = object;
109 Q_EMIT added(modelIndex, object);
110 }
111
112 // Context is passed in as parent because context needs to include the maps
113 // so we'd cause a circular dep if we were to try to use the instance here.
114 // Plus that's weird separation anyway.
115 void updateEntry(const PAInfo *info, QObject *parent)
116 {
117 Q_ASSERT(info);
118
119 if (m_pendingRemovals.remove(info->index)) {
120 // Was already removed again.
121 return;
122 }
123
124 auto *obj = m_hash.value(info->index);
125 if (!obj) {
126 obj = new Type(parent);
127 obj->d->update(info);
128 insert(obj);
129 } else {
130 obj->d->update(info);
131 }
132 }
133
134 void removeEntry(quint32 index)
135 {
136 if (!m_hash.contains(index)) {
137 m_pendingRemovals.insert(index);
138 } else {
139 const int modelIndex = m_data.indexOf(m_hash.value(index));
140 Q_EMIT aboutToBeRemoved(modelIndex);
141 m_data.removeAt(modelIndex);
142 auto object = m_hash.take(index);
143 Q_EMIT removed(modelIndex, object);
144 delete object;
145 }
146 }
147
148protected:
149 QList<Type *> m_data;
151 QSet<quint32> m_pendingRemovals;
152};
153
162
163} // PulseAudioQt
Maps a specific index to a specific object pointer.
Definition maps.h:68
The primary namespace of PulseAudioQt.
Definition card.cpp:17
bool contains(const Key &key) const const
bool isEmpty() const const
T take(const Key &key)
T value(const Key &key) const const
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool contains(const AT &value) const const
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
void removeAt(qsizetype i)
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
T qobject_cast(QObject *object)
void clear()
iterator insert(const T &value)
bool remove(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:52:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.