Panini 1.4.0
Header-only library for generating C++, written in C++17
panini::FeatureFlag Class Reference

Command for feature flags. More...

#include <FeatureFlag.hpp>

Inheritance diagram for panini::FeatureFlag:
panini::Command

Public Types

using TCallback = std::function< void(Writer &writer)>
 

Public Member Functions

 FeatureFlag (bool condition, const std::string &context, TCallback &&callbackThen) noexcept
 
 FeatureFlag (bool condition, const std::string &context, TCallback &&callbackThen, TCallback &&callbackElse) noexcept
 
void Visit (Writer &writer) override
 
- Public Member Functions inherited from panini::Command
virtual ~Command ()=default
 

Detailed Description

Command for feature flags.

When the condition is true, the "then" callback is used to output code to the active writer. Otherwise, if the "else" callback is specified, this callback is used instead.

If the condition was true, the output is wrapped in single-line comments to make the generated code stand out more. These comments are omitted if the context parameter is left blank.

Feature flags are useful when you are working on generated code that depends on the context. For example, you have developed a new way to access the main database, but want to roll out this change in controlled stages instead of simultaneously.

Example:

std::string query = R"(INSERT INTO Users (name) VALUES ('Little Bobby Tables'))";
writer << FeatureFlag(
newDbAccess,
"new-db-access",
[&query](Writer& writer) {
writer << "db->WriteQueryFast(\"" << query << ";\");" << NextLine();
},
[&query](Writer& writer) {
writer << "db->WriteQuery(\"" << query << ";\");" << NextLine();
}
);
FeatureFlag(bool condition, const std::string &context, TCallback &&callbackThen) noexcept
Definition: FeatureFlag.hpp:98

Output when the condition is true:

// new-db-access
db->WriteQueryFast("INSERT INTO Users (name) VALUES ('Little Bobby Tables');");
// new-db-access

Output when the condition is false:

db->WriteQuery("INSERT INTO Users (name) VALUES ('Little Bobby Tables');");

Member Typedef Documentation

◆ TCallback

using panini::FeatureFlag::TCallback = std::function<void(Writer& writer)>

Callback type used for conditions.

Parameters
writerActive writer.

Constructor & Destructor Documentation

◆ FeatureFlag() [1/2]

panini::FeatureFlag::FeatureFlag ( bool  condition,
const std::string &  context,
TCallback &&  callbackThen 
)
inlineexplicitnoexcept

Construct a FeatureFlag with a callback invoked when the condition is true.

Parameters
conditionDecide which callback to invoke
contextString added to comments for context
callbackThenInvoked when condition is true

◆ FeatureFlag() [2/2]

panini::FeatureFlag::FeatureFlag ( bool  condition,
const std::string &  context,
TCallback &&  callbackThen,
TCallback &&  callbackElse 
)
inlineexplicitnoexcept

Construct a FeatureFlag with a callback invoked when the condition is true and one when the condition is false.

Parameters
conditionDecide which callback to invoke
contextString added to comments for context
callbackThenInvoked when condition is true
callbackElseInvoked when condition is false

Member Function Documentation

◆ Visit()

void panini::FeatureFlag::Visit ( Writer writer)
inlineoverridevirtual

Accepts a Writer to write chunks to the output.

Implements panini::Command.


The documentation for this class was generated from the following file: