aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/function.h
blob: 654b0a4a338271569065f935d60ad36643523f7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once

#include "typelist.h"

#include <functional>

namespace NPrivate {
    template <class F>
    struct TRemoveClassImpl {
        using TSignature = F;
    };

#define Y_EMPTY_REF_QUALIFIER
#define Y_FOR_EACH_REF_QUALIFIERS_COMBINATION(XX) \
    XX(Y_EMPTY_REF_QUALIFIER)                     \
    XX(&)                                         \
    XX(&&)                                        \
    XX(const)                                     \
    XX(const&)                                    \
    XX(const&&)

#define Y_DECLARE_REMOVE_CLASS_IMPL(qualifiers)             \
    template <typename C, typename R, typename... Args>     \
    struct TRemoveClassImpl<R (C::*)(Args...) qualifiers> { \
        typedef R TSignature(Args...);                      \
    };

    Y_FOR_EACH_REF_QUALIFIERS_COMBINATION(Y_DECLARE_REMOVE_CLASS_IMPL)
#undef Y_DECLARE_REMOVE_CLASS_IMPL

    template <class T>
    struct TRemoveNoExceptImpl {
        using Type = T;
    };

    template <typename R, typename... Args>
    struct TRemoveNoExceptImpl<R(Args...) noexcept> {
        using Type = R(Args...);
    };

#define Y_DECLARE_REMOVE_NOEXCEPT_IMPL(qualifiers)                      \
    template <typename R, typename C, typename... Args>                 \
    struct TRemoveNoExceptImpl<R (C::*)(Args...) qualifiers noexcept> { \
        using Type = R (C::*)(Args...);                                 \
    };

    Y_FOR_EACH_REF_QUALIFIERS_COMBINATION(Y_DECLARE_REMOVE_NOEXCEPT_IMPL)
#undef Y_DECLARE_REMOVE_NOEXCEPT_IMPL

#undef Y_FOR_EACH_REF_QUALIFIERS_COMBINATION
#undef Y_EMPTY_REF_QUALIFIER

    template <class T>
    using TRemoveNoExcept = typename TRemoveNoExceptImpl<T>::Type;

    template <class F>
    using TRemoveClass = typename TRemoveClassImpl<TRemoveNoExcept<F>>::TSignature;

    template <class C>
    struct TFuncInfo {
        using TSignature = TRemoveClass<decltype(&C::operator())>;
    };

    template <class R, typename... Args>
    struct TFuncInfo<R(Args...)> {
        typedef R TSignature(Args...);
    };
} // namespace NPrivate

template <class C>
using TFunctionSignature = typename ::NPrivate::TFuncInfo<::NPrivate::TRemoveClass<std::remove_reference_t<std::remove_pointer_t<C>>>>::TSignature;

template <typename F>
struct TCallableTraits: public TCallableTraits<TFunctionSignature<F>> {
};

template <typename R, typename... Args>
struct TCallableTraits<R(Args...)> {
    using TResult = R;
    using TArgs = TTypeList<Args...>;
    typedef R TSignature(Args...);
};

template <typename C>
using TFunctionResult = typename TCallableTraits<C>::TResult;

template <typename C>
using TFunctionArgs = typename TCallableTraits<C>::TArgs;

template <typename C, size_t N>
struct TFunctionArgImpl {
    using TArgs = TFunctionArgs<C>;
    using TResult = typename TArgs::template TGet<N>;
};

template <typename C, size_t N>
using TFunctionArg = typename TFunctionArgImpl<C, N>::TResult;