MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / autogen_cpp_sum

Function autogen_cpp_sum

crates/codegen/src/unrealcpp.rs:6365–6657  ·  view source on GitHub ↗
(
    module_prefix: &str,
    module: &ModuleDef,
    name: &str,
    sum_type: &SumTypeDef,
    api_macro: &str,
    module_name: &str,
)

Source from the content-addressed store, hash-verified

6363}
6364
6365fn autogen_cpp_sum(
6366 module_prefix: &str,
6367 module: &ModuleDef,
6368 name: &str,
6369 sum_type: &SumTypeDef,
6370 api_macro: &str,
6371 module_name: &str,
6372) -> String {
6373 use convert_case::Case;
6374
6375 /* ------------------------------------------------------------------ */
6376 /* 1. gather #includes for every variant payload -------------------- */
6377 /* ------------------------------------------------------------------ */
6378 let mut includes = HashSet::<String>::new();
6379 for (_, alg_ty) in &sum_type.variants {
6380 collect_includes_for_type(module_prefix, module, alg_ty, &mut includes, module_name);
6381 }
6382
6383 includes.insert("Kismet/BlueprintFunctionLibrary.h".to_string());
6384
6385 let include_slices: Vec<&str> = includes.iter().map(|s| s.as_str()).collect();
6386
6387 // /* ------------------------------------------------------------------ */
6388 // /* 2. emit boiler-plate + forward decls ----------------------------- */
6389 // /* ------------------------------------------------------------------ */
6390 let type_name = format!("{name}Type");
6391 let mut output = UnrealCppAutogen::new(&include_slices, &type_name, false);
6392
6393 /* ------------------------------------------------------------------ */
6394 /* 3. generate the tag enum ----------------------------------------- */
6395 /* ------------------------------------------------------------------ */
6396 writeln!(output, "UENUM(BlueprintType)\nenum class E{name}Tag : uint8\n{{");
6397
6398 let mut variant_type = HashSet::<String>::new();
6399
6400 for (ix, (variant, _variant_type)) in sum_type.variants.iter().enumerate() {
6401 let comma = if ix + 1 == sum_type.variants.len() { "" } else { "," };
6402 writeln!(output, " {}{}", variant.to_case(Case::Pascal), comma);
6403
6404 let variant_cpp_type = cpp_ty_fmt_with_module(module_prefix, module, _variant_type, module_name).to_string();
6405 variant_type.insert(variant_cpp_type);
6406 }
6407 writeln!(output, "}};\n");
6408
6409 /* ------------------------------------------------------------------ */
6410 /* 4. generate the UStruct ----------------------------------- */
6411 /* ------------------------------------------------------------------ */
6412
6413 writeln!(output, "USTRUCT(BlueprintType)\nstruct {api_macro} F{name}Type\n{{");
6414 writeln!(output, " GENERATED_BODY()\n");
6415
6416 // default ctor
6417 writeln!(output, "public:\n F{name}Type() = default;\n");
6418
6419 // data + tag
6420 writeln!(
6421 output,
6422 " TVariant<{}> MessageData;",

Callers 1

generate_type_filesMethod · 0.85

Calls 15

cpp_ty_fmt_with_moduleFunction · 0.85
is_primitive_or_fstringFunction · 0.85
is_blueprintableFunction · 0.85
to_caseMethod · 0.80
insertMethod · 0.65
iterMethod · 0.65
newFunction · 0.50
to_stringMethod · 0.45
collectMethod · 0.45
mapMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…