CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CodeWriter.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
17
18#include <ostream>
19
20namespace CeresEngine {
21
24 private:
28
29 public:
31 virtual ~IndentHandler();
32
34 void setIndent(const String& indent);
35
38
41
43 [[nodiscard]] const String& getFullIndent() const;
44 };
45
55
56 // Output code writer.
57 class CodeWriter : public IndentHandler {
58 public:
59 struct Options {
60 bool enableNewLine = true;
61 bool enableIndent = true;
62 bool parseNewLines = true;
63 };
64
66
67 private:
77
83
84 struct ScopeState {
85 bool scopeCanContinue = false;
86 bool scopeUsedBraces = false;
87 bool beginLineQueued = false;
88 bool endLineQueued = false;
89 };
90
91 struct ScopeOptions {
92 bool compact;
95 };
96
99 bool isItemPending = false;
100 };
101
102 std::ostream* mStream = nullptr;
103
105 bool mOpenLine = true;
106
109
112
114
115 public:
116 // Write new line for each scope.
117 bool newLineOpenScope = false;
118
119 public:
121 explicit CodeWriter(std::ostream& stream);
122
123 public:
125 void setOutputStream(std::ostream& stream);
126
127 void pushOptions(const Options& options);
129
135
138
140 void separator();
141
144
147 void alignLine();
148
150 void beginLine();
151
154 void endLine();
155
157 void write(const StringView& text);
158
160 template<typename... Args> inline void write(const StringView& text1, const StringView& text2, const Args&... rest) {
161 write(text1);
162 write(text2);
163 ((void)write(StringView(rest)), ...);
164 }
165
167 void writeLine(const StringView& text);
168
176
179 void beginScope(bool compact = false, bool endWithSemicolon = false, bool useBraces = true);
180
183 void endScope();
184
187
189 [[nodiscard]] inline bool isOpenLine() const { return mOpenLine; }
190
191 private:
193
195
197
198 // Returns the output stream.
199 [[nodiscard]] inline std::ostream& getOutputStream() { return (*mStream); }
200 };
201
202} // namespace CeresEngine
Definition CodeWriter.hpp:57
bool isOpenLine() const
Returns true if the code writer is currently in an open line (i.e. beginLine was called without closi...
Definition CodeWriter.hpp:189
UInt32 mLineAlignmentLevel
Definition CodeWriter.hpp:107
void continueScope()
Continues the previously ended scope.
void endScope()
Ends the current scope with the '}' character.
void beginScope(bool compact=false, bool endWithSemicolon=false, bool useBraces=true)
Begins a new scope with the '{' character and adds a new line either before or after this character.
void beginLine()
Begins a new line and inserts the current indentation.
const Options & getCurrentOptions() const
ScopeState mScopeState
Definition CodeWriter.hpp:110
Stack< SeparatedList > mSeparatedLists
Definition CodeWriter.hpp:113
void beginSeparatedList(String separator=", ")
Begins a new separated list.
void write(const StringView &text)
Writes the given text to the writer stream.
Stack< Options > mOptionsStack
Definition CodeWriter.hpp:104
void endLine()
Ends the current line and inserts the new-line character to the output stream.
std::ostream * mStream
Definition CodeWriter.hpp:102
std::ostream & getOutputStream()
Definition CodeWriter.hpp:199
void pushOptions(const Options &options)
void endSeparatedList()
Ends a separated list.
void write(const StringView &text1, const StringView &text2, const Args &... rest)
Writes the given strings to the writer stream.
Definition CodeWriter.hpp:160
bool newLineOpenScope
Definition CodeWriter.hpp:117
void writeJoined(String separator, const InitializerList< WriterFunctor > &functors)
Writes multiple entries joined by separator.
bool mOpenLine
Definition CodeWriter.hpp:105
void setOutputStream(std::ostream &stream)
Throws std::runtime_error If stream is invalid.
FunctionView< void() const > WriterFunctor
Definition CodeWriter.hpp:65
AlignedLineQueue mQueuedAlignedLines
Definition CodeWriter.hpp:108
void separator()
Prints the separator, as indicated on the beginSeparatedList call.
void alignLine()
Inserts spaces to align with the previous groups if line alignment formatting is currently enabled.
Stack< ScopeOptions > mScopeOptionStack
Definition CodeWriter.hpp:111
CodeWriter(std::ostream &stream)
void writeLine(const StringView &text)
Shortcut for: beginLine(), write(text), endLine().
void flushSeparatedLines(AlignedLineQueue &lineQueue)
Indentation handler base class.
Definition CodeWriter.hpp:23
void decrementIndent()
Decrements the indentation.
String mIndentFull
Definition CodeWriter.hpp:26
Stack< String::size_type > mIndentStack
Definition CodeWriter.hpp:27
String mIndent
Definition CodeWriter.hpp:25
void incrementIndent()
Increments the indentation.
void setIndent(const String &indent)
Sets the next indentation string. By default two spaces.
IndentHandler(const String &initialIndent=" ")
const String & getFullIndent() const
Returns the current full indentation string.
Helper class for temporary indentation.
Definition CodeWriter.hpp:47
IndentHandler & mHandler
Definition CodeWriter.hpp:49
~ScopedIndent()
Definition CodeWriter.hpp:53
ScopedIndent(IndentHandler &handler)
Definition CodeWriter.hpp:52
Definition Application.hpp:19
std::stack< T, Container > Stack
The Stack class is a container adapter that gives the programmer the functionality of a stack - speci...
Definition Stack.hpp:18
std::initializer_list< T > InitializerList
An object of type InitializerList<T> is a lightweight proxy object that provides access to an array o...
Definition InitializerList.hpp:40
BasicStringView< char > StringView
Narrow string view used for handling narrow encoded text in UTF-8.
Definition String.hpp:190
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::list< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > List
List is a container that supports constant time insertion and removal of elements from anywhere in th...
Definition List.hpp:21
FunctionBase< false, true, fu2::capacity_default, true, false, Signatures... > FunctionView
A non owning copyable function wrapper for arbitrary callable types.
Definition Function.hpp:64
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition CodeWriter.hpp:68
void offsets(Vector< std::size_t > &offsets) const
Vector< String > parts
Definition CodeWriter.hpp:70
String indent
Definition CodeWriter.hpp:69
AlignedLine & operator<<(const StringView &text)
Definition CodeWriter.hpp:78
List< AlignedLine > lines
Definition CodeWriter.hpp:79
Definition CodeWriter.hpp:59
bool enableNewLine
Definition CodeWriter.hpp:60
bool parseNewLines
Definition CodeWriter.hpp:62
bool enableIndent
Definition CodeWriter.hpp:61
Definition CodeWriter.hpp:91
bool compact
Definition CodeWriter.hpp:92
bool useBraces
Definition CodeWriter.hpp:94
bool endWithSemicolon
Definition CodeWriter.hpp:93
Definition CodeWriter.hpp:84
bool endLineQueued
Definition CodeWriter.hpp:88
bool scopeUsedBraces
Definition CodeWriter.hpp:86
bool scopeCanContinue
Definition CodeWriter.hpp:85
bool beginLineQueued
Definition CodeWriter.hpp:87
Definition CodeWriter.hpp:97
String separator
Definition CodeWriter.hpp:98
bool isItemPending
Definition CodeWriter.hpp:99