-
Notifications
You must be signed in to change notification settings - Fork 29
Meta programming
Denis Koronchik edited this page Oct 29, 2016
·
1 revision
Content
There is a code generator that allows to create some common code by using metadata. It runs before code compilation and generate files with a name <header_name>.generated.<header_extension>
.
For example if you run it for a file text.h
it would produce a file text.generated.h
To make a metadata for you code you can use this set of macros:
- SC_CLASS - allows you to specify metadata for a class;
- SC_GENERATED_BODY - macros that need to be used after SC_CLASS, because it would be replaced in during compilation time with generated declaration for this class;
- SC_PROPERTY - allows to specify metadata for members of a class (including static members).
You should to specify SC_CLASS and SC_GENERATED_BODY for all child classes of ScObject
There is a syntax rule that used for a metadata specification:
[<PropertyName> [ (<PropertyValue>, <PropertyValue>, ...) ] ], …
For example:
SC_CLASS(Agent, CmdClass("command_update_power_usage"))
SC_CLASS(CmdClass("command_generate_text_from_template"), Agent)
SC_PROPERTY(Keynode("nrel_real_energy_usage"), ForceCreate)
SC_CLASS(Agent, Event(ActionManager::msActionPeriodical, SC_EVENT_ADD_OUTPUT_ARC))
You should to use SC_CLASS and SC_GENERATED_BODY in class declaration:
class AWhoAreYouAgent : public ScAgentAction
{
SC_CLASS(Agent, CmdClass("command_who_are_you"))
SC_GENERATED_BODY()
};
Table of available properties of class metadata (SC_CLASS):
Property | Description |
---|---|
Agent |
Parent class: ScAgent and all childs
You should always use it for all ScAgent child classes |
CmdClass | Determine system identifier of command class that implemented by sc-agent.
Parent class: ScAgentAction Arguments:
class AAddContentAgent : public ScAgentAction
{
SC_CLASS(Agent, CmdClass("command_add_content"))
SC_GENERATED_BODY()
};
|
Event | Specify condition to start sc-agent implementation. Parent class: ScAgent Arguments:
class ANewPeriodicalActionAgent : public ScAgent
{
SC_CLASS(Agent, Event(msActionPeriodical, SC_EVENT_ADD_OUTPUT_ARC))
SC_GENERATED_BODY()
};
|
LoadOrder | Specify order (priority) of module loading. Can be used just in ScModule child classes.
Parent class: ScModule Arguments:
class nlModule : public ScModule
{
SC_CLASS(LoadOrder(11))
SC_GENERATED_BODY()
sc_result initializeImpl();
sc_result shutdownImpl();
}; |
Table of available properties of class members metadata (SC_PROPERTY):
Property | Description |
---|---|
Keynode |
Arguments:
You can use this property just for members that has ScAddr type. SC_PROPERTY(Keynode("device"), ForceCreate)
static ScAddr device;
|
Template |
Arguments:
You can use this property just for members that has ScTemplate type. SC_PROPERTY(Template("test_template"))
ScTemplate mTestTemplate;
|
ForceCreate |
Arguments: none
Used just with Keynode property. Using of this property force sc-element creation, if it didn't found by system identifier. |