drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
FunctionTraits.h
1
14
15#pragma once
16
17#include <drogon/DrObject.h>
19#include <functional>
20#include <memory>
21#include <tuple>
22#include <type_traits>
23
24#ifdef __cpp_impl_coroutine
26#endif
27
28namespace drogon
29{
30class HttpRequest;
31class HttpResponse;
32using HttpRequestPtr = std::shared_ptr<HttpRequest>;
33using HttpResponsePtr = std::shared_ptr<HttpResponse>;
34
35namespace internal
36{
37#ifdef __cpp_impl_coroutine
38template <typename T>
39using resumable_type = is_resumable<T>;
40#else
41template <typename T>
42struct resumable_type : std::false_type
43{
44};
45#endif
46
47template <typename>
48struct FunctionTraits;
49
50//
51// Basic match, inherited by all other matches
52//
53template <typename ReturnType, typename... Arguments>
54struct 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
82template <typename ReturnType, typename... Arguments>
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
96template <typename T, typename ReturnType, typename... Arguments>
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
110template <typename ReturnType, typename... Arguments>
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//
128template <typename Function>
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
148template <typename ClassType, typename ReturnType, typename... Arguments>
149struct 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
164template <typename ClassType, typename ReturnType, typename... Arguments>
165struct 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
183template <typename... Arguments>
184struct 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
196template <typename... Arguments>
197struct 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
209template <typename... Arguments>
210struct 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
226template <typename ReturnType, typename... Arguments>
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
236template <typename ReturnType, typename... Arguments>
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
Abstract class for webapp developer to get or set the Http request;.
Definition HttpRequest.h:81
Definition HttpResponse.h:117
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
Definition coroutine.h:355
Definition coroutine.h:161
Definition FunctionTraits.h:132
Definition FunctionTraits.h:43