uSingnalMgr.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:5k
源码类别:

Email服务器

开发平台:

Delphi

  1. unit uSingnalMgr;
  2. interface
  3. uses classes,strutils,sysutils;
  4. type
  5.   PSingnalData=^TSingnalData;
  6.   TSingnalData=record
  7.     Caption:ShortString;
  8.     Text:array[0..3000]of AnsiChar;
  9.     Default:Boolean;
  10.   end;
  11.   IIterator=interface
  12.     function HasNext:Boolean; // 是否有下一个元素(遍历完了?)
  13.     function Next:Pointer;  //下一个元素
  14.     procedure Remove;   //删除当前元素
  15.     procedure First;    //定位到第一个元素
  16.   end;
  17.   TIterator=class(TInterfacedObject,IIterator) //可以被任何TList及其子类使用
  18.   private
  19.     FCurIndex:Integer;
  20.     FList:TList;
  21.   public
  22.     constructor Create(Container:TList);
  23.     function HasNext:Boolean;
  24.     function Next:Pointer;
  25.     procedure Remove;
  26.     procedure First;
  27.   end;
  28.   TSingnalList=class(TList)
  29.   protected
  30.     procedure Notify(Ptr: Pointer; Action: TListNotification); Override;
  31.   public
  32.     function NewSingnal:PSingnalData;  //使用此方法分配的内存不用自己释放,该类会自动管理。
  33.   end;
  34.   TSingnalMgr=class(TObject)
  35.   private
  36.     List:TSingnalList;
  37.     FIterator:IIterator;
  38.   protected
  39.     procedure SaveToFile; virtual;abstract;
  40.     procedure LoadFromFile;virtual;abstract;
  41.   public
  42.     constructor Create;
  43.     destructor Destroy; override;
  44.     Function Add:PSingnalData;
  45.     procedure Remove(Index:Integer);
  46.     function Get(Index:Integer):PSingnalData;
  47.     property Iterator:IIterator read FIterator;
  48.   end;
  49.   TAppSingnalMgr=class(TSingnalMgr)
  50.   public
  51.     procedure SaveToFile; override;
  52.     procedure LoadFromFile;  override;
  53.   end;
  54. implementation
  55. uses uCommon, NativeXml, NativeXmlObjectStorage, uMyXml;
  56. { TSingnalList }
  57. function TSingnalList.NewSingnal: PSingnalData;
  58. begin
  59.   GetMem(Result,Sizeof(TSingnalData));
  60.   FillChar(Result^,Sizeof(TSingnalData),0);
  61. end;
  62. procedure TSingnalList.Notify(Ptr: Pointer; Action: TListNotification);
  63. begin
  64.   case Action of
  65.     lnAdded:;
  66.     lnExtracted:;
  67.     lnDeleted:
  68.       if Ptr<>nil then
  69.       begin
  70.         FreeMem(PSingnalData(Ptr));
  71.         ptr:=nil;
  72.       end;
  73.   end;
  74. end;
  75. { TSingnalMgr }
  76. function TSingnalMgr.Add: PSingnalData;
  77. begin
  78.   Result:=List.NewSingnal;
  79.   List.Add(Result);
  80. end;
  81. constructor TSingnalMgr.Create;
  82. begin
  83.   List:=TSingnalList.Create;
  84.   FIterator:=TIterator.Create(Self.List);
  85. end;
  86. destructor TSingnalMgr.Destroy;
  87. begin
  88.   IIterator(FIterator):=nil;
  89.   List.Free;
  90.   inherited;
  91. end;
  92. function TSingnalMgr.Get(Index: Integer): PSingnalData;
  93. begin
  94.   Result:=PSingnalData(List[index]);
  95. end;
  96. procedure TSingnalMgr.Remove(Index: Integer);
  97. begin
  98.   List.Delete(Index);
  99. end;
  100. { TAppSingnalMgr }
  101. procedure TAppSingnalMgr.LoadFromFile;
  102. var
  103.   xml:TAppXml;
  104.   L:TList;
  105.   I:integer;
  106.   PData:PSingnalData;
  107.   Str:String;
  108. begin
  109.   xml:=TAppXml.Create;
  110.   try
  111.     if xml.Root.FindNode('Singnals')=nil then exit;
  112.     L:=TList.Create;
  113.     try
  114.       xml.Root.FindNode('Singnals').NodesByName('Singnal',L);
  115.       for I:=0 to L.Count-1 do
  116.       begin
  117.         PData:=List.NewSingnal;
  118.         PData.Caption:=TXmlNode(L[I]).AttributeByName['Caption']; //
  119.         PData.Default:=StrToBoolDef(TXmlNode(L[I]).AttributeByName['Default'],False);
  120.         Str:=TXmlNode(L[I]).ReadString('Text','');
  121.         if length(Str)>0 then
  122.           System.Move(Str[1],PData.Text,length(Str));
  123.         List.Add(PData);
  124.       end;
  125.     finally
  126.       L.free;
  127.     end;
  128.   finally
  129.     xml.Free;
  130.   end;
  131. end;
  132. procedure TAppSingnalMgr.SaveToFile;
  133. var
  134.   xml:TAppXml;
  135.   N:TXmlNode;
  136.   I:integer;
  137.   PData:PSingnalData;
  138. begin
  139.   xml:=TAppXml.Create;
  140.   try
  141.     if xml.Root.FindNode('Singnals')<>nil then
  142.       xml.Root.FindNode('Singnals').Delete;
  143.     N:=xml.Root.NodeNew('Singnals');
  144.     for I:=0 to List.Count-1 do
  145.     begin
  146.       PData:=PSingnalData(List[I]);
  147.       with N.NodeNew('Singnal')do
  148.       begin
  149.         AttributeByName['Caption']:=PData.Caption;
  150.         WriteString('Text',PData.Text);
  151.         AttributeByName['Default']:=BoolToStr(PData.Default);
  152.       end;
  153.     end;
  154.   finally
  155.     xml.Free;
  156.   end;
  157. end;
  158. { TSingnalMgrIterator }
  159. constructor TIterator.Create(Container:TList);
  160. begin
  161.   if Container=nil then Raise Exception.Create(' Value of Container DON''T allow  nil ');
  162.   FList:=Container;
  163.   FCurIndex:=0;
  164. end;
  165. procedure TIterator.First;
  166. begin
  167.   FCurIndex:=0;
  168. end;
  169. function TIterator.HasNext: Boolean;
  170. begin
  171.   Result:=FCurIndex<FList.Count;
  172. end;
  173. function TIterator.Next: Pointer;
  174. begin
  175.   if HasNext then
  176.   begin
  177.     Result:=FList[FCurIndex];
  178.     Inc(FCurIndex);
  179.   end
  180.   else  Raise Exception.Create('Next Element DO NOT Exsit.Index Out Of Bounds!');
  181. end;
  182. procedure TIterator.Remove;
  183. begin
  184.   if FCurIndex>=FList.Count then Raise Exception.Create('Index Out Of Bounds!');
  185.   FList.Remove(FList[FCurIndex]);
  186. end; 
  187. end.