SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
component_registry.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 <
core/component.hpp
>
19
#include <
core/serialization/component_registry.hpp
>
20
21
#include <algorithm>
22
#include <stdexcept>
23
#include <utility>
24
25
namespace
SILICON::core
{
26
27
ComponentRegistry::ComponentRegistry
() =
default
;
28
29
ComponentRegistry
ComponentRegistry::empty
()
30
{
31
return
ComponentRegistry
{};
32
}
33
34
ComponentRegistry
&
ComponentRegistry::instance
()
35
{
36
static
ComponentRegistry
registry;
37
return
registry;
38
}
39
40
namespace
{
41
42
bool
isValidMetadata(
const
ComponentMetadata
& metadata)
43
{
44
return
!metadata.
displayName
.empty() && !metadata.
description
.empty();
45
}
46
47
}
// namespace
48
49
void
ComponentRegistry::registerType
(std::string type,
Factory
factory)
50
{
51
if
(!factory)
52
throw
std::invalid_argument(
"Component registration requires a factory"
);
53
54
const
ComponentMetadata
metadata
= factory()->metadata();
55
if
(!isValidMetadata(
metadata
))
56
throw
std::invalid_argument(
"Component registration requires metadata"
);
57
58
auto
[it, inserted] =
types
.emplace(std::move(type),
Entry
{std::move(factory)});
59
if
(!inserted) {
60
throw
std::logic_error(std::string(
"Duplicate component registration: "
) + it->first);
61
}
62
}
63
64
Component_ptr
ComponentRegistry::create
(std::string_view type)
const
65
{
66
auto
it =
types
.find(type);
67
if
(it ==
types
.end()) {
68
throw
std::runtime_error(std::string(
"Unknown component type: "
) + std::string{type});
69
}
70
return
it->second.factory();
71
}
72
73
bool
ComponentRegistry::hasType
(std::string_view type)
const
74
{
75
return
types
.contains(type);
76
}
77
78
ComponentMetadata
ComponentRegistry::metadata
(std::string_view type)
const
79
{
80
auto
it =
types
.find(type);
81
if
(it ==
types
.end()) {
82
throw
std::runtime_error(std::string(
"Unknown component type: "
) + std::string{type});
83
}
84
return
it->second.factory()->metadata();
85
}
86
87
std::vector<std::string>
ComponentRegistry::availableTypes
()
const
88
{
89
std::vector<std::string>
types
;
90
types
.reserve(this->types.size());
91
for
(
const
auto
& [type, _] : this->types) {
92
types
.push_back(type);
93
}
94
return
types
;
95
}
96
97
std::string_view
componentCategoryName
(
const
ComponentCategory
category)
98
{
99
using
Category =
ComponentCategory
;
100
switch
(category) {
101
case
Category::Gates:
return
"Gates"
;
102
case
Category::Multiplexers:
return
"Multiplexers"
;
103
case
Category::Arithmetic:
return
"Arithmetic"
;
104
case
Category::FlipFlops:
return
"Flip Flops"
;
105
case
Category::Register:
return
"Register"
;
106
case
Category::Memory:
return
"Memory"
;
107
case
Category::Subcircuits:
return
"Subcircuits"
;
108
case
Category::Inputs:
return
"Inputs"
;
109
case
Category::Outputs:
return
"Outputs"
;
110
case
Category::Utils:
return
"Utils"
;
111
}
112
std::unreachable();
113
}
114
115
}
// namespace SILICON::core
SILICON::core::ComponentRegistry::ComponentRegistry
ComponentRegistry()
SILICON::core::ComponentRegistry::Factory
std::function< Component_ptr()> Factory
Definition
component_registry.hpp:37
SILICON::core::ComponentRegistry::types
std::map< std::string, Entry, std::less<> > types
Definition
component_registry.hpp:56
SILICON::core::ComponentRegistry::empty
static ComponentRegistry empty()
Definition
component_registry.cpp:29
SILICON::core::ComponentRegistry::create
Component_ptr create(std::string_view type) const
Definition
component_registry.cpp:64
SILICON::core::ComponentRegistry::metadata
ComponentMetadata metadata(std::string_view type) const
Definition
component_registry.cpp:78
SILICON::core::ComponentRegistry::instance
static ComponentRegistry & instance()
Definition
component_registry.cpp:34
SILICON::core::ComponentRegistry::availableTypes
std::vector< std::string > availableTypes() const
Definition
component_registry.cpp:87
SILICON::core::ComponentRegistry::registerType
void registerType(std::string type, Factory factory)
Definition
component_registry.cpp:49
SILICON::core::ComponentRegistry::ComponentMetadata
SILICON::core::ComponentMetadata ComponentMetadata
Definition
component_registry.hpp:39
SILICON::core::ComponentRegistry::hasType
bool hasType(std::string_view type) const
Definition
component_registry.cpp:73
component.hpp
component_registry.hpp
SILICON::core
Definition
activeKeyGuard.hpp:28
SILICON::core::Component_ptr
std::shared_ptr< Component > Component_ptr
Definition
component_registry.hpp:33
SILICON::core::ComponentCategory
ComponentCategory
Definition
component.hpp:47
SILICON::core::componentCategoryName
std::string_view componentCategoryName(const ComponentCategory category)
Definition
component_registry.cpp:97
SILICON::core::ComponentMetadata
Definition
component.hpp:72
SILICON::core::ComponentMetadata::description
std::string description
Definition
component.hpp:74
SILICON::core::ComponentMetadata::displayName
std::string displayName
Definition
component.hpp:73
SILICON::core::ComponentRegistry::Entry
Definition
component_registry.hpp:52
src
core
serialization
component_registry.cpp
Generated by
1.17.0