Skip to content

Commit

Permalink
feat: Added TInterfacedThread and tests
Browse files Browse the repository at this point in the history
Also renamed all classes to `TInterfaced*`
  • Loading branch information
gcarreno committed Sep 24, 2023
1 parent ad8e75a commit 867ba84
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 42 deletions.
21 changes: 21 additions & 0 deletions src/dispatcher/polykerma.dispatcher.common.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
unit PolyKerma.Dispatcher.Common;

{$mode objfpc}{$H+}

interface

uses
Classes
;

const
cDispatcherChannelControllerOut = 'controller.out';
cDispatcherChannelControllerIn = 'controller.in';
cDispatcherChannelCommsOut = 'comms.out';
cDispatcherChannelCommsIn = 'comms.in';
cDispatcherChannelModelOut = 'model.out';
cDispatcherChannelModelIn = 'model.in';

implementation

end.
8 changes: 4 additions & 4 deletions src/dispatcher/polykerma.dispatcher.interfaces.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ interface

uses
Classes
//, PolyLKerma.Messages.Interfaces
//, PolyLKerma.Modules.Interfaces
, PolyKerma.Messages.Interfaces
, PolyKerma.Modules.Interfaces
;

type
{ IDispatcher }
IDispatcher = Interface
['{1CD54522-D37B-486E-9181-A97CCF768DE9}']
function Register(const AChannel: String{, const AModule: IModule}): Boolean;
procedure Post(const AChannel: String{, const AMessage: IMessage});
function Register(const AChannel: String; const AModule: IModule): Boolean;
procedure Post(const AChannel: String; const AMessage: IMessage);
end;

implementation
Expand Down
22 changes: 13 additions & 9 deletions src/dispatcher/polykerma.dispatcher.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,46 @@ interface
uses
Classes
, PolyKerma.Dispatcher.Interfaces
//, PolyKerma.Dispatcher.Common
, PolyKerma.Messages.Interfaces
, PolyKerma.Modules.Interfaces
;

type
{ TDispatcher }
TDispatcher = class(TInterfacedObject, IDispatcher)
{ TInterfacedDispatcher }
TInterfacedDispatcher = class(TInterfacedObject, IDispatcher)
private
protected
public
constructor Create;
destructor Destroy; override;

function Register(const AChannel: String{, const AModule: IModule}): Boolean;
procedure Post(const AChannel: String{, const AMessage: IMessage});
function Register(const AChannel: String; const AModule: IModule): Boolean;
procedure Post(const AChannel: String; const AMessage: IMessage);
published
end;

implementation

{ TDispatcher }
{ TInterfacedDispatcher }

constructor TDispatcher.Create;
constructor TInterfacedDispatcher.Create;
begin

end;

destructor TDispatcher.Destroy;
destructor TInterfacedDispatcher.Destroy;
begin
inherited Destroy;
end;

function TDispatcher.Register(const AChannel: String): Boolean;
function TInterfacedDispatcher.Register(const AChannel: String;
const AModule: IModule): Boolean;
begin
Result:= false;
end;

procedure TDispatcher.Post(const AChannel: String);
procedure TInterfacedDispatcher.Post(const AChannel: String; const AMessage: IMessage);
begin

end;
Expand Down
8 changes: 4 additions & 4 deletions src/messages/polykerma.messages.message.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ interface
;

type
{ TMessage }
TMessage = class(TInterfacedObject, IMessage)
{ TInterfacedMessage }
TInterfacedMessage = class(TInterfacedObject, IMessage)
private
protected
public
Expand All @@ -25,12 +25,12 @@ implementation

{ TDispatcher }

constructor TMessage.Create;
constructor TInterfacedMessage.Create;
begin

end;

destructor TMessage.Destroy;
destructor TInterfacedMessage.Destroy;
begin
inherited Destroy;
end;
Expand Down
15 changes: 9 additions & 6 deletions src/modules/polykerma.modules.module.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ interface

uses
Classes
, PolyKerma.Dispatcher.Interfaces
, PolyKerma.Modules.Interfaces
;

type
{ TModule }
TModule = class(TInterfacedObject, IModule)
{ TInterfacedModule }
TInterfacedModule = class(TInterfacedObject, IModule)
private
FDispatcher: IDispatcher;
protected
public
constructor Create;
constructor Create(const ADispatcher: IDispatcher);
destructor Destroy; override;

published
Expand All @@ -25,13 +27,14 @@ implementation

{ TDispatcher }

constructor TModule.Create;
constructor TInterfacedModule.Create(const ADispatcher: IDispatcher);
begin

FDispatcher:= ADispatcher;
end;

destructor TModule.Destroy;
destructor TInterfacedModule.Destroy;
begin
FDispatcher:= nil;
inherited Destroy;
end;

Expand Down
26 changes: 26 additions & 0 deletions src/threads/polykerma.threads.interfaces.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
unit PolyKerma.Threads.Interfaces;

{$mode ObjFPC}{$H+}

interface

uses
Classes
;

type
{ IThread }
IThread = interface
['{5D340B25-31E7-40C3-A5AB-8AFE68AA31C0}']

procedure Start;
procedure Resume;
procedure Terminate;
function WaitFor: Integer;

end;

implementation

end.

75 changes: 75 additions & 0 deletions src/threads/polykerma.threads.thread.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
unit PolyKerma.Threads.Thread;

{$mode ObjFPC}{$H+}

interface

uses
Classes
, PolyKerma.Threads.Interfaces
;

type

{ TInterfacedThread }
TInterfacedThread = class(TThread, IThread)
protected
FRefCount : longint;
FDestroyCount : longint;

function QueryInterface(
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj
) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};

public
constructor Create(const CreateSuspended: Boolean); reintroduce;
destructor Destroy; override;

end;
TInterfacedThreadClass = class of TInterfacedThread;

implementation

{ TInterfacedThread }

function TInterfacedThread.QueryInterface(
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj
) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
if getinterface(iid,obj) then
Result:= S_OK
else
Result:= longint(E_NOINTERFACE);
end;

function TInterfacedThread._AddRef: longint;
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result:= interlockedincrement(FRefCount);
end;

function TInterfacedThread._Release: longint;
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result:=interlockeddecrement(FRefCount);
if Result = 0 then
begin
if interlockedincrement(FDestroyCount)=1 then
Self.destroy;
end;
end;

constructor TInterfacedThread.Create(const CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
end;

destructor TInterfacedThread.Destroy;
begin
inherited Destroy;
end;

end.

2 changes: 1 addition & 1 deletion tests/dispatcher/polykerma.test.dispatcher.pas
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ implementation

procedure TTestDispatcher.TestDispatcherCreate;
begin
FDispatcher:= TDispatcher.Create;
FDispatcher:= TInterfacedDispatcher.Create;
AssertNotNull('Dispatcher not null', FDispatcher);
end;

Expand Down
2 changes: 1 addition & 1 deletion tests/messages/polykerma.test.messages.pas
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ implementation

procedure TTestMessages.TestMessageCreate;
begin
FMessage:= TMessage.Create;
FMessage:= TInterfacedMessage.Create;
AssertNotNull('Message not null', FMessage);
end;

Expand Down
2 changes: 1 addition & 1 deletion tests/modules/polykerma.test.module.pas
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ implementation

procedure TTestModule.TestModuleCreate;
begin
FModule:= TModule.Create;
FModule:= TInterfacedModule.Create(nil);
AssertNotNull('Module not null', FModule);
end;

Expand Down
Loading

0 comments on commit 867ba84

Please sign in to comment.