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