Changeset 55cd829 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:20Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bfa86e5
Parents:
78d739d
git-author:
Dzejrou <dzejrou@…> (2018-03-30 15:28:07)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:20)
Message:

cpp: added tuple relational operators, changed tuple_ops to be ascending and removed enable_ifs on pair related constructors and assignment operators that weren't supposed to be there

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/tuple.hpp

    r78d739d r55cd829  
    288288    namespace aux
    289289    {
    290         template<size_t I>
     290        template<size_t I, size_t N>
    291291        struct tuple_ops
    292292        {
     
    296296                get<I>(forward<T>(lhs)) = get<I>(forward<U>(rhs));
    297297
    298                 tuple_ops<I - 1>::assign(forward<T>(lhs), forward<U>(rhs));
     298                tuple_ops<I + 1, N>::assign(forward<T>(lhs), forward<U>(rhs));
    299299            }
    300300
     
    304304                std::swap(get<I>(lhs), get<I>(rhs));
    305305
    306                 tuple_ops<I - 1>::swap(lhs, rhs);
    307             }
    308 
    309             // TODO: for rel ops we will need this to be ascending, not descending
    310             template<class T, class U>
    311             static bool eq(const T& lhs, const T& rhs)
    312             {
    313                 return (get<I>(lhs) == get<I>(rhs)) && tuple_ops<I - 1>::eq(lhs, rhs);
    314             }
    315         };
    316 
    317         template<>
    318         struct tuple_ops<0>
     306                tuple_ops<I + 1, N>::swap(lhs, rhs);
     307            }
     308
     309            template<class T, class U>
     310            static bool eq(const T& lhs, const U& rhs)
     311            {
     312                return (get<I>(lhs) == get<I>(rhs)) && tuple_ops<I + 1, N>::eq(lhs, rhs);
     313            }
     314
     315            template<class T, class U>
     316            static bool lt(const T& lhs, const U& rhs)
     317            {
     318                return (get<I>(lhs) < get<I>(rhs)) ||
     319                    (!(get<I>(rhs) < get<I>(lhs)) && tuple_ops<I + 1, N>::lt(lhs, rhs));
     320            }
     321        };
     322
     323        template<size_t N>
     324        struct tuple_ops<N, N>
    319325        {
    320326            template<class T, class U>
    321327            static void assign(T&& lhs, U&& rhs)
    322328            {
    323                 get<0>(forward<T>(lhs)) = get<0>(forward<U>(rhs));
     329                get<N>(forward<T>(lhs)) = get<N>(forward<U>(rhs));
    324330            }
    325331
     
    327333            static void swap(T& lhs, U& rhs)
    328334            {
    329                 std::swap(get<0>(lhs), get<0>(rhs));
     335                std::swap(get<N>(lhs), get<N>(rhs));
     336            }
     337
     338            template<class T, class U>
     339            static bool eq(const T& lhs, const U& rhs)
     340            {
     341                return get<N>(lhs) == get<N>(rhs);
     342            }
     343
     344            template<class T, class U>
     345            static bool lt(const T& lhs, const U& rhs)
     346            {
     347                return get<N>(lhs) < get<N>(rhs);
    330348            }
    331349        };
     
    374392            //{ /* DUMMY BODY */ }
    375393
    376             template<class U1, class U2, class = enable_if_t<sizeof...(Ts) == 2, void>>
     394            // TODO: pair related construction and assignment needs convertibility, not size
     395            template<class U1, class U2>
    377396            constexpr tuple(const pair<U1, U2>& p)
    378397                : base_t{}
     
    382401            }
    383402
    384             template<class U1, class U2, class = enable_if_t<sizeof...(Ts) == 2, void>>
     403            template<class U1, class U2>
    385404            constexpr tuple(pair<U1, U2>&& p)
    386405                : base_t{}
     
    398417            tuple& operator=(const tuple& other)
    399418            {
    400                 aux::tuple_ops<sizeof...(Ts) - 1>::assign(*this, other);
     419                aux::tuple_ops<0, sizeof...(Ts) - 1>::assign(*this, other);
    401420
    402421                return *this;
     
    405424            tuple& operator=(tuple&& other) noexcept(aux::tuple_noexcept_assignment<Ts...>::value)
    406425            {
    407                 aux::tuple_ops<sizeof...(Ts) - 1>::assign(*this, forward<tuple>(other));
     426                aux::tuple_ops<0, sizeof...(Ts) - 1>::assign(*this, forward<tuple>(other));
    408427
    409428                return *this;
     
    413432            tuple& operator=(const tuple<Us...>& other)
    414433            {
    415                 aux::tuple_ops<sizeof...(Ts) - 1>::assign(*this, other);
     434                aux::tuple_ops<0, sizeof...(Ts) - 1>::assign(*this, other);
    416435
    417436                return *this;
     
    421440            tuple& operator=(tuple<Us...>&& other)
    422441            {
    423                 aux::tuple_ops<sizeof...(Ts) - 1>::assign(*this, forward<Us>(other)...);
     442                aux::tuple_ops<0, sizeof...(Ts) - 1>::assign(*this, forward<Us>(other)...);
    424443
    425444                return *this;
    426445            }
    427446
    428             template<class U1, class U2, class = enable_if_t<sizeof...(Ts) == 2, void>>
     447            template<class U1, class U2>
    429448            tuple& operator=(const pair<U1, U2>& p)
    430449            {
     
    433452            }
    434453
    435             template<class U1, class U2, class = enable_if_t<sizeof...(Ts) == 2, void>>
     454            template<class U1, class U2>
    436455            tuple& operator=(pair<U1, U2>&& p)
    437456            {
     
    446465            void swap(tuple& other) noexcept(aux::tuple_noexcept_swap<Ts...>::value)
    447466            {
    448                 aux::tuple_ops<sizeof...(Ts) - 1>::swap(*this, other);
     467                aux::tuple_ops<0, sizeof...(Ts) - 1>::swap(*this, other);
    449468            }
    450469    };
     
    455474
    456475    template<class... Ts, class... Us>
    457     constexpr bool operator==(const tuple<Ts...>& lhs, const tuple<Us...> rhs);
     476    constexpr bool operator==(const tuple<Ts...>& lhs, const tuple<Us...> rhs)
     477    {
     478        if constexpr (sizeof...(Ts) == 0)
     479            return true;
     480        else
     481            return aux::tuple_ops<0, sizeof...(Ts) - 1>::eq(lhs, rhs);
     482    }
    458483
    459484    template<class... Ts, class... Us>
    460     constexpr bool operator<(const tuple<Ts...>& lhs, const tuple<Us...> rhs);
     485    constexpr bool operator<(const tuple<Ts...>& lhs, const tuple<Us...> rhs)
     486    {
     487        if constexpr (sizeof...(Ts) == 0)
     488            return false;
     489        else
     490            return aux::tuple_ops<0, sizeof...(Ts) - 1>::lt(lhs, rhs);
     491    }
    461492
    462493    template<class... Ts, class... Us>
    463     constexpr bool operator!=(const tuple<Ts...>& lhs, const tuple<Us...> rhs);
     494    constexpr bool operator!=(const tuple<Ts...>& lhs, const tuple<Us...> rhs)
     495    {
     496        return !(lhs == rhs);
     497    }
    464498
    465499    template<class... Ts, class... Us>
    466     constexpr bool operator>(const tuple<Ts...>& lhs, const tuple<Us...> rhs);
     500    constexpr bool operator>(const tuple<Ts...>& lhs, const tuple<Us...> rhs)
     501    {
     502        return rhs < lhs;
     503    }
    467504
    468505    template<class... Ts, class... Us>
    469     constexpr bool operator<=(const tuple<Ts...>& lhs, const tuple<Us...> rhs);
     506    constexpr bool operator<=(const tuple<Ts...>& lhs, const tuple<Us...> rhs)
     507    {
     508        return !(rhs < lhs);
     509    }
    470510
    471511    template<class... Ts, class... Us>
    472     constexpr bool operator>=(const tuple<Ts...>& lhs, const tuple<Us...> rhs);
     512    constexpr bool operator>=(const tuple<Ts...>& lhs, const tuple<Us...> rhs)
     513    {
     514        return !(lhs < rhs);
     515    }
    473516
    474517    /**
Note: See TracChangeset for help on using the changeset viewer.