aboutsummaryrefslogtreecommitdiffstats
path: root/src/textoutput.hpp
blob: 7ef12fab7bdb766bbc95b8dde2dc32dd31837487 (plain)
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
#ifndef TEXTOUTPUT_HPP
#define TEXTOUTPUT_HPP

class TextOutput
{
public:
    virtual void write(char c) noexcept = 0;

    void write(const char *s) noexcept {
        if (s) {
            while (*s)
                write(*s++);
        }
    }

    void write(int n) noexcept {
        char buf[32];
        auto ptr = buf + sizeof(buf);

        *--ptr = '\0';
        do {
            *--ptr = "0123456789"[n % 10];
            n /= 10;
        } while (n);

        write(ptr);
    }

    void write(unsigned n) noexcept {
        char buf[32];
        auto ptr = buf + sizeof(buf);

        *--ptr = '\0';
        do {
            *--ptr = "0123456789"[n % 10];
            n /= 10;
        } while (n);

        write(ptr);
    }
};

#endif // TEXTOUTPUT_HPP