Panini 1.4.0
Header-only library for generating C++, written in C++17
Scope.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/Braces.hpp"
25#include "commands/Command.hpp"
27#include "writers/Writer.hpp"
28
29namespace panini
30{
31
69 class Scope
70 : public Command
71 {
72
73 public:
79 using TCallback = std::function<void(Writer& writer)>;
80
94 inline explicit Scope(
95 std::string&& name,
96 TCallback&& callback,
97 BraceBreakingStyle breakingStyle = BraceBreakingStyle::Inherit) noexcept
98 : m_name(name)
99 , m_callback(std::move(callback))
100 {
101 m_options.breakingStyle = breakingStyle;
102 }
103
118 inline explicit Scope(
119 const std::string& name,
120 TCallback&& callback,
121 BraceBreakingStyle breakingStyle = BraceBreakingStyle::Inherit) noexcept
122 : m_name(name)
123 , m_callback(std::move(callback))
124 {
125 m_options.breakingStyle = breakingStyle;
126 }
127
133 inline explicit Scope(
134 std::string&& name,
135 TCallback&& callback,
136 const ScopeOptions& options) noexcept
137 : m_name(name)
138 , m_callback(std::move(callback))
139 , m_options(options)
140 {
141 }
142
148 inline explicit Scope(
149 const std::string& name,
150 TCallback&& callback,
151 const ScopeOptions& options) noexcept
152 : m_name(name)
153 , m_callback(std::move(callback))
154 , m_options(options)
155 {
156 }
157
158 inline void Visit(Writer& writer) override
159 {
160 const BraceBreakingStyle breakingStyle =
162 ? writer.GetBraceBreakingStyle()
163 : m_options.breakingStyle;
164
165 if (!m_name.empty())
166 {
167 writer << m_name;
168
169 if (breakingStyle == BraceBreakingStyle::Attach)
170 {
171 writer << m_options.chunkAttachSpacing;
172 }
173 }
174
175 writer << Braces(std::move(m_callback), m_options);
176 }
177
178 private:
179 std::string m_name;
180 TCallback m_callback;
181 ScopeOptions m_options;
182
183 };
184
185};
Command for outputting opening and closing (curly) braces.
Definition: Braces.hpp:77
Base class for commands.
Definition: Command.hpp:44
Command for outputting a scope with braces.
Definition: Scope.hpp:71
Scope(const std::string &name, TCallback &&callback, BraceBreakingStyle breakingStyle=BraceBreakingStyle::Inherit) noexcept
Definition: Scope.hpp:118
void Visit(Writer &writer) override
Definition: Scope.hpp:158
Scope(std::string &&name, TCallback &&callback, const ScopeOptions &options) noexcept
Definition: Scope.hpp:133
std::function< void(Writer &writer)> TCallback
Definition: Scope.hpp:79
Scope(const std::string &name, TCallback &&callback, const ScopeOptions &options) noexcept
Definition: Scope.hpp:148
Scope(std::string &&name, TCallback &&callback, BraceBreakingStyle breakingStyle=BraceBreakingStyle::Inherit) noexcept
Definition: Scope.hpp:94
Pure virtual interface for writers.
Definition: Writer.hpp:44
virtual BraceBreakingStyle GetBraceBreakingStyle() const =0
BraceBreakingStyle
Brace breaking style to use when writing to output.
Definition: BraceBreakingStyle.hpp:34
@ Attach
Open brace on the same line.
@ Inherit
Inherit setting from the config, not valid on Writer.
Definition: Braces.hpp:29
BraceBreakingStyle breakingStyle
Definition: BracesOptions.hpp:45
Options for the Scope command.
Definition: ScopeOptions.hpp:37
std::string chunkAttachSpacing
Definition: ScopeOptions.hpp:42