SILICON
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>
20
21#include <algorithm>
22#include <stdexcept>
23#include <utility>
24
25namespace SILICON::core {
26
28
33
35{
36 static ComponentRegistry registry;
37 return registry;
38}
39
40namespace {
41
42bool isValidMetadata(const ComponentMetadata& metadata)
43{
44 return !metadata.displayName.empty() && !metadata.description.empty();
45}
46
47} // namespace
48
49void 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
64Component_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
73bool ComponentRegistry::hasType(std::string_view type) const
74{
75 return types.contains(type);
76}
77
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
87std::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
97std::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
std::function< Component_ptr()> Factory
std::map< std::string, Entry, std::less<> > types
static ComponentRegistry empty()
Component_ptr create(std::string_view type) const
ComponentMetadata metadata(std::string_view type) const
static ComponentRegistry & instance()
std::vector< std::string > availableTypes() const
void registerType(std::string type, Factory factory)
SILICON::core::ComponentMetadata ComponentMetadata
bool hasType(std::string_view type) const
std::shared_ptr< Component > Component_ptr
std::string_view componentCategoryName(const ComponentCategory category)