yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

tareksanderReflection compiler option (#5507)7e51180ed

master
3.0 KiB122 linesraw
1#ifndef SLANG_CORE_PRETTY_WRITER_H
2#define SLANG_CORE_PRETTY_WRITER_H
3
4
5#include "../core/slang-char-util.h"
6#include "../core/slang-string-util.h"
7#include "../core/slang-string.h"
8
9namespace Slang
10{
11
12struct PrettyWriter
13{
14    typedef PrettyWriter ThisType;
15
16    friend struct CommaTrackerRAII;
17
18    struct CommaState
19    {
20        bool needComma = false;
21    };
22
23    void writeRaw(const UnownedStringSlice& slice) { m_builder.append(slice); }
24    void writeRaw(char const* begin, char const* end);
25    void writeRaw(char const* begin) { writeRaw(UnownedStringSlice(begin)); }
26
27    void writeRawChar(int c) { m_builder.appendChar(char(c)); }
28
29    void writeHexChar(int c) { writeRawChar(CharUtil::getHexChar(Index(c))); }
30
31    /// Adjusts indentation if at start of a line
32    void adjust();
33
34    /// Increase indentation
35    void indent() { m_indent++; }
36    /// Decreate indentation
37    void dedent();
38
39    /// Write taking into account any CR that might be in a slice
40    void write(const UnownedStringSlice& slice);
41    void write(char const* text) { write(UnownedStringSlice(text)); }
42    void write(char const* text, size_t length) { write(UnownedStringSlice(text, length)); }
43
44    /// Write the slice as an escaped string
45    void writeEscapedString(const UnownedStringSlice& slice);
46
47    /// Call before items in a comma-separated JSON list to emit the comma if/when needed
48    void maybeComma();
49
50    /// Get the builder the result is being constructed in
51    StringBuilder& getBuilder() { return m_builder; }
52
53    ThisType& operator<<(const UnownedStringSlice& slice)
54    {
55        write(slice);
56        return *this;
57    }
58    ThisType& operator<<(const char* text)
59    {
60        write(text);
61        return *this;
62    }
63    ThisType& operator<<(uint64_t val)
64    {
65        adjust();
66        m_builder << val;
67        return *this;
68    }
69    ThisType& operator<<(int64_t val)
70    {
71        adjust();
72        m_builder << val;
73        return *this;
74    }
75    ThisType& operator<<(int32_t val)
76    {
77        adjust();
78        m_builder << val;
79        return *this;
80    }
81    ThisType& operator<<(uint32_t val)
82    {
83        adjust();
84        m_builder << val;
85        return *this;
86    }
87    ThisType& operator<<(float val)
88    {
89        adjust();
90        // We want to use a specific format, so we use the StringUtil to specify format, and not
91        // just use <<
92        StringUtil::appendFormat(m_builder, "%f", val);
93        return *this;
94    }
95
96    bool m_startOfLine = true;
97    int m_indent = 0;
98    CommaState* m_commaState = nullptr;
99    StringBuilder m_builder;
100};
101
102/// Type for tracking whether a comma is needed in a comma-separated JSON list
103struct CommaTrackerRAII
104{
105    CommaTrackerRAII(PrettyWriter& writer)
106        : m_writer(&writer), m_previousState(writer.m_commaState)
107    {
108        writer.m_commaState = &m_state;
109    }
110
111    ~CommaTrackerRAII() { m_writer->m_commaState = m_previousState; }
112
113private:
114    PrettyWriter::CommaState m_state;
115    PrettyWriter* m_writer;
116    PrettyWriter::CommaState* m_previousState;
117};
118
119} // namespace Slang
120
121
122#endif