SILICON
Toggle main menu visibility
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
18
#include <
ui/serialization/gui_component_factory.hpp
>
19
20
21
namespace
SILICON::ui
{
22
using namespace
SILICON::core;
23
24
GUIComponentFactory::GUIComponentFactory
() =
default
;
25
26
GUIComponentFactory
GUIComponentFactory::empty
()
27
{
28
return
GUIComponentFactory
{};
29
}
30
31
GUIComponentFactory
&
GUIComponentFactory::instance
()
32
{
33
static
GUIComponentFactory
factory;
34
return
factory;
35
}
36
37
void
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
43
void
GUIComponentFactory::registerType
(std::string type,
Factory
factory,
44
EntryMetadata
metadata
)
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
60
std::unique_ptr<GraphicalComponent>
61
GUIComponentFactory::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
71
std::unique_ptr<GraphicalComponent>
72
GUIComponentFactory::createForCoreType
(std::string_view coreType,
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
101
std::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
111
bool
GUIComponentFactory::hasType
(std::string_view type)
const
112
{
113
return
factories
.contains(type);
114
}
115
116
const
GUIComponentFactory::EntryMetadata
&
117
GUIComponentFactory::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
SILICON::ui::GUIComponentFactory::instance
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
Definition
gui_component_factory.cpp:31
SILICON::ui::GUIComponentFactory::metadata
const EntryMetadata & metadata(std::string_view type) const
Definition
gui_component_factory.cpp:117
SILICON::ui::GUIComponentFactory::factories
std::map< std::string, Entry, std::less<> > factories
Definition
gui_component_factory.hpp:139
SILICON::ui::GUIComponentFactory::empty
static GUIComponentFactory empty()
Creates an empty factory without registered types.
Definition
gui_component_factory.cpp:26
SILICON::ui::GUIComponentFactory::Factory
std::function< std::unique_ptr< GraphicalComponent >(QGraphicsItem *)> Factory
Factory function type for creating components.
Definition
gui_component_factory.hpp:63
SILICON::ui::GUIComponentFactory::createForCoreType
std::unique_ptr< GraphicalComponent > createForCoreType(std::string_view coreType, QGraphicsItem *parent=nullptr) const
Creates the graphical component registered for a core component type.
Definition
gui_component_factory.cpp:72
SILICON::ui::GUIComponentFactory::GUIComponentFactory
GUIComponentFactory()
SILICON::ui::GUIComponentFactory::availableTypes
std::vector< std::string > availableTypes() const
Gets all available component types.
Definition
gui_component_factory.cpp:101
SILICON::ui::GUIComponentFactory::registerType
void registerType(std::string type, Factory factory)
Registers a component type.
Definition
gui_component_factory.cpp:37
SILICON::ui::GUIComponentFactory::hasType
bool hasType(std::string_view type) const
Checks if a type is registered.
Definition
gui_component_factory.cpp:111
SILICON::ui::GUIComponentFactory::create
std::unique_ptr< GraphicalComponent > create(std::string_view type, QGraphicsItem *parent=nullptr) const
Creates a component by type name.
Definition
gui_component_factory.cpp:61
gui_component_factory.hpp
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::GUIComponentFactory::EntryMetadata
Definition
gui_component_factory.hpp:65
SILICON::ui::GUIComponentFactory::Entry
Map of type names to factory functions.
Definition
gui_component_factory.hpp:134
SILICON::ui::GUIComponentFactory::Entry::factory
Factory factory
Definition
gui_component_factory.hpp:135
src
ui
serialization
gui_component_factory.cpp
Generated by
1.17.0