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-
4913template <typename F>
5014class RpcFunctionWrapper ;
5115
@@ -56,7 +20,7 @@ class IFunctionWrapper {
5620 };
5721
5822template <typename R, typename ... Args>
59- class RpcFunctionWrapper <R(Args...)>: public IFunctionWrapper {
23+ class RpcFunctionWrapper <std::function< R(Args...)> >: public IFunctionWrapper {
6024public:
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
168103template <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