diff --git a/include/rest_rpc/router.h b/include/rest_rpc/router.h index 8f7d268..bd1449c 100644 --- a/include/rest_rpc/router.h +++ b/include/rest_rpc/router.h @@ -154,52 +154,40 @@ class router : asio::noncopyable { result = msgpack_codec::pack_args_str(result_code::OK, r); } - template struct invoker { - static inline void apply(const Function &func, - std::weak_ptr conn, - nonstd::string_view str, std::string &result) { + template + void register_nonmember_func(uint32_t key, Function f) { + this->map_invokers_[key] = [f](std::weak_ptr conn, + nonstd::string_view str, + std::string &result) { using args_tuple = typename function_traits::bare_tuple_type; msgpack_codec codec; try { auto tp = codec.unpack(str.data(), str.size()); - call(func, conn, result, std::move(tp)); + call(f, conn, result, std::move(tp)); } catch (std::invalid_argument &e) { result = codec.pack_args_str(result_code::FAIL, e.what()); } catch (const std::exception &e) { result = codec.pack_args_str(result_code::FAIL, e.what()); } - } + }; + } - template - static inline void apply_member(const Function &func, Self *self, - std::weak_ptr conn, - nonstd::string_view str, - std::string &result) { + template + void register_member_func(uint32_t key, const Function &f, Self *self) { + this->map_invokers_[key] = [f, self](std::weak_ptr conn, + nonstd::string_view str, + std::string &result) { using args_tuple = typename function_traits::bare_tuple_type; msgpack_codec codec; try { auto tp = codec.unpack(str.data(), str.size()); - call_member(func, self, conn, result, std::move(tp)); + call_member(f, self, conn, result, std::move(tp)); } catch (std::invalid_argument &e) { result = codec.pack_args_str(result_code::FAIL, e.what()); } catch (const std::exception &e) { result = codec.pack_args_str(result_code::FAIL, e.what()); } - } - }; - - template - void register_nonmember_func(uint32_t key, Function f) { - this->map_invokers_[key] = {std::bind( - &invoker::apply, std::move(f), std::placeholders::_1, - std::placeholders::_2, std::placeholders::_3)}; - } - - template - void register_member_func(uint32_t key, const Function &f, Self *self) { - this->map_invokers_[key] = {std::bind( - &invoker::template apply_member, f, self, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)}; + }; } std::unordered_map