Search code examples
delphisuperobject

Using the Result arg in SuperObject


i'm using this superobject unit in one of my project as an rpc protocol, and inside a remote called procedure (signature has a var Result arg) i want to know how to use that arg...

isn't there a documentation ? thanks.

program test_rpc;

{$IFDEF FPC}
  {$MODE OBJFPC}{$H+}
{$ELSE}
  {$APPTYPE CONSOLE}
{$ENDIF}

uses
  SysUtils, superobject;

procedure controler_method1(const This, Params: ISuperObject; var Result: ISuperObject);
var
  i: Integer;
begin
  write('action called with params ');
  writeln(Params.AsString);

  try
    // How do i use Result arg to return a value ? as if it were a function returning string
    Result
  except
    exit;
  end;
end;

var
  s: ISuperObject;
begin
  s := TSuperObject.Create;
  s.M['controler.action1'] := @controler_method1;
  try
    s['controler.action1("HHAHAH")'];
  finally
    s := nil;
    writeln('Press enter ...');
    readln;
  end;
end.

Solution

  • When controler_method1 is called, the var parameter Result is nil. To return something to the caller you need to assign to Result.

    Result := TSuperObject.Create;
    

    That's an empty super object. You can now populate it with whatever values you like in the normal way.

    Here is a simple demonstration:

    program test_rpc;
    
    {$IFDEF FPC}
      {$MODE OBJFPC}{$H+}
    {$ELSE}
      {$APPTYPE CONSOLE}
    {$ENDIF}
    
    uses
      SysUtils,
      superobject in 'superobject.pas';
    
    procedure controler_method1(const This, Params: ISuperObject;
        var Result: ISuperObject);
    begin
      Result := TSuperObject.Create('Foo');
    end;
    
    var
      s: ISuperObject;
    begin
      s := TSuperObject.Create;
      s.M['controler.action1'] := controler_method1;
      Writeln(s['controler.action1("HHAHAH")'].AsString);
      Readln;
    end.