Inline Injection
The problem it solves
Section titled “The problem it solves”For rules that generate function implementations (.g.cpp), you typically also need the declaration in the original header. Without inline injection, you must manually add the declaration, which defeats the purpose of generation and breaks whenever the generated signature changes.
Inline injection lets the rule emit both the implementation and the declaration, keeping them in sync automatically.
How it works
Section titled “How it works”The inline field in the transformation return value is a list of objects:
return json.encode({ source = "std::string_view toString(Color e) { ... }", inline = { { source = "std::string_view toString(Color e);" } }})The engine looks for the anchor comment that triggered this rule invocation in the original header. It replaces the anchor comment line with the inline[1].source text.
Anchor comment format
Section titled “Anchor comment format”// [[codegen::generated::RuleName::qualified::EntityName]]enum class Color { Red, Green, Blue };The anchor comment must appear on its own line, immediately above or at the declaration site.
Multiple inline items
Section titled “Multiple inline items”The inline list can contain multiple items. They are injected in order, replacing the single anchor comment line with multiple lines:
inline = { { source = "std::string_view toString(Color e);" }, { source = "Color fromString(std::string_view s);" },}What gets replaced
Section titled “What gets replaced”The entire anchor comment line is replaced. The declaration it annotates is preserved, the engine only modifies the comment line itself.
Without inline injection
Section titled “Without inline injection”Rules that do not need to patch headers simply omit the inline field or return an empty list:
return json.encode({ source = impl })-- orreturn json.encode({ source = impl, inline = {} })- The
inlinefield injects text into the original header at the anchor comment site. - The anchor comment line is replaced by the injected text.
- The declaration the anchor annotates is preserved.
- Multiple
inlineitems replace the single anchor line with multiple lines in order. - Omit
inlineor return{}for rules that write to.g.cpponly.