μHAL
printer.h
1#ifndef PRINTER_H
2#define PRINTER_H
3
4#include <cstdint>
5#include <cstdio>
6#include <functional>
7#include <stdexcept>
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
35public:
36 Printer(const char *name, const char *description, PrinterType type);
37 Printer(
38 const char *name, const char *description, printing_function custom_fn);
39 Printer(const char *name, const char *description, const char *not_truth,
40 const char *truth);
41
42 PrinterType get_type() const;
43
44 template <typename T> void print(FILE *, bool, unsigned, T) const;
45};
46
47/* helper for defining std::unordered_map<std::string_view, Printer> */
48#define PRINTER(name, ...) \
49 { \
50 name, { name, __VA_ARGS__ } \
51 }
52
53#endif
Definition: printer.h:24