IdTask.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.2 2003.11.04 3:49:00 PM czhower
  18. Update to sync TC
  19. Rev 1.1 2003.10.21 12:19:02 AM czhower
  20. TIdTask support and fiber bug fixes.
  21. }
  22. unit IdTask;
  23. interface
  24. {$i IdCompilerDefines.inc}
  25. uses
  26. {$IFDEF USE_OBJECT_ARC}
  27. IdGlobal,
  28. {$ENDIF}
  29. IdYarn,
  30. SysUtils;
  31. type
  32. TIdTask = class(TObject)
  33. protected
  34. FBeforeRunDone: Boolean;
  35. {$IFDEF USE_OBJECT_ARC}
  36. // When ARC is enabled, object references MUST be valid objects.
  37. // It is common for users to store non-object values, though, so
  38. // we will provide separate properties for those purposes
  39. //
  40. // TODO; use TValue instead of separating them
  41. //
  42. FDataObject: TObject;
  43. FDataValue: PtrInt;
  44. {$ELSE}
  45. FData: TObject;
  46. {$ENDIF}
  47. FYarn: TIdYarn;
  48. //
  49. procedure AfterRun; virtual;
  50. procedure BeforeRun; virtual;
  51. function Run: Boolean; virtual; abstract;
  52. procedure HandleException(AException: Exception); virtual;
  53. public
  54. constructor Create(
  55. AYarn: TIdYarn
  56. ); reintroduce; virtual;
  57. destructor Destroy; override;
  58. // The Do's are separate so we can add events later if necessary without
  59. // needing the inherited calls to perform them, as well as allowing
  60. // us to keep the real runs as protected
  61. procedure DoAfterRun;
  62. procedure DoBeforeRun;
  63. function DoRun: Boolean;
  64. procedure DoException(AException: Exception);
  65. // BeforeRunDone property to allow flexibility in alternative schedulers
  66. property BeforeRunDone: Boolean read FBeforeRunDone;
  67. //
  68. {$IFDEF USE_OBJECT_ARC}
  69. property DataObject: TObject read FDataObject write FDataObject;
  70. property DataValue: PtrInt read FDataValue write FDataValue;
  71. {$ELSE}
  72. property Data: TObject read FData write FData;
  73. {$ENDIF}
  74. property Yarn: TIdYarn read FYarn;
  75. end;
  76. implementation
  77. {$IFNDEF USE_OBJECT_ARC}
  78. uses
  79. IdGlobal;
  80. {$ENDIF}
  81. { TIdTask }
  82. procedure TIdTask.AfterRun;
  83. begin
  84. end;
  85. procedure TIdTask.BeforeRun;
  86. begin
  87. end;
  88. procedure TIdTask.HandleException(AException: Exception);
  89. begin
  90. end;
  91. constructor TIdTask.Create(AYarn: TIdYarn);
  92. begin
  93. inherited Create;
  94. FYarn := AYarn;
  95. FBeforeRunDone := False;
  96. end;
  97. destructor TIdTask.Destroy;
  98. begin
  99. // Dont free the yarn, that is the responsibilty of the thread / fiber.
  100. // .Yarn here is just a reference, not an ownership
  101. FreeAndNil({$IFDEF USE_OBJECT_ARC}FDataObject{$ELSE}FData{$ENDIF});
  102. {$IFDEF USE_OBJECT_ARC}
  103. FDataValue := 0;
  104. {$ENDIF}
  105. inherited Destroy;
  106. end;
  107. procedure TIdTask.DoAfterRun;
  108. begin
  109. AfterRun;
  110. end;
  111. procedure TIdTask.DoBeforeRun;
  112. begin
  113. FBeforeRunDone := True;
  114. BeforeRun;
  115. end;
  116. function TIdTask.DoRun: Boolean;
  117. begin
  118. Result := Run;
  119. end;
  120. procedure TIdTask.DoException(AException: Exception);
  121. begin
  122. HandleException(AException);
  123. end;
  124. end.