summaryrefslogtreecommitdiffhomepage
path: root/compat/debug.hpp
blob: 1fdd9a7ecfd6f1fc836728b72bae4392dbc03161 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include <Corrade/Utility/Debug.h>
#include <Corrade/Utility/Move.h>
#include <Corrade/Containers/StringView.h>
#include <concepts>

namespace floormat {

template<typename T>
concept DebugPrintable = requires(Debug& dbg, const T& value) {
    { dbg << value } -> std::convertible_to<Debug&>;
};

} // namespace floormat

namespace floormat::detail::corrade_debug {

// ***** colon *****
struct Colon { char c; };
Debug& operator<<(Debug& dbg, Colon box);

// ***** error string *****
struct ErrorString { int value; };
Debug& operator<<(Debug& dbg, ErrorString box);

// ***** quoted *****
template<typename T> struct Quoted
{
    using type = T;
    T value; char c;
};

Debug::Flags quoted_begin(Debug& dbg, char c);
Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c);

template<typename T> Debug& operator<<(Debug& dbg, Quoted<T> box)
{
    Debug::Flags flags = quoted_begin(dbg, box.c);
    dbg << box.value;
    return quoted_end(dbg, flags, box.c);
}

// ***** float *****

struct Fraction
{
    float value;
    uint8_t decimal_points;
};

Debug& operator<<(Debug& dbg, Fraction frac);

} // namespace floormat::detail::corrade_debug

// ***** api *****

// ***** functions *****

namespace floormat {

floormat::detail::corrade_debug::Colon colon(char c = ':');
floormat::detail::corrade_debug::ErrorString error_string(int error);
floormat::detail::corrade_debug::ErrorString error_string();

floormat::detail::corrade_debug::Fraction fraction(float value, uint8_t decimal_points = 1);

template<DebugPrintable T>
auto quoted(T&& value, char c = '\'')
{
    using U = std::remove_cvref_t<T>;
    using floormat::detail::corrade_debug::Quoted;
    if constexpr(std::is_rvalue_reference_v<decltype(value)>)
        return Quoted<U>{ .value = Utility::move(value), .c = c };
    else
        return Quoted<const U&>{ .value = value, .c = c };
}

template<DebugPrintable T> auto quoted2(T&& value) { return quoted(Utility::forward<T>(value), '"'); }

} // namespace floormat