Skip to content

Commit 3887871

Browse files
committed
Support const methods (Closes #45)
1 parent 06e4752 commit 3887871

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

include/jsonrpccxx/typemapper.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,22 @@ namespace jsonrpccxx {
197197
};
198198
return GetHandle(function);
199199
}
200+
201+
template<typename T, typename ReturnType, typename... ParamTypes>
202+
MethodHandle GetHandle(ReturnType (T::*method)(ParamTypes...) const, const T &instance)
203+
{
204+
std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&...params) -> ReturnType {
205+
return (instance.*method)(std::forward<ParamTypes>(params)...);
206+
};
207+
return GetHandle(function);
208+
}
209+
210+
template<typename T, typename... ParamTypes>
211+
NotificationHandle GetHandle(void (T::*method)(ParamTypes...) const, const T &instance)
212+
{
213+
std::function<void(ParamTypes...)> function = [&instance, method](ParamTypes &&...params) -> void {
214+
return (instance.*method)(std::forward<ParamTypes>(params)...);
215+
};
216+
return GetHandle(function);
217+
}
200218
}

test/typemapper.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class SomeClass {
1717
void notify(const std::string &hello) { notifyResult = string("Hello world: ") + hello; }
1818
};
1919

20+
class SomeClassConst {
21+
public:
22+
int add(int a, int b) const { return a + b; }
23+
void notify(const std::string &hello) { notifyResult = string("Hello world: ") + hello; }
24+
};
25+
26+
2027
TEST_CASE("test function binding") {
2128
MethodHandle mh = GetHandle(&add);
2229
CHECK(mh(R"([3, 4])"_json) == 7);
@@ -40,6 +47,12 @@ TEST_CASE("test class member binding") {
4047
CHECK(notifyResult == "Hello world: someone");
4148
}
4249

50+
TEST_CASE("test const class member binding") {
51+
SomeClassConst instance;
52+
MethodHandle mh = GetHandle(&SomeClassConst::add, instance);
53+
CHECK(mh(R"([3, 4])"_json) == 7);
54+
}
55+
4356
TEST_CASE("test class member explicit binding") {
4457
SomeClass instance;
4558
MethodHandle mh = methodHandle(&SomeClass::add, instance);

0 commit comments

Comments
 (0)