Skip to content

Commit 0d20d18

Browse files
authored
Merge pull request #23 from bcmi-labs/templates-experiments
Create the RPC function wrapper directly in wrap.
2 parents aefd20f + 001f87c commit 0d20d18

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

examples/wrapper_example/wrapper_example.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void loop() {
5555
out_packer.clear();
5656

5757
blink_before();
58-
int out = wrapped_add(5, 3);
58+
int out = (*wrapped_add)(5, 3);
5959

60-
bool unpack_ok = wrapped_add(unpacker, out_packer);
60+
bool unpack_ok = (*wrapped_add)(unpacker, out_packer);
6161

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

85-
bool should_be_false = wrapped_divide(unpacker, out_packer);
85+
bool should_be_false = (*wrapped_divide)(unpacker, out_packer);
8686

8787
if (!should_be_false){
8888
Serial.println("RPC error call divide by zero ");
@@ -103,7 +103,7 @@ void loop() {
103103
unpacker.clear();
104104
unpacker.feed(packer.data(), packer.size());
105105
out_packer.clear();
106-
wrapped_hello(unpacker, out_packer);
106+
(*wrapped_hello)(unpacker, out_packer);
107107

108108
for (size_t i=0; i<out_packer.size(); i++){
109109
Serial.print(out_packer.data()[i], HEX);

src/dispatcher.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class RpcFunctionDispatcher {
1919

2020
if (isBound(name)) return false;
2121

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

src/wrapper.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ using namespace RpcUtils::detail;
1010
#include <stdexcept>
1111
#endif
1212

13+
class IFunctionWrapper {
14+
public:
15+
virtual ~IFunctionWrapper() {}
16+
virtual bool operator()(MsgPack::Unpacker& unpacker, MsgPack::Packer& packer) = 0;
17+
};
1318

1419
template<typename F>
1520
class RpcFunctionWrapper;
1621

17-
class IFunctionWrapper {
18-
public:
19-
virtual ~IFunctionWrapper() {}
20-
virtual bool operator()(MsgPack::Unpacker& unpacker, MsgPack::Packer& packer) = 0;
21-
};
22-
2322
template<typename R, typename... Args>
2423
class RpcFunctionWrapper<std::function<R(Args...)>>: public IFunctionWrapper {
2524
public:
@@ -100,11 +99,9 @@ class RpcFunctionWrapper<std::function<R(Args...)>>: public IFunctionWrapper {
10099
}
101100
};
102101

103-
104-
template<typename F>
105-
auto wrap(F&& f) -> RpcFunctionWrapper<typename arx::function_traits<typename std::decay<F>::type>::function_type> {
106-
using Signature = typename arx::function_traits<typename std::decay<F>::type>::function_type;
107-
return RpcFunctionWrapper<Signature>(std::forward<F>(f));
102+
template<typename F, typename Signature = typename arx::function_traits<typename std::decay<F>::type>::function_type>
103+
auto wrap(F&& f) -> RpcFunctionWrapper<Signature>* {
104+
return new RpcFunctionWrapper<Signature>(std::forward<F>(f));
108105
};
109106

110107
#endif

0 commit comments

Comments
 (0)