/****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2014 John Wellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef ETL_TUPLE_INCLUDED #define ETL_TUPLE_INCLUDED #include "platform.h" #if ETL_NOT_USING_CPP11 && !defined(ETL_IN_UNIT_TEST) #error NOT SUPPORTED FOR C++03 OR BELOW #endif #if ETL_USING_CPP11 #if ETL_USING_STL #include #endif #include "functional.h" #include "nth_type.h" #include "type_list.h" #include "type_traits.h" #include "utility.h" #include "private/tuple_element.h" #include "private/tuple_size.h" namespace etl { //*************************************************************************** /// A tuple type /// etl::tuple //*************************************************************************** template class tuple; //*************************************************************************** /// Type trait to check if a type is an etl::tuple /// Default implementation. //*************************************************************************** template struct is_tuple : etl::false_type { }; //*************************************************************************** /// Type trait to check if a type is an etl::tuple /// Specialisation for etl::tuple //*************************************************************************** template struct is_tuple> : etl::true_type { }; namespace private_tuple { //*************************************************************************** /// Get the base of a tuple type whose head type is T. //*************************************************************************** template struct tuple_type_base; // Specialisation for an empty tuple template struct tuple_type_base> { using type = void; }; // Recursive definition of the type. template struct tuple_type_base> { using type = etl::conditional_t::value, tuple, typename tuple_type_base>::type>; }; // Get the base of a tuple type whose head type is T. template using tuple_type_base_t = typename tuple_type_base::type; //*************************************************************************** /// ignore /// An object of unspecified type such that any value can be assigned to it /// with no effect. Intended for use with etl::tie when unpacking a /// etl::tuple, as a placeholder for the arguments that are not used. /// https://en.cppreference.com/w/cpp/utility/tuple/ignore //*************************************************************************** struct ignore_t { template ETL_CONSTEXPR ignore_t operator=(T&&) const ETL_NOEXCEPT { return *this; } }; } // namespace private_tuple //*************************************************************************** /// Empty tuple //*************************************************************************** template <> class tuple<> { public: using value_type = void; ///< The type contained by this tuple. using this_type = tuple<>; ///< The type of this tuple. using base_type = void; ///< The type of the base tuple. using type_list = etl::type_list<>; ///< The type list for this tuple. using index_sequence_type = etl::make_index_sequence<0>; ///< The index_sequence type for this tuple. //********************************* // No-op copy_assignment for the base case //********************************* ETL_CONSTEXPR14 void copy_assignment(const this_type& /*other*/) { } //********************************* // No-op forward_assignment for the base case //********************************* ETL_CONSTEXPR14 void forward_assignment(this_type&& /*other*/) { } //********************************* // No-op swap for the base case //********************************* ETL_CONSTEXPR14 void swap(this_type& /*other*/) { } //********************************* // Returns the size of the base case. // Always zero. //********************************* ETL_NODISCARD ETL_CONSTEXPR static size_t size() { return 0U; } }; //*************************************************************************** /// Tuple //*************************************************************************** template class tuple : public tuple { private: //********************************* /// Helper function to calculate the number /// of types from a type list. //********************************* template static constexpr size_t number_of_types() { return sizeof...(UTypes); } public: //********************************* /// Friends //********************************* template friend class tuple; template ETL_CONSTEXPR14 friend etl::tuple_element_t>& get(tuple&); template ETL_CONSTEXPR14 friend etl::tuple_element_t>&& get(tuple&&); template ETL_CONSTEXPR14 friend const etl::tuple_element_t>& get(const tuple&); template ETL_CONSTEXPR14 friend const etl::tuple_element_t>&& get(const tuple&&); template ETL_CONSTEXPR14 friend T& get(tuple&); template ETL_CONSTEXPR14 friend T&& get(tuple&&); template ETL_CONSTEXPR14 friend const T& get(const tuple&); template ETL_CONSTEXPR14 friend const T&& get(const tuple&&); //********************************* /// Types //********************************* using value_type = THead; ///< The type contained by this tuple. using this_type = tuple; ///< The type of this tuple. using base_type = tuple; ///< The type of the base tuple. using type_list = etl::type_list; ///< The type list for this tuple. using index_sequence_type = etl::make_index_sequence< number_of_types< THead, TTail...>()>; ///< The index_sequence type ///< for this tuple. //********************************* /// Default constructor. //********************************* ETL_CONSTEXPR14 tuple() : value() { } //********************************* /// Copy constructor. //********************************* ETL_CONSTEXPR14 tuple(const tuple& other) = default; //********************************* /// Move constructor. //********************************* ETL_CONSTEXPR14 tuple(tuple&& other) = default; //********************************* /// Copy assignment. //********************************* ETL_CONSTEXPR14 tuple& operator=(const tuple& other) = default; //********************************* /// Move assignment. //********************************* ETL_CONSTEXPR14 tuple& operator=(tuple&& other) = default; //********************************* /// Copy construct from lvalue reference tuple type. /// Implicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(tuple& other) : base_type(other.get_base()) , value(other.get_value()) { } //********************************* /// Copy construct from lvalue reference tuple type. /// Explicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(tuple& other) : base_type(other.get_base()) , value(other.get_value()) { } //********************************* /// Copy construct from const lvalue reference tuple type. /// Implicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(const tuple& other) : base_type(other.get_base()) , value(other.get_value()) { } //********************************* /// Copy construct from const lvalue reference tuple type. /// Explicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(const tuple& other) : base_type(other.get_base()) , value(other.get_value()) { } //********************************* /// Move construct from rvalue reference tuple type. /// Implicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(tuple&& other) : base_type(etl::forward>(other.get_base())) , value(etl::forward(other.get_value())) { } //********************************* /// Move construct from rvalue reference tuple type. /// Explicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(tuple&& other) : base_type(etl::forward>(other.get_base())) , value(etl::forward(other.get_value())) { } //********************************* /// Construct from const rvalue reference tuple type. /// Implicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(const tuple&& other) : base_type(other.get_base()) , value(other.get_value()) { } //********************************* /// Construct from const rvalue reference tuple type. /// Explicit conversion //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()) && (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(const tuple&& other) : base_type(other.get_base()) , value(other.get_value()) { } //********************************* /// Construct from arguments. //********************************* ETL_CONSTEXPR14 tuple(const THead& head, const TTail&... tail) : base_type(tail...) , value(head) { } //********************************* /// Construct from arguments. /// Implicit conversion. //********************************* template < typename UHead, typename... UTail, etl::enable_if_t< !is_tuple>::value && (number_of_types() == number_of_types()) && (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(UHead&& head, UTail&&... tail) ETL_NOEXCEPT : base_type(etl::forward(tail)...) , value(etl::forward(head)) { } //********************************* /// Construct from arguments. /// explicit conversion. //********************************* template < typename UHead, typename... UTail, etl::enable_if_t>::value && (number_of_types() == number_of_types()) && (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(UHead&& head, UTail&&... tail) ETL_NOEXCEPT : base_type(etl::forward(tail)...) , value(etl::forward(head)) { } //********************************* /// Construct from lvalue reference pair. /// Implicit conversion. //********************************* template () == 2U && etl::is_convertible::value && etl::is_convertible< U2, typename base_type::value_type>::value, int> = 0> ETL_CONSTEXPR14 tuple(ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) , value(p.first) { } //********************************* /// Construct from lvalue reference pair. /// Explicit conversion. //********************************* template < typename U1, typename U2, etl::enable_if_t() == 2U && (!etl::is_convertible::value || !etl::is_convertible< U2, typename base_type::value_type>::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) , value(p.first) { } //********************************* /// Construct from const lvalue reference pair. /// Implicit conversion. //********************************* template () == 2U && etl::is_convertible::value && etl::is_convertible< U2, typename base_type::value_type>::value, int> = 0> ETL_CONSTEXPR14 tuple(const ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) , value(p.first) { } //********************************* /// Construct from const lvalue reference pair. /// Explicit conversion. //********************************* template < typename U1, typename U2, etl::enable_if_t() == 2U && (!etl::is_convertible::value || !etl::is_convertible< U2, typename base_type::value_type>::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(const ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) , value(p.first) { } //********************************* /// Construct from rvalue reference pair. /// Implicit conversion. //********************************* template () == 2U && etl::is_convertible::value && etl::is_convertible< U2, typename base_type::value_type>::value, int> = 0> ETL_CONSTEXPR14 tuple(ETL_OR_STD::pair&& p) ETL_NOEXCEPT : base_type(etl::forward(p.second)) , value(etl::forward(p.first)) { } //********************************* /// Construct from rvalue reference pair. /// Explicit conversion. //********************************* template < typename U1, typename U2, etl::enable_if_t() == 2U && (!etl::is_convertible::value || !etl::is_convertible< U2, typename base_type::value_type>::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(ETL_OR_STD::pair&& p) ETL_NOEXCEPT : base_type(etl::forward(p.second)) , value(etl::forward(p.first)) { } //********************************* /// Construct from const rvalue reference pair. /// Implicit conversion. //********************************* template () == 2U && etl::is_convertible::value && etl::is_convertible< U2, typename base_type::value_type>::value, int> = 0> ETL_CONSTEXPR14 tuple(const ETL_OR_STD::pair&& p) ETL_NOEXCEPT : base_type(etl::forward(p.second)) , value(etl::forward(p.first)) { } //********************************* /// Construct from const rvalue reference pair. /// Explicit conversion. //********************************* template < typename U1, typename U2, etl::enable_if_t() == 2U && (!etl::is_convertible::value || !etl::is_convertible< U2, typename base_type::value_type>::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(const ETL_OR_STD::pair&& p) ETL_NOEXCEPT : base_type(etl::forward(p.second)) , value(etl::forward(p.first)) { } //********************************* /// Copy assign from other tuple type. //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()), int> = 0> ETL_CONSTEXPR14 tuple& operator=(const tuple& other) { copy_assignment(other); return *this; } //********************************* /// Move assign from other tuple type. //********************************* template < typename UHead, typename... UTail, etl::enable_if_t<(number_of_types() == number_of_types()), int> = 0> ETL_CONSTEXPR14 tuple& operator=(tuple&& other) { forward_assignment(etl::forward>(other)); return *this; } //********************************* /// Assign from lvalue pair tuple type. //********************************* template (), etl::enable_if_t = 0> ETL_CONSTEXPR14 tuple& operator=(ETL_OR_STD::pair& p) { get_value() = p.first; get_base().get_value() = p.second; return *this; } //********************************* /// Assign from const lvalue pair tuple type. //********************************* template (), etl::enable_if_t = 0> ETL_CONSTEXPR14 tuple& operator=(const ETL_OR_STD::pair& p) { get_value() = p.first; get_base().get_value() = p.second; return *this; } //********************************* /// Assign from rvalue pair tuple type. //********************************* template (), etl::enable_if_t = 0> ETL_CONSTEXPR14 tuple& operator=(ETL_OR_STD::pair&& p) { get_value() = etl::forward(p.first); get_base().get_value() = etl::forward(p.second); return *this; } //********************************* /// Assign from const rvalue pair tuple type. //********************************* template (), etl::enable_if_t = 0> ETL_CONSTEXPR14 tuple& operator=(const ETL_OR_STD::pair&& p) { get_value() = etl::forward(p.first); get_base().get_value() = etl::forward(p.second); return *this; } //********************************* /// Swaps this tuple with another. //********************************* ETL_CONSTEXPR14 void swap(this_type& other) { using ETL_OR_STD::swap; // Swap the head swap(get_value(), other.get_value()); auto& this_base = get_base(); auto& other_base = other.get_base(); // Recursively swap the tail by calling the base class's swap // implementation. this_base.swap(other_base); } //********************************* /// Returns the number of elements in the tuple. //********************************* ETL_NODISCARD constexpr static size_t size() { return number_of_types(); } protected: //********************************* /// Returns a reference to the head value. //********************************* ETL_NODISCARD ETL_CONSTEXPR14 THead& get_value() { return value; } //********************************* /// Returns a const reference to the head value. //********************************* ETL_CONSTEXPR const THead& get_value() const { return value; } //********************************* /// Get a reference to the base class. //********************************* ETL_NODISCARD ETL_CONSTEXPR14 base_type& get_base() { return static_cast(*this); } //********************************* /// Get a const reference to the base class. //********************************* ETL_NODISCARD ETL_CONSTEXPR14 const base_type& get_base() const { return static_cast(*this); } //********************************* /// Handles copy assignment from another tuple. //********************************* template ETL_CONSTEXPR14 void copy_assignment(const tuple& other) { // Assign the head this->value = other.get_value(); // Get the base classes auto& this_base = get_base(); const auto& other_base = other.get_base(); // Recursively assign the tail by calling the base class's assignment // implementation this_base.copy_assignment(other_base); } //********************************* /// Handles move assignment from another tuple. //********************************* template ETL_CONSTEXPR14 void forward_assignment(tuple&& other) { // Assign the head this->value = etl::forward(other.get_value()); auto& this_base = get_base(); auto&& other_base = other.get_base(); // Recursively assign the tail by calling the base class's move assignment // implementation this_base.forward_assignment(etl::forward>(other_base)); } private: THead value; }; #if ETL_USING_CPP17 //*************************************************************************** /// Template deduction guideline from variadic arguments. //*************************************************************************** template tuple(TArgs... args) -> tuple; //*************************************************************************** /// Template deduction guideline from pair. //*************************************************************************** template tuple(ETL_OR_STD::pair) -> tuple; #endif //*************************************************************************** /// Gets the element type at the index in the tuple. //*************************************************************************** template struct tuple_element> { using type = etl::nth_type_t; }; //*************************************************************************** /// Gets the size of the tuple. //*************************************************************************** template struct tuple_size> : etl::integral_constant { }; //*************************************************************************** /// Gets the common type of a tuple. //*************************************************************************** template struct common_type> { using type = etl::common_type_t; }; //*************************************************************************** /// Extracts the element at Index from the tuple. /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns a reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 etl::tuple_element_t>& get(tuple& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); // Get the type at this index. using tuple_type = etl::nth_base_t>&; // Cast the tuple to the selected type and get the value. return static_cast(t).get_value(); } //*************************************************************************** /// Extracts the element at Index from the tuple. /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns a const reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 const etl::tuple_element_t>& get(const tuple& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); // Get the type at this index. using tuple_type = const etl::nth_base_t>&; // Cast the tuple to the selected type and get the value. return static_cast(t).get_value(); } //*************************************************************************** /// Extracts the element at Index from the tuple. /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns an rvalue reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 etl::tuple_element_t>&& get(tuple&& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); // Get the type at this index. using tuple_type = etl::nth_base_t>&&; // Cast the tuple to the selected type and get the value. return etl::move(static_cast(t).get_value()); } //*************************************************************************** /// Extracts the element at Index from the tuple. /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns a const rvalue reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 const etl::tuple_element_t>&& get(const tuple&& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); // Get the type at this index. using tuple_type = const etl::nth_base_t>&&; // Cast the tuple to the selected type and get the value. return etl::move(static_cast(t).get_value()); } //*************************************************************************** /// Extracts the element with type T from the tuple. /// Static asserts if the tuple contain more than one T, or does not contain a /// T element. Returns a reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 T& get(tuple& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = etl::private_tuple::tuple_type_base_t>&; // Cast the tuple to the selected type and get the value. return static_cast(t).get_value(); } //*************************************************************************** /// Extracts the element with type T from the tuple. /// Static asserts if the tuple contain more than one T, or does not contain a /// T element. Returns a const reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 const T& get(const tuple& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = const etl::private_tuple::tuple_type_base_t>&; // Cast the tuple to the selected type and get the value. return static_cast(t).get_value(); } //*************************************************************************** /// Extracts the element with type T from the tuple. /// Static asserts if the tuple contain more than one T, or does not contain a /// T element. Returns an rvalue reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 T&& get(tuple&& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = etl::private_tuple::tuple_type_base_t>&&; // Cast the tuple to the selected type and get the value. return etl::move(static_cast(t).get_value()); } //*************************************************************************** /// Extracts the element with type T from the tuple. /// Static asserts if the tuple contain more than one T, or does not contain a /// T element. Returns a const rvalue reference. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 const T&& get(const tuple&& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = const etl::private_tuple::tuple_type_base_t>&&; // Cast the tuple to the selected type and get the value. return etl::move(static_cast(t).get_value()); } #if ETL_USING_CPP17 inline constexpr private_tuple::ignore_t ignore; #else static constexpr private_tuple::ignore_t ignore; #endif //*************************************************************************** /// Creates a tuple of references to the provided arguments. //*************************************************************************** template ETL_CONSTEXPR etl::tuple tie(TTypes&... args) { return {args...}; } //*************************************************************************** // Creates a tuple from the provided arguments. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 etl::tuple...> make_tuple(TTypes&&... args) { return etl::tuple...>(etl::forward(args)...); } //*************************************************************************** /// Creates a new tuple by selecting elements from another, given a run time /// index sequence. Static asserts if the number of indices does not match the /// tuple size. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto select_from_tuple(TTuple&& tuple, etl::index_sequence) -> etl::tuple>...> { ETL_STATIC_ASSERT(sizeof...(Indices) <= etl::tuple_size>::value, "Number of indices is greater than the tuple size"); return etl::make_tuple(etl::forward>>(etl::get(etl::forward(tuple)))...); } //*************************************************************************** /// Creates a new tuple by selecting elements from another, given a template /// parameter index sequence. Static asserts if the number of indices does not /// match the tuple size. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto select_from_tuple(TTuple&& tuple) -> etl::tuple>...> { return select_from_tuple(etl::forward(tuple), etl::index_sequence{}); } //*************************************************************************** /// Forwards the arguments as a tuple. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 etl::tuple forward_as_tuple(TTypes&&... args) { return tuple(etl::forward(args)...); } namespace private_tuple { //********************************** // Helper to concatenate two tuples //********************************** template ETL_CONSTEXPR14 auto tuple_cat_impl(Tuple1&& t1, etl::index_sequence, Tuple2&& t2, etl::index_sequence) -> etl::tuple>..., etl::tuple_element_t>...> { return etl::tuple>..., etl::tuple_element_t>...>( etl::get(etl::forward(t1))..., etl::get(etl::forward(t2))...); } } // namespace private_tuple //*************************************************************************** /// Base case for concatenating one tuple //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto tuple_cat(Tuple&& t) -> Tuple { return etl::forward(t); } //*************************************************************************** /// Recursive case for concatenating multiple tuples //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto tuple_cat(Tuple1&& t1, Tuple2&& t2, Tuples&&... ts) -> decltype(tuple_cat(private_tuple::tuple_cat_impl(etl::forward(t1), etl::make_index_sequence< etl::tuple_size>::value>{}, etl::forward(t2), etl::make_index_sequence< etl::tuple_size>::value>{}), etl::forward(ts)...)) { return tuple_cat( private_tuple::tuple_cat_impl(etl::forward(t1), etl::make_index_sequence< etl::tuple_size>::value>{}, etl::forward(t2), etl::make_index_sequence< etl::tuple_size>::value>{}), etl::forward(ts)...); } #if ETL_USING_STL //*************************************************************************** // Tuple conversion functions. // From ETL to STL //*************************************************************************** namespace private_tuple { ///********************************* template ETL_NODISCARD ETL_CONSTEXPR14 auto to_std_impl(const TEtl_Tuple& etl_tuple, etl::index_sequence) -> std::tuple...> { return std::tuple...>(etl::get(etl_tuple)...); } ///********************************* template ETL_NODISCARD ETL_CONSTEXPR14 auto to_std_impl(TEtl_Tuple&& etl_tuple, etl::index_sequence) -> std::tuple...> { return std::tuple...>(etl::move(etl::get(etl_tuple))...); } } // namespace private_tuple //*************************************************************************** /// Converts an etl::tuple to a std::tuple. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto to_std(const etl::tuple& etl_tuple) -> std::tuple...> { return private_tuple::to_std_impl(etl_tuple, etl::make_index_sequence_for()); } //*************************************************************************** /// Converts an etl::tuple to a std::tuple. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto to_std(etl::tuple&& etl_tuple) -> std::tuple...> { return private_tuple::to_std_impl(etl::move(etl_tuple), etl::make_index_sequence_for()); } //*************************************************************************** /// Tuple conversion functions. /// From STL to ETL //*************************************************************************** namespace private_tuple { ///********************************* template ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl_impl(const TStd_Tuple& std_tuple, etl::index_sequence) -> etl::tuple::type...> { return etl::tuple< typename std::tuple_element::type...>(std::get(std_tuple)...); } ///********************************* template ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl_impl(TStd_Tuple&& std_tuple, etl::index_sequence) -> etl::tuple::type...> { return etl::tuple< typename std::tuple_element::type...>(std::move(std::get(std_tuple))...); } } // namespace private_tuple //*************************************************************************** /// Converts a std::tuple to an etl::tuple. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl(const std::tuple& std_tuple) -> etl::tuple...> { return private_tuple::to_etl_impl(std_tuple, etl::make_index_sequence_for()); } //*************************************************************************** /// Converts a std::tuple to an etl::tuple. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl(std::tuple&& std_tuple) -> etl::tuple...> { return private_tuple::to_etl_impl(etl::move(std_tuple), etl::make_index_sequence_for()); } #endif namespace private_tuple { //*************************************************************************** /// Equality //*************************************************************************** // When there are no indices left to compare. template ETL_NODISCARD ETL_CONSTEXPR14 bool tuple_equality(const TTuple1& /*lhs*/, const TTuple2& /*rhs*/, etl::index_sequence<>) { return true; } // Recursive case: compare the current element and recurse. template ETL_NODISCARD ETL_CONSTEXPR14 bool tuple_equality(const TTuple1& lhs, const TTuple2& rhs, etl::index_sequence) { return etl::get(lhs) == etl::get(rhs) && tuple_equality(lhs, rhs, etl::index_sequence{}); } //*************************************************************************** /// Less than //*************************************************************************** // When there are no indices left to compare. template ETL_NODISCARD ETL_CONSTEXPR14 bool tuple_less_than(const TTuple1& /*lhs*/, const TTuple2& /*rhs*/, etl::index_sequence<>) { return false; } // Recursively compare the current element and the rest. template ETL_NODISCARD ETL_CONSTEXPR14 bool tuple_less_than(const TTuple1& lhs, const TTuple2& rhs, etl::index_sequence) { if (get(lhs) < get(rhs)) { return true; } if (get(rhs) < get(lhs)) { return false; } return tuple_less_than(lhs, rhs, etl::index_sequence{}); } } // namespace private_tuple //*************************************************************************** /// Equality operator. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 bool operator==(const etl::tuple& lhs, const etl::tuple& rhs) { ETL_STATIC_ASSERT(sizeof...(TTypes) == sizeof...(UTypes), "Cannot compare tuples of different sizes"); // Compare each element of the tuples. return private_tuple::tuple_equality(lhs, rhs, etl::make_index_sequence::size()>{}); } //*************************************************************************** /// Inequality operator. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 bool operator!=(const etl::tuple& lhs, const etl::tuple& rhs) { return !(lhs == rhs); } //*************************************************************************** /// Less than operator. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 bool operator<(const etl::tuple& lhs, const etl::tuple& rhs) { ETL_STATIC_ASSERT(sizeof...(TTypes) == sizeof...(UTypes), "Cannot compare tuples of different sizes"); // Compare the elements. return private_tuple::tuple_less_than(lhs, rhs, etl::make_index_sequence::size()>{}); } //*************************************************************************** /// Less than or equals operator. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 bool operator<=(const etl::tuple& lhs, const etl::tuple& rhs) { return !(rhs < lhs); } //*************************************************************************** /// Greater than operator. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 bool operator>(const etl::tuple& lhs, const etl::tuple& rhs) { return (rhs < lhs); } //*************************************************************************** /// Greater than or equals operator. //*************************************************************************** template ETL_NODISCARD ETL_CONSTEXPR14 bool operator>=(const etl::tuple& lhs, const etl::tuple& rhs) { return !(lhs < rhs); } //*************************************************************************** /// Swap two tuples. //*************************************************************************** template ETL_CONSTEXPR14 void swap(etl::tuple& lhs, etl::tuple& rhs) { lhs.swap(rhs); } //*************************************************************************** /// Helper to turn etl::type_list into etl::tuple template struct tuple_from_type_list; template struct tuple_from_type_list> { using type = etl::tuple; }; template using tuple_from_type_list_t = typename tuple_from_type_list::type; } // namespace etl namespace std { #if ETL_NOT_USING_STL && !((defined(ETL_DEVELOPMENT_OS_APPLE) || (ETL_COMPILER_FULL_VERSION >= 190000)) && defined(ETL_COMPILER_CLANG)) template struct tuple_size; template struct tuple_element; #endif //*************************************************************************** /// Specialisation of tuple_size to allow the use of C++ structured bindings. //*************************************************************************** template struct tuple_size> : etl::integral_constant { }; //*************************************************************************** /// Specialisation of tuple_element to allow the use of C++ structured /// bindings. //*************************************************************************** template struct tuple_element> { using type = etl::nth_type_t; }; } // namespace std #endif #endif