Panini 1.4.0
Header-only library for generating C++, written in C++17
FeatureFlag.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"
25
26namespace panini
27{
28
79 : public Command
80 {
81
82 public:
88 using TCallback = std::function<void(Writer& writer)>;
89
98 inline explicit FeatureFlag(
99 bool condition,
100 const std::string& context,
101 TCallback&& callbackThen) noexcept
102 : m_condition(condition)
103 , m_context(context)
104 , m_callbackThen(std::move(callbackThen))
105 {
106 }
107
117 inline explicit FeatureFlag(
118 bool condition,
119 const std::string& context,
120 TCallback&& callbackThen,
121 TCallback&& callbackElse) noexcept
122 : m_condition(condition)
123 , m_context(context)
124 , m_callbackThen(std::move(callbackThen))
125 , m_callbackElse(std::move(callbackElse))
126 {
127 }
128
129 inline void Visit(Writer& writer) override
130 {
131 if (m_condition)
132 {
133 if (!m_context.empty())
134 {
135 writer << CommentLine(m_context) << NextLine();
136 }
137
138 m_callbackThen(writer);
139
140 if (!m_context.empty())
141 {
142 writer << CommentLine(m_context);
143 }
144 }
145 else if (
146 m_callbackElse)
147 {
148 m_callbackElse(writer);
149 }
150 }
151
152 private:
153 bool m_condition = false;
154 std::string m_context;
155 TCallback m_callbackThen;
156 TCallback m_callbackElse;
157
158 };
159
160};
Base class for commands.
Definition: Command.hpp:44
Command for outputting a single-line comment.
Definition: CommentLine.hpp:53
Command for feature flags.
Definition: FeatureFlag.hpp:80
void Visit(Writer &writer) override
Definition: FeatureFlag.hpp:129
std::function< void(Writer &writer)> TCallback
Definition: FeatureFlag.hpp:88
FeatureFlag(bool condition, const std::string &context, TCallback &&callbackThen, TCallback &&callbackElse) noexcept
Definition: FeatureFlag.hpp:117
FeatureFlag(bool condition, const std::string &context, TCallback &&callbackThen) noexcept
Definition: FeatureFlag.hpp:98
Pure virtual interface for writers.
Definition: Writer.hpp:44
Definition: Braces.hpp:29
Command for outtputing a new line chunk.
Definition: NextLine.hpp:36