drogon
C++14/17-based HTTP application framework
Toggle main menu visibility
Loading...
Searching...
No Matches
FunctionTraits.h
1
14
15
#pragma once
16
17
#include <
drogon/DrObject.h
>
18
#include <
drogon/RequestStream.h
>
19
#include <functional>
20
#include <memory>
21
#include <tuple>
22
#include <type_traits>
23
24
#ifdef __cpp_impl_coroutine
25
#include <
drogon/utils/coroutine.h
>
26
#endif
27
28
namespace
drogon
29
{
30
class
HttpRequest
;
31
class
HttpResponse
;
32
using
HttpRequestPtr = std::shared_ptr<HttpRequest>;
33
using
HttpResponsePtr = std::shared_ptr<HttpResponse>;
34
35
namespace
internal
36
{
37
#ifdef __cpp_impl_coroutine
38
template
<
typename
T>
39
using
resumable_type
= is_resumable<T>;
40
#else
41
template
<
typename
T>
42
struct
resumable_type
: std::false_type
43
{
44
};
45
#endif
46
47
template
<
typename
>
48
struct
FunctionTraits
;
49
50
//
51
// Basic match, inherited by all other matches
52
//
53
template
<
typename
ReturnType,
typename
... Arguments>
54
struct
FunctionTraits
<ReturnType (*)(Arguments...)>
55
{
56
using
result_type = ReturnType;
57
58
template
<std::
size_t
Index>
59
using
argument =
60
typename
std::tuple_element_t<Index, std::tuple<Arguments...>>;
61
62
static
const
std::size_t arity =
sizeof
...(Arguments);
63
using
class_type = void;
64
using
return_type = ReturnType;
65
static
const
bool
isHTTPFunction =
false
;
66
static
const
bool
isClassFunction =
false
;
67
static
const
bool
isStreamHandler =
false
;
68
static
const
bool
isDrObjectClass =
false
;
69
static
const
bool
isCoroutine =
false
;
70
71
static
const
std::string name()
72
{
73
return
std::string(
"Normal or Static Function"
);
74
}
75
};
76
77
//
78
// Match normal functions
79
//
80
81
// normal function for HTTP handling
82
template
<
typename
ReturnType,
typename
... Arguments>
83
struct
FunctionTraits
<
84
ReturnType (*)(
const
HttpRequestPtr &req,
85
std::function<
void
(
const
HttpResponsePtr &)> &&callback,
86
Arguments...)> :
FunctionTraits
<ReturnType (*)(Arguments...)>
87
{
88
static
const
bool
isHTTPFunction = !
resumable_type<ReturnType>::value
;
89
static
const
bool
isCoroutine =
false
;
90
using
class_type = void;
91
using
first_param_type = HttpRequestPtr;
92
using
return_type = ReturnType;
93
};
94
95
// normal function with custom request object
96
template
<
typename
T,
typename
ReturnType,
typename
... Arguments>
97
struct
FunctionTraits
<
98
ReturnType (*)(T &&customReq,
99
std::function<
void
(
const
HttpResponsePtr &)> &&callback,
100
Arguments...)> :
FunctionTraits
<ReturnType (*)(Arguments...)>
101
{
102
static
const
bool
isHTTPFunction = !
resumable_type<ReturnType>::value
;
103
static
const
bool
isCoroutine =
false
;
104
using
class_type = void;
105
using
first_param_type = T;
106
using
return_type = ReturnType;
107
};
108
109
// normal function with stream handler
110
template
<
typename
ReturnType,
typename
... Arguments>
111
struct
FunctionTraits
<
112
ReturnType (*)(
const
HttpRequestPtr &req,
113
RequestStreamPtr &&streamCtx,
114
std::function<
void
(
const
HttpResponsePtr &)> &&callback,
115
Arguments...)> :
FunctionTraits
<ReturnType (*)(Arguments...)>
116
{
117
static
const
bool
isHTTPFunction = !
resumable_type<ReturnType>::value
;
118
static
const
bool
isCoroutine =
false
;
119
static
const
bool
isStreamHandler =
true
;
120
using
class_type = void;
121
using
first_param_type = HttpRequestPtr;
122
using
return_type = ReturnType;
123
};
124
125
//
126
// Match functor,lambda,std::function... inherits normal function matches
127
//
128
template
<
typename
Function>
129
struct
FunctionTraits
130
:
public
FunctionTraits
<
131
decltype(&std::remove_reference_t<Function>::operator())>
132
{
133
static
const
bool
isClassFunction =
false
;
134
static
const
bool
isDrObjectClass =
false
;
135
using
class_type = void;
136
137
static
const
std::string name()
138
{
139
return
std::string(
"Functor"
);
140
}
141
};
142
143
//
144
// Match class functions, inherits normal function matches
145
//
146
147
// class const method
148
template
<
typename
ClassType,
typename
ReturnType,
typename
... Arguments>
149
struct
FunctionTraits
<ReturnType (ClassType::*)(Arguments...) const>
150
:
FunctionTraits
<ReturnType (*)(Arguments...)>
151
{
152
static
const
bool
isClassFunction =
true
;
153
static
const
bool
isDrObjectClass =
154
std::is_base_of<DrObject<ClassType>, ClassType>::value;
155
using
class_type = ClassType;
156
157
static
const
std::string name()
158
{
159
return
std::string(
"Class Function"
);
160
}
161
};
162
163
// class non-const method
164
template
<
typename
ClassType,
typename
ReturnType,
typename
... Arguments>
165
struct
FunctionTraits
<ReturnType (ClassType::*)(Arguments...)>
166
:
FunctionTraits
<ReturnType (*)(Arguments...)>
167
{
168
static
const
bool
isClassFunction =
true
;
169
static
const
bool
isDrObjectClass =
170
std::is_base_of<DrObject<ClassType>, ClassType>::value;
171
using
class_type = ClassType;
172
173
static
const
std::string name()
174
{
175
return
std::string(
"Class Function"
);
176
}
177
};
178
179
//
180
// Match coroutine functions
181
//
182
#ifdef __cpp_impl_coroutine
183
template
<
typename
... Arguments>
184
struct
FunctionTraits
<
185
AsyncTask
(*)(HttpRequestPtr req,
186
std::function<
void
(
const
HttpResponsePtr &)> callback,
187
Arguments...)> :
FunctionTraits
<
AsyncTask
(*)(Arguments...)>
188
{
189
static
const
bool
isHTTPFunction =
true
;
190
static
const
bool
isCoroutine =
true
;
191
using
class_type = void;
192
using
first_param_type = HttpRequestPtr;
193
using
return_type =
AsyncTask
;
194
};
195
196
template
<
typename
... Arguments>
197
struct
FunctionTraits<
198
Task
<> (*)(HttpRequestPtr req,
199
std::function<
void
(
const
HttpResponsePtr &)> callback,
200
Arguments...)> : FunctionTraits<
AsyncTask
(*)(Arguments...)>
201
{
202
static
const
bool
isHTTPFunction =
true
;
203
static
const
bool
isCoroutine =
true
;
204
using
class_type = void;
205
using
first_param_type = HttpRequestPtr;
206
using
return_type =
Task<>
;
207
};
208
209
template
<
typename
... Arguments>
210
struct
FunctionTraits<Task<HttpResponsePtr> (*)(HttpRequestPtr req,
211
Arguments...)>
212
: FunctionTraits<AsyncTask (*)(Arguments...)>
213
{
214
static
const
bool
isHTTPFunction =
true
;
215
static
const
bool
isCoroutine =
true
;
216
using
class_type = void;
217
using
first_param_type = HttpRequestPtr;
218
using
return_type = Task<HttpResponsePtr>;
219
};
220
#endif
221
222
//
223
// Bad matches
224
//
225
226
template
<
typename
ReturnType,
typename
... Arguments>
227
struct
FunctionTraits
<
228
ReturnType (*)(HttpRequestPtr &req,
229
std::function<
void
(
const
HttpResponsePtr &)> &&callback,
230
Arguments...)> :
FunctionTraits
<ReturnType (*)(Arguments...)>
231
{
232
static
const
bool
isHTTPFunction =
false
;
233
using
class_type = void;
234
};
235
236
template
<
typename
ReturnType,
typename
... Arguments>
237
struct
FunctionTraits
<
238
ReturnType (*)(HttpRequestPtr &&req,
239
std::function<
void
(
const
HttpResponsePtr &)> &&callback,
240
Arguments...)> :
FunctionTraits
<ReturnType (*)(Arguments...)>
241
{
242
static
const
bool
isHTTPFunction =
false
;
243
using
class_type = void;
244
};
245
246
}
// namespace internal
247
}
// namespace drogon
DrObject.h
RequestStream.h
drogon::HttpRequest
Abstract class for webapp developer to get or set the Http request;.
Definition
HttpRequest.h:81
drogon::HttpResponse
Definition
HttpResponse.h:117
coroutine.h
drogon
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition
Attribute.h:23
drogon::AsyncTask
Definition
coroutine.h:355
drogon::Task
Definition
coroutine.h:161
drogon::internal::FunctionTraits
Definition
FunctionTraits.h:132
drogon::internal::resumable_type
Definition
FunctionTraits.h:43
lib
inc
drogon
utils
FunctionTraits.h
Generated by
1.17.0