-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_usage.cpp
55 lines (46 loc) · 1.8 KB
/
basic_usage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//In this example the catalog feature of the remote_fmt is not used!
//For more information see the catalog example.
#define REMOTE_FMT_USE_CATALOG false
#include "remote_fmt/remote_fmt.hpp"
#include "remote_fmt/parser.hpp"
#include "remote_fmt/catalog.hpp"
#include "remote_fmt/type_identifier.hpp"
#include <span>
#include <cstddef>
#include <vector>
#include <fmt/format.h>
//CommunicationBackend class provides an interface between the
//remote_fmt printer and the communication channel (Socket/UART/etc).
//In this example the communication channel is abstracted by a std::vector.
struct CommunicationBackend{
std::vector<std::byte> memory;
void initTransfer(){
fmt::print("Before write\n");
}
void finalizeTransfer(){
fmt::print("After write\n");
}
void write(std::span<std::byte const> s){
fmt::print("Write {}\n", s.size());
memory.insert(memory.end(), s.begin(), s.end());
}
};
int main(){
using namespace sc::literals;
//The remote_fmt printer is instanced with the CommunicationBackend class
//as a template parameter.
remote_fmt::Printer<CommunicationBackend> printer{};
//The print-function is called and the CommunicationBackend handles
//communication with the remote device.
printer.print("Test {}"_sc, 123);
assert(!printer.get_com_backend().memory.empty());
//The data is sent to the input buffer of the remote device.
auto const& buffer = printer.get_com_backend().memory;
//The remote device parses the data from the buffer without a catalog
auto const& [message, remainingBytes, discardedBytes] = remote_fmt::parse(std::span{buffer}, {});
assert(remainingBytes.size() == 0);
assert(discardedBytes == 0);
assert(message.has_value());
assert(message.value() == "Test 123");
return 0;
}