SILICON
Loading...
Searching...
No Matches
gui_component_factory.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19
20
21namespace SILICON::ui {
22using namespace SILICON::core;
23
25
30
32{
33 static GUIComponentFactory factory;
34 return factory;
35}
36
37void GUIComponentFactory::registerType(std::string type, Factory factory)
38{
39 const std::string coreType = type;
40 registerType(std::move(type), std::move(factory), {.coreType = coreType});
41}
42
43void GUIComponentFactory::registerType(std::string type, Factory factory,
45{
46 if (!factory)
47 throw std::invalid_argument("GUI component registration requires a factory");
48 if (metadata.coreType.empty() && !metadata.graphicalMetadata)
49 throw std::invalid_argument(
50 "GUI component registration requires core or graphical metadata");
51
52 auto [it, inserted] =
53 factories.emplace(std::move(type), Entry{std::move(factory), std::move(metadata)});
54 if (!inserted) {
55 throw std::logic_error(std::string("Duplicate GUI component registration: ")
56 + it->first);
57 }
58}
59
60std::unique_ptr<GraphicalComponent>
61GUIComponentFactory::create(std::string_view type, QGraphicsItem* parent) const
62{
63 auto it = factories.find(type);
64 if (it == factories.end()) {
65 throw std::runtime_error(std::string("Unknown GUI component type: ")
66 + std::string{type});
67 }
68 return it->second.factory(parent);
69}
70
71std::unique_ptr<GraphicalComponent>
73 QGraphicsItem* parent) const
74{
75 const Entry* match = nullptr;
76 std::string_view matchedGuiType;
77
78 for (const auto& [guiType, entry] : factories) {
79 if (entry.metadata.coreType.empty() || entry.metadata.coreType != coreType)
80 continue;
81
82 if (match) {
83 throw std::runtime_error(
84 std::string("Ambiguous GUI component mapping for core type: ")
85 + std::string{coreType} + " (" + std::string{matchedGuiType} + ", " + guiType
86 + ")");
87 }
88
89 match = &entry;
90 matchedGuiType = guiType;
91 }
92
93 if (!match) {
94 throw std::runtime_error(std::string("No GUI component registered for core type: ")
95 + std::string{coreType});
96 }
97
98 return match->factory(parent);
99}
100
101std::vector<std::string> GUIComponentFactory::availableTypes() const
102{
103 std::vector<std::string> types;
104 types.reserve(factories.size());
105 for (const auto& [type, _] : factories) {
106 types.push_back(type);
107 }
108 return types;
109}
110
111bool GUIComponentFactory::hasType(std::string_view type) const
112{
113 return factories.contains(type);
114}
115
117GUIComponentFactory::metadata(std::string_view type) const
118{
119 auto it = factories.find(type);
120 if (it == factories.end()) {
121 throw std::runtime_error(std::string("Unknown GUI component type: ")
122 + std::string{type});
123 }
124 return it->second.metadata;
125}
126
127} // namespace SILICON::ui
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
const EntryMetadata & metadata(std::string_view type) const
std::map< std::string, Entry, std::less<> > factories
static GUIComponentFactory empty()
Creates an empty factory without registered types.
std::function< std::unique_ptr< GraphicalComponent >(QGraphicsItem *)> Factory
Factory function type for creating components.
std::unique_ptr< GraphicalComponent > createForCoreType(std::string_view coreType, QGraphicsItem *parent=nullptr) const
Creates the graphical component registered for a core component type.
std::vector< std::string > availableTypes() const
Gets all available component types.
void registerType(std::string type, Factory factory)
Registers a component type.
bool hasType(std::string_view type) const
Checks if a type is registered.
std::unique_ptr< GraphicalComponent > create(std::string_view type, QGraphicsItem *parent=nullptr) const
Creates a component by type name.
Map of type names to factory functions.