SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
circuitDocument.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 "
circuitDocument.hpp
"
19
20
#include <limits>
21
#include <memory>
22
#include <ranges>
23
#include <stdexcept>
24
#include <string>
25
#include <utility>
26
#include <vector>
27
28
#include <nlohmann/json.hpp>
29
30
#include <
core/component.hpp
>
31
#include <
core/serialization/component_registry.hpp
>
32
33
namespace
SILICON::core
{
34
36
uint64_t
deserializePortId
(
const
nlohmann::json& value)
37
{
38
if
(!value.is_number_unsigned())
39
throw
std::runtime_error(
"Port identity must be an unsigned integer"
);
40
const
auto
id
= value.get<uint64_t>();
41
if
(
id
== 0 ||
id
== std::numeric_limits<uint64_t>::max())
42
throw
std::runtime_error(
"Port identity must be a valid nonzero integer"
);
43
return
id;
44
}
45
46
uint64_t
deserializeBoundaryPortId
(
const
nlohmann::json& component)
47
{
48
if
(
const
auto
id
= component.find(
"portId"
);
id
!= component.end())
49
return
deserializePortId
(*
id
);
50
51
// main did not serialize port IDs. Derive one from the persisted vertex until
52
// the first save, when it becomes a stable explicit identity.
53
const
auto
& vertex = component.at(
"id"
);
54
if
(!vertex.is_number_unsigned())
55
throw
std::runtime_error(
"Invalid legacy boundary vertex"
);
56
const
auto
number = vertex.get<uint64_t>();
57
if
(number >= std::numeric_limits<uint64_t>::max() - 1)
58
throw
std::runtime_error(
"Legacy boundary vertex exceeds the supported range"
);
59
return
number + 1;
60
}
61
62
Circuit
deserializeCircuitDocument
(
const
std::string_view contents,
63
const
ComponentRegistry
& registry,
64
const
CircuitResolver
* resolver)
65
{
66
auto
document = nlohmann::json::parse(contents);
67
if
(
const
auto
circuit = document.find(
"circuit"
); circuit != document.end())
68
document = *circuit;
69
if
(!document.is_object())
70
throw
std::runtime_error(
"Circuit document must contain a circuit object"
);
71
72
return
Circuit::deserialize
(document.dump(), registry, resolver);
73
}
74
75
SubcircuitDefinition
parseCircuitDocument
(
const
std::string_view contents,
76
const
ComponentRegistry
& registry,
77
const
CircuitResolver
* resolver)
78
{
79
auto
circuit =
deserializeCircuitDocument
(contents, registry, resolver);
80
auto
inputs = circuit.getInputPorts();
81
auto
outputs = circuit.getOutputPorts();
82
83
std::vector<Component_ptr> boundaryComponents;
84
for
(
const
auto
& [component, _vertex] : circuit.getComponentToVertex()) {
85
if
(component && component->metadata().portRole !=
PortRole::None
)
86
boundaryComponents.push_back(circuit.getComponentByVertexId(_vertex));
87
}
88
for
(
const
auto
& component : boundaryComponents)
89
circuit.removeComponent(component);
90
91
return
{.circuit = std::move(circuit),
92
.inputs = std::move(inputs),
93
.outputs = std::move(outputs)};
94
}
95
96
}
// namespace SILICON::core
circuitDocument.hpp
SILICON::core::CircuitResolver
Definition
circuitDocument.hpp:52
SILICON::core::Circuit
Represents a digital circuit as a directed graph of components.
Definition
circuit.hpp:125
SILICON::core::Circuit::deserialize
static Circuit deserialize(const std::string &jsonStr, const ComponentRegistry ®, const CircuitResolver *resolver=nullptr)
Deserializes the core circuit model from a JSON string.
Definition
circuit.cpp:749
SILICON::core::ComponentRegistry
Definition
component_registry.hpp:35
component.hpp
component_registry.hpp
SILICON::core
Definition
activeKeyGuard.hpp:28
SILICON::core::parseCircuitDocument
SubcircuitDefinition parseCircuitDocument(const std::string_view contents, const ComponentRegistry ®istry, const CircuitResolver *resolver)
Definition
circuitDocument.cpp:75
SILICON::core::deserializeCircuitDocument
Circuit deserializeCircuitDocument(const std::string_view contents, const ComponentRegistry ®istry, const CircuitResolver *resolver)
Definition
circuitDocument.cpp:62
SILICON::core::deserializePortId
uint64_t deserializePortId(const nlohmann::json &value)
Definition
circuitDocument.cpp:36
SILICON::core::deserializeBoundaryPortId
uint64_t deserializeBoundaryPortId(const nlohmann::json &component)
Definition
circuitDocument.cpp:46
SILICON::core::PortRole::None
@ None
Definition
component.hpp:67
SILICON::core::SubcircuitDefinition
Definition
circuitDocument.hpp:39
src
core
circuitDocument.cpp
Generated by
1.17.0