-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioEffectFactory.h
More file actions
48 lines (41 loc) · 1.66 KB
/
AudioEffectFactory.h
File metadata and controls
48 lines (41 loc) · 1.66 KB
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
// ./AudioEffectFactory.h
// エフェクト名(文字列)からAudioEffectのインスタンスを生成するファクトリー
#pragma once
#include "AudioEffect.h"
#include <string>
#include <memory>
#include <functional>
#include <map>
#include <stdexcept>
class AudioEffectFactory {
public:
// シングルトンインスタンスを取得
static AudioEffectFactory&getInstance() {
static AudioEffectFactory instance;
return instance;
}
// ファクトリーに関数(コンストラクタ)を登録する
template<typename T>
void registerEffect(const std::string&name) {
creators_[name] = []() { return std::make_unique<T>(); };
}
// 名前を指定してエフェクトのインスタンスを作成する
std::unique_ptr<AudioEffect>createEffect(const std::string&name) {
auto it = creators_.find(name);
if (it == creators_.end()) {
// 見つからない場合はエラーメッセージを出すか、nullptrを返す
// ここではnullptrを返し、呼び出し元でログ出力などをハンドルする
return nullptr;
}
return it->second();
}
private:
// プライベートコンストラクタ(シングルトンパターン)
AudioEffectFactory() = default;
~AudioEffectFactory() = default;
// コピーとムーブを禁止
AudioEffectFactory(const AudioEffectFactory&) = delete;
AudioEffectFactory&operator=(const AudioEffectFactory&) = delete;
// 文字列名と、AudioEffectを生成する関数のマップ
std::map<std::string, std::function<std::unique_ptr<AudioEffect>()>> creators_;
};