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 <QObject>
12#include <QSet>
13#include <QVector>
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 virtual ~MapBase()
71 {
72 }
73
74 const QVector<Type *> &data() const
75 {
76 return m_data;
77 }
78
79 int count() const override
80 {
81 return m_data.count();
82 }
83
84 int indexOfObject(QObject *object) const override
85 {
86 return m_data.indexOf(static_cast<Type *>(object));
87 }
88
89 QObject *objectAt(int index) const override
90 {
91 return m_data.at(index);
92 }
93
94 void reset()
95 {
96 while (!m_hash.isEmpty()) {
97 removeEntry(m_data.at(m_data.count() - 1)->index());
98 }
99 m_pendingRemovals.clear();
100 }
101
102 void insert(Type *object)
103 {
104 Q_ASSERT(!m_data.contains(object));
105
106 const int modelIndex = m_data.count();
107
108 Q_EMIT aboutToBeAdded(modelIndex);
109 m_data.append(object);
110 m_hash[object->index()] = object;
111 Q_EMIT added(modelIndex, object);
112 }
113
114 // Context is passed in as parent because context needs to include the maps
115 // so we'd cause a circular dep if we were to try to use the instance here.
116 // Plus that's weird separation anyway.
117 void updateEntry(const PAInfo *info, QObject *parent)
118 {
119 Q_ASSERT(info);
120
121 if (m_pendingRemovals.remove(info->index)) {
122 // Was already removed again.
123 return;
124 }
125
126 auto *obj = m_hash.value(info->index);
127 if (!obj) {
128 obj = new Type(parent);
129 obj->d->update(info);
130 insert(obj);
131 } else {
132 obj->d->update(info);
133 }
134 }
135
136 void removeEntry(quint32 index)
137 {
138 if (!m_hash.contains(index)) {
139 m_pendingRemovals.insert(index);
140 } else {
141 const int modelIndex = m_data.indexOf(m_hash.value(index));
142 Q_EMIT aboutToBeRemoved(modelIndex);
143 m_data.removeAt(modelIndex);
144 auto object = m_hash.take(index);
145 Q_EMIT removed(modelIndex, object);
146 delete object;
147 }
148 }
149
150protected:
151 QVector<Type *> m_data;
153 QSet<quint32> m_pendingRemovals;
154};
155
164
165} // 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
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 Tue Mar 26 2024 11:20:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.