-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathPresetFactoryManager.cpp
144 lines (119 loc) · 3.41 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "PresetFactoryManager.hpp"
#include <MilkdropPreset/Factory.hpp>
#include <Utils.hpp>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <sstream>
namespace libprojectM {
PresetFactoryManager::~PresetFactoryManager()
{
ClearFactories();
}
void PresetFactoryManager::ClearFactories()
{
m_factoryMap.clear();
for (std::vector<PresetFactory*>::iterator pos = m_factoryList.begin();
pos != m_factoryList.end(); ++pos)
{
assert(*pos);
delete (*pos);
}
m_factoryList.clear();
}
void PresetFactoryManager::initialize()
{
ClearFactories();
auto* milkdropFactory = new MilkdropPreset::Factory();
registerFactory(milkdropFactory->supportedExtensions(), milkdropFactory);
}
// 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;
m_factoryList.push_back(factory);
while (ss >> extension)
{
if (m_factoryMap.count(extension))
{
std::cerr << "[PresetFactoryManager] Warning: extension \"" << extension << "\" already has a factory. New factory handler ignored." << std::endl;
}
else
{
m_factoryMap.insert(std::make_pair(extension, factory));
}
}
}
std::unique_ptr<Preset> PresetFactoryManager::CreatePresetFromFile(const std::string& filename)
{
try
{
const std::string extension = "." + ParseExtension(filename);
return factory(extension).LoadPresetFromFile(filename);
}
catch (const PresetFactoryException&)
{
throw;
}
catch (const std::exception& e)
{
throw PresetFactoryException(e.what());
}
catch (...)
{
throw PresetFactoryException("Uncaught preset factory exception");
}
}
std::unique_ptr<Preset> PresetFactoryManager::CreatePresetFromStream(const std::string& extension, std::istream& data)
{
try
{
return factory(extension).LoadPresetFromStream(data);
}
catch (const PresetFactoryException&)
{
throw;
}
catch (const std::exception& e)
{
throw PresetFactoryException(e.what());
}
catch (...)
{
throw PresetFactoryException("Uncaught preset factory exception");
}
}
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 *m_factoryMap[extension];
}
bool PresetFactoryManager::extensionHandled(const std::string& extension) const
{
return m_factoryMap.count(extension);
}
std::vector<std::string> PresetFactoryManager::extensionsHandled() const
{
std::vector<std::string> retval;
for (auto const& element : m_factoryMap)
{
retval.push_back(element.first);
}
return retval;
}
//CPP17: std::filesystem::path::extension
auto PresetFactoryManager::ParseExtension(const std::string& filename) -> std::string
{
const auto start = filename.find_last_of('.');
if (start == std::string::npos || start >= (filename.length() - 1)) {
return "";
}
return Utils::ToLower(filename.substr(start + 1, filename.length()));
}
} // namespace libprojectM