Skip to content

Commit

Permalink
Minor refactoring #20
Browse files Browse the repository at this point in the history
  • Loading branch information
bero committed Dec 6, 2024
1 parent 31ef375 commit 4592450
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
90 changes: 59 additions & 31 deletions Source/BoldHandles.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface
BoldSystem,
BoldSystemRT,
BoldHandle,
BoldContainers,
BoldComponentValidator; // maybe move IBoldValidateableComponent here instead ?

type
Expand Down Expand Up @@ -46,6 +47,8 @@ TBoldElementHandle = class(TBoldSubscribableComponent, IBoldValidateableCompon
function GetCanSetValue: boolean; virtual;
procedure SetValue(NewValue: TBoldElement); virtual;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
destructor Destroy; override;
function RefersToComponent(Component: TBoldSubscribableComponent): Boolean; virtual;
property StaticSystemTypeInfo: TBoldSystemTypeInfo read GetStaticSystemTypeInfo;
Expand Down Expand Up @@ -129,17 +132,18 @@ TBoldAbstractSystemHandle = class(TBoldElementHandle)
TBoldNonSystemHandle = class(TBoldElementHandle)
private
fStaticSystemHandle: TBoldAbstractSystemHandle;
fStaticSystemHandleSubscriber: TBoldPassthroughSubscriber;
fSubscriber: TBoldPassthroughSubscriber;
procedure _Receive(Originator: TObject; OriginalEvent: TBoldEvent; RequestedEvent: TBoldRequestedEvent);
function GetSubscriber: TBoldPassthroughSubscriber;
protected
function GetBoldSystem: TBoldSystem; override;
function GetStaticSystemHandle: TBoldAbstractSystemHandle; virtual;
procedure SetStaticSystemHandle(Value: TBoldAbstractSystemHandle); virtual;
function GetStaticSystemTypeInfo: TBoldSystemTypeInfo; override;
procedure DoAssign(Source: TPersistent); virtual;
function IsStaticSystemHandleStored: boolean; virtual;
property Subscriber: TBoldPassthroughSubscriber read GetSubscriber;
public
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
Expand All @@ -149,22 +153,26 @@ TBoldNonSystemHandle = class(TBoldElementHandle)
TBoldSystemExtensionComponent = class(TBoldHandle)
private
fStaticSystemHandle: TBoldAbstractSystemHandle;
fStaticSystemHandleSubscriber: TBoldPassthroughSubscriber;
fSubscriber: TBoldPassthroughSubscriber;
function GetBoldSystem: TBoldSystem;
function GetSubscriber: TBoldPassthroughSubscriber;
protected
procedure _Receive(Originator: TObject; OriginalEvent: TBoldEvent; RequestedEvent: TBoldRequestedEvent); virtual;
function GetHandledObject: TObject; override;
function GetStaticSystemHandle: TBoldAbstractSystemHandle; virtual;
procedure SetStaticSystemHandle(Value: TBoldAbstractSystemHandle); virtual;
procedure StaticBoldTypeChanged; virtual;
procedure PlaceSubscriptions; virtual;
property BoldSystem: TBoldSystem read GetBoldSystem;
property Subscriber: TBoldPassthroughSubscriber read GetSubscriber;
public
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
published
property StaticSystemHandle: TBoldAbstractSystemHandle read GetStaticSystemHandle write SetStaticSystemHandle;
end;

var
BoldElementHandleList: TBoldObjectArray;

implementation

Expand All @@ -174,8 +182,7 @@ implementation

BoldCoreConsts,
BoldDefs,
BoldregionDefinitionParser,
BoldContainers;
BoldRegionDefinitionParser;

const
breModelDestroyed = 42;
Expand All @@ -189,6 +196,20 @@ implementation

{---TBoldElementHandle---}

procedure TBoldElementHandle.AfterConstruction;
begin
inherited;
if assigned(BoldHandleList) then
BoldHandleList.Add(self);
end;

procedure TBoldElementHandle.BeforeDestruction;
begin
inherited;
if assigned(BoldHandleList) then
BoldHandleList.Remove(self);
end;

destructor TBoldElementHandle.Destroy;
begin
FreePublisher;
Expand Down Expand Up @@ -272,12 +293,12 @@ procedure TBoldNonSystemHandle.SetStaticSystemHandle(Value: TBoldAbstractSystemH
begin
if (fStaticSystemHandle <> Value) then
begin
fStaticSystemHandleSubscriber.CancelAllSubscriptions;
FreeAndNil(fSubscriber);
fStaticSystemHandle := Value;
if Assigned(fStaticSystemHandle) then
begin
fStaticSystemHandle.AddSmallSubscription(fStaticSystemHandleSubscriber, [beDestroying], breFreeHandle);
fStaticSystemHandle.AddSmallSubscription(fStaticSystemHandleSubscriber, [beValueIDentityChanged], breValueIdentityChanged);
fStaticSystemHandle.AddSmallSubscription(Subscriber, [beDestroying], breFreeHandle);
fStaticSystemHandle.AddSmallSubscription(Subscriber, [beValueIDentityChanged], breValueIdentityChanged);
end;
StaticBoldTypeChanged;
end;
Expand Down Expand Up @@ -307,6 +328,13 @@ function TBoldNonSystemHandle.GetStaticSystemTypeInfo: TBoldSystemTypeInfo;
Result := TBoldAbstractSystemHandle.DefaultBoldSystemTypeInfo
end;

function TBoldNonSystemHandle.GetSubscriber: TBoldPassthroughSubscriber;
begin
if not Assigned(fSubscriber) then
fSubscriber := TBoldPassthroughSubscriber.Create(_Receive);
result := fSubscriber;
end;

function TBoldNonSystemHandle.IsStaticSystemHandleStored: boolean;
begin
result := true;
Expand All @@ -328,16 +356,10 @@ procedure TBoldNonSystemHandle.DoAssign(Source: TPersistent);
end
end;

constructor TBoldNonSystemHandle.Create(Owner: TComponent);
begin
inherited;
fStaticSystemHandleSubscriber := TBoldPassthroughSubscriber.Create(_Receive);
end;

destructor TBoldNonSystemHandle.Destroy;
begin
FreePublisher;
FreeAndNil(fStaticSystemHandleSubscriber);
FreeAndNil(fSubscriber);
inherited;
end;

Expand Down Expand Up @@ -595,15 +617,9 @@ function TBoldSystemTypeInfoHandle.GetRegionDefinitions: TBoldRegionDefinitions;

{ TBoldSystemExtensionComponent }

constructor TBoldSystemExtensionComponent.Create(Owner: TComponent);
begin
inherited;
fStaticSystemHandleSubscriber := TBoldPassthroughSubscriber.Create(_Receive);
end;

destructor TBoldSystemExtensionComponent.Destroy;
begin
FreeAndNil(fStaticSystemHandleSubscriber);
FreeAndNil(fSubscriber);
inherited;
end;

Expand All @@ -625,25 +641,36 @@ function TBoldSystemExtensionComponent.GetStaticSystemHandle: TBoldAbstractSyste
result := fStaticSystemHandle;
end;

function TBoldSystemExtensionComponent.GetSubscriber: TBoldPassthroughSubscriber;
begin
if not Assigned(fSubscriber) then
fSubscriber := TBoldPassthroughSubscriber.Create(_Receive);
result := fSubscriber;
end;

procedure TBoldSystemExtensionComponent.PlaceSubscriptions;
begin
FreeAndNil(fSubscriber);
if Assigned(fStaticSystemHandle) then
begin
fStaticSystemHandle.AddSmallSubscription(Subscriber, [beDestroying], breFreeHandle);
fStaticSystemHandle.AddSmallSubscription(Subscriber, [beValueIDentityChanged], breValueIdentityChanged);
end;
end;

procedure TBoldSystemExtensionComponent.SetStaticSystemHandle(
Value: TBoldAbstractSystemHandle);
begin
if (fStaticSystemHandle <> Value) then
begin
fStaticSystemHandleSubscriber.CancelAllSubscriptions;
fStaticSystemHandle := Value;
if Assigned(fStaticSystemHandle) then
begin
fStaticSystemHandle.AddSmallSubscription(fStaticSystemHandleSubscriber, [beDestroying], breFreeHandle);
fStaticSystemHandle.AddSmallSubscription(fStaticSystemHandleSubscriber, [beValueIDentityChanged], breValueIdentityChanged);
end;
StaticBoldTypeChanged;
end;
end;

procedure TBoldSystemExtensionComponent.StaticBoldTypeChanged;
begin

PlaceSubscriptions;
end;

procedure TBoldSystemExtensionComponent._Receive(Originator: TObject;
Expand All @@ -657,9 +684,10 @@ procedure TBoldSystemExtensionComponent._Receive(Originator: TObject;
end;

initialization
G_BoldSystemHandleList := TBoldObjectArray.Create(10, []);
G_BoldSystemHandleList := TBoldObjectArray.Create(128, []);

finalization
Assert(G_BoldSystemHandleList.Count=0, Format('There are %d unreleased handles in finalization', [G_BoldSystemHandleList.Count]));
FreeAndNil(G_BoldSystemHandleList);

end.
4 changes: 1 addition & 3 deletions Source/BoldRootedHandles.pas
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ function TBoldRootedHandle.GetStaticSystemHandle: TBoldAbstractSystemHandle;
else
if (RootHandle is TBoldAbstractSystemHandle) then
Result := RootHandle as TBoldAbstractSystemHandle
else
if Assigned(RootHandle) then
Result := nil
else
Result := inherited GetStaticSystemHandle;
end;
Expand Down Expand Up @@ -413,6 +410,7 @@ procedure TBoldRootedHandle.SetRootHandle(const Value: TBoldElementHandle);

function TBoldRootedHandle.GetBoldSystem: TBoldSystem;
begin
result := nil;
if Assigned(RootHandle) then
result := RootHandle.BoldSystem;
if not Assigned(result) then
Expand Down

0 comments on commit 4592450

Please sign in to comment.