我正在构建一个3D应用程序。我的表单上有一个3D布局(TLayout3D),我在运行时创建和定位TSpheres。 Spheres是我创建的自定义TNode类的一部分:
" Add shortcut for clipboard registers
noremap <leader>p "*p
noremap <leader>y "+y
noremap <leader>d "+d
我有一个由TNode类组成的数组
TNode = Class;
Sphere :TSphere;
ID :String;
NodeType :string;
TotalDistance :integer; //used in Dijkstras algorithm
End;
我需要知道如何保存数组,然后从文件中加载它,以便我将来不必再次“手动”创建它。
感谢任何帮助,谢谢。
答案 0 :(得分:2)
虽然我同意David的看法,JSON今天对于持久化业务对象和容器是一个有效的选择,但我想在这里发布使用TCollection和TCollectionItem执行此操作的代码。这是Delphi以来一直使用的旧方法,用于将组件属性流式传输到dfm文件。
但这里有一个警告。我已经测试了这段代码并且它不起作用...不是因为它被破坏了(我多年来使用类似的代码来持久化业务对象),但是因为TSphere不支持TPersistent Assign和AssignTo接口。为此,集合项的属性必须是简单的数据类型和记录,或者是正确的TPersistent实现。并且TSphere都不是这些。
无论如何,这是代码:
<强>接口强>
Type
TNode = Class(TCollectionItem)
Private
FSphere : TSphere;
FID : String;
FNodeType : String;
FTotalDistance: integer;
Procedure SetSphere(Const Value: TSphere);
Public
Constructor Create(Collection: TCollection); Override;
Destructor Destroy; Override;
Procedure Assign(Source: TPersistent); Override;
Published
Property Sphere : TSphere Read FSphere Write SetSphere;
Property ID : String Read FID Write FID;
Property NodeType : String Read FNodeType Write FNodeType;
Property TotalDistance: integer Read FTotalDistance Write FTotalDistance; // used in Dijkstras algorithm
End;
Type
TNodes = Class(TCollection)
Private
Function GetItem(Index: integer): TNode;
Procedure SetItem(Index: integer; Value: TNode);
Public
Constructor Create; Reintroduce;
Function Add: TNode;
Procedure LoadFromFile(Const Filename: String);
Procedure LoadFromStream(S: TStream);
Procedure SaveToFile(Const Filename: String);
Procedure SaveToStream(S: TStream);
Property Items[Index: integer]: TNode Read GetItem Write SetItem; Default;
End;
Type
TNodesWrapper = Class(TComponent)
Private
FCollection: TNodes;
Public
Constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
Published
Property Collection: TNodes Read FCollection Write FCollection;
End;
和实施
{ TNode }
Procedure TNode.Assign(Source: TPersistent);
Begin
If Source Is TNode Then Begin
If Assigned(Collection) Then
Collection.BeginUpdate;
Try
Sphere := TNode(Source).Sphere;
ID := TNode(Source).ID;
NodeType := TNode(Source).NodeType;
TotalDistance := TNode(Source).TotalDistance;
Finally
If Assigned(Collection) Then
Collection.EndUpdate;
End;
End
Else
Inherited;
End;
Constructor TNode.Create(Collection: TCollection);
Begin
Inherited;
FSphere := TSphere.Create(Nil);
// Set default values here
End;
Destructor TNode.Destroy;
Begin
FreeAndNil(FSphere);
Inherited;
End;
Procedure TNode.SetSphere(Const Value: TSphere);
Begin
FSphere.Assign(Value);
End;
{ TNodes }
Function TNodes.Add: TNode;
Begin
Result := TNode(Inherited Add);
End;
Constructor TNodes.Create;
Begin
Inherited Create(TNode);
End;
Function TNodes.GetItem(Index: integer): TNode;
Begin
Result := TNode(Inherited GetItem(Index));
End;
Procedure TNodes.LoadFromFile(Const Filename: String);
Var
S: TFileStream;
Begin
S := TFileStream.Create(Filename, fmOpenRead);
Try
LoadFromStream(S);
Finally
S.Free;
End;
End;
Procedure TNodes.LoadFromStream(S: TStream);
Var
Wrapper: TNodesWrapper;
SBin : TMemoryStream;
Begin
SBin := TMemoryStream.Create;
Wrapper := TNodesWrapper.Create(Nil);
Try
ObjectTextToBinary(S, SBin);
SBin.Position := 0;
SBin.ReadComponent(Wrapper);
Assign(Wrapper.Collection);
Finally
Wrapper.Free;
SBin.Free;
End;
End;
Procedure TNodes.SaveToFile(Const Filename: String);
Var
S: TStream;
Begin
S := TFileStream.Create(Filename, fmCreate);
Try
SaveToStream(S);
Finally
S.Free;
End;
End;
Procedure TNodes.SaveToStream(S: TStream);
Var
Wrapper: TNodesWrapper;
SBin : TMemoryStream;
Begin
SBin := TMemoryStream.Create;
Wrapper := TNodesWrapper.Create(Nil);
Try
Wrapper.Collection.Assign(Self);
SBin.WriteComponent(Wrapper);
SBin.Position := 0;
ObjectBinaryToText(SBin, S);
Finally
Wrapper.Free;
SBin.Free;
End;
End;
Procedure TNodes.SetItem(Index: integer; Value: TNode);
Begin
Inherited SetItem(Index, Value);
End;
{ TNodesWrapper }
Constructor TNodesWrapper.Create(AOwner: TComponent);
Begin
Inherited;
FCollection := TNodes.Create;
End;
Destructor TNodesWrapper.Destroy;
Begin
FreeAndNil(FCollection);
Inherited;
End;
TCollections仍在使用,尽管这些代码中的某些代码对于习惯使用泛型容器的人来说可能看起来很奇怪......大多数代码都与类型转换有关,并将一个TCollectionItem类链接到其特定的TCollection类。
神奇发生在Stream ReadComponent和WriteComponent方法中。不幸的是,由于TCollection不是TComponent,它必须包装在TComponent中......这就是TNodesWrapper的用途。
它可能看起来很奇怪和复杂,但大部分内容都可以在通用的TCollection后代中进行抽象,后者增加了加载和保存到文件/流/字符串的功能。因此,大部分代码都可以隐藏在引擎盖下。
并且...我重复... 只有当您想要保留的属性本身可以持久化时才有效 ......而TSphere则不然。所以我现在想知道FireMonkey 3D表格是如何形成的,因为我还没有真正开发3D应用程序。
答案 1 :(得分:1)
根本不要使用数组和静态大小/绑定的东西!应用一个责任规则。为您的实体创建序列化程序。在这种情况下,您可以改变主意并为任何格式创建任意数量的序列化程序。您的代码变得更加灵活,可变且可测试。
O(log(log(n)))
下一个过程的输出文件格式取决于作为参数传递的TSerializerFactory(sf _):
TSphere = class
// Entity. Just fields with getter/setter methods
end;
TContext = TDictionary<string><TObject>;
CONST_ctxkey_Factory = 'factory';
IStream = interface (IInvokable )
procedure load( var data_; size_ : cardinal );
procedure store( var data_; size_ : cardinal );
end;
ISerializer = interface ( TInvokable )
procedure load( ctx_ : TContext );
procedure store( ctx_ : TContext );
end;
TSerializer = class ( TInterfaceObject, ISerializer )
protected
// Attributes
fObject : TObject;
fStream : IStream;
public
constructor create( object_ : TObject; stream_ : IStream );
// Realized methods (ISerializer)
procedure load( ctx_ : TContext ); virtual; abstract;
procedure store( ctx_ : TContext ); virtual; abstract;
end;
TSphereSerializer_XML = class ( TSerializer )
public
// Overriden methods
procedure load( ctx_ : TContext ); override;
procedure store( ctx_ : TContext ); override;
end;
TSpheresMainSerializer_BIN = class ( TSerializer )
public
// Overriden methods
procedure load( ctx_ : TContext ); override;
procedure store( ctx_ : TContext ); override;
end;
TSpheresMainSerializer_BIN.store( ctx_ : TContext );
var
spheres : TSphereList;
sf : TSerializerContext;
sph : TSphere;
iSe : ISerializer;
begin
spheres := TSphereList( fObject );
sf := ctx_.items[CONST_ctxkey_Factory];
fStream.write( version_number, sizeOf( cardinal ) );
fStream.write( spheres.count, sizeOf( cardinal ) );
for sph in shperes_ do
begin
iSe := sf.createShpereSerializer( sph );
iSe.store( ctx );
end;
end;
TSphereSerializer_BIN = class ( TSerializer )
public
// Overriden methods
procedure load( ctx_ : TContext ); override;
procedure store( ctx_ : TContext ); override;
end;
TSphereList = TList<TSphere>;
TSerializerFactory = class
public
// It creates serializers for XML format
function createContext : TContext; virtual;
function createSpheresMainSerializer( spheres_ : TSphereList; stream_ : IStream ) : ISerializer; virtual; abstract;
function createSphereSerializer( sphere_ : TSphere; stream_ : IStream ) : ISerializer; virtual; abstract;
end;
TSerializerFactory_BIN = class ( TSerializerFactory )
public
// It creates serializers for binary format
function createSpheresMainSerializer( spheres_ : TSphereList; stream_ : IStream ) : ISerializer; override;
function createSphereSerializer( sphere_ : TSphere; stream_ : IStream ) : ISerializer; override;
end;
function TSerializerFactory_BIN.createSpheresMainSerializer( spheres_ : TSphereList; stream_ : IStream ) : ISerializer;
begin
result := TSpheresMainSerializer_BIN.create( TObject( spheres_ ), stream_ );
end;
function TSerializerFactory_BIN.createSphereSerializer( sphere_ : TSphere; stream_ : IStream ) : ISerializer;
begin
result := TSphereSerializer_BIN.create( sphere_, stream_ );
end;
我知道它太厚了,乍一看还是太长了......但也许有用! :)