|
| 1 | +#ifndef CMCPP_MONOSTATE_HPP |
| 2 | +#define CMCPP_MONOSTATE_HPP |
| 3 | + |
| 4 | +#include "context.hpp" |
| 5 | +#include "traits.hpp" |
| 6 | +#include "util.hpp" |
| 7 | + |
| 8 | +namespace cmcpp |
| 9 | +{ |
| 10 | + // Monostate (unit type for variant cases without payload) --------------- |
| 11 | + template <typename T> |
| 12 | + requires std::is_same_v<T, std::monostate> |
| 13 | + inline void store(LiftLowerContext &cx, const T &v, uint32_t ptr) |
| 14 | + { |
| 15 | + // Monostate has no data to store (size = 0) |
| 16 | + } |
| 17 | + |
| 18 | + template <typename T> |
| 19 | + requires std::is_same_v<T, std::monostate> |
| 20 | + inline T load(const LiftLowerContext &cx, uint32_t ptr) |
| 21 | + { |
| 22 | + // Monostate has no data to load (size = 0) |
| 23 | + return T{}; |
| 24 | + } |
| 25 | + |
| 26 | + template <typename T> |
| 27 | + requires std::is_same_v<T, std::monostate> |
| 28 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
| 29 | + { |
| 30 | + // Monostate has no flat representation (empty vector) |
| 31 | + return {}; |
| 32 | + } |
| 33 | + |
| 34 | + template <typename T> |
| 35 | + requires std::is_same_v<T, std::monostate> |
| 36 | + inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
| 37 | + { |
| 38 | + // Monostate has no data to lift |
| 39 | + return T{}; |
| 40 | + } |
| 41 | + |
| 42 | + // Result-specific monostates (for result<_, _> edge cases) -------------- |
| 43 | + template <typename T> |
| 44 | + requires std::is_same_v<T, result_ok_monostate> || std::is_same_v<T, result_err_monostate> |
| 45 | + inline void store(LiftLowerContext &cx, const T &v, uint32_t ptr) |
| 46 | + { |
| 47 | + // Result monostates have no data to store (size = 0) |
| 48 | + } |
| 49 | + |
| 50 | + template <typename T> |
| 51 | + requires std::is_same_v<T, result_ok_monostate> || std::is_same_v<T, result_err_monostate> |
| 52 | + inline T load(const LiftLowerContext &cx, uint32_t ptr) |
| 53 | + { |
| 54 | + // Result monostates have no data to load (size = 0) |
| 55 | + return T{}; |
| 56 | + } |
| 57 | + |
| 58 | + template <typename T> |
| 59 | + requires std::is_same_v<T, result_ok_monostate> || std::is_same_v<T, result_err_monostate> |
| 60 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const T &v) |
| 61 | + { |
| 62 | + // Result monostates have no flat representation (empty vector) |
| 63 | + return {}; |
| 64 | + } |
| 65 | + |
| 66 | + template <typename T> |
| 67 | + requires std::is_same_v<T, result_ok_monostate> || std::is_same_v<T, result_err_monostate> |
| 68 | + inline T lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
| 69 | + { |
| 70 | + // Result monostates have no data to lift |
| 71 | + return T{}; |
| 72 | + } |
| 73 | + |
| 74 | + // Result wrappers (for result<T, T> edge cases) ------------------------- |
| 75 | + template <typename T> |
| 76 | + inline void store(LiftLowerContext &cx, const result_ok_wrapper<T> &v, uint32_t ptr) |
| 77 | + { |
| 78 | + store(cx, v.value, ptr); |
| 79 | + } |
| 80 | + |
| 81 | + template <typename T> |
| 82 | + inline void store(LiftLowerContext &cx, const result_err_wrapper<T> &v, uint32_t ptr) |
| 83 | + { |
| 84 | + store(cx, v.value, ptr); |
| 85 | + } |
| 86 | + |
| 87 | + template <typename T> |
| 88 | + inline result_ok_wrapper<T> load(const LiftLowerContext &cx, uint32_t ptr) |
| 89 | + { |
| 90 | + return result_ok_wrapper<T>{load<T>(cx, ptr)}; |
| 91 | + } |
| 92 | + |
| 93 | + template <typename T> |
| 94 | + inline result_err_wrapper<T> load(const LiftLowerContext &cx, uint32_t ptr) |
| 95 | + { |
| 96 | + return result_err_wrapper<T>{load<T>(cx, ptr)}; |
| 97 | + } |
| 98 | + |
| 99 | + template <typename T> |
| 100 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const result_ok_wrapper<T> &v) |
| 101 | + { |
| 102 | + return lower_flat(cx, v.value); |
| 103 | + } |
| 104 | + |
| 105 | + template <typename T> |
| 106 | + inline WasmValVector lower_flat(LiftLowerContext &cx, const result_err_wrapper<T> &v) |
| 107 | + { |
| 108 | + return lower_flat(cx, v.value); |
| 109 | + } |
| 110 | + |
| 111 | + template <typename T> |
| 112 | + inline result_ok_wrapper<T> lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
| 113 | + { |
| 114 | + return result_ok_wrapper<T>{lift_flat<T>(cx, vi)}; |
| 115 | + } |
| 116 | + |
| 117 | + template <typename T> |
| 118 | + inline result_err_wrapper<T> lift_flat(const LiftLowerContext &cx, const CoreValueIter &vi) |
| 119 | + { |
| 120 | + return result_err_wrapper<T>{lift_flat<T>(cx, vi)}; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// std::tuple_size and std::tuple_element specializations for result wrappers |
| 125 | +// These are needed for std::apply to work with result_ok_wrapper/result_err_wrapper |
| 126 | +// The wrappers act as single-element tuples containing their wrapped value |
| 127 | +namespace std |
| 128 | +{ |
| 129 | + template <typename T> |
| 130 | + struct tuple_size<cmcpp::result_ok_wrapper<T>> : std::integral_constant<std::size_t, std::tuple_size_v<T>> |
| 131 | + { |
| 132 | + }; |
| 133 | + |
| 134 | + template <std::size_t I, typename T> |
| 135 | + struct tuple_element<I, cmcpp::result_ok_wrapper<T>> |
| 136 | + { |
| 137 | + using type = std::tuple_element_t<I, T>; |
| 138 | + }; |
| 139 | + |
| 140 | + template <typename T> |
| 141 | + struct tuple_size<cmcpp::result_err_wrapper<T>> : std::integral_constant<std::size_t, std::tuple_size_v<T>> |
| 142 | + { |
| 143 | + }; |
| 144 | + |
| 145 | + template <std::size_t I, typename T> |
| 146 | + struct tuple_element<I, cmcpp::result_err_wrapper<T>> |
| 147 | + { |
| 148 | + using type = std::tuple_element_t<I, T>; |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +// std::get specializations for result wrappers to support structured bindings and std::apply |
| 153 | +// Forward get<I> calls to the wrapped tuple |
| 154 | +namespace std |
| 155 | +{ |
| 156 | + template <std::size_t I, typename T> |
| 157 | + constexpr decltype(auto) get(cmcpp::result_ok_wrapper<T> &wrapper) noexcept |
| 158 | + { |
| 159 | + return std::get<I>(wrapper.value); |
| 160 | + } |
| 161 | + |
| 162 | + template <std::size_t I, typename T> |
| 163 | + constexpr decltype(auto) get(const cmcpp::result_ok_wrapper<T> &wrapper) noexcept |
| 164 | + { |
| 165 | + return std::get<I>(wrapper.value); |
| 166 | + } |
| 167 | + |
| 168 | + template <std::size_t I, typename T> |
| 169 | + constexpr decltype(auto) get(cmcpp::result_ok_wrapper<T> &&wrapper) noexcept |
| 170 | + { |
| 171 | + return std::get<I>(std::move(wrapper.value)); |
| 172 | + } |
| 173 | + |
| 174 | + template <std::size_t I, typename T> |
| 175 | + constexpr decltype(auto) get(const cmcpp::result_ok_wrapper<T> &&wrapper) noexcept |
| 176 | + { |
| 177 | + return std::get<I>(std::move(wrapper.value)); |
| 178 | + } |
| 179 | + |
| 180 | + template <std::size_t I, typename T> |
| 181 | + constexpr decltype(auto) get(cmcpp::result_err_wrapper<T> &wrapper) noexcept |
| 182 | + { |
| 183 | + return std::get<I>(wrapper.value); |
| 184 | + } |
| 185 | + |
| 186 | + template <std::size_t I, typename T> |
| 187 | + constexpr decltype(auto) get(const cmcpp::result_err_wrapper<T> &wrapper) noexcept |
| 188 | + { |
| 189 | + return std::get<I>(wrapper.value); |
| 190 | + } |
| 191 | + |
| 192 | + template <std::size_t I, typename T> |
| 193 | + constexpr decltype(auto) get(cmcpp::result_err_wrapper<T> &&wrapper) noexcept |
| 194 | + { |
| 195 | + return std::get<I>(std::move(wrapper.value)); |
| 196 | + } |
| 197 | + |
| 198 | + template <std::size_t I, typename T> |
| 199 | + constexpr decltype(auto) get(const cmcpp::result_err_wrapper<T> &&wrapper) noexcept |
| 200 | + { |
| 201 | + return std::get<I>(std::move(wrapper.value)); |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +#endif // CMCPP_MONOSTATE_HPP |
0 commit comments