μHAL
printer.h
1#ifndef PRINTER_H
2#define PRINTER_H
3
4#include <cstdint>
5#include <cstdio>
6#include <stdexcept>
7#include <functional>
8
9enum class PrinterType {
10 /* boolean values */
11 boolean,
12 progress,
13 enable,
14 /* directly print value */
15 value, /* generic, so we don't need signed or unsigned versions */
16 value_hex,
17 value_float,
18 /* allow it to print its own value */
19 internal_custom_function,
20};
21
22typedef std::function<void(FILE *, bool, uint32_t)> printing_function;
23
24class Printer {
25 PrinterType type;
26 const char *name, *description;
27
28 struct {
29 const char *truth;
30 const char *not_truth;
31 } boolean_names{};
32
33 printing_function custom_fn;
34
35 public:
36 Printer(const char *name, const char *description, PrinterType type);
37 Printer(const char *name, const char *description, printing_function custom_fn);
38 Printer(const char *name, const char *description, const char *not_truth, const char *truth);
39
40 PrinterType get_type() const;
41
42 template<typename T>
43 void print(FILE *, bool, unsigned, T) const;
44};
45
46/* helper for defining std::unordered_map<std::string_view, Printer> */
47#define PRINTER(name, ...) {name, {name, __VA_ARGS__}}
48
49#endif
Definition: printer.h:24