Skip to content

Commit 24442a3

Browse files
authored
Merge pull request #8 from ZaMaZaN4iK/feature/void_methods
Add possibility to register void methods as a JSON-RPC message
2 parents 77cba6a + 6a9cdf5 commit 24442a3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/jsonrpccxx/typemapper.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace jsonrpccxx {
2424
constexpr json::value_t GetType(type<T>) {
2525
return json::value_t::object;
2626
}
27+
constexpr json::value_t GetType(type<void>) { return json::value_t::null; }
2728
constexpr json::value_t GetType(type<std::string>) { return json::value_t::string; }
2829
constexpr json::value_t GetType(type<bool>) { return json::value_t::boolean; }
2930
constexpr json::value_t GetType(type<float>) { return json::value_t::number_float; }
@@ -148,6 +149,22 @@ namespace jsonrpccxx {
148149
//
149150
// Mapping for classes
150151
//
152+
template <typename T, typename ReturnType, typename... ParamTypes>
153+
MethodHandle methodHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
154+
std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {
155+
return (instance.*method)(std::forward<ParamTypes>(params)...);
156+
};
157+
return methodHandle(function);
158+
}
159+
160+
template <typename T, typename... ParamTypes>
161+
NotificationHandle notificationHandle(void (T::*method)(ParamTypes...), T &instance) {
162+
std::function<void(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> void {
163+
return (instance.*method)(std::forward<ParamTypes>(params)...);
164+
};
165+
return notificationHandle(function);
166+
}
167+
151168
template <typename T, typename ReturnType, typename... ParamTypes>
152169
MethodHandle GetHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
153170
std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {

test/typemapper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ TEST_CASE("test class member binding", TEST_MODULE) {
4343
CHECK(notifyResult == "Hello world: someone");
4444
}
4545

46+
TEST_CASE("test class member explicit binding", TEST_MODULE) {
47+
SomeClass instance;
48+
MethodHandle mh = methodHandle(&SomeClass::add, instance);
49+
CHECK(mh(R"([3, 4])"_json) == 7);
50+
51+
notifyResult = "";
52+
NotificationHandle mh2 = notificationHandle(&SomeClass::notify, instance);
53+
CHECK(notifyResult.empty());
54+
mh2(R"(["someone"])"_json);
55+
CHECK(notifyResult == "Hello world: someone");
56+
}
57+
4658
TEST_CASE("test incorrect params", TEST_MODULE) {
4759
SomeClass instance;
4860
MethodHandle mh = GetHandle(&SomeClass::add, instance);

0 commit comments

Comments
 (0)