Changeset db54a9d in mainline


Ignore:
Timestamp:
2018-07-05T21:41:23Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
baed175
Parents:
f509d40
git-author:
Dzejrou <dzejrou@…> (2018-05-09 23:05:44)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:23)
Message:

cpp: moved arithmetic operations to their own header

Location:
uspace/lib/cpp/include
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/functional

    rf509d40 rdb54a9d  
    2828
    2929#include <impl/functional.hpp>
     30#include <internal/functional/arithmetic_operations.hpp>
    3031#include <internal/functional/bind.hpp>
    3132#include <internal/functional/function.hpp>
  • uspace/lib/cpp/include/impl/functional.hpp

    rf509d40 rdb54a9d  
    5050
    5151    /**
    52      * 20.9.5, arithmetic operations:
    53      */
    54 
    55     template<class T = void>
    56     struct plus
    57     {
    58         constexpr T operator()(const T& lhs, const T& rhs) const
    59         {
    60             return lhs + rhs;
    61         }
    62 
    63         using first_argument_type  = T;
    64         using second_argument_type = T;
    65         using result_type          = T;
    66     };
    67 
    68     template<class T = void>
    69     struct minus
    70     {
    71         constexpr T operator()(const T& lhs, const T& rhs) const
    72         {
    73             return lhs - rhs;
    74         }
    75 
    76         using first_argument_type  = T;
    77         using second_argument_type = T;
    78         using result_type          = T;
    79     };
    80 
    81     template<class T = void>
    82     struct multiplies
    83     {
    84         constexpr T operator()(const T& lhs, const T& rhs) const
    85         {
    86             return lhs * rhs;
    87         }
    88 
    89         using first_argument_type  = T;
    90         using second_argument_type = T;
    91         using result_type          = T;
    92     };
    93 
    94     template<class T = void>
    95     struct divides
    96     {
    97         constexpr T operator()(const T& lhs, const T& rhs) const
    98         {
    99             return lhs / rhs;
    100         }
    101 
    102         using first_argument_type  = T;
    103         using second_argument_type = T;
    104         using result_type          = T;
    105     };
    106 
    107     template<class T = void>
    108     struct modulus
    109     {
    110         constexpr T operator()(const T& lhs, const T& rhs) const
    111         {
    112             return lhs % rhs;
    113         }
    114 
    115         using first_argument_type  = T;
    116         using second_argument_type = T;
    117         using result_type          = T;
    118     };
    119 
    120     template<class T = void>
    121     struct negate
    122     {
    123         constexpr T operator()(const T& x) const
    124         {
    125             return -x;
    126         }
    127 
    128         using argument_type = T;
    129         using result_type   = T;
    130     };
    131 
    132     namespace aux
    133     {
    134         /**
    135          * Used by some functions like std::set::find to determine
    136          * whether a functor is transparent.
    137          */
    138         struct transparent_t;
    139 
    140         template<class T, class = void>
    141         struct is_transparent: false_type
    142         { /* DUMMY BODY */ };
    143 
    144         template<class T>
    145         struct is_transparent<T, void_t<typename T::is_transparent>>
    146             : true_type
    147         { /* DUMMY BODY */ };
    148 
    149         template<class T>
    150         inline constexpr bool is_transparent_v = is_transparent<T>::value;
    151     }
    152 
    153     template<>
    154     struct plus<void>
    155     {
    156         template<class T, class U>
    157         constexpr auto operator()(T&& lhs, U&& rhs) const
    158             -> decltype(forward<T>(lhs) + forward<U>(rhs))
    159         {
    160             return forward<T>(lhs) + forward<T>(rhs);
    161         }
    162 
    163         using is_transparent = aux::transparent_t;
    164     };
    165 
    166     template<>
    167     struct minus<void>
    168     {
    169         template<class T, class U>
    170         constexpr auto operator()(T&& lhs, U&& rhs) const
    171             -> decltype(forward<T>(lhs) - forward<U>(rhs))
    172         {
    173             return forward<T>(lhs) - forward<T>(rhs);
    174         }
    175 
    176         using is_transparent = aux::transparent_t;
    177     };
    178 
    179     template<>
    180     struct multiplies<void>
    181     {
    182         template<class T, class U>
    183         constexpr auto operator()(T&& lhs, U&& rhs) const
    184             -> decltype(forward<T>(lhs) * forward<U>(rhs))
    185         {
    186             return forward<T>(lhs) * forward<T>(rhs);
    187         }
    188 
    189         using is_transparent = aux::transparent_t;
    190     };
    191 
    192     template<>
    193     struct divides<void>
    194     {
    195         template<class T, class U>
    196         constexpr auto operator()(T&& lhs, U&& rhs) const
    197             -> decltype(forward<T>(lhs) / forward<U>(rhs))
    198         {
    199             return forward<T>(lhs) / forward<T>(rhs);
    200         }
    201 
    202         using is_transparent = aux::transparent_t;
    203     };
    204 
    205     template<>
    206     struct modulus<void>
    207     {
    208         template<class T, class U>
    209         constexpr auto operator()(T&& lhs, U&& rhs) const
    210             -> decltype(forward<T>(lhs) % forward<U>(rhs))
    211         {
    212             return forward<T>(lhs) % forward<T>(rhs);
    213         }
    214 
    215         using is_transparent = aux::transparent_t;
    216     };
    217 
    218     template<>
    219     struct negate<void>
    220     {
    221         template<class T>
    222         constexpr auto operator()(T&& x) const
    223             -> decltype(-forward<T>(x))
    224         {
    225             return -forward<T>(x);
    226         }
    227 
    228         using is_transparent = aux::transparent_t;
    229     };
    230 
    231     /**
    232      * 20.9.6, comparisons:
    233      */
    234 
    235     template<class T = void>
    236     struct equal_to
    237     {
    238         constexpr bool operator()(const T& lhs, const T& rhs) const
    239         {
    240             return lhs == rhs;
    241         }
    242 
    243         using first_argument_type  = T;
    244         using second_argument_type = T;
    245         using result_type          = bool;
    246     };
    247 
    248     template<class T = void>
    249     struct not_equal_to
    250     {
    251         constexpr bool operator()(const T& lhs, const T& rhs) const
    252         {
    253             return lhs != rhs;
    254         }
    255 
    256         using first_argument_type  = T;
    257         using second_argument_type = T;
    258         using result_type          = bool;
    259     };
    260 
    261     template<class T = void>
    262     struct greater
    263     {
    264         constexpr bool operator()(const T& lhs, const T& rhs) const
    265         {
    266             return lhs > rhs;
    267         }
    268 
    269         using first_argument_type  = T;
    270         using second_argument_type = T;
    271         using result_type          = bool;
    272     };
    273 
    274     template<class T = void>
    275     struct less
    276     {
    277         constexpr bool operator()(const T& lhs, const T& rhs) const
    278         {
    279             return lhs < rhs;
    280         }
    281 
    282         using first_argument_type  = T;
    283         using second_argument_type = T;
    284         using result_type          = bool;
    285     };
    286 
    287     template<class T = void>
    288     struct greater_equal
    289     {
    290         constexpr bool operator()(const T& lhs, const T& rhs) const
    291         {
    292             return lhs >= rhs;
    293         }
    294 
    295         using first_argument_type  = T;
    296         using second_argument_type = T;
    297         using result_type          = bool;
    298     };
    299 
    300     template<class T = void>
    301     struct less_equal
    302     {
    303         constexpr bool operator()(const T& lhs, const T& rhs) const
    304         {
    305             return lhs <= rhs;
    306         }
    307 
    308         using first_argument_type  = T;
    309         using second_argument_type = T;
    310         using result_type          = bool;
    311     };
    312 
    313     template<>
    314     struct equal_to<void>
    315     {
    316         template<class T, class U>
    317         constexpr auto operator()(T&& lhs, U&& rhs) const
    318             -> decltype(forward<T>(lhs) == forward<U>(rhs))
    319         {
    320             return forward<T>(lhs) == forward<U>(rhs);
    321         }
    322 
    323         using is_transparent = aux::transparent_t;
    324     };
    325 
    326     template<>
    327     struct not_equal_to<void>
    328     {
    329         template<class T, class U>
    330         constexpr auto operator()(T&& lhs, U&& rhs) const
    331             -> decltype(forward<T>(lhs) != forward<U>(rhs))
    332         {
    333             return forward<T>(lhs) != forward<U>(rhs);
    334         }
    335 
    336         using is_transparent = aux::transparent_t;
    337     };
    338 
    339     template<>
    340     struct greater<void>
    341     {
    342         template<class T, class U>
    343         constexpr auto operator()(T&& lhs, U&& rhs) const
    344             -> decltype(forward<T>(lhs) > forward<U>(rhs))
    345         {
    346             return forward<T>(lhs) > forward<U>(rhs);
    347         }
    348 
    349         using is_transparent = aux::transparent_t;
    350     };
    351 
    352     template<>
    353     struct less<void>
    354     {
    355         template<class T, class U>
    356         constexpr auto operator()(T&& lhs, U&& rhs) const
    357             -> decltype(forward<T>(lhs) < forward<U>(rhs))
    358         {
    359             return forward<T>(lhs) < forward<U>(rhs);
    360         }
    361 
    362         using is_transparent = aux::transparent_t;
    363     };
    364 
    365     template<>
    366     struct greater_equal<void>
    367     {
    368         template<class T, class U>
    369         constexpr auto operator()(T&& lhs, U&& rhs) const
    370             -> decltype(forward<T>(lhs) >= forward<U>(rhs))
    371         {
    372             return forward<T>(lhs) >= forward<U>(rhs);
    373         }
    374 
    375         using is_transparent = aux::transparent_t;
    376     };
    377 
    378     template<>
    379     struct less_equal<void>
    380     {
    381         template<class T, class U>
    382         constexpr auto operator()(T&& lhs, U&& rhs) const
    383             -> decltype(forward<T>(lhs) <= forward<U>(rhs))
    384         {
    385             return forward<T>(lhs) <= forward<U>(rhs);
    386         }
    387 
    388         using is_transparent = aux::transparent_t;
    389     };
    390 
    391     /**
    392      * 20.9.7, logical operations:
    393      */
    394 
    395     template<class T = void>
    396     struct logical_and
    397     {
    398         constexpr bool operator()(const T& lhs, const T& rhs) const
    399         {
    400             return lhs && rhs;
    401         }
    402 
    403         using first_argument_type  = T;
    404         using second_argument_type = T;
    405         using result_type          = bool;
    406     };
    407 
    408     template<class T = void>
    409     struct logical_or
    410     {
    411         constexpr bool operator()(const T& lhs, const T& rhs) const
    412         {
    413             return lhs || rhs;
    414         }
    415 
    416         using first_argument_type  = T;
    417         using second_argument_type = T;
    418         using result_type          = bool;
    419     };
    420 
    421     template<class T = void>
    422     struct logical_not
    423     {
    424         constexpr bool operator()(const T& x) const
    425         {
    426             return !x;
    427         }
    428 
    429         using argument_type = T;
    430         using result_type   = bool;
    431     };
    432 
    433     template<>
    434     struct logical_and<void>
    435     {
    436         template<class T, class U>
    437         constexpr auto operator()(T&& lhs, U&& rhs) const
    438             -> decltype(forward<T>(lhs) && forward<U>(rhs))
    439         {
    440             return forward<T>(lhs) && forward<U>(rhs);
    441         }
    442 
    443         using is_transparent = aux::transparent_t;
    444     };
    445 
    446     template<>
    447     struct logical_or<void>
    448     {
    449         template<class T, class U>
    450         constexpr auto operator()(T&& lhs, U&& rhs) const
    451             -> decltype(forward<T>(lhs) || forward<U>(rhs))
    452         {
    453             return forward<T>(lhs) || forward<U>(rhs);
    454         }
    455 
    456         using is_transparent = aux::transparent_t;
    457     };
    458 
    459     template<>
    460     struct logical_not<void>
    461     {
    462         template<class T>
    463         constexpr auto operator()(T&& x) const
    464             -> decltype(!forward<T>(x))
    465         {
    466             return !forward<T>(x);
    467         }
    468 
    469         using is_transparent = aux::transparent_t;
    470     };
    471 
    472     /**
    473      * 20.9.8, bitwise operations:
    474      */
    475 
    476     template<class T = void>
    477     struct bit_and
    478     {
    479         constexpr T operator()(const T& lhs, const T& rhs) const
    480         {
    481             return lhs & rhs;
    482         }
    483 
    484         using first_argument_type  = T;
    485         using second_argument_type = T;
    486         using result_type          = T;
    487     };
    488 
    489     template<class T = void>
    490     struct bit_or
    491     {
    492         constexpr T operator()(const T& lhs, const T& rhs) const
    493         {
    494             return lhs | rhs;
    495         }
    496 
    497         using first_argument_type  = T;
    498         using second_argument_type = T;
    499         using result_type          = T;
    500     };
    501 
    502     template<class T = void>
    503     struct bit_xor
    504     {
    505         constexpr T operator()(const T& lhs, const T& rhs) const
    506         {
    507             return lhs ^ rhs;
    508         }
    509 
    510         using first_argument_type  = T;
    511         using second_argument_type = T;
    512         using result_type          = T;
    513     };
    514 
    515     template<class T = void>
    516     struct bit_not
    517     {
    518         constexpr bool operator()(const T& x) const
    519         {
    520             return ~x;
    521         }
    522 
    523         using argument_type = T;
    524         using result_type   = T;
    525     };
    526 
    527     template<>
    528     struct bit_and<void>
    529     {
    530         template<class T, class U>
    531         constexpr auto operator()(T&& lhs, U&& rhs) const
    532             -> decltype(forward<T>(lhs) & forward<U>(rhs))
    533         {
    534             return forward<T>(lhs) & forward<U>(rhs);
    535         }
    536 
    537         using is_transparent = aux::transparent_t;
    538     };
    539 
    540     template<>
    541     struct bit_or<void>
    542     {
    543         template<class T, class U>
    544         constexpr auto operator()(T&& lhs, U&& rhs) const
    545             -> decltype(forward<T>(lhs) | forward<U>(rhs))
    546         {
    547             return forward<T>(lhs) | forward<U>(rhs);
    548         }
    549 
    550         using is_transparent = aux::transparent_t;
    551     };
    552 
    553     template<>
    554     struct bit_xor<void>
    555     {
    556         template<class T, class U>
    557         constexpr auto operator()(T&& lhs, U&& rhs) const
    558             -> decltype(forward<T>(lhs) ^ forward<U>(rhs))
    559         {
    560             return forward<T>(lhs) ^ forward<U>(rhs);
    561         }
    562 
    563         using is_transparent = aux::transparent_t;
    564     };
    565 
    566     template<>
    567     struct bit_not<void>
    568     {
    569         template<class T>
    570         constexpr auto operator()(T&& x) const
    571             -> decltype(~forward<T>(x))
    572         {
    573             return ~forward<T>(x);
    574         }
    575 
    576         using is_transparent = aux::transparent_t;
    577     };
    578 
    579     /**
    580      * 20.9.9, negators:
    581      */
    582 
    583     template<class Predicate>
    584     class unary_negate
    585     {
    586         public:
    587             using result_type   = bool;
    588             using argument_type = typename Predicate::argument_type;
    589 
    590             constexpr explicit unary_negate(const Predicate& pred)
    591                 : pred_{pred}
    592             { /* DUMMY BODY */ }
    593 
    594             constexpr result_type operator()(const argument_type& arg)
    595             {
    596                 return !pred_(arg);
    597             }
    598 
    599         private:
    600             Predicate pred_;
    601     };
    602 
    603     template<class Predicate>
    604     constexpr unary_negate<Predicate> not1(const Predicate& pred)
    605     {
    606         return unary_negate<Predicate>{pred};
    607     }
    608 
    609     template<class Predicate>
    610     class binary_negate
    611     {
    612         public:
    613             using result_type          = bool;
    614             using first_argument_type  = typename Predicate::first_argument_type;
    615             using second_argument_type = typename Predicate::second_argument_type;
    616 
    617             constexpr explicit binary_negate(const Predicate& pred)
    618                 : pred_{pred}
    619             { /* DUMMY BODY */ }
    620 
    621             constexpr result_type operator()(const first_argument_type& arg1,
    622                                              const second_argument_type& arg2)
    623             {
    624                 return !pred_(arg1, arg2);
    625             }
    626 
    627         private:
    628             Predicate pred_;
    629     };
    630 
    631     template<class Predicate>
    632     constexpr binary_negate<Predicate> not2(const Predicate& pred);
    633 
    634     /**
    63552     * 20.9.11, member function adaptors:
    63653     */
Note: See TracChangeset for help on using the changeset viewer.