Skip to content

Instantly share code, notes, and snippets.

@Jackarain
Created March 12, 2019 06:09
Show Gist options
  • Save Jackarain/38778f7d5de9bd759919ad73a890bd7e to your computer and use it in GitHub Desktop.
Save Jackarain/38778f7d5de9bd759919ad73a890bd7e to your computer and use it in GitHub Desktop.
anycall.cpp
template <typename Handler, typename Request, typename Reply>
class type_erasure_handler
{
public:
type_erasure_handler(Handler&& handler)
: handler_(std::forward<Handler>(handler))
{}
~type_erasure_handler()
{}
static void true_func_call(std::any handler,
const ::google::protobuf::Message& req, ::google::protobuf::Message& ret)
{
auto this_object = std::any_cast<type_erasure_handler<Handler, Request, Reply>>(handler);
this_object.handler_(
static_cast<const Request&>(req), static_cast<Reply&>(ret));
}
Handler handler_;
};
typedef void(*type_erasure_call_function)(std::any handler,
const ::google::protobuf::Message& req, ::google::protobuf::Message& ret);
type_erasure_call_function func_call_;
std::any handler_;
handler_ = type_erasure_handler<Handler, Request, Reply>(std::forward<Handler>(handler));
func_call_ = type_erasure_handler<Handler, Request, Reply>::true_func_call;
func_call_(handler_, *msg, *ret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment