1
0
mirror of synced 2024-11-16 15:33:50 +00:00
G940LEDControl/G940LEDControl/Units/FSXSimConnectStateMonitor.pas

115 lines
2.6 KiB
ObjectPascal

unit FSXSimConnectStateMonitor;
interface
uses
System.Classes,
System.SyncObjs,
FSXSimConnectIntf;
type
TFSXSimConnectStateMonitor = class(TObject)
private
FObservers: TInterfaceList;
FCurrentStateLock: TCriticalSection;
FCurrentState: TFSXSimConnectState;
procedure DoSetCurrentState(const Value: TFSXSimConnectState);
protected
property CurrentStateLock: TCriticalSection read FCurrentStateLock;
property Observers: TInterfaceList read FObservers;
public
constructor Create;
destructor Destroy; override;
class function Instance: TFSXSimConnectStateMonitor;
class procedure SetCurrentState(AState: TFSXSimConnectState);
procedure Attach(AObserver: IFSXSimConnectStateObserver);
procedure Detach(AObserver: IFSXSimConnectStateObserver);
property CurrentState: TFSXSimConnectState read FCurrentState write DoSetCurrentState;
end;
implementation
uses
System.SysUtils;
var
FSXSimConnectStateInstance: TFSXSimConnectStateMonitor;
{ TFSXSimConnectState }
class function TFSXSimConnectStateMonitor.Instance: TFSXSimConnectStateMonitor;
begin
Result := FSXSimConnectStateInstance;
end;
class procedure TFSXSimConnectStateMonitor.SetCurrentState(AState: TFSXSimConnectState);
begin
Instance.DoSetCurrentState(AState);
end;
constructor TFSXSimConnectStateMonitor.Create;
begin
inherited Create;
FCurrentStateLock := TCriticalSection.Create;
FObservers := TInterfaceList.Create;
end;
destructor TFSXSimConnectStateMonitor.Destroy;
begin
FreeAndNil(FObservers);
FreeAndNil(FCurrentStateLock);
inherited Destroy;
end;
procedure TFSXSimConnectStateMonitor.Attach(AObserver: IFSXSimConnectStateObserver);
begin
Observers.Add(AObserver as IFSXSimConnectStateObserver);
end;
procedure TFSXSimConnectStateMonitor.Detach(AObserver: IFSXSimConnectStateObserver);
begin
Observers.Remove(AObserver as IFSXSimConnectStateObserver);
end;
procedure TFSXSimConnectStateMonitor.DoSetCurrentState(const Value: TFSXSimConnectState);
var
observer: IInterface;
begin
if Value <> FCurrentState then
begin
CurrentStateLock.Acquire;
try
FCurrentState := Value;
finally
CurrentStateLock.Release;
end;
for observer in Observers do
(observer as IFSXSimConnectStateObserver).ObserverStateUpdate(CurrentState);
end;
end;
initialization
FSXSimConnectStateInstance := TFSXSimConnectStateMonitor.Create;
finalization
FreeAndNil(FSXSimConnectStateInstance);
end.