Skip to content

MacroPropertyCodeGen

Julien SOYSOUVANH edited this page Nov 26, 2021 · 3 revisions

Introduction

The MacroPropertyCodeGen inherits from the PropertyCodeGen class.

Create a MacroPropertyCodeGen

To create a new MacroPropertyCodeGen, you must inherit from MacroPropertyCodeGen instead of PropertyCodeGen:

#include <Kodgen/CodeGen/Macro/MacroPropertyCodeGen.h>

class ExamplePCG : public kodgen::MacroPropertyCodeGen
{
};

Override methods

getGenerationOrder

Same as PropertyCodeGen::getGenerationOrder.

shouldGenerateCode

Same as PropertyCodeGen::shouldGenerateCode.

initialGenerateCode

The initialGenerateCode method is sealed in the MacroPropertyCodeGen implementation. Instead, 3 methods are called once to initially generate code in each location:

  • initialGenerateHeaderFileHeaderCode
  • initialGenerateHeaderFileFooterCode
  • initialGenerateSourceFileHeaderCode
bool initialGenerateHeaderFileHeaderCode(kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
    //Generate code at the beginning of the generated header file header
    //...

    return true;
}

finalGenerateCode

The finalGenerateCode method is sealed in the MacroPropertyCodeGen implementation. Instead, 3 methods are called once to generate code in each location:

  • finalGenerateHeaderFileHeaderCode
  • finalGenerateHeaderFileFooterCode
  • finalGenerateSourceFileHeaderCode
bool finalGenerateHeaderFileHeaderCode(kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
    //Generate code at the end of the generated header file header
    //...

    return true;
}

preGenerateCodeForEntity

Called once before generating code for an eligible entity/property pair. Returns false to abort the code generation for the running CodeGenUnit.

bool preGenerateCodeForEntity(kodgen::EntityInfo const& entity, kodgen::Property const& property, kodgen::uint8 propertyIndex, kodgen::MacroCodeGenEnv& env) noexcept override
{
    //Do some stuff
    return true; //return false if you want to abort the code generation for the running unit
}

postGenerateCodeForEntity

Called once after generating code for an entity/property pair. Returns false to abort the code generation for the running CodeGenUnit.

bool preGenerateCodeForEntity(kodgen::EntityInfo const& entity, kodgen::Property const& property, kodgen::uint8 propertyIndex, kodgen::MacroCodeGenEnv& env) noexcept override
{
    //Do some stuff
    return true; //return false if you want to abort the code generation for the running unit
}

generateHeaderFileHeaderCodeForEntity

This method generates code for the attached entity in the HeaderFileHeader location (see this section for more info about code generation locations). Returns false to abort the code generation for the running CodeGenUnit.

bool generateHeaderFileHeaderCodeForEntity(kodgen::EntityInfo const& entity, kodgen::Property const& property, kodgen::uint8 propertyIndex, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
    //TODO: Append generated code to inout_result here
    return true; //or return false if failure + abort
}

generateClassFooterCodeForEntity

This method generates code for the attached entity in the ClassFooter location (see this section for more info about code generation locations). Note that this method is called only for properties attached to structs, classes, or struct/class nested entities. Returns false to abort the code generation for the running CodeGenUnit.

bool generateClassFooterCodeForEntity(kodgen::EntityInfo const& entity, kodgen::Property const& property, kodgen::uint8 propertyIndex, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
    //TODO: Append generated code to inout_result here
    return true; //or return false if failure + abort
}

generateHeaderFileFooterCodeForEntity

This method generates code for the attached entity in the HeaderFileFooter location (see this section for more info about code generation locations). Returns false to abort the code generation for the running CodeGenUnit.

bool generateHeaderFileFooterCodeForEntity(kodgen::EntityInfo const& entity, kodgen::Property const& property, kodgen::uint8 propertyIndex, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
    //TODO: Append generated code to inout_result here
    return true; //or return false if failure + abort
}

generateSourceFileHeaderCodeForEntity

This method generates code for the attached entity in the SourceFileHeader location (see this section for more info about code generation locations). Returns false to abort the code generation for the running CodeGenUnit.

bool generateSourceFileHeaderCodeForEntity(kodgen::EntityInfo const& entity, kodgen::Property const& property, kodgen::uint8 propertyIndex, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
    //TODO: Append generated code to inout_result here
    return true; //or return false if failure + abort
}

Tips

Tip 1

You can use the MacroCodeGenEnv::getSeparator() method to insert spaces and generate human-readable (and more easily debuggable) code.
See this section for more details.