From 40cbc63f5ddc3b6db89a510b6240ac2243e19153 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 30 May 2025 16:32:43 +0200 Subject: [PATCH] mod: wrapper decluttering + function_traits from arx dependency --- src/rpclite_utils.h | 46 +++++++++++++++++++++++++++ src/wrapper.h | 77 ++++----------------------------------------- 2 files changed, 52 insertions(+), 71 deletions(-) create mode 100644 src/rpclite_utils.h diff --git a/src/rpclite_utils.h b/src/rpclite_utils.h new file mode 100644 index 0000000..153f596 --- /dev/null +++ b/src/rpclite_utils.h @@ -0,0 +1,46 @@ +#pragma once +#ifndef RPCLITE_UTILS_H +#define RPCLITE_UTILS_H + +#include +#include + +namespace RpcUtils { +namespace detail { + +template +bool deserialize_single(MsgPack::Unpacker& unpacker, T& value) { + if (!unpacker.unpackable(value)) return false; + unpacker.deserialize(value); + return true; +} + +template +typename std::enable_if::type +deserialize_tuple(MsgPack::Unpacker&, std::tuple&) { + return true; +} + +template +typename std::enable_if::type +deserialize_tuple(MsgPack::Unpacker& unpacker, std::tuple& out) { + if (!deserialize_single(unpacker, std::get(out))) return false; + return deserialize_tuple(unpacker, out); +} + +template +bool deserialize_all(MsgPack::Unpacker& unpacker, std::tuple& values) { + return deserialize_tuple(unpacker, values); +} + +// Helper to invoke a function with a tuple of arguments +template +auto invoke_with_tuple(F&& f, Tuple&& t, arx::stdx::index_sequence) + -> decltype(f(std::get(std::forward(t))...)) { + return f(std::get(std::forward(t))...); +} + +} // namespace detail +} // namespace RpcUtils + +#endif //RPCLITE_UTILS_H \ No newline at end of file diff --git a/src/wrapper.h b/src/wrapper.h index b43fab3..3d317be 100644 --- a/src/wrapper.h +++ b/src/wrapper.h @@ -2,50 +2,14 @@ #define RPCLITE_WRAPPER_H #include "error.h" +#include "rpclite_utils.h" + +using namespace RpcUtils::detail; #ifdef HANDLE_RPC_ERRORS #include #endif -//TODO maybe use arx::function_traits - -// C++11-compatible function_traits -// Primary template: fallback -template -struct function_traits; - -// Function pointer -template -struct function_traits { - using signature = R(Args...); -}; - -// std::function -template -struct function_traits> { - using signature = R(Args...); -}; - -// Member function pointer (including lambdas) -template -struct function_traits { - using signature = R(Args...); -}; - -// Deduction helper for lambdas -template -struct function_traits { - using signature = typename function_traits::signature; -}; - - -// Helper to invoke a function with a tuple of arguments -template -auto invoke_with_tuple(F&& f, Tuple&& t, arx::stdx::index_sequence) - -> decltype(f(std::get(std::forward(t))...)) { - return f(std::get(std::forward(t))...); -}; - template class RpcFunctionWrapper; @@ -56,7 +20,7 @@ class IFunctionWrapper { }; template -class RpcFunctionWrapper: public IFunctionWrapper { +class RpcFunctionWrapper>: public IFunctionWrapper { public: RpcFunctionWrapper(std::function func) : _func(func) {} @@ -133,41 +97,12 @@ class RpcFunctionWrapper: public IFunctionWrapper { packer.serialize(nil, out); return true; } - - template - inline typename std::enable_if::type - deserialize_tuple(MsgPack::Unpacker& unpacker, std::tuple& out) { - (void)unpacker; // silence unused parameter warning - (void)out; - return true; // Base case - } - - template - inline typename std::enable_if::type - deserialize_tuple(MsgPack::Unpacker& unpacker, std::tuple& out) { - if (!deserialize_single(unpacker, std::get(out))) { - return false; - } - return deserialize_tuple(unpacker, out); // Recursive unpacking - } - - template - bool deserialize_all(MsgPack::Unpacker& unpacker, std::tuple& values) { - return deserialize_tuple(unpacker, values); - } - - template - static bool deserialize_single(MsgPack::Unpacker& unpacker, T& value) { - if (!unpacker.unpackable(value)) return false; - unpacker.deserialize(value); - return true; - } }; template -auto wrap(F&& f) -> RpcFunctionWrapper::type>::signature> { - using Signature = typename function_traits::type>::signature; +auto wrap(F&& f) -> RpcFunctionWrapper::type>::function_type> { + using Signature = typename arx::function_traits::type>::function_type; return RpcFunctionWrapper(std::forward(f)); };