Line data Source code
1 : #pragma once
2 :
3 : #include <functional>
4 : #include <map>
5 : #include <memory>
6 : #include <string>
7 : #include <vector>
8 :
9 : #include "audio/backend/i_audio_backend.h"
10 :
11 : namespace Amplitron {
12 :
13 : /**
14 : * @brief Registry for available audio backends.
15 : * Satisfies the Open/Closed Principle (OCP) and Registry pattern.
16 : */
17 : class AudioBackendRegistry {
18 : public:
19 : using Creator = std::function<std::unique_ptr<IAudioBackend>()>;
20 :
21 828 : static AudioBackendRegistry& instance() {
22 830 : static AudioBackendRegistry inst;
23 828 : return inst;
24 : }
25 :
26 14 : void register_backend(const std::string& name, Creator creator) { creators_[name] = creator; }
27 :
28 814 : std::unique_ptr<IAudioBackend> create(const std::string& name) {
29 814 : auto it = creators_.find(name);
30 814 : if (it != creators_.end()) {
31 814 : return it->second();
32 : }
33 0 : return nullptr;
34 276 : }
35 :
36 0 : std::vector<std::string> available() const {
37 0 : std::vector<std::string> keys;
38 0 : keys.reserve(creators_.size());
39 0 : for (const auto& pair : creators_) {
40 0 : keys.push_back(pair.first);
41 : }
42 0 : return keys;
43 0 : }
44 :
45 : private:
46 8 : AudioBackendRegistry() = default;
47 2 : ~AudioBackendRegistry() = default;
48 : AudioBackendRegistry(const AudioBackendRegistry&) = delete;
49 : AudioBackendRegistry& operator=(const AudioBackendRegistry&) = delete;
50 :
51 : std::map<std::string, Creator> creators_;
52 : };
53 :
54 : template <typename T>
55 : class BackendRegistrar {
56 : public:
57 18 : BackendRegistrar(const std::string& name) {
58 18 : AudioBackendRegistry::instance().register_backend(name,
59 822 : []() { return std::make_unique<T>(); });
60 18 : }
61 : };
62 :
63 : } // namespace Amplitron
|