Skip to content

ToString: enum ⇒ switch

The ToString example is the canonical demonstration of anchor-based triggering and inline injection. It generates a std::string_view toString(EnumType e) function for any enum you annotate, placing the implementation in a .g.cpp file and injecting the declaration back into the header.

Given:

include/color.h
// [[codegen::generated::ToString::qualified::Color]]
enum class Color { Red, Green, Blue };

The engine produces:

include/color.h: anchor comment replaced by declaration:

include/color.h (after generation)
std::string_view toString(Color e);
enum class Color { Red, Green, Blue };

generated/include/color.g.cpp: implementation:

color.g.cpp
#include <string_view>
std::string_view toString(Color e)
{
switch (e)
{
case Color::Red: return "Red";
case Color::Green: return "Green";
case Color::Blue: return "Blue";
default: return "<unknown>";
}
}

The anchor comment path ::qualified::Color instructs the rule to use the fully-qualified enum name (Color) for the generated function signature. For nested enums in namespaces:

// [[codegen::generated::ToString::qualified::network::Status]]
namespace network {
enum class Status { Ok, Timeout, Refused };
}

Produces std::string_view toString(network::Status e).

See the Quick Start for the full script listings. The key behaviors:

  • Preamble emits #include <string_view>.
  • Transformation collects node.enumerators, builds one case per enumerator, wraps in a switch, and returns { source = impl, inline = { { source = decl } } }.
  • Scoped vs unscoped: node.isScoped controls whether case values use the EnumName:: prefix.
  • No grouping script: 1:1 routing: one header ☛ one .g.cpp.

Add a reverse fromString: Return a second item in the inline list and a second function body in source.

Handle explicit values: node.enumerators[].value contains the explicit value as a source text string when set. Use it to make the switch exhaustive under -Wswitch.

Namespace-qualified output: Add a grouping script that routes output to generated/<namespace>/ subdirectories.

Key Takeaways
  • Anchor comments trigger rules and control inline injection site.
  • The ::qualified::EnumName path segment sets the generated function’s qualified name.
  • node.isScoped distinguishes enum class (needs Enum::Value prefix) from plain enum.
  • node.enumerators[].value carries explicit enumerator values as source text.
  • No grouping script ☛ 1:1 routing. One enum, one .g.cpp.