通过原代码学习TComponent类的机制
2006-02-04 13:39:13 来源:WEB开发网{ TComponent }
constructor TComponent.Create(AOwner: TComponent);
begin
FComponentStyle := [csInheritable];
if AOwner <> nil then AOwner.InsertComponent(Self);
end;
PRocedure TComponent.InsertComponent(AComponent: TComponent);
begin
AComponent.ValidateContainer(Self);
ValidateRename(AComponent, '', AComponent.FName);//看是否有重名的对象如果有则发异常
Insert(AComponent);//在自己的子组件列表里加入AComponent的引用
AComponent.SetReference(True);
if csDesigning in ComponentState then
AComponent.SetDesigning(True);
Notification(AComponent, opInsert);//给自己发一个通知消息
end;
procedure TComponent.ValidateRename(AComponent: TComponent;
const CurName, NewName: string);
begin
if (AComponent <> nil) and not SameText(CurName, NewName) and
(AComponent.Owner = Self) and (FindComponent(NewName) <> nil) then
raise EComponentError.CreateResFmt(@SDuplicateName, [NewName]);
if (csDesigning in ComponentState) and (Owner <> nil) then
Owner.ValidateRename(AComponent, CurName, NewName);//递归调用查是否有重名的组件
end;
procedure TComponent.ValidateContainer(AComponent: TComponent);
begin
AComponent.ValidateInsert(Self);
end;
procedure TComponent.ValidateInsert(AComponent: TComponent);
begin
end;
procedure TComponent.Insert(AComponent: TComponent);//我们可以看到他的子组件的引用是用TLIST来保存的
begin
if FComponents = nil then FComponents := TList.Create;
FComponents.Add(AComponent);
AComponent.FOwner := Self;//给刚刚加上的主件赋上拥有者属性
end;
destructor TComponent.Destroy; file://可以看到每一个组件都维护一个FFREENOTIFILES的列表
begin
Destroying;
if FFreeNotifies <> nil then
begin
while Assigned(FFreeNotifies) and (FFreeNotifies.Count > 0) do
TComponent(FFreeNotifies[FFreeNotifies.Count - 1]).Notification(Self, opRemove);
FreeAndNil(FFreeNotifies);//如果自己析构的时候,对自己FFREENOTIFILES里面引用的对象调用Notification,把自己析构造的事件告诉他们
end;
DestroyComponents;
if FOwner <> nil then FOwner.RemoveComponent(Self);
inherited Destroy;
end;
procedure TComponent.FreeNotification(AComponent: TComponent);//通过这个过程可以证实FFREENOTIFILES是一个列表这个列表维护自己析构的时候给那些对象通知
begin
if (Owner = nil) or (AComponent.Owner <> Owner) then
begin
// Never acquire a reference to a component that is being deleted.
assert(not (csDestroying in (ComponentState + AComponent.ComponentState)));
if not Assigned(FFreeNotifies) then FFreeNotifies := TList.Create;
if FFreeNotifies.IndexOf(AComponent) < 0 then
begin
FFreeNotifies.Add(AComponent);
AComponent.FreeNotification(Self);
end;
end;
Include(FComponentState, csFreeNotification);
end;
更多精彩
赞助商链接