aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/functiontraits.h
blob: 3bb3b354481e1abab01db7ad4152d9cecdde38c7 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include <tuple>

namespace Utils {

/*
    struct functionTraits<Function>
    {
        using ResultType; // Return type of Function
        struct argument<unsigned index>
        {
            using type; // type of Function argument at index (starting with 0)
        }
    }

    struct functionTakesArgument<Function, unsigned index, ArgumentType>;

    Is derived from std::true_type if Function takes an argument of type ArgumentType at index.
    Otherwise derived from std::false_type.
*/

////////////////////
// functionTraits
////////////////////

// for callables. defined below.
template <typename Callable>
struct functionTraits;

// function
template <typename Result, typename... Args>
struct functionTraits<Result(Args...)>
{
    using ResultType = Result;
    static const unsigned arity = sizeof...(Args); // TODO const -> constexpr with MSVC2015

    template <unsigned i>
    struct argument
    {
        using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
    };
};

// function pointer
template <typename Result, typename... Args>
struct functionTraits<Result(*)(Args...)> : public functionTraits<Result(Args...)>
{
};

// const function pointer
template <typename Result, typename... Args>
struct functionTraits<Result(* const)(Args...)> : public functionTraits<Result(Args...)>
{
};

// member function
template <typename Type, typename Result, typename... Args>
struct functionTraits<Result(Type::*)(Args...)> : public functionTraits<Result(Type&,Args...)>
{
};

// const member function
template <typename Type, typename Result, typename... Args>
struct functionTraits<Result(Type::*)(Args...) const> : public functionTraits<Result(Type&,Args...)>
{
};

// const pointer to member function
template <typename Type, typename Result, typename... Args>
struct functionTraits<Result(Type::* const)(Args...)> : public functionTraits<Result(Type&,Args...)>
{
};

// const pointer to const member function
template <typename Type, typename Result, typename... Args>
struct functionTraits<Result(Type::* const)(Args...) const> : public functionTraits<Result(Type&,Args...)>
{
};

// TODO: enable lvalue and rvalue ref member function later (MSVC 2015?)
//// lvalue ref member function
//template <typename Type, typename Result, typename... Args>
//struct functionTraits<Result(Type::*)(Args...) &> : public functionTraits<Result(Type&,Args...)>
//{
//};

//// const lvalue ref member function
//template <typename Type, typename Result, typename... Args>
//struct functionTraits<Result(Type::*)(Args...) const &> : public functionTraits<Result(Type&,Args...)>
//{
//};

//// rvalue ref member function
//template <typename Type, typename Result, typename... Args>
//struct functionTraits<Result(Type::*)(Args...) &&> : public functionTraits<Result(Type&,Args...)>
//{
//};

// callables. only works if operator() is not overloaded.
template <typename Callable>
struct functionTraits
{
    using callableTraits = functionTraits<decltype(&Callable::operator())>;
    using ResultType = typename callableTraits::ResultType;
    static const unsigned arity = callableTraits::arity - 1; // ignore object pointer arg // TODO const -> constexpr with MSVC2015

    template <unsigned i>
    struct argument
    {
        using type = typename callableTraits::template argument<i+1>::type; // ignore object pointer arg
    };
};

// lvalue ref callables
template <typename Callable>
struct functionTraits<Callable&> : public functionTraits<Callable>
{
};

// const lvalue ref callables
template <typename Callable>
struct functionTraits<const Callable&> : public functionTraits<Callable>
{
};

// rvalue ref callables
template <typename Callable>
struct functionTraits<Callable&&> : public functionTraits<Callable>
{
};

template <typename F>
using functionResult_t = typename functionTraits<F>::ResultType;

////////////////////
// functionTakesArgument
////////////////////
namespace Internal {

template <typename HasArity/*true_type or false_type*/,
          typename Function, unsigned index, typename T>
struct functionTakesArgumentArityDispatch;

template <typename Function, unsigned index, typename T>
struct functionTakesArgumentArityDispatch<std::false_type, Function, index, T>
        : public std::false_type
{
};

template <typename Function, unsigned index, typename T>
struct functionTakesArgumentArityDispatch<std::true_type, Function, index, T>
        : public std::is_same<T, typename functionTraits<Function>::template argument<index>::type>
{
};

} // Internal

template <typename Function, unsigned index, typename T>
struct functionTakesArgument : public Internal::functionTakesArgumentArityDispatch<
        std::integral_constant<bool, (functionTraits<Function>::arity > index)>,
        Function, index, T>
{
};

} // Utils