Delphi-int.ru: портал программистов

Вход Регистрация | Забыли пароль?

Просмотр кода

Идентификатор: c152eaca Описание: EClasses.pas Код загружен: 3 июля 2011, 17:21 (mirt.steelwater)

  1. unit EClasses;
  2. {******************************************************************************}
  3. {* Unit to work with Exceptions in the Classes *}
  4. {* Class declaration, the owner of an exception *}
  5. {* must be declared with the directive $M+ *}
  6. {* Revolutionary Confederation of Anarcho Syndicalists *}
  7. {* Written by: black.rabbit 2010 *}
  8. {******************************************************************************}
  9. interface
  10.  
  11. {$I 'std.inc'}
  12.  
  13. uses
  14. Windows, SysUtils, TypInfo,
  15. VarRecs;
  16.  
  17. type
  18. {$M+}
  19. EClass = class (Exception)
  20. private
  21. f_EGUID: String; { уникальный идентификатор исключения }
  22. protected
  23. procedure SetEGUID (const aValue: String);
  24. public
  25. constructor Create (anArgs: array of const;
  26. const anEGUID: String = ''); overload;
  27. constructor Create (anArgs: array of const;
  28. anEGUID: array of const); overload;
  29. property EGUID: String read f_EGUID write SetEGUID;
  30. end;
  31. {$M-}
  32.  
  33.  
  34. function toExceptionMessage (const aValue: TVarRec) : String;
  35.  
  36. function RaiseErrorInClass (doRaise: Boolean;
  37. anArgs: array of const;
  38. const anEGUID: String = '') : String;
  39.  
  40. implementation
  41.  
  42. function toExceptionMessage (const aValue: TVarRec) : String;
  43. begin
  44. Result := '';
  45. with aValue do
  46. try
  47. case VType of
  48. vtObject: if VObject.InheritsFrom (Exception) then
  49. Result := Exception (VObject).Message
  50. else
  51. Result := VObject.ClassName;
  52. else Result := toString (aValue);
  53. end;
  54. except
  55. Result := 'Unknown Error';
  56. end;
  57. end;
  58.  
  59. function RaiseErrorInClass (doRaise: Boolean;
  60. anArgs: array of const;
  61. const anEGUID: String = '') : String;
  62. var
  63. I : Integer;
  64.  
  65. { функция определения исполняемого модуля приложения }
  66. function GetClassPackageName (aClass: TClass) : String;
  67. var
  68. M : TMemoryBasicInformation;
  69. begin
  70. { определяем хэндл DLL, которая владеет классом }
  71. VirtualQuery ( aClass, M, SizeOf (M) );
  72. SetLength (Result,MAX_PATH+1);
  73. { если это не главная программа }
  74. if ( hModule (M.AllocationBase) <> hInstance ) then
  75. begin
  76. GetModuleFileName ( hModule (M.AllocationBase), PChar (Result), MAX_PATH );
  77. SetLength ( Result, StrLen ( PChar (Result) ) );
  78. Result := ExtractFileName (Result);
  79. end
  80. else
  81. Result := ExtractFileName ( ParamStr (0) );
  82. end;
  83.  
  84. { функция определения внутреннего модуля }
  85. function GetClassUnitName (aClass: TClass) : String;
  86. var
  87. C : Pointer;
  88. begin
  89. Result := 'Unknown';
  90. C := aClass.ClassInfo;
  91. if Assigned (C) then
  92. Result := GetTypeData (C).UnitName;
  93. end;
  94.  
  95. begin
  96. Result := '';
  97. for I := Low (anArgs) to High (anArgs) do
  98. with anArgs [I] do
  99. begin
  100. { первый параметр - класс, в котором возникло исключение }
  101. if ( I = 0 ) then
  102. begin
  103. case VType of
  104. vtClass: Result := Format( '%s::%s::%s',[ GetClassPackageName (VClass),
  105. GetClassUnitName (VClass),
  106. VClass.ClassName ] );
  107. vtObject: if VObject.InheritsFrom (Exception) then
  108. Result := Exception (VObject).Message
  109. else
  110. Result := Format( '%s::%s::%s',[ GetClassPackageName (VObject.ClassType),
  111. GetClassUnitName (VObject.ClassType),
  112. VObject.ClassName ] );
  113. else Result := toExceptionMessage (anArgs [I]);
  114. end;
  115. end
  116. { второй параметр - имя метода класса, в котором возникло исключение }
  117. else if ( I = 1 ) then
  118. begin
  119. case VType of
  120. vtChar: Result := Format ('%s.%s',[Result,VChar]);
  121. vtString: Result := Format ('%s.%s',[Result,VString^]);
  122. vtPChar: Result := Format ( '%s.%s',[ Result, StrPas (VPChar) ] );
  123. vtAnsiString: Result := Format ( '%s.%s',[ Result, String (VAnsiString) ] );
  124. vtWideChar: Result := Format ( '%s.%s',[ Result, Char (VWideChar) ] );
  125. vtPWideChar: Result := Format ( '%s.%s',[ Result, WideCharToString (VPWideChar) ] );
  126. vtWideString: Result := Format ( '%s.%s',[ Result, WideCharToString (VWideString) ] );
  127. vtVariant: Result := Format ('%s.%s',[Result,VVariant^]);
  128. else Result := Format ( '%s : '#13#10'%s',[ Result, toExceptionMessage (anArgs [I]) ] );
  129. end;
  130. end
  131. { остальные параметры - текстовые сообщения или экземпляры класса исключения }
  132. else
  133. Result := Format ( '%s : '#13#10'%s',[ Result, toExceptionMessage (anArgs [I]) ] );
  134. end;
  135. { уникальный идентификатор исключения }
  136. if ( anEGUID <> '' ) then
  137. Result := Format ('%s'#13#10'%s',[anEGUID,Result]);
  138. if doRaise then
  139. raise Exception.Create (Result);
  140. end;
  141.  
  142. procedure EClass.SetEGUID (const aValue: String);
  143. begin
  144. {$IFDEF HEX_UPPER_CASE}
  145. f_EGUID := UpperCase (aValue);
  146. {$ELSE}
  147. f_EGUID := LowerCase (aValue);
  148. {$ENDIF HEX_UPPER_CASE}
  149. end;
  150.  
  151. constructor EClass.Create (anArgs: array of const;
  152. const anEGUID: String = '');
  153. begin
  154. EGUID := anEGUID;
  155. inherited Create ( RaiseErrorInClass (FALSE,anArgs,anEGUID) );
  156. end;
  157.  
  158. constructor EClass.Create (anArgs: array of const;
  159. anEGUID: array of const);
  160. var
  161. I : Integer;
  162. begin
  163. EGUID := '';
  164. for I := Low (anEGUID) to High (anEGUID) do
  165. EGUID := Format ('%s%s',[ EGUID, toString (anEGUID [I]) ]);
  166. inherited Create ( RaiseErrorInClass (FALSE,anArgs,EGUID) );
  167. end;
  168.  
  169. end.

Ссылка на данный код:

На главную страницу сервиса обмена кодом »