Panini 1.4.0
Header-only library for generating C++, written in C++17
Braces.hpp
Go to the documentation of this file.
1/*
2 MIT No Attribution
3
4 Copyright 2021-2023 Mr. Hands
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 DEALINGS IN THE SOFTWARE.
20*/
21
22#pragma once
23
24#include "commands/Command.hpp"
26#include "writers/Writer.hpp"
27
28namespace panini
29{
30
75 class Braces
76 : public Command
77 {
78
79 public:
80 using TCallback = std::function<void(Writer&)>;
81
96 inline explicit Braces(
97 TCallback&& callback,
98 BraceBreakingStyle breakingStyle = BraceBreakingStyle::Inherit) noexcept
99 : m_callback(std::move(callback))
100 {
101 m_options.breakingStyle = breakingStyle;
102 }
103
116 inline explicit Braces(
117 TCallback&& callback,
118 const BracesOptions& options) noexcept
119 : m_callback(std::move(callback))
120 , m_options(options)
121 {
122 }
123
124 inline void Visit(Writer& writer) override
125 {
126 const BraceBreakingStyle breakingStyle =
128 ? writer.GetBraceBreakingStyle()
129 : m_options.breakingStyle;
130
131 const bool wasNewLine = writer.IsOnNewLine();
132
133 switch (breakingStyle)
134 {
135
137 {
138 writer << m_options.chunkBraceOpen << IndentPush() << NextLine();
139 m_callback(writer);
140 writer << IndentPop() << m_options.chunkBraceClose;
141
142 } break;
143
145 {
146 if (!wasNewLine)
147 {
148 writer << NextLine();
149 }
150
151 writer << m_options.chunkBraceOpen << IndentPush() << NextLine();
152 m_callback(writer);
153 writer << IndentPop() << m_options.chunkBraceClose;
154
155 } break;
156
158 {
159 if (!wasNewLine)
160 {
161 writer << NextLine() << IndentPush();
162 }
163
164 writer << m_options.chunkBraceOpen << NextLine();
165 m_callback(writer);
166 writer << m_options.chunkBraceClose;
167
168 if (!wasNewLine)
169 {
170 writer << IndentPop();
171 }
172
173 } break;
174
175 default:
176 break;
177
178 }
179 }
180
181 private:
182 TCallback m_callback;
183 BracesOptions m_options;
184
185 };
186
187};
Command for outputting opening and closing (curly) braces.
Definition: Braces.hpp:77
Braces(TCallback &&callback, BraceBreakingStyle breakingStyle=BraceBreakingStyle::Inherit) noexcept
Definition: Braces.hpp:96
std::function< void(Writer &)> TCallback
Definition: Braces.hpp:80
Braces(TCallback &&callback, const BracesOptions &options) noexcept
Definition: Braces.hpp:116
void Visit(Writer &writer) override
Definition: Braces.hpp:124
Base class for commands.
Definition: Command.hpp:44
Pure virtual interface for writers.
Definition: Writer.hpp:44
virtual bool IsOnNewLine() const =0
virtual BraceBreakingStyle GetBraceBreakingStyle() const =0
BraceBreakingStyle
Brace breaking style to use when writing to output.
Definition: BraceBreakingStyle.hpp:34
@ Whitesmiths
New line and indent before brace open and brace close.
@ Allman
New line before brace open.
@ Attach
Open brace on the same line.
@ Inherit
Inherit setting from the config, not valid on Writer.
Definition: Braces.hpp:29
Options for the Braces command.
Definition: BracesOptions.hpp:38
std::string chunkBraceOpen
Definition: BracesOptions.hpp:50
std::string chunkBraceClose
Definition: BracesOptions.hpp:55
BraceBreakingStyle breakingStyle
Definition: BracesOptions.hpp:45
Command for decrementing the indentation level on the writer.
Definition: IndentPop.hpp:36
Command for incrementing the indentation level on the writer.
Definition: IndentPush.hpp:36
Command for outtputing a new line chunk.
Definition: NextLine.hpp:36