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