MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WriterRegistry.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::IO {
4
5template <typename Writer>
6using WriterFactory = std::function<std::unique_ptr<Writer>()>;
7
8/**
9 * @class WriterRegistry
10 * @brief Singleton registry dispatching single-shot writes by file extension.
11 *
12 * Shared by every single-shot writer family (ImageWriter, ModelWriter,
13 * VolumeWriter, ...). Concrete writers register themselves during subsystem
14 * init via register_writer(); create_writer(path) looks up the extension
15 * and returns a fresh instance, or nullptr if none is registered.
16 *
17 * The nullptr is the whole point of the indirection: a format whose backing
18 * library is not present on a given build simply has no entry, and the
19 * caller gets a logged miss at the call site rather than a link error at
20 * startup.
21 *
22 * @tparam Writer Abstract single-shot writer type this registry dispatches.
23 */
24template <typename Writer>
26public:
28 {
29 static WriterRegistry registry;
30 return registry;
31 }
32
34 const std::vector<std::string>& extensions,
35 const WriterFactory<Writer>& factory)
36 {
37 for (const auto& ext : extensions) {
38 m_factories[ext] = factory;
39 }
40 }
41
42 [[nodiscard]] std::unique_ptr<Writer> create_writer(const std::string& filepath) const
43 {
44 auto ext = std::filesystem::path(filepath).extension().string();
45 if (!ext.empty() && ext[0] == '.') {
46 ext = ext.substr(1);
47 }
48
49 auto it = m_factories.find(ext);
50 if (it != m_factories.end()) {
51 return it->second();
52 }
53 return nullptr;
54 }
55
56 [[nodiscard]] std::vector<std::string> get_registered_extensions() const
57 {
58 std::vector<std::string> exts;
59 exts.reserve(m_factories.size());
60 for (const auto& [ext, _] : m_factories) {
61 exts.push_back(ext);
62 }
63 return exts;
64 }
65
66private:
67 std::unordered_map<std::string, WriterFactory<Writer>> m_factories;
68};
69
70} // namespace MayaFlux::IO
std::vector< std::string > get_registered_extensions() const
std::unordered_map< std::string, WriterFactory< Writer > > m_factories
static WriterRegistry & instance()
std::unique_ptr< Writer > create_writer(const std::string &filepath) const
void register_writer(const std::vector< std::string > &extensions, const WriterFactory< Writer > &factory)
Singleton registry dispatching single-shot writes by file extension.
std::function< std::unique_ptr< Writer >()> WriterFactory