-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathPresetFactoryManager.hpp
92 lines (71 loc) · 2.74 KB
/
PresetFactoryManager.hpp
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
#pragma once
#include "PresetFactory.hpp"
#include <map>
#include <utility>
#include <vector>
namespace libprojectM {
/// A simple exception class to strongly type all preset factory related issues
class PresetFactoryException : public std::exception
{
public:
inline PresetFactoryException(std::string message)
: m_message(std::move(message))
{
}
virtual ~PresetFactoryException() = default;
const std::string& message() const
{
return m_message;
}
private:
std::string m_message;
};
/// A manager of preset factories
class PresetFactoryManager
{
public:
PresetFactoryManager() = default;
~PresetFactoryManager();
/**
* @brief Initializes the manager.
*/
void initialize();
/// Requests a factory given a preset extension type
/// \param extension a string denoting the preset suffix type
/// \throws PresetFactoryException if the extension is unhandled
/// \returns a valid preset factory associated with the extension
PresetFactory& factory(const std::string& extension);
/// Tests if an extension has been registered with a factory
/// \param extension the file name extension to verify
/// \returns true if a factory exists, false otherwise
bool extensionHandled(const std::string& extension) const;
/**
* @brief Loads a preset by a given filename or URL.
*
* Supported URLs are "idle://" (loads the idle preset) and "file://". Other URL schemes will
* throw an exception.
*
* @param filename The filename/URL to load.
* @throws PresetFactoryException If any error occurs during preset loading. Exception message
* contains additional details.
* @return A valid pointer to the loaded preset.
*/
std::unique_ptr<Preset> CreatePresetFromFile(const std::string& filename);
/**
* @brief Loads a preset from a stream.
* @param extension The "original" extension. Used to determine preset data format.
* @param data A stream with preset data to load.
* @throws PresetFactoryException If any error occurs during preset loading. Exception message
* contains additional details.
* @return A valid pointer to the loaded preset.
*/
std::unique_ptr<Preset> CreatePresetFromStream(const std::string& extension, std::istream& data);
std::vector<std::string> extensionsHandled() const;
private:
void registerFactory(const std::string& extension, PresetFactory* factory);
auto ParseExtension(const std::string& filename) -> std::string;
mutable std::map<std::string, PresetFactory*> m_factoryMap;
mutable std::vector<PresetFactory*> m_factoryList;
void ClearFactories();
};
} // namespace libprojectM