Please enable JavaScript to view this site.

Codolex

Now comes the most important part: code generation.

 

In the CodeGen file, you can find an empty procedure code that has the parameter SourceCode. This is a function from the interface and will be used in the code generation for a flow when the activity is used. Use this function to add lines to the generated code. This can be done by the add function on the SourceCode object. For example, hello world:

procedure TFlowActivityExampleDelphiSource.Code(const SourceCode: ISourceCode);

begin

  Sourcecode.Add('Hello world!');

end;

 

The source code object has multiple functions that can help with designing the code. We will cover a few functions that will be used in most activities. Keep in mind that every function in the SourceCode object returns the SourceCode to be used in the next line. That enables you to chain the SourcCode functions to make it easier and more readable.

 

.Add

The add function adds lines to the code. To start a new line, call another add function. The contents can be a string or an array of strings.

 

.IncreaseIndent and .DecreaseIndent

When designing the generated code, it's a good idea to keep in mind that it should be readable. Thus, having proper indentation is very important. Thankfully, Codolex uses IncreaseIndent/DecreaseIndent to places the code with spaces before it in the result. The SourceCode remembers the last indent, so, for a function or an if-statement where multiple lines need to have a greater indent, use IncreaseIndent once before, and Decrease Indent once after.

 

.Begin_ and .End_

This is useful for creating pieces of code that need a begin and end, like if-statements and loops, but also for functions and procedures. These two functions simply place a beginning and end tags to your code.

 

The .Begin_ function automatically increases the indent in the code and the .End_ function automatically decreases it.

SourceCode

  .Add('if not ::SoundFile.IsEmpty then')

  .Begin_

    .Add('sndPlaySound(::SoundFile, ::PlayMode);')

  .End_;

 

will result in

if not Soundpath.IsEmpty then

begin

  PlaySound(Soundpath, 0, SND_NODEFAULT);

end;

 

.Param

To help with designing the code, you can define parameters that the SourceCode object can parse when creating the result. Those parameters can be used in the add function with double points. "::", as seen above for the SoundFile and PlayMode parameters.

SourceCode
  .Param('SoundFile', FActivity.SoundFile.Name)
  .Param('PlayMode', TRttiEnumerationType.GetName(FActivity.PlayMode))
  .Add('sndPlaySound(::SoundFile, 0, ::PlayMode);');

 

will result in

sndPlaySound(Soundpath, SND_NODEFAULT);

 

 

Given that FActivity.SoundFile.Name = 'SoundPath' and FActivity.PlayMode = 'SND_NODEFAULT'.

 

.Variabele

Sometimes you might need a new variable in your generated code. This can be added with the .Variable function. Provide a name and a datatype. Optionally, provide a dependency when you need to import a unit for the datatype.

.Variable('TempString', 'string');

 

will result in

var TempString: string;

 

There is also an option to declare the param, so that you can use the variable directly in your code.

.VariableWithParam('TempParamName','TempEnumVar','TMyOwnEnum','Unit.of.enum')
...
.add('::TempParamName');

 
will result in

uses
  Unit.of.enum
...
var TempEnumVar: TMyOwnEnum;

on the place where the param was added.

 

.AddDependancy

Speaking of dependencies, you can also use the .AddDependancy function to add "Unit.of.enum". By default, this use is added in the implementation section. If you want it in the interface, use:

.AddDependancy('Unit.of.Enum',TUsesSection.Implementation_)

 

While there are more functions and variations of the mentioned function in the SourceCode object, you can always find them in the auto-complete, and just try them out with a few example projects. For our activity, this is enough for now.

 

SourceCode
  .AddDependency('MMSystem',TUsesSection.Interface_)
  .Param('SoundFile', FActivity.SoundFile.Name)
  .Param('PlayMode', TRttiEnumerationType.GetName(FActivity.PlayMode))
  .Add('if not (::SoundFile = '''') then')
  .Begin_
    .VariableWithParam('SoundFileChar', 'SoundFileWideChar', 'PWideChar')
    .Add('::SoundFileChar')
    .add('::SoundFileChar := PWideChar(::SoundFile);')
   
    .Add('sndPlaySound(::SoundFileChar, ::PlayMode);')
  .end_;

 

© by GDK Software