unit Chilkat.Base;

{
  Base class for all generated Chilkat Pascal wrappers.
  Every generated Chilkat.Xxx unit inherits TChilkatBase so that methods
  accepting Chilkat object arguments can use TChilkatBase instead of the
  specific subtype -- eliminating cross-unit dependencies and circular
  unit references.
}

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}

interface

type

  TChilkatBase = class
  protected
    // Opaque handle to the underlying Chilkat C++ object.
    // Generated subclasses assign this in their constructor.
    FHandle: Pointer;
  public
    // Default constructor -- FHandle stays nil until a subclass sets it.
    constructor Create; overload;

    // Lightweight constructor used when a bridge function returns a handle
    // that is owned by the Chilkat library (borrowed reference).
    // The destructor does NOT call any dispose function for these instances.
    constructor CreateFromHandle(h: Pointer); overload;

    destructor Destroy; override;

    // The raw C bridge handle.  Pass to bridge functions that accept a
    // Chilkat object as a parameter.
    property Handle: Pointer read FHandle;

    // True when the underlying Chilkat object was successfully allocated.
    function IsValid: Boolean;
  end;

  { Event handler types shared by all event-capable Chilkat classes
    (OnAbortCheck / OnPercentDone / OnProgressInfo properties in the
    generated wrapper units).  Return True from an AbortCheck or
    PercentDone handler to abort the running method.  Callbacks are
    invoked on the thread that called the Chilkat method. }
  TChilkatAbortCheckEvent   = function(Sender: TChilkatBase): Boolean of object;
  TChilkatPercentDoneEvent  = function(Sender: TChilkatBase; PctDone: Integer): Boolean of object;
  TChilkatProgressInfoEvent = procedure(Sender: TChilkatBase; const Name, Value: string) of object;

implementation

constructor TChilkatBase.Create;
begin
  inherited Create;
  FHandle := nil;
end;

constructor TChilkatBase.CreateFromHandle(h: Pointer);
begin
  inherited Create;
  FHandle := h;
end;

destructor TChilkatBase.Destroy;
begin
  // The base class does not dispose the handle.
  // Each generated subclass destructor calls the appropriate Ck*_Dispose().
  inherited Destroy;
end;

function TChilkatBase.IsValid: Boolean;
begin
  Result := FHandle <> nil;
end;

end.
