~maelkum/viuavm

704c33ff9f9b991563684be9a1ff52d8ab4f661b — Marek Marecki 11 days ago 5091096
Fix Cell_view

It now correctly supports all types that a Cell may hold and does not
create void views of non-void cells.
1 files changed, 20 insertions(+), 9 deletions(-)

M new/src/vm/types.cpp
M new/src/vm/types.cpp => new/src/vm/types.cpp +20 -9
@@ 1,21 1,32 @@
#include <sstream>
#include <string>

#include <viua/support/variant.h>
#include <viua/vm/types.h>


namespace viua::vm::types {
Cell_view::Cell_view(Cell& cell) : content{std::monostate{}}
{
    if (std::holds_alternative<int64_t>(cell.content)) {
        content =
            std::reference_wrapper<int64_t>{std::get<int64_t>(cell.content)};
    } else if (std::holds_alternative<Cell::boxed_type>(cell.content)) {
        content = std::reference_wrapper<boxed_type>{
            *std::get<Cell::boxed_type>(cell.content)};
    } else {
        content = std::monostate{};
    }
    std::visit([this](auto&& arg) -> void
    {
        using T = std::decay_t<decltype(arg)>;
        if constexpr (std::is_same_v<T, std::monostate>) {
            // do nothing
        } else if constexpr (std::is_same_v<T, int64_t>) {
            content = std::reference_wrapper<int64_t>(arg);
        } else if constexpr (std::is_same_v<T, uint64_t>) {
            content = std::reference_wrapper<uint64_t>(arg);
        } else if constexpr (std::is_same_v<T, float>) {
            content = std::reference_wrapper<float>(arg);
        } else if constexpr (std::is_same_v<T, double>) {
            content = std::reference_wrapper<double>(arg);
        } else if constexpr (std::is_same_v<T, Cell::boxed_type>) {
            content = std::reference_wrapper<boxed_type>(*arg.get());
        } else {
            viua::support::non_exhaustive_visitor<T>();
        }
    }, cell.content);
}
Cell_view::Cell_view(boxed_type& v) : content{v}
{}