Skip to content

Commit 40cbc63

Browse files
committed
mod: wrapper decluttering + function_traits from arx dependency
1 parent ae2c885 commit 40cbc63

File tree

2 files changed

+52
-71
lines changed

2 files changed

+52
-71
lines changed

src/rpclite_utils.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
#ifndef RPCLITE_UTILS_H
3+
#define RPCLITE_UTILS_H
4+
5+
#include <tuple>
6+
#include <utility>
7+
8+
namespace RpcUtils {
9+
namespace detail {
10+
11+
template<typename T>
12+
bool deserialize_single(MsgPack::Unpacker& unpacker, T& value) {
13+
if (!unpacker.unpackable(value)) return false;
14+
unpacker.deserialize(value);
15+
return true;
16+
}
17+
18+
template<std::size_t I = 0, typename... Ts>
19+
typename std::enable_if<I == sizeof...(Ts), bool>::type
20+
deserialize_tuple(MsgPack::Unpacker&, std::tuple<Ts...>&) {
21+
return true;
22+
}
23+
24+
template<std::size_t I = 0, typename... Ts>
25+
typename std::enable_if<I < sizeof...(Ts), bool>::type
26+
deserialize_tuple(MsgPack::Unpacker& unpacker, std::tuple<Ts...>& out) {
27+
if (!deserialize_single(unpacker, std::get<I>(out))) return false;
28+
return deserialize_tuple<I + 1>(unpacker, out);
29+
}
30+
31+
template<typename... Ts>
32+
bool deserialize_all(MsgPack::Unpacker& unpacker, std::tuple<Ts...>& values) {
33+
return deserialize_tuple(unpacker, values);
34+
}
35+
36+
// Helper to invoke a function with a tuple of arguments
37+
template<typename F, typename Tuple, std::size_t... I>
38+
auto invoke_with_tuple(F&& f, Tuple&& t, arx::stdx::index_sequence<I...>)
39+
-> decltype(f(std::get<I>(std::forward<Tuple>(t))...)) {
40+
return f(std::get<I>(std::forward<Tuple>(t))...);
41+
}
42+
43+
} // namespace detail
44+
} // namespace RpcUtils
45+
46+
#endif //RPCLITE_UTILS_H

src/wrapper.h

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,14 @@
22
#define RPCLITE_WRAPPER_H
33

44
#include "error.h"
5+
#include "rpclite_utils.h"
6+
7+
using namespace RpcUtils::detail;
58

69
#ifdef HANDLE_RPC_ERRORS
710
#include <stdexcept>
811
#endif
912

10-
//TODO maybe use arx::function_traits
11-
12-
// C++11-compatible function_traits
13-
// Primary template: fallback
14-
template<typename T>
15-
struct function_traits;
16-
17-
// Function pointer
18-
template<typename R, typename... Args>
19-
struct function_traits<R(*)(Args...)> {
20-
using signature = R(Args...);
21-
};
22-
23-
// std::function
24-
template<typename R, typename... Args>
25-
struct function_traits<std::function<R(Args...)>> {
26-
using signature = R(Args...);
27-
};
28-
29-
// Member function pointer (including lambdas)
30-
template<typename C, typename R, typename... Args>
31-
struct function_traits<R(C::*)(Args...) const> {
32-
using signature = R(Args...);
33-
};
34-
35-
// Deduction helper for lambdas
36-
template<typename T>
37-
struct function_traits {
38-
using signature = typename function_traits<decltype(&T::operator())>::signature;
39-
};
40-
41-
42-
// Helper to invoke a function with a tuple of arguments
43-
template<typename F, typename Tuple, std::size_t... I>
44-
auto invoke_with_tuple(F&& f, Tuple&& t, arx::stdx::index_sequence<I...>)
45-
-> decltype(f(std::get<I>(std::forward<Tuple>(t))...)) {
46-
return f(std::get<I>(std::forward<Tuple>(t))...);
47-
};
48-
4913
template<typename F>
5014
class RpcFunctionWrapper;
5115

@@ -56,7 +20,7 @@ class IFunctionWrapper {
5620
};
5721

5822
template<typename R, typename... Args>
59-
class RpcFunctionWrapper<R(Args...)>: public IFunctionWrapper {
23+
class RpcFunctionWrapper<std::function<R(Args...)>>: public IFunctionWrapper {
6024
public:
6125
RpcFunctionWrapper(std::function<R(Args...)> func) : _func(func) {}
6226

@@ -133,41 +97,12 @@ class RpcFunctionWrapper<R(Args...)>: public IFunctionWrapper {
13397
packer.serialize(nil, out);
13498
return true;
13599
}
136-
137-
template<size_t I = 0, typename... Ts>
138-
inline typename std::enable_if<I == sizeof...(Ts), bool>::type
139-
deserialize_tuple(MsgPack::Unpacker& unpacker, std::tuple<Ts...>& out) {
140-
(void)unpacker; // silence unused parameter warning
141-
(void)out;
142-
return true; // Base case
143-
}
144-
145-
template<size_t I = 0, typename... Ts>
146-
inline typename std::enable_if<I < sizeof...(Ts), bool>::type
147-
deserialize_tuple(MsgPack::Unpacker& unpacker, std::tuple<Ts...>& out) {
148-
if (!deserialize_single(unpacker, std::get<I>(out))) {
149-
return false;
150-
}
151-
return deserialize_tuple<I+1>(unpacker, out); // Recursive unpacking
152-
}
153-
154-
template<typename... Ts>
155-
bool deserialize_all(MsgPack::Unpacker& unpacker, std::tuple<Ts...>& values) {
156-
return deserialize_tuple(unpacker, values);
157-
}
158-
159-
template<typename T>
160-
static bool deserialize_single(MsgPack::Unpacker& unpacker, T& value) {
161-
if (!unpacker.unpackable(value)) return false;
162-
unpacker.deserialize(value);
163-
return true;
164-
}
165100
};
166101

167102

168103
template<typename F>
169-
auto wrap(F&& f) -> RpcFunctionWrapper<typename function_traits<typename std::decay<F>::type>::signature> {
170-
using Signature = typename function_traits<typename std::decay<F>::type>::signature;
104+
auto wrap(F&& f) -> RpcFunctionWrapper<typename arx::function_traits<typename std::decay<F>::type>::function_type> {
105+
using Signature = typename arx::function_traits<typename std::decay<F>::type>::function_type;
171106
return RpcFunctionWrapper<Signature>(std::forward<F>(f));
172107
};
173108

0 commit comments

Comments
 (0)