forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PresetFactoryManager.cpp
109 lines (87 loc) · 2.76 KB
/
PresetFactoryManager.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
//
// C++ Implementation: PresetFactoryManager
//
// Description:
//
//
// Author: Carmelo Piccione <[email protected]>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "PresetFactoryManager.hpp"
#ifndef DISABLE_MILKDROP_PRESETS
#include "MilkdropPresetFactory/MilkdropPresetFactory.hpp"
#endif
#ifndef DISABLE_NATIVE_PRESETS
#include "NativePresetFactory/NativePresetFactory.hpp"
#endif
#include "Common.hpp"
#include <sstream>
PresetFactoryManager::PresetFactoryManager() : _gx(0), _gy(0), initialized(false) {}
PresetFactoryManager::~PresetFactoryManager() {
for (std::vector<PresetFactory *>::iterator pos = _factoryList.begin();
pos != _factoryList.end(); ++pos) {
assert(*pos);
delete(*pos);
}
initialized = false;
}
void PresetFactoryManager::initialize(int gx, int gy) {
_gx = gx;
_gy = gy;
if (!initialized) {
initialized = true;
} else {
std::cout << "already initialized " << std::endl;
return;
}
PresetFactory * factory;
#ifndef DISABLE_MILKDROP_PRESETS
factory = new MilkdropPresetFactory(_gx, _gy);
registerFactory(factory->supportedExtensions(), factory);
#endif
#ifndef DISABLE_NATIVE_PRESETS
factory = new NativePresetFactory();
registerFactory(factory->supportedExtensions(), factory);
#endif
}
// Current behavior if a conflict is occurs is to override the previous request
void PresetFactoryManager::registerFactory(const std::string & extensions, PresetFactory * factory) {
std::stringstream ss(extensions);
std::string extension;
_factoryList.push_back(factory);
while (ss >> extension) {
if (_factoryMap.count(extension)) {
std::cerr << "[PresetFactoryManager] Warning: extension \"" << extension <<
"\" already has a factory. New factory handler ignored." << std::endl;
} else {
_factoryMap.insert(std::make_pair(extension, factory));
}
}
}
std::unique_ptr<Preset> PresetFactoryManager::allocate(const std::string & url, const std::string & name)
{
try {
const std::string extension = parseExtension (url);
return factory(extension).allocate(url, name);
} catch (const PresetFactoryException & e) {
throw e;
} catch (const std::exception & e) {
throw PresetFactoryException(e.what());
} catch (...) {
throw PresetFactoryException("uncaught preset factory exception");
}
return std::unique_ptr<Preset>();
}
PresetFactory & PresetFactoryManager::factory(const std::string & extension) {
if (!extensionHandled(extension)) {
std::ostringstream os;
os << "No preset factory associated with \"" << extension << "\"." << std::endl;
throw PresetFactoryException(os.str());
}
return *_factoryMap[extension];
}
bool PresetFactoryManager::extensionHandled(const std::string & extension) const {
return _factoryMap.count(extension);
}