diff --git a/include/etl/private/variant_variadic.h b/include/etl/private/variant_variadic.h index 44334669..6874f7f9 100644 --- a/include/etl/private/variant_variadic.h +++ b/include/etl/private/variant_variadic.h @@ -31,13 +31,14 @@ SOFTWARE. #include "../platform.h" #include "../utility.h" #include "../largest.h" +#include "../nth_type.h" #include "../exception.h" #include "../type_traits.h" #include "../integral_limits.h" #include "../static_assert.h" #include "../alignment.h" #include "../error_handler.h" -#include "../parameter_pack.h" +#include "../type_list.h" #include "../placement_new.h" #include "../visitor.h" #include "../memory.h" @@ -66,82 +67,6 @@ namespace etl { namespace private_variant { - //*************************************************************************** - // This is a copy of the normal etl::parameter_pack, but without the static_assert - // so that the C++11 versions of do_visitor() & do_operator() do not throw a compile time error. - //*************************************************************************** - template - class parameter_pack - { - public: - - static constexpr size_t size = sizeof...(TTypes); - - //*************************************************************************** - /// index_of_type - //*************************************************************************** - template - class index_of_type - { - private: - - using type = etl::remove_cvref_t; - - //*********************************** - template - struct index_of_type_helper - { - static constexpr size_t value = etl::is_same::value ? 1 : 1 + index_of_type_helper::value; - }; - - //*********************************** - template - struct index_of_type_helper - { - static constexpr size_t value = 1UL; - }; - - public: - - static_assert(etl::is_one_of::value, "T is not in parameter pack"); - - /// The index value. - static constexpr size_t value = index_of_type_helper::value - 1; - }; - - //*************************************************************************** - /// type_from_index - //*************************************************************************** - template - class type_from_index - { - private: - - //*********************************** - template - struct type_from_index_helper - { - using type = typename etl::conditional::type>::type; - }; - - //*********************************** - template - struct type_from_index_helper - { - using type = T1; - }; - - public: - - /// Template alias - using type = typename type_from_index_helper::type; - }; - - //*********************************** - template - using type_from_index_t = typename type_from_index::type; - }; - //******************************************* // The traits an object may have. //******************************************* @@ -327,7 +252,7 @@ namespace etl template struct variant_alternative> { - using type = typename etl::private_variant::parameter_pack::template type_from_index::type; + using type = nth_type_t; }; template @@ -529,13 +454,13 @@ namespace etl // Get the index of a type. //******************************************* template - using index_of_type = typename etl::private_variant::parameter_pack::template index_of_type>; + using index_of_type = etl::type_list_index_of_type, etl::remove_cvref_t>; //******************************************* // Get the type from the index. //******************************************* template - using type_from_index = typename etl::private_variant::parameter_pack::template type_from_index::type; + using type_from_index = typename etl::type_list_type_at_index, Index>::type; public: @@ -546,7 +471,7 @@ namespace etl #include "diagnostic_uninitialized_push.h" ETL_CONSTEXPR14 variant() { - using type = typename etl::private_variant::parameter_pack::template type_from_index<0U>::type; + using type = type_from_index<0U>; default_construct_in_place(data); operation = operation_type::value, etl::is_move_constructible::value>::do_operation; @@ -592,7 +517,7 @@ namespace etl ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t, TArgs&&... args) : type_id(Index) { - using type = typename private_variant::parameter_pack:: template type_from_index_t; + using type = type_from_index; static_assert(etl::is_one_of ::value, "Unsupported type"); construct_in_place_args(data, etl::forward(args)...); @@ -625,7 +550,7 @@ namespace etl ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t, std::initializer_list init, TArgs&&... args) : type_id(Index) { - using type = typename private_variant::parameter_pack:: template type_from_index_t; + using type = type_from_index; static_assert(etl::is_one_of ::value, "Unsupported type"); construct_in_place_args(data, init, etl::forward(args)...); @@ -715,7 +640,7 @@ namespace etl operation = operation_type::value, etl::is_move_constructible::value>::do_operation; - type_id = etl::private_variant::parameter_pack::template index_of_type::value; + type_id = index_of_type::value; return *static_cast(data); } @@ -737,7 +662,7 @@ namespace etl operation = operation_type::value, etl::is_move_constructible::value>::do_operation; - type_id = etl::private_variant::parameter_pack::template index_of_type::value; + type_id = index_of_type::value; return *static_cast(data); } @@ -747,9 +672,9 @@ namespace etl /// Emplace by index with variadic constructor parameters. //*************************************************************************** template - typename etl::variant_alternative>::type& emplace(TArgs&&... args) + typename etl::variant_alternative>::type& emplace(TArgs&&... args) { - static_assert(Index < etl::private_variant::parameter_pack::size, "Index out of range"); + static_assert(Index < sizeof...(TTypes), "Index out of range"); using type = type_from_index; @@ -769,9 +694,9 @@ namespace etl /// Emplace by index with variadic constructor parameters. //*************************************************************************** template - typename etl::variant_alternative>::type& emplace(std::initializer_list il, TArgs&&... args) + typename etl::variant_alternative>::type& emplace(std::initializer_list il, TArgs&&... args) { - static_assert(Index < etl::private_variant::parameter_pack::size, "Index out of range"); + static_assert(Index < sizeof...(TTypes), "Index out of range"); using type = type_from_index; @@ -803,7 +728,7 @@ namespace etl construct_in_place(data, etl::forward(value)); operation = operation_type::value, etl::is_move_constructible::value>::do_operation; - type_id = etl::private_variant::parameter_pack::template index_of_type::value; + type_id = index_of_type::value; return *this; } @@ -896,7 +821,7 @@ namespace etl template (), int> = 0> constexpr bool is_type() const noexcept { - return (type_id == etl::private_variant::parameter_pack::template index_of_type::value); + return (type_id == index_of_type::value); } //*************************************************************************** @@ -1469,7 +1394,7 @@ namespace etl template ETL_CONSTEXPR14 bool holds_alternative(const etl::variant& v) noexcept { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; return (Index == variant_npos) ? false : (v.index() == Index); } @@ -1560,7 +1485,7 @@ namespace etl template ETL_CONSTEXPR14 T& get(etl::variant& v) { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; return get(v); } @@ -1569,7 +1494,7 @@ namespace etl template ETL_CONSTEXPR14 T&& get(etl::variant&& v) { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; return get(etl::move(v)); } @@ -1578,7 +1503,7 @@ namespace etl template ETL_CONSTEXPR14 const T& get(const etl::variant& v) { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; return get(v); } @@ -1587,7 +1512,7 @@ namespace etl template ETL_CONSTEXPR14 const T&& get(const etl::variant&& v) { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; return get(etl::move(v)); } @@ -1628,7 +1553,7 @@ namespace etl template< class T, typename... TTypes > ETL_CONSTEXPR14 etl::add_pointer_t get_if(etl::variant* pv) noexcept { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; if ((pv != nullptr) && (pv->index() == Index)) { @@ -1644,7 +1569,7 @@ namespace etl template< typename T, typename... TTypes > ETL_CONSTEXPR14 etl::add_pointer_t get_if(const etl::variant* pv) noexcept { - constexpr size_t Index = etl::private_variant::parameter_pack::template index_of_type::value; + constexpr size_t Index = etl::type_list_index_of_type, T>::value; if ((pv != nullptr) && (pv->index() == Index)) {