#pragma once #include "gp_config.hpp" #include #include "gp/allocator/dummy.hpp" #include "gp/exception.hpp" #include "gp/function.hpp" #include "gp/memory.hpp" #include #include namespace gp{ template class fixed_variant final { std::size_t index = std::numeric_limits::max(); char buffer[max_size()]; gp::function cpytor = [](void*, void*){}; gp::function mvtor = [](void*, void*){}; gp::function dtor = [](void*){}; static_assert(all_of_fixed_size::value, "not fixed"); public: template//, typename std::enable_if::value,int>::type> constexpr fixed_variant(U& value) : index{r_index_of::value} { new(buffer) U(value); dtor = gp::function([](void* thing){((U*)thing)->~U();}); cpytor = gp::function([](void* src, void* dest){new(dest) U(*(U*)src);}); mvtor = gp::function([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}); } template//, typename std::enable_if::value,int>::type> constexpr fixed_variant(U&& value) : index{r_index_of::value} { new(buffer) U(std::move(value)); dtor = gp::function([](void* thing){((U*)thing)->~U();}); cpytor = gp::function([](void* src, void* dest){new(dest) U(*(U*)src);}); mvtor = gp::function([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}); } constexpr fixed_variant(fixed_variant& oth) : index{oth.index} , dtor{oth.dtor} , cpytor{oth.cpytor} , mvtor{oth.mvtor} { cpytor(oth.buffer, buffer); } template constexpr size_t alt() const { return r_index_of::value; } size_t type() const { return index; } void operator=(fixed_variant& value) { if(index != std::numeric_limits::max()) { dtor((void*)buffer); } index = value.index; cpytor = value.cpytor; dtor = value.dtor; mvtor(value.buffer, buffer); } template::value,int>::type> void operator=(U& value) { if(index != std::numeric_limits::max()) { dtor((void*)buffer); } index = r_index_of::value; new(buffer) U(value); dtor = gp::function([](void* thing){((U*)thing)->~U();}); cpytor = gp::function([](void* src, void* dest){new(dest) U(*(U*)src);}); mvtor = gp::function([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}); } template::value,int>::type> void operator=(U&& value) { if(index != std::numeric_limits::max()) { dtor((void*)buffer); } index = r_index_of::value; new(buffer) U(std::move(value)); dtor = gp::function([](void* thing){((U*)thing)->~U();}); cpytor = gp::function([](void* src, void* dest){new(dest) U(*(U*)src);}); mvtor = gp::function([](void* src, void* dest){new(dest) U(gp::move(*(U*)src));}); } ~fixed_variant() { if(index != std::numeric_limits::max()) { dtor((void*)buffer); } } template constexpr U& value() { if constexpr (gp_config::has_exceptions) { if(r_index_of::value != index) { throw bad_variant_access{}; } } return *reinterpret_cast(buffer); } template constexpr U& is_a() { if(r_index_of::value == index) { return true; } else { return false; } } }; /*template class variant{ std::size_t index = std::numeric_limits::max(); void* ptr; gp::function dtor = [](void*){}; allocator_t allocator; public: template::value,int>::type> constexpr variant(U& value) : index{r_index_of::value} { ptr = (void*)new(allocator.allocate(sizeof(U))) U(value); dtor = gp::function([](void* thing){((U*)thing)->~U();}); // TODO:replae with delete(p,t) } template::value,int>::type> constexpr variant(U&& value) : index{r_index_of::value} { ptr = (void*)new(allocator.allocate(sizeof(U))) U(std::move(value)); dtor = gp::function([](void* thing){((U*)thing)->~U();}); // TODO:replae with delete(p,t) } template::value,int>::type> void operator=(U& value) { if(index != std::numeric_limits::max()) { dtor(ptr); allocator.deallocate(ptr); } index = r_index_of::value; ptr = (void*)new(allocator.allocate(sizeof(U))) U(value); dtor = gp::function([](void* thing){((U*)thing)->~U();}); // TODO:replae with delete(p,t) } template::value,int>::type> void operator=(U&& value) { if(index != std::numeric_limits::max()) { dtor(ptr); allocator.deallocate(ptr); } index = r_index_of::value; ptr = (void*)new(allocator.allocate(sizeof(U))) U(std::move(value)); dtor = gp::function([](void* thing){((U*)thing)->~U();}); // TODO: replace with delete(p, t) } ~variant() { if(index != std::numeric_limits::max()) { dtor(ptr); allocator.deallocate(ptr); } } template constexpr U& value() { if constexpr (gp_config::has_exceptions) { if(r_index_of::value != index) { throw bad_variant_access{}; } } return *reinterpret_cast(ptr); } template constexpr U& is_a() { if(r_index_of::value == index) { return true; } else { return false; } } };*/ }