-
Notifications
You must be signed in to change notification settings - Fork 10
MacroCodeGenModule
The MacroCodeGenModule inherits from the CodeGenModule class. It only accepts MacroPropertyCodeGen, and provides a few more methods to override to generate code.
To create a new MacroCodeGenModule, you must inherit from MacroCodeGenModule instead of CodeGenModule:
#include <Kodgen/CodeGen/Macro/MacroCodeGenModule.h>
class ExampleCGM : public kodgen::MacroCodeGenModule
{
};
Same as CodeGenModule::getGenerationOrder.
The initialGenerateCode
method is sealed in the MacroCodeGenModule
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;
}
The finalGenerateCode
method is sealed in the MacroCodeGenModule
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;
}
Called once before generating code for each entity. Returns false to abort the code generation for the running CodeGenUnit
.
bool preGenerateCodeForEntity(kodgen::EntityInfo const& entity, kodgen::MacroCodeGenEnv& data) noexcept override
{
//Do some stuff
return true; //return false if you want to abort the code generation for the running unit
}
Called once after generating code for each entity. Returns false to abort the code generation for the running CodeGenUnit
.
bool postGenerateCodeForEntity(kodgen::EntityInfo const& entity, kodgen::MacroCodeGenEnv& data) noexcept override
{
//Do some stuff
return true; //return false if you want to abort the code generation for the running unit
}
This method generates code for the given entity in the HeaderFileHeader location.
Check the CodeGenModule::generateCodeForEntity method documentation for more info on the returned value.
kodgen::ETraversalBehaviour generateHeaderFileHeaderCodeForEntity(kodgen::EntityInfo const& entity, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
//Generate some code here by appending it to inout_result
return kodgen::ETraversalBehaviour::Recurse;
}
This method generates code for the given entity in the ClassFooter location.
Check the CodeGenModule::generateCodeForEntity method documentation for more info on the returned value.
kodgen::ETraversalBehaviour generateClassFooterCodeForEntity(kodgen::EntityInfo const& entity, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
//Generate some code here by appending it to inout_result
return kodgen::ETraversalBehaviour::Recurse;
}
This method generates code for the given entity in the HeaderFileFooter location.
Check the CodeGenModule::generateCodeForEntity method documentation for more info on the returned value.
kodgen::ETraversalBehaviour generateHeaderFileFooterCodeForEntity(kodgen::EntityInfo const& entity, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
//Generate some code here by appending it to inout_result
return kodgen::ETraversalBehaviour::Recurse;
}
This method generates code for the given entity in the SourceFileHeader location.
Check the CodeGenModule::generateCodeForEntity method documentation for more info on the returned value.
kodgen::ETraversalBehaviour generateSourceFileHeaderCodeForEntity(kodgen::EntityInfo const& entity, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
//Generate some code here by appending it to inout_result
return kodgen::ETraversalBehaviour::Recurse;
}
You can use the MacroCodeGenEnv::getSeparator() method to insert spaces and generate human-readable (and more easily debuggable) code. It basically returns a backspace or backspace + \ depending on the generation location (if the code is generated in a macro, backspace + \ is returned).
kodgen::ETraversalBehaviour generateClassFooterCode(kodgen::EntityInfo const* entity, kodgen::MacroCodeGenEnv& env, std::string& inout_result) noexcept override
{
inout_result += "void someFunction(){}";
inout_result += "void someFunction2(){}";
return kodgen::ETraversalBehaviour::Recurse;
}
The above would generate something like:
#define GENERATED_MACRO \
void someFunction(){}void someFunction2(){}
Now, if we decide to use separators:
kodgen::ETraversalBehaviour generateClassFooterCode(kodgen::EntityInfo const* entity, kodgen::MacroCodeGenEnv& env , std::string& inout_result) noexcept override
{
inout_result += "void someFunction(){}" + env.getSeparator();
inout_result += "void someFunction2(){}";
return kodgen::ETraversalBehaviour::Recurse;
}
#define GENERATED_MACRO \
void someFunction(){} \
void someFunction2(){}
The default implementation for the 4 code generation methods return ETraversalBehaviour::Break, so one must override at least one of generateHeaderFileHeaderCode, generateClassFooterCode, generateHeaderFileFooterCode or generateSourceFileHeaderCode to give a specific traversal behaviour to the module. The less restrictive returned traversal behaviour will always have the priority over the others.
The priority order is: AbortWithFailure > AbortWithSuccess > Recurse > Continue > Break.