-
Notifications
You must be signed in to change notification settings - Fork 1
/
catalog.cpp
75 lines (60 loc) · 2.33 KB
/
catalog.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <type_traits>
#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 <map>
#include <vector>
#include <fmt/format.h>
using namespace sc::literals;
//#### Generated Code ####
//This code can be completely generated by the toolchain with a python script.
//However to make this example understandable it is stated here.
//String in the catalog
static constexpr auto testString{"Test {}"_sc};
//Specification of the catalog function
template<>
std::uint16_t remote_fmt::catalog<std::remove_cvref_t<decltype(testString)>>(){
return 0;
}
//Message catalog used on the remote system
std::map<std::uint16_t, std::string> messageCatalog{
{remote_fmt::catalog<std::remove_cvref_t<decltype(testString)>>(), std::string{std::string_view{testString}}}
};
//#### Non-Generated Code ####
//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(){
//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 with a messageCatalog
auto const& [message, remainingBytes, discardedBytes] = remote_fmt::parse(std::span{buffer}, messageCatalog);
assert(remainingBytes.size() == 0);
assert(discardedBytes == 0);
assert(message.has_value());
assert(message.value() == "Test 123");
return 0;
}