unit uACClasses;

interface

uses SysUtils, uInterfaces, Windows;

type
  TAOImple = class(TAggregatedObject, IImplInterface)
    //IImplInterface
  private
    FUSDRate : Double;
    FRMBRate : Double;

    function GetUSDRate : Double; stdcall;
    function GetRMBRate : Double; stdcall;
    procedure SetUSDRate(const dRate : Double); stdcall;
    procedure SetRMBRate(const dRate : Double); stdcall;
  public
    function ConvertToUSD(const iNTD : Integer) : Double; stdcall;
    function ConvertToRMB(const iNTD : Integer) : Double; stdcall;
  end;

  TController = class(TObject, IInterface, IImplInterface)
  private
    aAOObj : TAOImple;
//    FImple : IImplInterface;
    FRefCount : Integer;
  protected
    { IInterface }
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  public
    Constructor Create;
    Destructor Destroy; override;
    Property MyImpl : TAOImple read aAOObj implements IImplInterface;
  end;

implementation

uses fmMain2;

{ TController }

function TController._AddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TController._Release: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
//  if Result = 0 then
//    Destroy;
end;

constructor TController.Create;
begin
  aAOObj := TAOImple.Create(Self);
//  FImple := aAOObj;
  aAOObj.SetUSDRate(34.8);
  aAOObj.SetRMBRate(4.05);
end;

destructor TController.Destroy;
begin
//  if Assigned(aAOObj) then
//    FreeAndNil(aAOObj);
  Form1.AppMsg('TController Destroyed At : ' + DateTimeToStr(Now));
  inherited;
end;

function TController.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

{ TAOImple }

function TAOImple.ConvertToRMB(const iNTD: Integer): Double;
begin
  Result := iNTD / FRMBRate;
end;

function TAOImple.ConvertToUSD(const iNTD: Integer): Double;
begin
  Result := iNTD / FUSDRate;
end;

function TAOImple.GetRMBRate: Double;
begin
  Result := FRMBRate;
end;

function TAOImple.GetUSDRate: Double;
begin
  Result := FUSDRate;
end;

procedure TAOImple.SetRMBRate(const dRate: Double);
begin
  FRMBRate := dRate;
end;

procedure TAOImple.SetUSDRate(const dRate: Double);
begin
  FUSDRate := dRate;
end;

end.
