LiveBindings Inc Method (Delphi)
Contents
Description
This example shows how to create a LiveBindings method, Inc, that increases an integer by 1.
To build and test this example:
- Create a package and add a unit to it.
 - Add the following units to the 
usessubsection of theimplementationsection:- System.Bindings.Methods
 - System.Bindings.EvalProtocol
 - System.Bindings.Consts
 - System.TypInfo
 
 - Define a function that returns an instance of IInvokable. This instance is a function that expects an integer and returns this integer increased by one. For example, if the function receives 5, it returns 6.
 - In the 
initializationsection, register your invokable function with'Inc'as both name and ID. - In the 
finalizationsection, unregister your invokable function. 
You can now install your package. Your method is registered and you can use it in your custom format and parse expressions. Your method is also listed in the LiveBindings Methods editor.
Code
unit IncMethod;
interface
implementation
uses System.Bindings.Methods, System.Bindings.EvalProtocol, System.Bindings.Consts, System.TypInfo;
function MakeMethodInc: IInvokable;
begin
  Result := MakeInvokable(function(Args: TArray<IValue>): IValue
  var
    InputValue: IValue;
    InputInteger, OutputInteger: Int64;
  begin
    // Ensure only one argument is received.
    if Length(Args) <> 1 then
      raise EEvaluatorError.Create(sFormatArgError);
    // Ensure that the received argument is an integer.
    InputValue := Args[0];
    if not (InputValue.GetType.Kind in [tkInteger, tkInt64]) then
      raise EEvaluatorError.Create(sFormatExpectedStr);
    // Calculate the output integer.
    InputInteger := InputValue.GetValue.AsInt64;
    OutputInteger := InputInteger + 1;
    // Return the output integer.
    Result := TValueWrapper.Create(OutputInteger);
  end);
end;
initialization
  TBindingMethodsFactory.RegisterMethod(
    TMethodDescription.Create(
      MakeMethodInc,
      'Inc',
      'Inc', '', True,
      sFormatDesc,
      nil));
finalization
  TBindingMethodsFactory.UnRegisterMethod('Inc');
end.
Uses
- System.Bindings.EvalProtocol.IInvokable ( fr | de | ja )
 - System.Bindings.Methods.MakeInvokable ( fr | de | ja )
 - System.Bindings.EvalProtocol.IValue ( fr | de | ja )
 - System.Bindings.EvalProtocol.TValueWrapper ( fr | de | ja )
 - System.Bindings.Methods.TBindingMethodsFactory.RegisterMethod ( fr | de | ja )
 - System.Bindings.Methods.TBindingMethodsFactory.UnRegisterMethod ( fr | de | ja )
 - System.Bindings.Methods.TMethodDescription ( fr | de | ja )
 

