📄 dnabstracttimer.pas
字号:
// The contents of this file are used with permission, subject to
// the Mozilla Public License Version 1.1 (the "License"); you may
// not use this file except in compliance with the License. You may
// obtain a copy of the License at
// http://www.mozilla.org/MPL/MPL-1.1.html
//
// Software distributed under the License is distributed on an
// "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
// implied. See the License for the specific language governing
// rights and limitations under the License.
{$I DnConfig.inc}
unit DnAbstractTimer;
interface
uses
Classes, SysUtils,
DnRtl, DnInterfaces, DnAbstractExecutor, DnAbstractLogger,
DnConst;
type
TDnTimerNotify = procedure (Context: TDnThreadContext; Channel: IDnChannel;
ExpiredTacts: Cardinal; Key: Pointer) of object;
IDnTimerObserver = interface
['{161C30D6-F949-41c5-B434-225F2E46F636}']
function FireAt: Cardinal;
procedure Cancel;
end;
IDnTimerHandler = interface
procedure TimerExpired(Context: TDnThreadContext; Channel: IDnChannel;
ExpiredTacts: Cardinal; Key: Pointer);
end;
TDnAbstractTimerEngine = class (TObject)
protected
FExecutor: TDnAbstractExecutor;
FLogger: TDnAbstractLogger;
FLogLevel: TDnLogLevel;
FActive: Boolean;
procedure SetActive(Value: Boolean);
function TurnOn: Boolean; virtual; abstract;
function TurnOff: Boolean; virtual; abstract;
public
constructor Create;
destructor Destroy; override;
function RequestTimerNotify(Channel: IDnChannel; Tacts: Cardinal;
Key: Pointer; Handler: IDnTimerHandler): IDnTimerObserver; virtual; abstract;
procedure CancelNotify(Request: IDnTimerObserver); virtual; abstract;
property Active: Boolean read FActive write SetActive;
property Logger: TDnAbstractLogger read FLogger write FLogger;
property Executor: TDnAbstractExecutor read FExecutor write FExecutor;
property LogLevel: TDnLogLevel read FLogLevel write FLogLevel;
end;
{$IFDEF ROOTISCOMPONENT}
TDnAbstractTimer = class (TComponent, IDnTimerHandler)
{$ELSE}
TDnAbstractTimer = class (TDnObject, IDnTimerHandler)
{$ENDIF}
protected
FTimerEngine: TDnAbstractTimerEngine;
FNotify: TDnTimerNotify;
procedure SetActive(Value: Boolean);
function GetActive: Boolean;
procedure SetExecutor(Value: TDnAbstractExecutor);
function GetExecutor: TDnAbstractExecutor;
procedure SetLogger(Value: TDnAbstractLogger);
function GetLogger: TDnAbstractLogger;
procedure SetLogLevel(Value: TDnLogLevel);
function GetLogLevel: TDnLogLevel;
procedure TimerExpired(Context: TDnThreadContext; Channel: IDnChannel;
ExpiredTacts: Cardinal; Key: Pointer); virtual;
{$IFDEF ROOTISCOMPONENT}
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
{$ENDIF}
public
constructor Create{$IFDEF ROOTISCOMPONENT}(AOwner: TComponent); override{$ENDIF};
destructor Destroy; override;
function RequestTimerNotify(Channel: IDnChannel; Tacts: Cardinal;
Key: Pointer): IDnTimerObserver; virtual;
procedure CancelNotify(Request: IDnTimerObserver); virtual;
published
property Active: Boolean read GetActive write SetActive;
property OnTimerNotify: TDnTimerNotify read FNotify write FNotify;
property Executor: TDnAbstractExecutor read GetExecutor write SetExecutor;
property Logger: TDnAbstractLogger read GetLogger write SetLogger;
property LogLevel: TDnLogLevel read GetLogLevel write SetLogLevel;
end;
implementation
constructor TDnAbstractTimerEngine.Create;
begin
inherited Create;
FActive := False;
FExecutor := Nil;
FLogger := Nil;
FLogLevel := llMandatory;
end;
destructor TDnAbstractTimerEngine.Destroy;
begin
SetActive(False);
inherited Destroy;
end;
procedure TDnAbstractTimerEngine.SetActive(Value: Boolean);
begin
if Value and not FActive then
FActive := TurnOn
else
if not Value and FActive then
FActive := TurnOff;
end;
//-------------------------------------------------------------------
//-------------------------------------------------------------------
constructor TDnAbstractTimer.Create{$IFDEF ROOTISCOMPONENT}(AOwner: TComponent){$ENDIF};
begin
inherited Create{$IFDEF ROOTISCOMPONENT}(AOwner){$ENDIF};
FNotify := Nil;
end;
destructor TDnAbstractTimer.Destroy;
begin
SetActive(False);
FreeAndNil(FTimerEngine);
inherited Destroy;
end;
function TDnAbstractTimer.GetActive: Boolean;
begin
Result := FTimerEngine.Active;
end;
procedure TDnAbstractTimer.SetActive(Value: Boolean);
begin
FTimerEngine.Active := Value;
end;
procedure TDnAbstractTimer.SetExecutor(Value: TDnAbstractExecutor);
begin
FTimerEngine.Executor := Value;
end;
function TDnAbstractTimer.GetExecutor: TDnAbstractExecutor;
begin
Result := FTimerEngine.Executor;
end;
procedure TDnAbstractTimer.SetLogger(Value: TDnAbstractLogger);
begin
FTimerEngine.Logger := Value;
end;
function TDnAbstractTimer.GetLogger: TDnAbstractLogger;
begin
Result := FTimerEngine.Logger;
end;
procedure TDnAbstractTimer.SetLogLevel(Value: TDnLogLevel);
begin
FTimerEngine.LogLevel := Value;
end;
function TDnAbstractTimer.GetLogLevel: TDnLogLevel;
begin
Result := FTimerEngine.LogLevel;
end;
function TDnAbstractTimer.RequestTimerNotify(Channel: IDnChannel; Tacts: Cardinal;
Key: Pointer): IDnTimerObserver;
begin
Result := FTimerEngine.RequestTimerNotify(Channel, Tacts, Key, Self);
end;
procedure TDnAbstractTimer.CancelNotify(Request: IDnTimerObserver);
begin
FTimerEngine.CancelNotify(Request);
end;
procedure TDnAbstractTimer.TimerExpired(Context: TDnThreadContext; Channel: IDnChannel;
ExpiredTacts: Cardinal; Key: Pointer);
begin
if @FNotify <> Nil then
FNotify(Context, Channel, ExpiredTacts, Key);
end;
{$IFDEF ROOTISCOMPONENT}
procedure TDnAbstractTimer.Notification(AComponent: TComponent; Operation: TOperation);
begin
if FTimerEngine <> Nil then
begin
if (AComponent = FTimerEngine.Executor) and (Operation = opRemove) then
FTimerEngine.Executor := Nil
else
if (AComponent = FTimerEngine.Logger) and (Operation = opRemove) then
FTimerEngine.Logger := Nil;
end;
end;
{$ENDIF}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -