fix ambiguous for HandlerWithResponse and Handler in set_error_handler

using lambda expression as Handler in set_error_handler will cause ambiguous.
Template forwarding can forward HandlerWithResponse to the correct overloading function
This commit is contained in:
Congcong Cai 2024-03-07 19:07:39 +08:00
parent 6791a8364d
commit 4dd2f3d03d

View file

@ -231,6 +231,7 @@ using socket_t = int;
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <thread> #include <thread>
#include <type_traits>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
@ -871,8 +872,13 @@ public:
Server &set_default_file_mimetype(const std::string &mime); Server &set_default_file_mimetype(const std::string &mime);
Server &set_file_request_handler(Handler handler); Server &set_file_request_handler(Handler handler);
Server &set_error_handler(HandlerWithResponse handler); template <class ErrorHandlerFunc>
Server &set_error_handler(Handler handler); Server &set_error_handler(ErrorHandlerFunc &&handler) {
return set_error_handler_impl(
std::forward<ErrorHandlerFunc>(handler),
std::is_convertible<ErrorHandlerFunc, HandlerWithResponse>{});
}
Server &set_exception_handler(ExceptionHandler handler); Server &set_exception_handler(ExceptionHandler handler);
Server &set_pre_routing_handler(HandlerWithResponse handler); Server &set_pre_routing_handler(HandlerWithResponse handler);
Server &set_post_routing_handler(Handler handler); Server &set_post_routing_handler(Handler handler);
@ -934,6 +940,9 @@ protected:
size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH;
private: private:
Server &set_error_handler_impl(HandlerWithResponse handler, std::true_type);
Server &set_error_handler_impl(Handler handler, std::false_type);
using Handlers = using Handlers =
std::vector<std::pair<std::unique_ptr<detail::MatcherBase>, Handler>>; std::vector<std::pair<std::unique_ptr<detail::MatcherBase>, Handler>>;
using HandlersForContentReader = using HandlersForContentReader =
@ -5797,12 +5806,14 @@ inline Server &Server::set_file_request_handler(Handler handler) {
return *this; return *this;
} }
inline Server &Server::set_error_handler(HandlerWithResponse handler) { inline Server &Server::set_error_handler_impl(HandlerWithResponse handler,
std::true_type) {
error_handler_ = std::move(handler); error_handler_ = std::move(handler);
return *this; return *this;
} }
inline Server &Server::set_error_handler(Handler handler) { inline Server &Server::set_error_handler_impl(Handler handler,
std::false_type) {
error_handler_ = [handler](const Request &req, Response &res) { error_handler_ = [handler](const Request &req, Response &res) {
handler(req, res); handler(req, res);
return HandlerResponse::Handled; return HandlerResponse::Handled;