Skip to content

Commit 1592a02

Browse files
committed
Create the RPC function wrapper directly in wrap.
This allows a simplified usage in dispatcher. Possibly an IFunctionWrapper* may be directly returned in future refactorings.
1 parent 13c66b4 commit 1592a02

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ class RpcFunctionWrapper<std::function<R(Args...)>>: public IFunctionWrapper {
100100
}
101101
};
102102

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

110109
#endif

0 commit comments

Comments
 (0)