MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ComputationSignature.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::Yantra {
4
5template <typename E>
6 requires std::is_enum_v<E>
8
9namespace detail {
10
12
13 template <typename E>
14 concept LocalComputationEnum = std::is_enum_v<E>
15 && requires(E value) {
16 computation_enum(value);
17 };
18
19 template <typename Left, typename Right>
20 concept ComposableEnums = std::is_enum_v<Left>
21 && std::is_enum_v<Right>
22 && (std::is_scoped_enum_v<Left>
23 || std::is_scoped_enum_v<Right>
25
26}
27
28/** @brief Ordered operation selections, preserving each enum's type and value. */
30public:
32
33 template <typename E>
34 requires std::is_enum_v<E>
36 {
37 static_assert(sizeof(std::underlying_type_t<E>) <= sizeof(uint64_t));
38 m_selections.push_back({ enum_identity<E>(), static_cast<uint64_t>(selection) });
39 }
40
41 [[nodiscard]] bool empty() const { return m_selections.empty(); }
42 [[nodiscard]] size_t size() const { return m_selections.size(); }
43
44 /** @brief The single selection at this stage, retaining its enum identity. */
45 [[nodiscard]] ComputationSignature at(size_t index) const
46 {
48 result.m_selections.push_back(m_selections.at(index));
49 return result;
50 }
51
52 /** @brief Read a selection when its enum type matches E. */
53 template <typename E>
54 requires std::is_enum_v<E>
55 [[nodiscard]] std::optional<E> selection(size_t index) const
56 {
57 if (index >= size() || m_selections[index].type != enum_identity<E>()) {
58 return std::nullopt;
59 }
60 return static_cast<E>(m_selections[index].value);
61 }
62
63 bool operator==(const ComputationSignature&) const = default;
64
65 /** @brief Whether the signature begins with all selections in prefix. */
66 [[nodiscard]] bool starts_with(const ComputationSignature& prefix) const
67 {
68 return prefix.size() <= size()
69 && std::equal(prefix.m_selections.begin(), prefix.m_selections.end(), m_selections.begin());
70 }
71
72 /** @brief Remaining selections after a resolved prefix. */
73 [[nodiscard]] ComputationSignature after(size_t count) const
74 {
76 const auto offset = std::min(count, size());
77 result.m_selections.assign(m_selections.begin() + offset, m_selections.end());
78 return result;
79 }
80
82 {
83 left.m_selections.insert(left.m_selections.end(), right.m_selections.begin(), right.m_selections.end());
84 return left;
85 }
86
87private:
88 struct Selection {
89 const void* type;
90 uint64_t value;
91
92 bool operator==(const Selection&) const = default;
93 };
94
95 template <typename E>
96 static const void* enum_identity()
97 {
98 static const char identity {};
99 return &identity;
100 }
101
102 std::vector<Selection> m_selections;
103};
104
105/** @brief Compose enum selections in execution order without combining their bits. */
106template <typename Left, typename Right>
108ComputationSignature operator|(Left left, Right right)
109{
110 return ComputationSignature(left) | ComputationSignature(right);
111}
112
113}
friend ComputationSignature operator|(ComputationSignature left, const ComputationSignature &right)
std::optional< E > selection(size_t index) const
Read a selection when its enum type matches E.
bool starts_with(const ComputationSignature &prefix) const
Whether the signature begins with all selections in prefix.
ComputationSignature at(size_t index) const
The single selection at this stage, retaining its enum identity.
ComputationSignature after(size_t count) const
Remaining selections after a resolved prefix.
bool operator==(const ComputationSignature &) const =default
Ordered operation selections, preserving each enum's type and value.
void computation_enum(E)
ComputationSignature operator|(Left left, Right right)
Compose enum selections in execution order without combining their bits.
bool operator==(const Selection &) const =default