wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Enums.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#include "App/Core/Enums.h"
5
6#include <QDataStream>
7
8namespace {
9
10// Single-source-of-truth morph cycle table.
11// nextElmType() follows From→To; prevElmType() follows To→From (inverse lookup).
12// Each group is a closed cycle; types in different groups do NOT cross-cycle.
13struct MorphStep { ElementType from; ElementType to; };
14constexpr MorphStep kMorphCycle[] = {
15 // Not / Node (symmetric pair)
16 { ElementType::Not, ElementType::Node },
17 { ElementType::Node, ElementType::Not },
18
19 // Gates
20 { ElementType::And, ElementType::Or },
21 { ElementType::Or, ElementType::Nand },
22 { ElementType::Nand, ElementType::Nor },
23 { ElementType::Nor, ElementType::Xor },
24 { ElementType::Xor, ElementType::Xnor },
25 { ElementType::Xnor, ElementType::And },
26
27 // Inputs
28 { ElementType::InputVcc, ElementType::InputGnd },
29 { ElementType::InputGnd, ElementType::InputButton },
30 { ElementType::InputButton, ElementType::InputSwitch },
31 { ElementType::InputSwitch, ElementType::InputRotary },
32 { ElementType::InputRotary, ElementType::Clock },
33 { ElementType::Clock, ElementType::InputVcc },
34
35 // Flip-flops (symmetric pairs)
36 { ElementType::DFlipFlop, ElementType::TFlipFlop },
37 { ElementType::TFlipFlop, ElementType::DFlipFlop },
38 { ElementType::JKFlipFlop, ElementType::SRFlipFlop },
39 { ElementType::SRFlipFlop, ElementType::JKFlipFlop },
40
41 // Outputs
42 { ElementType::Led, ElementType::Buzzer },
43 { ElementType::Buzzer, ElementType::AudioBox },
44 { ElementType::AudioBox, ElementType::Led },
45};
46
47} // namespace
48
50{
51 for (const auto &step : kMorphCycle) {
52 if (step.from == type) return step.to;
53 }
54 return ElementType::Unknown;
55}
56
58{
59 for (const auto &step : kMorphCycle) {
60 if (step.to == type) return step.from;
61 }
62 return ElementType::Unknown;
63}
64
65// Pre-increment used to iterate over all enum values sequentially (e.g. in loops
66// that walk the full ElementType range); does not wrap around.
68{
69 return type = static_cast<ElementType>(static_cast<int>(type) + 1);
70}
71
72// Serialise as quint64 so the on-disk representation is stable even if the enum
73// underlying type changes or new values are inserted in future versions.
74QDataStream &operator>>(QDataStream &stream, ElementType &type)
75{
76 // Read into a temporary so that a partially-read stream doesn't leave 'type'
77 // in an indeterminate state if the cast would produce an out-of-range value
78 quint64 temp; stream >> temp;
79 type = static_cast<ElementType>(temp);
80 return stream;
81}
82
83QDataStream &operator<<(QDataStream &stream, const ElementType &type)
84{
85 stream << static_cast<quint64>(type);
86 return stream;
87}
QDataStream & operator<<(QDataStream &stream, const ElementType &type)
Serializes type into stream.
Definition Enums.cpp:83
ElementType & operator++(ElementType &type)
Pre-increment operator; advances type to the next ElementType in sequence.
Definition Enums.cpp:67
QDataStream & operator>>(QDataStream &stream, ElementType &type)
Deserializes an ElementType from stream into type.
Definition Enums.cpp:74
Central enumeration types for element types, groups, and signal status.
Enums::ElementType ElementType
Definition Enums.h:107
ElementType
Numeric identifiers for every concrete element type.
Definition Enums.h:41
static ElementType nextElmType(ElementType type)
Returns the ElementType that follows type in the defined sequence.
Definition Enums.cpp:49
static ElementType prevElmType(ElementType type)
Returns the ElementType that precedes type in the defined sequence.
Definition Enums.cpp:57