Skip to content

Commit fe7419e

Browse files
committed
feat: function wrapper to be used for server binding
1 parent 61c1150 commit fe7419e

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

examples/rpc_lite_server/rpc_lite_server.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <RpcLite.h>
1+
#include <RPClite.h>
22
#include <HardwareSerial.h>
33

44
HardwareSerial* uart = new HardwareSerial(0);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <RPClite.h>
2+
3+
int add(int x, int y) {
4+
return x + y;
5+
}
6+
7+
void say_hello() {
8+
Serial.println("Hello!");
9+
}
10+
11+
auto wrapped_add = wrap(add);
12+
auto wrapped_hello = wrap(say_hello);
13+
14+
void blink_before(){
15+
digitalWrite(LED_BUILTIN, HIGH);
16+
delay(200);
17+
digitalWrite(LED_BUILTIN, LOW);
18+
delay(200);
19+
digitalWrite(LED_BUILTIN, HIGH);
20+
delay(200);
21+
digitalWrite(LED_BUILTIN, LOW);
22+
delay(200);
23+
digitalWrite(LED_BUILTIN, HIGH);
24+
delay(200);
25+
digitalWrite(LED_BUILTIN, LOW);
26+
delay(200);
27+
}
28+
29+
void setup() {
30+
Serial.begin(9600);
31+
}
32+
33+
void loop() {
34+
blink_before();
35+
wrapped_add(5, 3);
36+
wrapped_hello();
37+
delay(1000);
38+
}

src/RPClite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "transport.h"
1212
#include "client.h"
1313
#include "server.h"
14+
#include "wrapper.h"
1415

1516
#include "DummyTransport.h"
1617
#include "SerialTransport.h"

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define RPCLITE_SERVER_H
77

88
#include "rpc.h"
9+
#include "error.h"
910

1011
class RPCServer {
1112
ITransport& transport;

src/wrapper.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <functional>
3+
#include <tuple>
4+
5+
// --- FunctionWrapper definition ---
6+
template<typename R, typename... Args>
7+
class FunctionWrapper {
8+
public:
9+
using FuncType = R (*)(Args...);
10+
11+
FunctionWrapper(FuncType f) : func(f) {}
12+
13+
R operator()(Args... args) {
14+
Serial.println("Calling function with args...");
15+
16+
if constexpr (std::is_void<R>::value) {
17+
func(args...);
18+
Serial.println("Return: void");
19+
return;
20+
} else {
21+
R result = func(args...);
22+
Serial.print("Result: ");
23+
Serial.print(result);
24+
Serial.println();
25+
return result;
26+
}
27+
28+
}
29+
30+
private:
31+
FuncType func;
32+
};
33+
34+
// --- Wraps function function pointers only. Lambdas and member functions not currently supported ---
35+
template<typename R, typename... Args>
36+
FunctionWrapper<R, Args...> wrap(R (*f)(Args...)) {
37+
return FunctionWrapper<R, Args...>(f);
38+
}

0 commit comments

Comments
 (0)