Skip to content

Commit

Permalink
WIP: Strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Oct 20, 2024
1 parent 0f40a16 commit acba9d1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 49 deletions.
10 changes: 4 additions & 6 deletions src/read.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ void rpcRead(Stream& io, T* data) {
/*! \ingroup read
* \copydoc rpcRead(Stream&, T*) */
inline void rpcRead(Stream& io, String* data) {
size_t size;
rpcRead(io, &size);
char c {};
rpcRead(io, &c);

(*data).reserve(size);
for (size_t i {0}; i < size; ++i) {
char c {};
while (c) {
*data += c;
rpcRead(io, &c);
(*data) += c;
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/rpcCall.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ void call_(Stream& io, void (*)(T const*, Ts...), F f, Us&... args) {
call_(io, f_, f, args..., data_);
}

//! \copydoc call_(Stream&, void (*)(T, Ts...), F, Us&...)
template <class... Ts, class F, class... Us>
void call_(Stream& io, void (*)(char const*, Ts...), F f, Us&... args) {
String data;
rpcRead(io, &data);

void (*f_)(Ts...) {};
char const* data_ {data.c_str()};
call_(io, f_, f, args..., data_);
}
template <class... Ts, class F, class... Us> // WIP
void call_(Stream& io, void (*)(char*, Ts...), F f, Us&... args) {
String data;
rpcRead(io, &data);

void (*f_)(Ts...) {};
char* data_ {const_cast<char*>(data.c_str())};
call_(io, f_, f, args..., data_);
}
template <class... Ts, class F, class... Us> // WIP
void call_(Stream& io, void (*)(String&, Ts...), F f, Us&... args) {
String data;
rpcRead(io, &data);

void (*f_)(Ts...) {};
call_(io, f_, f, args..., data);
}

//! \copydoc call_(Stream&, void (*)(T, Ts...), F, Us&...)
template <class T, class... Ts, class F, class... Us>
void call_(Stream& io, void (*)(T*, Ts...), F f, Us&... args) {
Expand Down
1 change: 0 additions & 1 deletion src/write.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void rpcWrite(Stream& io, T* data) {
* \copydoc rpcWrite(Stream&, T) */
inline void rpcWrite(Stream& io, char const** data) {
size_t size {strlen(*data) + 1};
rpcWrite(io, &size);
for (size_t i {0}; i < size; ++i) {
rpcWrite(io, (*data)[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST_CASE("Read basic types", "[read][basic]") {

TEST_CASE("Read String", "[read][string]") {
Serial.reset();
Serial.prepare(4ul, "xyz");
Serial.prepare("xyz");

String s;
rpcRead(Serial, &s);
Expand Down
73 changes: 35 additions & 38 deletions tests/test_rpcCall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,56 +115,55 @@ TEST_CASE("RPC call function with String types", "[call][string]") {

// Void function, first parameter is of type String.
Serial.reset();
Serial.prepare(4ul, "xyz", 1234);
Serial.prepare("xyz", 1234);
rpcCall(Serial, S::f0);
REQUIRE(Serial.rx == sizeof(size_t) + 4 + sizeof(int));
REQUIRE(Serial.rx == 4 + sizeof(int));
REQUIRE(Serial.tx == 1);

// Void function, second parameter is of type String.
Serial.reset();
Serial.prepare(1234, 4ul, "xxx");
Serial.prepare(1234, "xxx");
rpcCall(Serial, S::f1);
REQUIRE(Serial.rx == sizeof(int) + sizeof(size_t) + 4);
REQUIRE(Serial.rx == sizeof(int) + 4);
REQUIRE(Serial.tx == 1);

// Function with return type String.
Serial.reset();
rpcCall(Serial, S::f2);
REQUIRE(Serial.inspect<size_t>() == 4);
REQUIRE(Serial.inspect<String>() == "xyz");
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == sizeof(size_t) + 4);
REQUIRE(Serial.tx == 4);

// Various combinations of references and const parameters.
Serial.reset();
Serial.prepare(1234, 4ul, "abc", 4ul, "xyz");
Serial.prepare(1234, "abc", "xyz");
rpcCall(Serial, S::f3);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(1234, 4ul, "abc", 4ul, "xyz");
Serial.prepare(1234, "abc", "xyz");
rpcCall(Serial, S::f4);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 1234, 4ul, "xyz");
Serial.prepare("abc", 1234, "xyz");
rpcCall(Serial, S::f5);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 4ul, "xyz", 1234);
Serial.prepare("abc", "xyz", 1234);
rpcCall(Serial, S::f6);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 1234, 4ul, "xyz");
Serial.prepare("abc", 1234, "xyz");
rpcCall(Serial, S::f7);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 4ul, "xyz", 1234);
Serial.prepare("abc", "xyz", 1234);
rpcCall(Serial, S::f8);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);
}

TEST_CASE("RPC call function with C string types", "[call][string]") {
Expand All @@ -188,64 +187,62 @@ TEST_CASE("RPC call function with C string types", "[call][string]") {

// Void function, first parameter is of type char*.
Serial.reset();
Serial.prepare(4ul, "xyz", 1234);
Serial.prepare("xyz", 1234);
rpcCall(Serial, S::f0);
REQUIRE(Serial.rx == sizeof(size_t) + 4 + sizeof(int));
REQUIRE(Serial.rx == 4 + sizeof(int));
REQUIRE(Serial.tx == 1);

// Void function, second parameter is of type char*.
Serial.reset();
Serial.prepare(1234, 4ul, "xxx");
Serial.prepare(1234, "xxx");
rpcCall(Serial, S::f1);
REQUIRE(Serial.rx == sizeof(int) + sizeof(size_t) + 4);
REQUIRE(Serial.rx == sizeof(int) + 4);
REQUIRE(Serial.tx == 1);

// Function with return type char*.
Serial.reset();
rpcCall(Serial, S::f2);
REQUIRE(Serial.inspect<size_t>() == 4);
REQUIRE(Serial.inspect<String>() == "xyz");
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == sizeof(size_t) + 4);
REQUIRE(Serial.tx == 4);

// Function with return type char const*.
Serial.reset();
rpcCall(Serial, S::f3);
REQUIRE(Serial.inspect<size_t>() == 4);
REQUIRE(Serial.inspect<String>() == "xyz");
REQUIRE(Serial.rx == 0);
REQUIRE(Serial.tx == sizeof(size_t) + 4);
REQUIRE(Serial.tx == 4);

// Various combinations of references and const parameters.
Serial.reset();
Serial.prepare(1234, 4ul, "abc", 4ul, "xyz");
Serial.prepare(1234, "abc", "xyz");
rpcCall(Serial, S::f4);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(1234, 4ul, "abc", 4ul, "xyz");
Serial.prepare(1234, "abc", "xyz");
rpcCall(Serial, S::f5);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 1234, 4ul, "xyz");
Serial.prepare("abc", 1234, "xyz");
rpcCall(Serial, S::f6);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 4ul, "xyz", 1234);
Serial.prepare("abc", "xyz", 1234);
rpcCall(Serial, S::f7);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 1234, 4ul, "xyz");
Serial.prepare("abc", 1234, "xyz");
rpcCall(Serial, S::f8);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);

Serial.reset();
Serial.prepare(4ul, "abc", 4ul, "xyz", 1234);
Serial.prepare("abc", "xyz", 1234);
rpcCall(Serial, S::f9);
REQUIRE(Serial.rx == sizeof(int) + 2 * sizeof(size_t) + 8);
REQUIRE(Serial.rx == sizeof(int) + 8);
}

TEST_CASE("RPC call function with Tuple types", "[call][tuple]") {
Expand Down
3 changes: 0 additions & 3 deletions tests/test_write.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ TEST_CASE("Write String", "[write][string]") {

Serial.reset();
rpcWrite(Serial, &s);
REQUIRE(Serial.inspect<size_t>() == 4);
REQUIRE(Serial.inspect<String>() == "xyz");
}

Expand All @@ -44,7 +43,6 @@ TEST_CASE("Write C string of type char*", "[write][string]") {

Serial.reset();
rpcWrite(Serial, &p);
REQUIRE(Serial.inspect<size_t>() == 4);
REQUIRE(Serial.inspect<String>() == "xyz");
}

Expand All @@ -53,7 +51,6 @@ TEST_CASE("Write C string of type char const*", "[write][string]") {

Serial.reset();
rpcWrite(Serial, &s);
REQUIRE(Serial.inspect<size_t>() == 4);
REQUIRE(Serial.inspect<String>() == "xyz");
}

Expand Down

0 comments on commit acba9d1

Please sign in to comment.