PulseAudio Qt Bindings

maps.h
1 /*
2  SPDX-FileCopyrightText: 2014-2015 Harald Sitter <[email protected]>
3  SPDX-FileCopyrightText: 2018 David Rosca <[email protected]>
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 
27 namespace PulseAudioQt
28 {
29 // Used for typedefs.
30 class Card;
31 class Client;
32 class Sink;
33 class SinkInput;
34 class Source;
35 class SourceOutput;
36 class StreamRestore;
37 class Module;
38 
39 /**
40  * @see MapBase
41  * This class is nothing more than the QObject base since moc cannot handle
42  * templates.
43  */
44 class MapBaseQObject : public QObject
45 {
46  Q_OBJECT
47 
48 public:
49  virtual int count() const = 0;
50  virtual QObject *objectAt(int index) const = 0;
51  virtual int indexOfObject(QObject *object) const = 0;
52 
53 Q_SIGNALS:
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  */
66 template<typename Type, typename PAInfo>
67 class MapBase : public MapBaseQObject
68 {
69 public:
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 
150 protected:
151  QVector<Type *> m_data;
152  QHash<quint32, Type *> m_hash;
153  QSet<quint32> m_pendingRemovals;
154 };
155 
164 
165 } // PulseAudioQt
Q_OBJECTQ_OBJECT
const T value(const Key &key) const const
bool remove(const T &value)
Q_EMITQ_EMIT
void append(const T &value)
int indexOf(const T &value, int from) const const
The primary namespace of PulseAudioQt.
Definition: card.cpp:16
T take(const Key &key)
Maps a specific index to a specific object pointer.
Definition: maps.h:67
const T & at(int i) const const
bool contains(const T &value) const const
Q_SIGNALSQ_SIGNALS
void clear()
bool isEmpty() const const
QSet::iterator insert(const T &value)
int count(const T &value) const const
bool contains(const Key &key) const const
void removeAt(int i)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:12:40 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.