schema_salad.cpp_codegen

C++17 code generator for a given Schema Salad definition.

Currently only supports emiting YAML from the C++ objects, not yet parsing YAML into C++ objects.

The generated code requires the libyaml-cpp library & headers

To see an example of usage, look at schema_salad/tests/codegen/cwl.cpp which can be combined with the CWL V1.0 schema as shown below:

schema-salad-tool --codegen cpp           schema_salad/tests/test_schema/CommonWorkflowLanguage.yml           > cwl_v1_0.h

g++ --std=c++20 -I. -lyaml-cpp schema_salad/tests/codegen/cwl.cpp -o cwl-v1_0-test
./cwl-v1_0-test

# g++ versions older than version 10 may need "--std=c++2a" instead of "--std=c++20"

Module Contents

Classes

ClassDefinition

Prototype of a class.

FieldDefinition

Prototype of a single field from a class definition.

MapDefinition

Prototype of a map.

UnionDefinition

Prototype of a union.

EnumDefinition

Prototype of a enum.

CppCodeGen

Generation of C++ code for a given Schema Salad definition.

Functions

q(s)

Put quotes around a string.

replaceKeywords(s)

Rename keywords that are reserved in C++.

safename(name)

Create a C++ safe name.

safename2(name)

Create a namespaced safename.

split_name(s)

Split url name into its components.

split_field(s)

Split field into its components.

isPrimitiveType(v)

Check if v is a primitve type.

hasFieldValue(e, f, v)

Check if e has a field f value.

isRecordSchema(v)

Check if v is of type record schema.

isEnumSchema(v)

Check if v is of type enum schema.

isArray(v)

Check if v is of type array.

pred(i)

Check if v is any of the simple types.

isArraySchema(v)

Check if v is of type array schema.

isMapSchema(v)

Check if v is of type map schema.

isUnionSchema(v)

Check if v is of type union schema.

schema_salad.cpp_codegen.q(s)

Put quotes around a string.

Parameters:

s (str) –

Return type:

str

schema_salad.cpp_codegen.replaceKeywords(s)

Rename keywords that are reserved in C++.

Parameters:

s (str) –

Return type:

str

schema_salad.cpp_codegen.safename(name)

Create a C++ safe name.

Parameters:

name (str) –

Return type:

str

schema_salad.cpp_codegen.safename2(name)

Create a namespaced safename.

Parameters:

name (Dict[str, str]) –

Return type:

str

schema_salad.cpp_codegen.split_name(s)

Split url name into its components.

Splits names like https://xyz.xyz/blub#cwl/class into its class path and non class path

Parameters:

s (str) –

Return type:

Tuple[str, str]

schema_salad.cpp_codegen.split_field(s)

Split field into its components.

similar to split_name but for field names

Parameters:

s (str) –

Return type:

Tuple[str, str, str]

class schema_salad.cpp_codegen.ClassDefinition(name)

Prototype of a class.

Parameters:

name (str) –

writeFwdDeclaration(target, fullInd, ind)

Write forward declaration.

Parameters:
  • target (IO[str]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

writeDefinition(target, fullInd, ind)

Write definition of the class.

Parameters:
  • target (IO[Any]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

writeImplDefinition(target, fullInd, ind)

Write definition with implementation.

Parameters:
  • target (IO[str]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

class schema_salad.cpp_codegen.FieldDefinition(name, typeStr, optional, remap)

Prototype of a single field from a class definition.

Parameters:
  • name (str) –

  • typeStr (str) –

  • optional (bool) –

  • remap (str) –

writeDefinition(target, fullInd, ind, namespace)

Write a C++ definition for the class field.

Parameters:
  • target (IO[Any]) –

  • fullInd (str) –

  • ind (str) –

  • namespace (str) –

Return type:

None

class schema_salad.cpp_codegen.MapDefinition(name, values)

Prototype of a map.

Parameters:
  • name (str) –

  • values (List[str]) –

writeFwdDeclaration(target, fullInd, ind)

Write forward declaration.

Parameters:
  • target (IO[str]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

writeDefinition(target, ind)

Write map definition to output.

Parameters:
  • target (IO[str]) –

  • ind (str) –

Return type:

None

writeImplDefinition(target, fullInd, ind)

Write definition with implementation.

Parameters:
  • target (IO[str]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

class schema_salad.cpp_codegen.UnionDefinition(name, types)

Prototype of a union.

Parameters:
  • name (str) –

  • types (List[str]) –

writeFwdDeclaration(target, fullInd, ind)

Write forward declaration.

Parameters:
  • target (IO[str]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

writeDefinition(target, ind)

Write union definition to output.

Parameters:
  • target (IO[str]) –

  • ind (str) –

Return type:

None

writeImplDefinition(target, fullInd, ind)

Write definition with implementation.

Parameters:
  • target (IO[str]) –

  • fullInd (str) –

  • ind (str) –

Return type:

None

class schema_salad.cpp_codegen.EnumDefinition(name, values)

Prototype of a enum.

Parameters:
  • name (str) –

  • values (List[str]) –

writeDefinition(target, ind)

Write enum definition to output.

Parameters:
  • target (IO[str]) –

  • ind (str) –

Return type:

None

schema_salad.cpp_codegen.isPrimitiveType(v)

Check if v is a primitve type.

Parameters:

v (Any) –

Return type:

bool

schema_salad.cpp_codegen.hasFieldValue(e, f, v)

Check if e has a field f value.

Parameters:
  • e (Any) –

  • f (str) –

  • v (Any) –

Return type:

bool

schema_salad.cpp_codegen.isRecordSchema(v)

Check if v is of type record schema.

Parameters:

v (Any) –

Return type:

bool

schema_salad.cpp_codegen.isEnumSchema(v)

Check if v is of type enum schema.

Parameters:

v (Any) –

Return type:

bool

schema_salad.cpp_codegen.isArray(v)

Check if v is of type array.

Parameters:

v (Any) –

Return type:

bool

schema_salad.cpp_codegen.pred(i)

Check if v is any of the simple types.

Parameters:

i (Any) –

Return type:

bool

schema_salad.cpp_codegen.isArraySchema(v)

Check if v is of type array schema.

Parameters:

v (Any) –

Return type:

bool

schema_salad.cpp_codegen.isMapSchema(v)

Check if v is of type map schema.

Parameters:

v (Any) –

Return type:

bool

schema_salad.cpp_codegen.isUnionSchema(v)

Check if v is of type union schema.

Parameters:

v (Any) –

Return type:

bool

class schema_salad.cpp_codegen.CppCodeGen(base, target, examples, package, copyright, spdx_copyright_text, spdx_license_identifier)

Bases: schema_salad.codegen_base.CodeGenBase

digraph inheritanceb32d88fe87 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "CodeGenBase" [URL="../codegen_base/index.html#schema_salad.codegen_base.CodeGenBase",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Abstract base class for schema salad code generators."]; "CppCodeGen" [URL="#schema_salad.cpp_codegen.CppCodeGen",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Generation of C++ code for a given Schema Salad definition."]; "CodeGenBase" -> "CppCodeGen" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

Generation of C++ code for a given Schema Salad definition.

Parameters:
  • base (str) –

  • target (IO[str]) –

  • examples (Optional[str]) –

  • package (str) –

  • copyright (Optional[str]) –

  • spdx_copyright_text (Optional[List[str]]) –

  • spdx_license_identifier (Optional[str]) –

convertTypeToCpp(type_declaration)

Convert a Schema Salad type to a C++ type.

Parameters:

type_declaration (Union[List[Any], Dict[str, Any], str]) –

Return type:

str

epilogue(root_loader)

Generate final part of our cpp file.

Parameters:

root_loader (Optional[schema_salad.codegen_base.TypeDef]) –

Return type:

None

parseRecordField(field)

Parse a record field.

Parameters:

field (Dict[str, Any]) –

Return type:

FieldDefinition

parseRecordSchema(stype)

Parse a record schema.

Parameters:

stype (Dict[str, Any]) –

Return type:

None

parseMapSchema(stype)

Parse a map schema.

Parameters:

stype (Dict[str, Any]) –

Return type:

str

parseUnionSchema(stype)

Parse a union schema.

Parameters:

stype (Dict[str, Any]) –

Return type:

str

parseEnum(stype)

Parse a schema salad enum.

Parameters:

stype (Dict[str, Any]) –

Return type:

str

parse(items)

Parse sechema salad items.

This function is being called from the outside and drives the whole code generation.

Parameters:

items (List[Dict[str, Any]]) –

Return type:

None