uSingnalMgr.~pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:5k
- unit uSingnalMgr;
- interface
- uses classes,strutils,sysutils;
- type
- PSingnalData=^TSingnalData;
- TSingnalData=record
- Caption:ShortString;
- Text:array[0..3000]of AnsiChar;
- Default:Boolean;
- end;
- IIterator=interface
- function HasNext:Boolean; // 是否有下一个元素(遍历完了?)
- function Next:Pointer; //下一个元素
- procedure Remove; //删除当前元素
- procedure First; //定位到第一个元素
- end;
- TIterator=class(TInterfacedObject,IIterator) //可以被任何TList及其子类使用
- private
- FCurIndex:Integer;
- FList:TList;
- public
- constructor Create(Container:TList);
- function HasNext:Boolean;
- function Next:Pointer;
- procedure Remove;
- procedure First;
- end;
- TSingnalList=class(TList)
- protected
- procedure Notify(Ptr: Pointer; Action: TListNotification); Override;
- public
- function NewSingnal:PSingnalData; //使用此方法分配的内存不用自己释放,该类会自动管理。
- end;
- TSingnalMgr=class(TObject)
- private
- List:TSingnalList;
- FIterator:IIterator;
- protected
- procedure SaveToFile; virtual;abstract;
- procedure LoadFromFile;virtual;abstract;
- public
- constructor Create;
- destructor Destroy; override;
- Function Add:PSingnalData;
- procedure Remove(Index:Integer);
- function Get(Index:Integer):PSingnalData;
- property Iterator:IIterator read FIterator;
- end;
- TAppSingnalMgr=class(TSingnalMgr)
- public
- procedure SaveToFile; override;
- procedure LoadFromFile; override;
- end;
- implementation
- uses uCommon, NativeXml, NativeXmlObjectStorage, uMyXml;
- { TSingnalList }
- function TSingnalList.NewSingnal: PSingnalData;
- begin
- GetMem(Result,Sizeof(TSingnalData));
- FillChar(Result^,Sizeof(TSingnalData),0);
- end;
- procedure TSingnalList.Notify(Ptr: Pointer; Action: TListNotification);
- begin
- case Action of
- lnAdded:;
- lnExtracted:;
- lnDeleted:
- if Ptr<>nil then
- begin
- FreeMem(PSingnalData(Ptr));
- ${warning off}
- ptr:=nil;
- ${warning on}
- end;
- end;
- end;
- { TSingnalMgr }
- function TSingnalMgr.Add: PSingnalData;
- begin
- Result:=List.NewSingnal;
- List.Add(Result);
- end;
- constructor TSingnalMgr.Create;
- begin
- List:=TSingnalList.Create;
- FIterator:=TIterator.Create(Self.List);
- end;
- destructor TSingnalMgr.Destroy;
- begin
- IIterator(FIterator):=nil;
- List.Free;
- inherited;
- end;
- function TSingnalMgr.Get(Index: Integer): PSingnalData;
- begin
- Result:=PSingnalData(List[index]);
- end;
- procedure TSingnalMgr.Remove(Index: Integer);
- begin
- List.Delete(Index);
- end;
- { TAppSingnalMgr }
- procedure TAppSingnalMgr.LoadFromFile;
- var
- xml:TAppXml;
- L:TList;
- I:integer;
- PData:PSingnalData;
- Str:String;
- begin
- xml:=TAppXml.Create;
- try
- if xml.Root.FindNode('Singnals')=nil then exit;
- L:=TList.Create;
- try
- xml.Root.FindNode('Singnals').NodesByName('Singnal',L);
- for I:=0 to L.Count-1 do
- begin
- PData:=List.NewSingnal;
- PData.Caption:=TXmlNode(L[I]).AttributeByName['Caption']; //
- PData.Default:=StrToBoolDef(TXmlNode(L[I]).AttributeByName['Default'],False);
- Str:=TXmlNode(L[I]).ReadString('Text','');
- if length(Str)>0 then
- System.Move(Str[1],PData.Text,length(Str));
- List.Add(PData);
- end;
- finally
- L.free;
- end;
- finally
- xml.Free;
- end;
- end;
- procedure TAppSingnalMgr.SaveToFile;
- var
- xml:TAppXml;
- N:TXmlNode;
- I:integer;
- PData:PSingnalData;
- begin
- xml:=TAppXml.Create;
- try
- if xml.Root.FindNode('Singnals')<>nil then
- xml.Root.FindNode('Singnals').Delete;
- N:=xml.Root.NodeNew('Singnals');
- for I:=0 to List.Count-1 do
- begin
- PData:=PSingnalData(List[I]);
- with N.NodeNew('Singnal')do
- begin
- AttributeByName['Caption']:=PData.Caption;
- WriteString('Text',PData.Text);
- AttributeByName['Default']:=BoolToStr(PData.Default);
- end;
- end;
- finally
- xml.Free;
- end;
- end;
- { TSingnalMgrIterator }
- constructor TIterator.Create(Container:TList);
- begin
- if Container=nil then Raise Exception.Create(' Value of Container DON''T allow nil ');
- FList:=Container;
- FCurIndex:=0;
- end;
- procedure TIterator.First;
- begin
- FCurIndex:=0;
- end;
- function TIterator.HasNext: Boolean;
- begin
- Result:=FCurIndex<FList.Count;
- end;
- function TIterator.Next: Pointer;
- begin
- if HasNext then
- begin
- Result:=FList[FCurIndex];
- Inc(FCurIndex);
- end
- else Raise Exception.Create('Next Element DO NOT Exsit.Index Out Of Bounds!');
- end;
- procedure TIterator.Remove;
- begin
- if FCurIndex>=FList.Count then Raise Exception.Create('Index Out Of Bounds!');
- FList.Remove(FList[FCurIndex]);
- end;
- end.