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.         ${warning off}
  72.         ptr:=nil;
  73.         ${warning on}
  74.       end;
  75.   end;
  76. end;
  77. { TSingnalMgr }
  78. function TSingnalMgr.Add: PSingnalData;
  79. begin
  80.   Result:=List.NewSingnal;
  81.   List.Add(Result);
  82. end;
  83. constructor TSingnalMgr.Create;
  84. begin
  85.   List:=TSingnalList.Create;
  86.   FIterator:=TIterator.Create(Self.List);
  87. end;
  88. destructor TSingnalMgr.Destroy;
  89. begin
  90.   IIterator(FIterator):=nil;
  91.   List.Free;
  92.   inherited;
  93. end;
  94. function TSingnalMgr.Get(Index: Integer): PSingnalData;
  95. begin
  96.   Result:=PSingnalData(List[index]);
  97. end;
  98. procedure TSingnalMgr.Remove(Index: Integer);
  99. begin
  100.   List.Delete(Index);
  101. end;
  102. { TAppSingnalMgr }
  103. procedure TAppSingnalMgr.LoadFromFile;
  104. var
  105.   xml:TAppXml;
  106.   L:TList;
  107.   I:integer;
  108.   PData:PSingnalData;
  109.   Str:String;
  110. begin
  111.   xml:=TAppXml.Create;
  112.   try
  113.     if xml.Root.FindNode('Singnals')=nil then exit;
  114.     L:=TList.Create;
  115.     try
  116.       xml.Root.FindNode('Singnals').NodesByName('Singnal',L);
  117.       for I:=0 to L.Count-1 do
  118.       begin
  119.         PData:=List.NewSingnal;
  120.         PData.Caption:=TXmlNode(L[I]).AttributeByName['Caption']; //
  121.         PData.Default:=StrToBoolDef(TXmlNode(L[I]).AttributeByName['Default'],False);
  122.         Str:=TXmlNode(L[I]).ReadString('Text','');
  123.         if length(Str)>0 then
  124.           System.Move(Str[1],PData.Text,length(Str));
  125.         List.Add(PData);
  126.       end;
  127.     finally
  128.       L.free;
  129.     end;
  130.   finally
  131.     xml.Free;
  132.   end;
  133. end;
  134. procedure TAppSingnalMgr.SaveToFile;
  135. var
  136.   xml:TAppXml;
  137.   N:TXmlNode;
  138.   I:integer;
  139.   PData:PSingnalData;
  140. begin
  141.   xml:=TAppXml.Create;
  142.   try
  143.     if xml.Root.FindNode('Singnals')<>nil then
  144.       xml.Root.FindNode('Singnals').Delete;
  145.     N:=xml.Root.NodeNew('Singnals');
  146.     for I:=0 to List.Count-1 do
  147.     begin
  148.       PData:=PSingnalData(List[I]);
  149.       with N.NodeNew('Singnal')do
  150.       begin
  151.         AttributeByName['Caption']:=PData.Caption;
  152.         WriteString('Text',PData.Text);
  153.         AttributeByName['Default']:=BoolToStr(PData.Default);
  154.       end;
  155.     end;
  156.   finally
  157.     xml.Free;
  158.   end;
  159. end;
  160. { TSingnalMgrIterator }
  161. constructor TIterator.Create(Container:TList);
  162. begin
  163.   if Container=nil then Raise Exception.Create(' Value of Container DON''T allow  nil ');
  164.   FList:=Container;
  165.   FCurIndex:=0;
  166. end;
  167. procedure TIterator.First;
  168. begin
  169.   FCurIndex:=0;
  170. end;
  171. function TIterator.HasNext: Boolean;
  172. begin
  173.   Result:=FCurIndex<FList.Count;
  174. end;
  175. function TIterator.Next: Pointer;
  176. begin
  177.   if HasNext then
  178.   begin
  179.     Result:=FList[FCurIndex];
  180.     Inc(FCurIndex);
  181.   end
  182.   else  Raise Exception.Create('Next Element DO NOT Exsit.Index Out Of Bounds!');
  183. end;
  184. procedure TIterator.Remove;
  185. begin
  186.   if FCurIndex>=FList.Count then Raise Exception.Create('Index Out Of Bounds!');
  187.   FList.Remove(FList[FCurIndex]);
  188. end; 
  189. end.