Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/jsonrpccxx/typemapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace jsonrpccxx {
constexpr json::value_t GetType(type<T>) {
return json::value_t::object;
}
constexpr json::value_t GetType(type<void>) { return json::value_t::null; }
constexpr json::value_t GetType(type<std::string>) { return json::value_t::string; }
constexpr json::value_t GetType(type<bool>) { return json::value_t::boolean; }
constexpr json::value_t GetType(type<float>) { return json::value_t::number_float; }
Expand Down Expand Up @@ -148,6 +149,22 @@ namespace jsonrpccxx {
//
// Mapping for classes
//
template <typename T, typename ReturnType, typename... ParamTypes>
MethodHandle methodHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {
return (instance.*method)(std::forward<ParamTypes>(params)...);
};
return methodHandle(function);
}

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

template <typename T, typename ReturnType, typename... ParamTypes>
MethodHandle GetHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {
Expand Down
12 changes: 12 additions & 0 deletions test/typemapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ TEST_CASE("test class member binding", TEST_MODULE) {
CHECK(notifyResult == "Hello world: someone");
}

TEST_CASE("test class member explicit binding", TEST_MODULE) {
SomeClass instance;
MethodHandle mh = methodHandle(&SomeClass::add, instance);
CHECK(mh(R"([3, 4])"_json) == 7);

notifyResult = "";
NotificationHandle mh2 = notificationHandle(&SomeClass::notify, instance);
CHECK(notifyResult.empty());
mh2(R"(["someone"])"_json);
CHECK(notifyResult == "Hello world: someone");
}

TEST_CASE("test incorrect params", TEST_MODULE) {
SomeClass instance;
MethodHandle mh = GetHandle(&SomeClass::add, instance);
Expand Down