aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/format_arg.h
blob: 544e2657667f50d2a9ff82cb13892b3eab8f1c76 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once

#include <util/generic/strbuf.h>

#include <array>
#include <string_view>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

class TStringBuilderBase;

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

template <class T>
constexpr std::string_view QualidName();

template <class T>
constexpr bool IsNYTName();

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

// Base used for flag checks for each type independently.
// Use it for overrides.
class TFormatArgBase
{
public:
    // TODO(arkady-e1ppa): Consider more strict formatting rules.
    static constexpr std::array ConversionSpecifiers = {
        'v', '1', 'c', 's', 'd', 'i', 'o',
        'x', 'X', 'u', 'f', 'F', 'e', 'E',
        'a', 'A', 'g', 'G', 'n', 'p'
    };

    static constexpr std::array FlagSpecifiers = {
        '-', '+', ' ', '#', '0',
        '1', '2', '3', '4', '5',
        '6', '7', '8', '9',
        '*', '.', 'h', 'l', 'j',
        'z', 't', 'L', 'q', 'Q'
    };

    template <class T>
    static constexpr bool IsSpecifierList = requires (T t) {
        [] <size_t N> (std::array<char, N>) { } (t);
    };

    // Hot = |true| adds specifiers to the beggining
    // of a new array.
    template <bool Hot, size_t N, std::array<char, N> List, class TFrom = TFormatArgBase>
    static consteval auto ExtendConversion();

    template <bool Hot, size_t N, std::array<char, N> List, class TFrom = TFormatArgBase>
    static consteval auto ExtendFlags();

private:
    template <bool Hot, size_t N, std::array<char, N> List, auto* From>
    static consteval auto AppendArrays();
};

////////////////////////////////////////////////////////////////////////////////

template <class T>
struct TFormatArg
    : public TFormatArgBase
{ };

// Ultimate customization point mechanism --- define an overload
// of FormatValue in order to support formatting of your type.
// Semantic requirement:
// Said field must be constexpr.
template <class T>
concept CFormattable =
    requires {
        TFormatArg<T>::ConversionSpecifiers;
        requires TFormatArgBase::IsSpecifierList<decltype(TFormatArg<T>::ConversionSpecifiers)>;

        TFormatArg<T>::FlagSpecifiers;
        requires TFormatArgBase::IsSpecifierList<decltype(TFormatArg<T>::FlagSpecifiers)>;
    } &&
    requires (
        TStringBuilderBase* builder,
        const T& value,
        TStringBuf spec
    ) {
        { FormatValue(builder, value, spec) } -> std::same_as<void>;
    };

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define FORMAT_ARG_INL_H_
#include "format_arg-inl.h"
#undef FORMAT_ARG_INL_H_