jsonrpcx/json-rpc-cxx

[BUG] - Using GetHandle with a member function using a const qualifier doesn't work

phelter opened this issue · 2 comments

Say I have an object:

class Foo {
public:
    int bar() const {return 42;}
};

Using the MethodHandle GetHandle(&Foo::bar, foo) templated function doesn't work because there is no templated function for const functions.

Could you please add the following to your list of templated types:

namespace jsonrpccxx
{
template<typename T, typename ReturnType, typename... ParamTypes>
MethodHandle GetHandle(ReturnType (T::*method)(ParamTypes...) const, const T &instance)
{
    std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&...params) -> ReturnType {
        return (instance.*method)(std::forward<ParamTypes>(params)...);
    };
    return GetHandle(function);
}

template<typename T, typename... ParamTypes>
NotificationHandle GetHandle(void (T::*method)(ParamTypes...) const, const T &instance)
{
    std::function<void(ParamTypes...)> function = [&instance, method](ParamTypes &&...params) -> void {
        return (instance.*method)(std::forward<ParamTypes>(params)...);
    };
    return GetHandle(function);
}
} // namespace jsonrpccxx

This will allow const functions of a const instance to be called as well.

I believe these should be in include/jsonrpccxx/typemapper.hpp

Thank you, I integrated it.