StringQueue.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:3k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit StringQueue;
  2.    (*********************************************************************
  3.     * The contents of this file are used with permission, subject to    *
  4.     * the Mozilla Public License Version 1.1 (the "License"); you may   *
  5.     * not use this file except in compliance with the License. You may  *
  6.     * obtain a copy of the License at                                   *
  7.     * http://www.mozilla.org/MPL/MPL-1.1.html                           *
  8.     *                                                                   *
  9.     * Software distributed under the License is distributed on an       *
  10.     * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or    *
  11.     * implied. See the License for the specific language governing      *
  12.     * rights and limitations under the License.                         *
  13.     *                                                                   *
  14.     * (C) 2004 Martin Offenwanger: coder@dsplayer.de                    *
  15.     *********************************************************************)
  16. {
  17. @author(Martin Offenwanger: coder@dsplayer.de)
  18. @created(Apr 22, 2004)
  19. @lastmod(Sep 09, 2004)
  20. }
  21. interface
  22. uses
  23.   classes, SyncObjs;
  24. type
  25.   TStringQueue = class
  26.   public
  27.     constructor Create;
  28.     destructor Destroy; override;
  29.     procedure Push(AItem: string);
  30.     function Pop: string;
  31.     procedure InsertItem(AItem: string; Pos: integer);
  32.     function GetItem(Count: integer): string;
  33.     function GetAllItems: TStrings;
  34.     function GetCount: integer;
  35.   private
  36.     FItemlist: TStringlist;
  37.     FCriticalSection: TCriticalsection;
  38.   end;
  39. implementation
  40. procedure TStringQueue.InsertItem(AItem: string; Pos: integer);
  41. begin
  42.   FCriticalSection.Enter;
  43.   FItemlist.Insert(Pos, AItem);
  44.   FCriticalsection.Leave;
  45. end;
  46. function TStringQueue.GetCount: integer;
  47. begin
  48.   FCriticalsection.Enter;
  49.   Result := FItemlist.Count;
  50.   FCriticalsection.Leave;
  51. end;
  52. function TStringQueue.GetItem(Count: integer): string;
  53. begin
  54.   FCriticalsection.Enter;
  55.   Result := FItemlist[Count];
  56.   FCriticalsection.Leave;
  57. end;
  58. constructor TStringQueue.Create;
  59. begin
  60.   inherited Create;
  61.   FItemlist := TStringList.Create;
  62.   FCriticalsection := TCriticalSection.Create;
  63. end;
  64. destructor TStringQueue.Destroy;
  65. begin
  66.   inherited Destroy;
  67.   FItemlist.Destroy;
  68.   FCriticalsection.Destroy;
  69. end;
  70. function TStringQueue.Pop: string;
  71. begin
  72.   FCriticalsection.Enter;
  73.   Result := FItemlist[0];
  74.   FItemlist.Delete(0);
  75.   FCriticalsection.Leave;
  76. end;
  77. procedure TStringQueue.Push(AItem: string);
  78. begin
  79.   FCriticalsection.Enter;
  80.   FItemlist.Add(AItem);
  81.   FCriticalsection.Leave;
  82. end;
  83. function TStringQueue.GetAllItems: TStrings;
  84. begin
  85.   FCriticalsection.Enter;
  86.   result := FItemlist;
  87.   FCriticalsection.Leave;
  88. end;
  89. end.