{ Minimized for Castle Game Engine to support TPropInfoList, to be able to use it in Delphi. } { This file is part of the Free Pascal run time library. Copyright (c) 2004 by the Free Pascal development team Some RTTI utils, based on RX rtti utils. See the file COPYING.FPC, included in this distribution, for details about the copyright. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. **********************************************************************} { ********************************************************************** Based on the rttiutils unit that comes with RXLib. Adapted to work with FCL, free of VCL dependencies. Fixed some errors along the way as well. MVC. To make it work across the 'Root Component' (Form/Datamodule etc), you MUST set the FindGlobalComponentCallBack event handler. Original copyright: Delphi VCL Extensions (RX) Copyright (c) 1995, 1996 AO ROSNO Copyright (c) 1997 Master-Bank **********************************************************************} {$I castleconf.inc} {$ifdef FPC} {$mode objfpc} {$H+} {$endif} unit RttiUtils; interface uses SysUtils, Classes, {Graphics, Controls, Forms,} TypInfo, StrUtils; type { TPropInfoList } TPropInfoList = class(TObject) private FList: PPropList; FCount: Integer; FSize: Integer; function Get(Index: Integer): PPropInfo; public constructor Create(AObject: TObject; Filter: TTypeKinds); destructor Destroy; override; function Contains(P: PPropInfo): Boolean; function Find(const AName: string): PPropInfo; procedure Delete(Index: Integer); procedure Intersect(List: TPropInfoList); property Count: Integer read FCount; property Items[Index: Integer]: PPropInfo read Get; default; end; implementation const sCount = 'Count'; sItem = 'Item%d'; sNull = '(null)'; type TCardinalSet = set of 0..SizeOf(Cardinal) * 8 - 1; function GetPropType(PropInfo: PPropInfo): PTypeInfo; begin Result := PropInfo^.PropType {$ifndef FPC}^{$endif}; end; { TPropInfoList } constructor TPropInfoList.Create(AObject: TObject; Filter: TTypeKinds); begin if AObject <> nil then begin FCount := GetPropList(AObject.ClassInfo, Filter, nil); FSize := FCount * SizeOf(Pointer); GetMem(FList, FSize); GetPropList(AObject.ClassInfo, Filter, FList); end else begin FCount := 0; FList := nil; end; end; destructor TPropInfoList.Destroy; begin if FList <> nil then FreeMem(FList, FSize); end; function TPropInfoList.Contains(P: PPropInfo): Boolean; var I: Integer; begin for I := 0 to FCount - 1 do with FList^[I]^ do if (PropType = P^.PropType) and (CompareText(Name, P^.Name) = 0) then begin Result := True; Exit; end; Result := False; end; function TPropInfoList.Find(const AName: string): PPropInfo; var I: Integer; begin for I := 0 to FCount - 1 do with FList^[I]^ do if (CompareText(Name, AName) = 0) then begin Result := FList^[I]; Exit; end; Result := nil; end; procedure TPropInfoList.Delete(Index: Integer); begin Dec(FCount); if Index < FCount then Move(FList^[Index + 1], FList^[Index], (FCount - Index) * SizeOf(Pointer)); end; function TPropInfoList.Get(Index: Integer): PPropInfo; begin Result := FList^[Index]; end; procedure TPropInfoList.Intersect(List: TPropInfoList); var I: Integer; begin for I := FCount - 1 downto 0 do if not List.Contains(FList^[I]) then Delete(I); end; { Utility routines } function CreateStoredItem(const CompName, PropName: string): string; begin Result := ''; if (CompName <> '') and (PropName <> '') then Result := CompName + '.' + PropName; end; function ParseStoredItem(const Item: string; var CompName, PropName: string): Boolean; var I: Integer; begin Result := False; if Length(Item) = 0 then Exit; I := Pos('.', Item); if I > 0 then begin CompName := Trim(Copy(Item, 1, I - 1)); PropName := Trim(Copy(Item, I + 1, MaxInt)); Result := (Length(CompName) > 0) and (Length(PropName) > 0); end; end; function ReplaceComponentName(const Item, CompName: string): string; var ACompName, APropName: string; begin Result := ''; if ParseStoredItem(Item, ACompName, APropName) then Result := CreateStoredItem(CompName, APropName); end; procedure UpdateStoredList(AComponent: TComponent; AStoredList: TStrings; FromForm: Boolean); var I: Integer; Component: TComponent; CompName, PropName: string; begin if (AStoredList = nil) or (AComponent = nil) then Exit; for I := AStoredList.Count - 1 downto 0 do begin if ParseStoredItem(AStoredList[I], CompName, PropName) then begin if FromForm then begin Component := AComponent.FindComponent(CompName); if Component = nil then AStoredList.Delete(I) else AStoredList.Objects[I]:=Component; end else begin Component := TComponent(AStoredList.Objects[I]); if Component <> nil then AStoredList[I] := ReplaceComponentName(AStoredList[I], Component.Name) else AStoredList.Delete(I); end; end else AStoredList.Delete(I); end; end; end.