extension for scripting

Hello Wagner,

We have a base class for all entities.
The entities have standardized primary keys.
"Entity name" + "ID"
In the base class we want to add an additional property for accessing the primary key. "property ID: TGUID"

  protected
    function GetID: TGUID; virtual; abstract;
    procedure SetID( Value: TGUID); virtual; abstract;
  public
    property ID: TGUID  read GetID  write SetID;

We have now built in an automatic script creation function.

procedure OnClassGenerated(Args: TClassGeneratedArgs);
var
  Func: TCodeMemberMethod;  
  Proc: TCodeMemberMethod;
  s: String;            
begin              
   // *** ID functions  ***
  s := copy(Args.CodeType.Name, 2, length(Args.CodeType.Name)-1);                         
  Func := Args.CodeType. AddFunction( 'GetID', 'TGUID', mvProtected);      
  Func.AddSnippet('Result := '+s+'ID;');      
  Proc := Args.CodeType.AddProcedure( 'SetID( Value: TGUID)', mvProtected);
  Proc.AddSnippet(s+' := Value;');

The "override" is still missing in the declaration.
We then made this change.

  Func := Args.CodeType.AddFunction( 'GetID', 'TGUID; override', mvProtected);      

  Proc := Args.CodeType.AddProcedure( 'SetID( Value: TGUID); override', mvProtected);

Now the "override" is also used in the implementation and it has to be removed manually.

Is there a solution here?

The resulting code should look like this:

// *** interface ***
// class TMyEntity

  protected
    function GetID: TGUID; override;
    procedure SetID( Value: TGUID); override;

// *** implementation ***

function TMyEntity.GetID: TGUID;
begin
  Result := MyEntityID;
end;

procedure TMyEntity.SetID( Value: TGUID);
begin
  MyEntityID := Value;
end;

Hi @Kaeswurm_Thomas,

Sure, you can use this to add the override directives:

Func.Directives := Func.Directives or SetOf([mdOverride]);
Proc.Directives := Proc.Directives or SetOf([mdOverride]);
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.