#pragma once /* * Copyright (C) 2024 Brett Terpstra * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLT_STD_VARIANT_H #define BLT_STD_VARIANT_H #include #include #include #include #include #include #include #include #include namespace blt { // TODO rewrite all of the metaprogramming related to this + the meta programming lib template class variant_t; namespace detail { template struct member_func_meta { using can_invoke = std::is_invocable; using return_type = typename std::conditional_t, meta::passthrough>::type ; }; template struct member_call_return_type; template struct member_call_return_type, Types...> { using result_types = std::tuple...>; using non_void_result_types = meta::filter_void_t::return_type...>; static constexpr bool all_void = std::tuple_size_v == 0; static constexpr bool some_void = std::tuple_size_v != sizeof...(Types); using first_return = std::conditional_t>; using return_type = std::conditional_t, first_return>>; }; template struct visit_func_meta { using result_tuple = std::tuple, std::invoke_result, meta::passthrough>::type...>; using non_void_results = meta::filter_void_t; using return_type = std::tuple_element_t< 0, std::conditional_t == 0, std::tuple, non_void_results>>; }; template struct visit_return_type; template struct visit_return_type, std::tuple> { using return_tuple = std::tuple::return_type...>; using non_void_returns = meta::unique_tuple_t>; using first_return = std::tuple_element_t< 0, std::conditional_t == 0, std::tuple, non_void_returns>>; static constexpr bool all_void = std::tuple_size_v == 0; static constexpr bool some_void = std::tuple_size_v != std::tuple_size_v; using same_type = meta::filter_func_t, non_void_returns>; static constexpr bool all_same = std::tuple_size_v == std::tuple_size_v; using variant_type = typename meta::apply_tuple::type; using base_type = std::conditional_t; using return_type = std::conditional_t, base_type>>; }; } /* * std::visit(blt::lambda_visitor{ * lambdas... * }, data_variant); */ template struct lambda_visitor : TLambdas... { using TLambdas::operator()...; }; #if __cplusplus < 202002L // explicit deduction guide (not needed as of C++20) template lambda_visitor(TLambdas...) -> lambda_visitor; #endif template class variant_t { public: using value_type = std::variant; size_t variant_size = sizeof...(Types); constexpr variant_t(): m_variant() {} constexpr variant_t(const variant_t& variant) noexcept(std::is_nothrow_copy_constructible_v): m_variant(variant.m_variant) {} constexpr variant_t(variant_t&& variant) noexcept(std::is_nothrow_move_constructible_v): m_variant(std::move(variant.m_variant)) {} explicit constexpr variant_t(const value_type& variant) noexcept(std::is_nothrow_copy_constructible_v): m_variant(variant) {} explicit constexpr variant_t(value_type&& variant) noexcept(std::is_nothrow_move_constructible_v): m_variant(std::move(variant)) {} explicit constexpr variant_t(Types&&... args) noexcept(std::is_nothrow_constructible_v): m_variant( std::forward(args)...) {} template explicit constexpr variant_t(std::in_place_type_t, C_Args&&... args): m_variant(std::in_place_type, std::forward(args)...) {} template constexpr explicit variant_t(std::in_place_type_t, std::initializer_list il, C_Args&&... args): m_variant( std::in_place_type, il, std::forward(args)...) {} template explicit constexpr variant_t(std::in_place_index_t, C_Args&&... args): m_variant(std::in_place_index, std::forward(args)...) {} template constexpr explicit variant_t(std::in_place_index_t, std::initializer_list il, C_Args&&... args): m_variant( std::in_place_index, il, std::forward(args)...) {} template T& emplace(Args&&... args) { return m_variant.template emplace(std::forward(args)...); } template T& emplace(std::initializer_list il, Args&&... args) { return m_variant.template emplace(il, std::forward(args)...); } template std::variant_alternative_t& emplace(Args&&... args) { return m_variant.template emplace(std::forward(args)...); } template std::variant_alternative_t& emplace(std::initializer_list il, Args&&... args) { return m_variant.template emplace(il, std::forward(args)...); } [[nodiscard]] constexpr std::size_t index() const noexcept { return m_variant.index(); } [[nodiscard]] constexpr bool valueless_by_exception() const noexcept { return m_variant.valueless_by_exception(); } template static constexpr auto make_visitor(Visitee&&... visitees) { // TODO: this is probably not the best way to handle these cases... using meta_t = detail::visit_return_type, std::tuple>; if constexpr (meta_t::all_same) { if constexpr (meta_t::some_void) { return lambda_visitor{ [&](std::tuple_element_t<0, typename meta::function_like::args_tuple> value) { if constexpr (std::is_void_v::return_type>) { std::forward(visitees)(std::forward(value)); return typename meta_t::return_type{}; } else { return typename meta_t::return_type(std::forward(visitees)(std::forward(value))); } }... }; } else { return lambda_visitor{std::forward(visitees)...}; } } else { if constexpr (meta_t::some_void) { return lambda_visitor{ [&](std::tuple_element_t<0, typename meta::function_like::args_tuple> value) { if constexpr (std::is_void_v::return_type>) { std::forward(visitees)(std::forward(value)); return typename meta_t::return_type{}; } else { return typename meta_t::return_type( typename meta_t::base_type(std::forward(visitees)(std::forward(value)))); } }... }; } else { return lambda_visitor{ [&](std::tuple_element_t<0, typename meta::function_like::args_tuple> value) { return typename meta_t::return_type{std::forward(visitees)(std::forward(value))}; }... }; } } } /** * Automatic visitor generation * @param visitees user lambdas */ template constexpr auto visit(Visitee&&... visitees) -> decltype(auto) { return std::visit(make_visitor(std::forward(visitees)...), m_variant); } /** * Automatic visitor generation * @param visitees user lambdas */ template constexpr auto visit(Visitee&&... visitees) const -> decltype(auto) { return std::visit(make_visitor(std::forward(visitees)...), m_variant); } template constexpr auto visit_value(Default&& default_value, Visitee&&... visitees) -> decltype(auto) { return visit(std::forward(visitees)..., [default_value=std::forward(default_value)](auto&&) { return std::forward(default_value); }); } template constexpr auto call_member(const MemberFunc func, Args&&... args) -> decltype(auto) { using meta = detail::member_call_return_type, Types...>; return visit([&](auto&& value) -> typename meta::return_type { if constexpr (std::is_invocable_v) return ((value).*(func))(std::forward(args)...); else return {}; }); } template [[nodiscard]] constexpr bool has_index() const noexcept { return m_variant.index() == I; } template [[nodiscard]] constexpr bool has_type() const noexcept { return std::holds_alternative(m_variant); } template [[nodiscard]] constexpr auto get() -> decltype(auto) { return std::get(m_variant); } template [[nodiscard]] constexpr auto get() const -> decltype(auto) { return std::get(m_variant); } template [[nodiscard]] constexpr auto get() -> decltype(auto) { return std::get(m_variant); } template [[nodiscard]] constexpr auto get() const -> decltype(auto) { return std::get(m_variant); } template constexpr std::add_pointer_t> get_if() noexcept { return std::get_if(m_variant); } template constexpr std::add_pointer_t> get_if() noexcept { return std::get_if(m_variant); } template constexpr std::add_pointer_t get_if() noexcept { return std::get_if(m_variant); } template constexpr std::add_pointer_t get_if() noexcept { return std::get_if(m_variant); } template constexpr T value_or(T&& t) const { if (has_type()) return get(); return std::forward(t); } template constexpr std::variant_alternative_t value_or(const std::variant_alternative_t& t) const { if (has_type>()) return get(); return t; } template constexpr std::variant_alternative_t value_or(std::variant_alternative_t&& t) const { if (has_type>()) return get(); return t; } template constexpr const value_type& variant() const { return m_variant; } constexpr value_type& variant() { return m_variant; } [[nodiscard]] constexpr size_t size() const { return variant_size; } friend bool operator==(const variant_t& lhs, const variant_t& rhs) { return lhs.m_variant == rhs.m_variant; } friend bool operator!=(const variant_t& lhs, const variant_t& rhs) { return lhs.m_variant != rhs.m_variant; } friend bool operator<(const variant_t& lhs, const variant_t& rhs) { return lhs.m_variant(const variant_t& lhs, const variant_t& rhs) { return lhs.m_variant > rhs.m_variant; } friend bool operator<=(const variant_t& lhs, const variant_t& rhs) { return lhs.m_variant <= rhs.m_variant; } friend bool operator>=(const variant_t& lhs, const variant_t& rhs) { return lhs.m_variant >= rhs.m_variant; } private: value_type m_variant; }; namespace detail { template class variant_is_base_of {}; template class variant_is_base_of> { public: using value_type = bool; template static constexpr bool value = std::conjunction_v...>; }; template class variant_is_base_of> { public: using value_type = bool; template static constexpr bool value = std::conjunction_v...>; }; template static constexpr bool variant_is_base_of_v = variant_is_base_of::value; } } #endif //BLT_STD_VARIANT_H