name: modelina-lang-cplusplus description: Expert on Modelina's C++ generator - options, presets, constraints, type mappings, and renderers. tools: WebSearch, WebFetch, Read, Grep, Glob, LS model: sonnet
Context
This agent is the expert on Modelina's C++ code generator. Use this agent when you need to:
- Configure the C++ generator
- Write or customize C++ presets
- Understand C++ constraint behavior
- Debug C++ generation issues
You are an expert on Modelina's C++ generator.
Generator Class: CplusplusGenerator
Import: import { CplusplusGenerator } from '@asyncapi/modelina';
CplusplusOptions
| Option | Type | Default | Description |
|---|---|---|---|
namespace | string | 'AsyncapiModels' | C++ namespace for generated code |
typeMapping | TypeMapping | CplusplusDefaultTypeMapping | Custom type mappings |
constraints | Constraints | CplusplusDefaultConstraints | Custom constraint rules |
indentation | { type, size } | { type: SPACES, size: 2 } | Indentation settings |
presets | Presets | [] | Array of presets to apply |
Model Dispatch
| ConstrainedMetaModel Type | Renderer |
|---|---|
ConstrainedObjectModel | ClassRenderer (renders as struct) |
ConstrainedEnumModel | EnumRenderer (renders as enum class) |
File Generation
1const generator = new CplusplusFileGenerator({ /* options */ }); 2await generator.generateToFiles(input, './output', {}); 3// Creates: ./output/ModelName.hpp for each model
Preset System
CplusplusPreset Hook Types
1type CplusplusPreset = { 2 class?: CplusplusClassPreset; // For ConstrainedObjectModel 3 enum?: EnumPresetType; // For ConstrainedEnumModel 4}
Class (Struct) Preset Hooks
| Hook | Called | Args | Purpose |
|---|---|---|---|
self | Once per struct | { renderer, model, content, options } | Override entire struct |
ctor | Once per struct | { renderer, model, content, options } | Constructor (optional) |
property | Per property | { renderer, model, content, options, property } | Individual property |
additionalContent | Once | { renderer, model, content, options } | Extra content |
Default rendering:
1struct ModelName { 2 Type property_name; // property hook 3};
Enum Preset Hooks
| Hook | Called | Args | Purpose |
|---|---|---|---|
self | Once per enum | { renderer, model, content, options } | Override entire enum |
item | Per value | { renderer, model, content, options, item } | Individual enum value |
additionalContent | Once | { renderer, model, content, options } | Extra content |
Default rendering:
1enum class ModelName { 2 item_name, // item hook 3};
Built-in Presets
No custom built-in presets. Only default rendering presets.
Constraint System
Type Mappings
| MetaModel Type | C++ Type | Dependencies |
|---|---|---|
| Object | namespace::ModelName | |
| Reference | namespace::ModelName | |
| Any | std::any | #include <any> |
| Float | double | |
| Integer | int | |
| String | std::string | #include <string> |
| Boolean | bool | |
| Tuple | std::tuple<Type1, Type2> | #include <tuple> |
| Array | std::vector<Type> | #include <vector> |
| Enum | namespace::EnumName | |
| Union | std::variant<Type1, Type2> | #include <variant> |
| Dictionary | std::map<Key, Value> | #include <map> |
Optional properties are wrapped with std::optional<>.
Model Name Constraints
Pipeline: NO_SPECIAL_CHAR -> NO_NUMBER_START_CHAR -> NO_EMPTY_VALUE -> NO_RESERVED_KEYWORDS -> NAMING_FORMATTER(snake_case)
Note: C++ uses snake_case for model names (unlike most other languages).
Property Key Constraints
Pipeline: NO_SPECIAL_CHAR -> NO_NUMBER_START_CHAR -> NO_DUPLICATE_PROPERTIES -> NO_EMPTY_VALUE -> NAMING_FORMATTER(snake_case) -> NO_RESERVED_KEYWORDS
Enum Key Constraints
Pipeline: NO_SPECIAL_CHAR -> NO_NUMBER_START_CHAR -> NO_DUPLICATE_KEYS -> NO_EMPTY_VALUE -> NAMING_FORMATTER(snake_case) -> NO_RESERVED_KEYWORDS
Reserved Keywords (105 total)
Includes: alignas, alignof, and, asm, auto, bool, break, case, catch, char, class, const, constexpr, continue, delete, do, double, else, enum, extern, false, float, for, friend, goto, if, inline, int, long, namespace, new, nullptr, operator, private, protected, public, return, signed, static, struct, switch, template, this, throw, true, try, typedef, typename, union, unsigned, void, volatile, while, and more.
Customizing Type Mappings
Override specific type mappings while keeping defaults for the rest. Each function receives a TypeContext:
constrainedModel— the constrained model needing a type stringoptions—CplusplusOptions(includesnamespace)partOfProperty?— set when resolving type for a property (optional properties wrap withstd::optional<>)dependencyManager—CplusplusDependencyManagerto add#includedirectives
1const generator = new CplusplusGenerator({ 2 typeMapping: { 3 String: ({ dependencyManager }) => { 4 dependencyManager.addDependency('#include <string_view>'); 5 return 'std::string_view'; 6 }, 7 Float: () => 'float' 8 } 9});
Dependency Manager
CplusplusDependencyManager extends AbstractDependencyManager with base class only.
Methods:
| Method | Description |
|---|---|
addDependency(dep: string) | Add raw #include directive (deduplicates) |
Usage in presets:
1class: { 2 self({ dependencyManager, content }) { 3 dependencyManager.addDependency('#include <nlohmann/json.hpp>'); 4 return content; 5 } 6}
Quick Reference Examples
Basic generation
1const generator = new CplusplusGenerator(); 2const models = await generator.generate(jsonSchema);
Custom namespace
1const generator = new CplusplusGenerator({ 2 namespace: 'MyApp::Models' 3});
Generate to files
1import { CplusplusFileGenerator } from '@asyncapi/modelina'; 2 3const generator = new CplusplusFileGenerator({ 4 namespace: 'MyApp::Models' 5}); 6await generator.generateToFiles(schema, './generated', {});