forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundsourceproviderregistry.cpp
114 lines (101 loc) · 4.23 KB
/
soundsourceproviderregistry.cpp
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
#include "sources/soundsourceproviderregistry.h"
#include "util/logger.h"
namespace mixxx {
namespace {
const Logger kLogger("SoundSourceProviderRegistry");
} // anonymous namespace
void SoundSourceProviderRegistry::registerProvider(
const SoundSourceProviderPointer& pProvider) {
const QStringList supportedFileExtensions(
pProvider->getSupportedFileExtensions());
if (supportedFileExtensions.isEmpty()) {
kLogger.warning() << "SoundSource provider"
<< pProvider->getName()
<< "does not support any file extensions";
return; // abort registration
}
for (const auto& fileExt : supportedFileExtensions) {
SoundSourceProviderPriority providerPriority(
pProvider->getPriorityHint(fileExt));
registerProviderForFileExtension(
fileExt,
pProvider,
providerPriority);
}
}
void SoundSourceProviderRegistry::registerProviderForFileExtension(
const QString& fileExtension,
const SoundSourceProviderPointer& pProvider,
SoundSourceProviderPriority providerPriority) {
SoundSourceProviderRegistration registration(pProvider, providerPriority);
addRegistrationForFileExtension(fileExtension, std::move(registration));
}
void SoundSourceProviderRegistry::addRegistrationForFileExtension(
const QString& fileExtension,
SoundSourceProviderRegistration registration) {
DEBUG_ASSERT(registration.getProvider());
QList<SoundSourceProviderRegistration>& registrationsForFileExtension =
m_registry[fileExtension];
insertRegistration(®istrationsForFileExtension, registration);
}
void SoundSourceProviderRegistry::insertRegistration(
QList<SoundSourceProviderRegistration>* pRegistrations,
SoundSourceProviderRegistration registration) {
QList<SoundSourceProviderRegistration>::iterator listIter(
pRegistrations->begin());
// Perform a linear search through the list & insert
while (pRegistrations->end() != listIter) {
// Priority comparison with <=: New registrations will be inserted
// before existing registrations with equal priority, but after
// existing registrations with higher priority.
if (listIter->getProviderPriority() <= registration.getProviderPriority()) {
listIter = pRegistrations->insert(listIter, std::move(registration));
DEBUG_ASSERT(pRegistrations->end() != listIter);
return; // done
} else {
++listIter; // continue loop
}
}
if (pRegistrations->end() == listIter) {
// List was empty or registration has the lowest priority
pRegistrations->append(std::move(registration));
}
}
void SoundSourceProviderRegistry::deregisterProvider(
const SoundSourceProviderPointer& pProvider) {
const QStringList supportedFileExtensions(
pProvider->getSupportedFileExtensions());
for (const auto& fileExt : supportedFileExtensions) {
deregisterProviderForFileExtension(fileExt, pProvider);
}
}
void SoundSourceProviderRegistry::deregisterProviderForFileExtension(
const QString& fileExtension,
const SoundSourceProviderPointer& pProvider) {
auto registryIter(m_registry.find(fileExtension));
if (m_registry.end() != registryIter) {
QList<SoundSourceProviderRegistration>& registrationsForFileExtension = registryIter.value();
auto listIter = registrationsForFileExtension.begin();
while (registrationsForFileExtension.end() != listIter) {
if (listIter->getProvider() == pProvider) {
listIter = registrationsForFileExtension.erase(listIter);
} else {
++listIter;
}
}
if (registrationsForFileExtension.isEmpty()) {
m_registry.erase(registryIter);
}
}
}
QList<SoundSourceProviderRegistration>
SoundSourceProviderRegistry::getRegistrationsForFileExtension(
const QString& fileExtension) const {
auto i = m_registry.constFind(fileExtension);
if (m_registry.constEnd() != i) {
return i.value();
} else {
return QList<SoundSourceProviderRegistration>();
}
}
} // namespace mixxx