summaryrefslogtreecommitdiff
path: root/deimos/core/format.h
blob: 3424dc20bc31fe80853eed243201e87cd7295245 (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
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once

#include "deimos/core/base.h"

namespace deimos
{

class IWriter;

struct FormatArg
{
    enum Type : uint8_t
    {
        kInteger,
        kUnsignedInteger,
        kString,
    };

    Type type;

    union
    {
        int64_t     integer;
        uint64_t    unsigned_integer;
        StringView  string;
    };

    explicit FormatArg(std::signed_integral auto value) :
        type{kInteger},
        integer{value}
    {}

    explicit FormatArg(std::unsigned_integral auto value) :
        type{kUnsignedInteger},
        unsigned_integer{value}
    {}

    explicit FormatArg(StringView value) :
        type{kString},
        string{value}
    {}
};

template<typename T>
concept Formattable = requires (T value) { FormatArg(value); };

void FormatVa(IWriter*, gsl::czstring fmt, Span<const FormatArg>);

template<Formattable... Args>
void Format(IWriter* writer, gsl::czstring fmt, Args&&... args)
{
    FormatVa(writer, fmt, { FormatArg(std::forward<Args>(args))... });
}

} // namespace deimos