Here's the situation, I want to map Method
enum and lambda function to make a simple web server, but I'm stuck now.
I can't find a way to map enum and lambda funciton, can anyone give an example?
Also I tried to use function pointer, but I got segmentation fault
, so what's the correct way to do this trick?
Need your help, any help would be great appreciated.
method.cpp
enum class Method {
Delete = 0,
Get,
Head,
Post,
Put,
Connect,
Options,
Trace,
Patch,
Purge,
InternalMethodCount,
};
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
dispatcher.cpp
// typedef void (*request_handler)(const Request &, Response &);
typedef void (*request_handler)(const Request &, Response &);
typedef Match self_type;
self_type &get(request_handler handler) {
_handlers.insert(std::make_pair(Method::Get, handler));
return *this;
}
std::unordered_map<Method, request_handler, EnumClassHash> _handlers;
main.cpp
simple_handler.crud_match(std::string("/ok"))
.get([&](const Request &request,Response &response){
response.body = request.url;
});
Aucun commentaire:
Enregistrer un commentaire