File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -197,4 +197,22 @@ namespace jsonrpccxx {
197
197
};
198
198
return GetHandle (function);
199
199
}
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
+ }
200
218
}
Original file line number Diff line number Diff line change @@ -17,6 +17,13 @@ class SomeClass {
17
17
void notify (const std::string &hello) { notifyResult = string (" Hello world: " ) + hello; }
18
18
};
19
19
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
+
20
27
TEST_CASE (" test function binding" ) {
21
28
MethodHandle mh = GetHandle (&add);
22
29
CHECK (mh (R"( [3, 4])" _json) == 7 );
@@ -40,6 +47,12 @@ TEST_CASE("test class member binding") {
40
47
CHECK (notifyResult == " Hello world: someone" );
41
48
}
42
49
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
+
43
56
TEST_CASE (" test class member explicit binding" ) {
44
57
SomeClass instance;
45
58
MethodHandle mh = methodHandle (&SomeClass::add, instance);
You can’t perform that action at this time.
0 commit comments