functional.hpp 2.02 KB
Newer Older
1
2
3
#ifndef CK_FUNCTIONAL_HPP
#define CK_FUNCTIONAL_HPP

Chao Liu's avatar
Chao Liu committed
4
#include "integral_constant.hpp"
Chao Liu's avatar
Chao Liu committed
5
#include "sequence.hpp"
Chao Liu's avatar
Chao Liu committed
6
#include "type.hpp"
7

8
9
namespace ck {

Chao Liu's avatar
Chao Liu committed
10
// TODO: right? wrong?
Chao Liu's avatar
Chao Liu committed
11
12
13
struct forwarder
{
    template <typename T>
Chao Liu's avatar
Chao Liu committed
14
    __host__ __device__ constexpr T&& operator()(T&& x) const
Chao Liu's avatar
Chao Liu committed
15
    {
Chao Liu's avatar
Chao Liu committed
16
        return static_cast<T&&>(x);
Chao Liu's avatar
Chao Liu committed
17
18
19
    }
};

Chao Liu's avatar
Chao Liu committed
20
21
struct swallow
{
Chao Liu's avatar
Chao Liu committed
22
    template <typename... Ts>
Chao Liu's avatar
Chao Liu committed
23
    __host__ __device__ constexpr swallow(Ts&&...)
Chao Liu's avatar
Chao Liu committed
24
25
26
27
    {
    }
};

Chao Liu's avatar
Chao Liu committed
28
// Emulate if constexpr
Chao Liu's avatar
Chao Liu committed
29
30
template <bool>
struct static_if;
Chao Liu's avatar
Chao Liu committed
31
32
33
34
35
36

template <>
struct static_if<true>
{
    using Type = static_if<true>;

Chao Liu's avatar
Chao Liu committed
37
    template <typename F>
Chao Liu's avatar
Chao Liu committed
38
39
40
    __host__ __device__ constexpr auto operator()(F f) const
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
41
        //   Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will use it,
Chao Liu's avatar
Chao Liu committed
42
43
        //   this will make "f" a generic lambda, so that "f" won't be compiled until being
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
44
45
46
47
        f(forwarder{});
        return Type{};
    }

Chao Liu's avatar
Chao Liu committed
48
    template <typename F>
49
    __host__ __device__ static constexpr auto Else(F)
Chao Liu's avatar
Chao Liu committed
50
51
52
53
54
55
56
57
58
59
    {
        return Type{};
    }
};

template <>
struct static_if<false>
{
    using Type = static_if<false>;

Chao Liu's avatar
Chao Liu committed
60
    template <typename F>
Chao Liu's avatar
Chao Liu committed
61
62
63
64
65
    __host__ __device__ constexpr auto operator()(F) const
    {
        return Type{};
    }

Chao Liu's avatar
Chao Liu committed
66
    template <typename F>
67
    __host__ __device__ static constexpr auto Else(F f)
Chao Liu's avatar
Chao Liu committed
68
69
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
70
        //   Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will use it,
Chao Liu's avatar
Chao Liu committed
71
72
        //   this will make "f" a generic lambda, so that "f" won't be compiled until being
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
73
74
75
76
        f(forwarder{});
        return Type{};
    }
};
77

Chao Liu's avatar
Chao Liu committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
template <bool predicate, class X, class Y>
struct conditional;

template <class X, class Y>
struct conditional<true, X, Y>
{
    using type = X;
};

template <class X, class Y>
struct conditional<false, X, Y>
{
    using type = Y;
};

template <bool predicate, class X, class Y>
using conditional_t = typename conditional<predicate, X, Y>::type;

96
97
} // namespace ck
#endif