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
8 changes: 4 additions & 4 deletions examples/wrapper_example/wrapper_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void loop() {
out_packer.clear();

blink_before();
int out = wrapped_add(5, 3);
int out = (*wrapped_add)(5, 3);

bool unpack_ok = wrapped_add(unpacker, out_packer);
bool unpack_ok = (*wrapped_add)(unpacker, out_packer);

Serial.print("simple call: ");
Serial.println(out);
Expand All @@ -82,7 +82,7 @@ void loop() {
unpacker.feed(packer.data(), packer.size());
out_packer.clear();

bool should_be_false = wrapped_divide(unpacker, out_packer);
bool should_be_false = (*wrapped_divide)(unpacker, out_packer);

if (!should_be_false){
Serial.println("RPC error call divide by zero ");
Expand All @@ -103,7 +103,7 @@ void loop() {
unpacker.clear();
unpacker.feed(packer.data(), packer.size());
out_packer.clear();
wrapped_hello(unpacker, out_packer);
(*wrapped_hello)(unpacker, out_packer);

for (size_t i=0; i<out_packer.size(); i++){
Serial.print(out_packer.data()[i], HEX);
Expand Down
4 changes: 1 addition & 3 deletions src/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class RpcFunctionDispatcher {

if (isBound(name)) return false;

using WrapperT = decltype(wrap(std::forward<F>(f)));
WrapperT* instance = new WrapperT(wrap(std::forward<F>(f)));
_entries[_count++] = {name, tag, instance};
_entries[_count++] = {name, tag, wrap(std::forward<F>(f))};
return true;
}

Expand Down
19 changes: 8 additions & 11 deletions src/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ using namespace RpcUtils::detail;
#include <stdexcept>
#endif

class IFunctionWrapper {
public:
virtual ~IFunctionWrapper() {}
virtual bool operator()(MsgPack::Unpacker& unpacker, MsgPack::Packer& packer) = 0;
};

template<typename F>
class RpcFunctionWrapper;

class IFunctionWrapper {
public:
virtual ~IFunctionWrapper() {}
virtual bool operator()(MsgPack::Unpacker& unpacker, MsgPack::Packer& packer) = 0;
};

template<typename R, typename... Args>
class RpcFunctionWrapper<std::function<R(Args...)>>: public IFunctionWrapper {
public:
Expand Down Expand Up @@ -100,11 +99,9 @@ class RpcFunctionWrapper<std::function<R(Args...)>>: public IFunctionWrapper {
}
};


template<typename F>
auto wrap(F&& f) -> RpcFunctionWrapper<typename arx::function_traits<typename std::decay<F>::type>::function_type> {
using Signature = typename arx::function_traits<typename std::decay<F>::type>::function_type;
return RpcFunctionWrapper<Signature>(std::forward<F>(f));
template<typename F, typename Signature = typename arx::function_traits<typename std::decay<F>::type>::function_type>
auto wrap(F&& f) -> RpcFunctionWrapper<Signature>* {
return new RpcFunctionWrapper<Signature>(std::forward<F>(f));
};

#endif