PageRenderTime 22ms CodeModel.GetById 19ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/jcl/examples/common/rtti/RTTIDemoMain.pas

https://github.com/the-Arioch/jcl
Pascal | 331 lines | 297 code | 27 blank | 7 comment | 0 complexity | 78bb2bffb9fab24fc12895b62f1aac13 MD5 | raw file
Possible License(s): BSD-3-Clause
  1unit RTTIDemoMain;
  2
  3interface
  4
  5{$I jcl.inc}
  6
  7uses
  8  SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
  9
 10type
 11  TForm1 = class(TForm)
 12    mmResult: TMemo;
 13    Button1: TButton;
 14    Button2: TButton;
 15    Button3: TButton;
 16    Button4: TButton;
 17    procedure Button1Click(Sender: TObject);
 18    procedure Button2Click(Sender: TObject);
 19    procedure Button3Click(Sender: TObject);
 20    procedure Button4Click(Sender: TObject);
 21  private
 22    { Private declarations }
 23  public
 24    { Public declarations }
 25  end;
 26
 27var
 28  Form1: TForm1;
 29
 30implementation
 31
 32{$R *.dfm}
 33
 34uses
 35  JclSysUtils, JclRTTI, TypInfo;
 36
 37type
 38  TDifficultEvent = procedure(const Sender: TObject; var I: Integer; out Stuff;
 39    IntArr: array of Integer; const VarArray: array of const) of object;
 40
 41  TLargeEnum = (
 42    le001, le002, le003, le004, le005, le006, le007, le008, le009, le010,
 43    le011, le012, le013, le014, le015, le016, le017, le018, le019, le020,
 44    le021, le022, le023, le024, le025, le026, le027, le028, le029, le030,
 45    le031, le032, le033, le034, le035, le036, le037, le038, le039, le040,
 46    le041, le042, le043, le044, le045, le046, le047, le048, le049, le050,
 47    le051, le052, le053, le054, le055, le056, le057, le058, le059, le060,
 48    le061, le062, le063, le064, le065, le066, le067, le068, le069, le070,
 49    le071, le072, le073, le074, le075, le076, le077, le078, le079, le080,
 50    le081, le082, le083, le084, le085, le086, le087, le088, le089, le090,
 51    le091, le092, le093, le094, le095, le096, le097, le098, le099, le100,
 52    le101, le102, le103, le104, le105, le106, le107, le108, le109, le110,
 53    le111, le112, le113, le114, le115, le116, le117, le118, le119, le120,
 54    le121, le122, le123, le124, le125, le126, le127, le128, le129, le130,
 55    le131, le132, le133, le134, le135, le136, le137, le138, le139, le140,
 56    le141, le142, le143, le144, le145, le146, le147, le148, le149, le150,
 57    le151, le152, le153, le154, le155, le156, le157, le158, le159, le160);
 58    
 59  TLargeSet = set of TLargeEnum;
 60  TLargeSubEnum = le019 .. le150;
 61  TLargeSubSet = set of TLargeSubEnum;
 62
 63  TIntRange = 0 .. 112;
 64
 65  TSetNoEnum = set of (st01, st02, st03, st04);
 66  TSetOfByte = set of Byte;
 67  TInt2Range = 4..11;
 68  TSetOfIntRange = set of TInt2Range;
 69
 70  TUpcaseRange = 'A' .. 'Z';
 71
 72  TMyDouble = Double;
 73  TMyDouble2 = type Double;
 74
 75  TIntArray = array of Integer;
 76  TIntArray2 = array of array of Integer;
 77  TEnumArray = array of (ar1, ar2, ar3);
 78  TRecArray = array of record x1: Integer; x2: Integer; end;
 79  TSetArray = array of set of (ars1, ars2, ars3);
 80  TSetArray2 = array of array of array of TSetNoEnum;
 81  TWideStrArray = array of Widestring;
 82
 83var
 84  MyEnum: PTypeInfo;
 85  MySubRange: PTypeInfo;
 86  MySet: PTypeInfo;
 87  MyCutLowerEnum: PTypeInfo;
 88
 89procedure TForm1.Button1Click(Sender: TObject);
 90var
 91  Writer: IJclInfoWriter;
 92
 93begin
 94  mmResult.Lines.Clear;
 95  Writer := TJclInfoStringsWriter.Create(mmResult.Lines, 72);
 96  JclTypeInfo(TypeInfo(Word)).WriteTo(Writer);
 97  Writer.Writeln('');
 98  JclTypeInfo(TypeInfo(TIntRange)).WriteTo(Writer);
 99  Writer.Writeln('');
100  JclTypeInfo(TypeInfo(TLargeEnum)).WriteTo(Writer);
101  Writer.Writeln('');
102  JclTypeInfo(TypeInfo(TLargeSubEnum)).WriteTo(Writer);
103  Writer.Writeln('');
104  JclTypeInfo(TypeInfo(TLargeSet)).WriteTo(Writer);
105  Writer.Writeln('');
106  JclTypeInfo(TypeInfo(TLargeSubSet)).WriteTo(Writer);
107  Writer.Writeln('');
108  JclTypeInfo(TypeInfo(TSetNoEnum)).WriteTo(Writer);
109  Writer.Writeln('');
110  JclTypeInfo(TypeInfo(TSetOfByte)).WriteTo(Writer);
111  Writer.Writeln('');
112  JclTypeInfo(TypeInfo(TSetOfIntRange)).WriteTo(Writer);
113  Writer.Writeln('');
114  JclTypeInfo(TypeInfo(Single)).WriteTo(Writer);
115  Writer.Writeln('');
116  JclTypeInfo(TypeInfo(Double)).WriteTo(Writer);
117  Writer.Writeln('');
118  JclTypeInfo(TypeInfo(Extended)).WriteTo(Writer);
119  Writer.Writeln('');
120  JclTypeInfo(TypeInfo(Comp)).WriteTo(Writer);
121  Writer.Writeln('');
122  JclTypeInfo(TypeInfo(Currency)).WriteTo(Writer);
123  Writer.Writeln('');
124  JclTypeInfo(TypeInfo(Real)).WriteTo(Writer);
125  Writer.Writeln('');
126  JclTypeInfo(TypeInfo(TDateTime)).WriteTo(Writer);
127  Writer.Writeln('');
128  JclTypeInfo(TypeInfo(TMyDouble)).WriteTo(Writer);
129  Writer.Writeln('');
130  JclTypeInfo(TypeInfo(TMyDouble2)).WriteTo(Writer);
131  Writer.Writeln('');
132  JclTypeInfo(TypeInfo(ShortString)).WriteTo(Writer);
133  Writer.Writeln('');
134  JclTypeInfo(TypeInfo(TScrollingWinControl)).WriteTo(Writer);
135  Writer.Writeln('');
136  JclTypeInfo(TypeInfo(TDifficultEvent)).WriteTo(Writer);
137  Writer.Writeln('');
138  JclTypeInfo(TypeInfo(IJclOrdinalRangeTypeInfo)).WriteTo(Writer);
139  Writer.Writeln('');
140  JclTypeInfo(TypeInfo(Int64)).WriteTo(Writer);
141  Writer.Writeln('');
142  JclTypeInfo(TypeInfo(Longword)).WriteTo(Writer);
143  Writer.Writeln('');
144  JclTypeInfo(TypeInfo(TIntArray)).WriteTo(Writer);
145  Writer.Writeln('');
146  JclTypeInfo(TypeInfo(TIntArray2)).WriteTo(Writer);
147  Writer.Writeln('');
148  JclTypeInfo(TypeInfo(TEnumArray)).WriteTo(Writer);
149  Writer.Writeln('');
150  JclTypeInfo(TypeInfo(TRecArray)).WriteTo(Writer);
151  Writer.Writeln('');
152  JclTypeInfo(TypeInfo(TSetArray)).WriteTo(Writer);
153  Writer.Writeln('');
154  JclTypeInfo(TypeInfo(TSetArray2)).WriteTo(Writer);
155end;
156
157procedure TForm1.Button2Click(Sender: TObject);
158var
159  Writer: IJclInfoWriter;
160  LargeSubSet: TLargeSubSet;
161  GUID: TGUID;
162
163begin
164  mmResult.Lines.Clear;
165  Writer := TJclInfoStringsWriter.Create(mmResult.Lines, 72);
166  Writer.Writeln('Set conversions:');
167  Writer.Indent;
168  try
169    Writer.Writeln('StrToSet with string=''[le019..le023, le033, le045..le049]''');
170    JclStrToSet(TypeInfo(TLargeSubSet), LargeSubSet, '[le019..le023, le033, le045..le049]');
171    Writer.Writeln('SetToStr of StrToSet = ''' + JclSetToStr(TypeInfo(TLargeSubSet), LargeSubSet, True, True) + ''', with WantRanges=True');
172    Writer.Writeln('SetToStr of StrToSet = ''' + JclSetToStr(TypeInfo(TLargeSubSet), LargeSubSet, True, False) + ''', with WantRanges=False');
173    Writer.Writeln('');
174    Writer.Writeln('StrToSet with string=''''');
175    JclStrToSet(TypeInfo(TLargeSubSet), LargeSubSet, '');
176    Writer.Writeln('SetToStr of StrToSet = ''' + JclSetToStr(TypeInfo(TLargeSubSet), LargeSubSet, True, True) + ''', with WantRanges=True');
177    Writer.Writeln('SetToStr of StrToSet = ''' + JclSetToStr(TypeInfo(TLargeSubSet), LargeSubSet, True, False) + ''', with WantRanges=False');
178    Writer.Writeln('');
179    Writer.Writeln('StrToSet with string=''le019    ..    le023,le033     , le045 ..           le049           ''');
180    JclStrToSet(TypeInfo(TLargeSubSet), LargeSubSet, 'le019    ..    le023,le033     , le045 ..           le049           ');
181    Writer.Writeln('SetToStr of StrToSet = ''' + JclSetToStr(TypeInfo(TLargeSubSet), LargeSubSet, True, True) + ''', with WantRanges=True');
182    Writer.Writeln('SetToStr of StrToSet = ''' + JclSetToStr(TypeInfo(TLargeSubSet), LargeSubSet, True, False) + ''', with WantRanges=False');
183    Writer.Writeln('');
184  finally
185    Writer.Outdent;
186  end;
187  Writer.Writeln('GUID conversions:');
188  Writer.Indent;
189  try
190    Writer.Writeln('GUIDToStr: ' + JclGUIDToString(IJclTypeInfo));
191    GUID := JclStringToGUID(JclGUIDToString(IJclTypeInfo));
192    Writer.Writeln('StrToGUID: ' + JclGUIDToString(GUID));
193  finally
194    Writer.Outdent;
195  end;
196  {$IFDEF COMPILER5_UP}
197  Writer.Writeln('');
198  Writer.Writeln('Integer conversions:');
199  Writer.Indent;
200  try
201    Writer.Writeln('TypedIntToStr: ' + JclTypedIntToStr(crArrow, TypeInfo(TCursor)));
202    Writer.Writeln('StrToTypedInt: ' + IntToStr(JclStrToTypedInt('crArrow', TypeInfo(TCursor))) + ' (should be ' + IntToStr(crArrow) + ')');
203    Writer.Writeln('');
204    Writer.Writeln('TypedIntToStr: ' + JclTypedIntToStr(1, TypeInfo(TCursor)));
205    Writer.Writeln('StrToTypedInt: ' + IntToStr(JclStrToTypedInt('1', TypeInfo(TCursor))) + ' (should be 1)');
206  finally
207    Writer.Outdent;
208  end;
209  {$ENDIF}
210end;
211
212procedure TForm1.Button3Click(Sender: TObject);
213var
214  Writer: IJclInfoWriter;
215
216begin
217  mmResult.Lines.Clear;
218  Writer := TJclInfoStringsWriter.Create(mmResult.Lines, 80);
219  Writer.Writeln('Declarations:');
220  Writer.Indent;
221  try
222    JclTypeInfo(TypeInfo(TLargeEnum)).DeclarationTo(Writer);
223    Writer.Writeln('');
224    JclTypeInfo(TypeInfo(TLargeSubEnum)).DeclarationTo(Writer);
225    Writer.Writeln('');
226    JclTypeInfo(TypeInfo(TLargeSet)).DeclarationTo(Writer);
227    Writer.Writeln('');
228    JclTypeInfo(TypeInfo(TSetNoEnum)).DeclarationTo(Writer);
229    Writer.Writeln('');
230    JclTypeInfo(TypeInfo(Byte)).DeclarationTo(Writer);
231    Writer.Writeln('');
232    JclTypeInfo(TypeInfo(TSetOfByte)).DeclarationTo(Writer);
233    Writer.Writeln('');
234    JclTypeInfo(TypeInfo(Char)).DeclarationTo(Writer);
235    Writer.Writeln('');
236    JclTypeInfo(TypeInfo(TUpcaseRange)).DeclarationTo(Writer);
237    Writer.Writeln('');
238    JclTypeInfo(TypeInfo(TDifficultEvent)).DeclarationTo(Writer);
239    Writer.Writeln('');
240    JclTypeInfo(TypeInfo(IJclBaseInfo)).DeclarationTo(Writer);
241    Writer.Writeln('');
242    JclTypeInfo(TypeInfo(IJclTypeInfo)).DeclarationTo(Writer);
243    Writer.Writeln('');
244    JclTypeInfo(TypeInfo(TDateTime)).DeclarationTo(Writer);
245    Writer.Writeln('');
246    JclTypeInfo(TypeInfo(TMyDouble)).DeclarationTo(Writer);
247    Writer.Writeln('');
248    JclTypeInfo(TypeInfo(TMyDouble2)).DeclarationTo(Writer);
249    Writer.Writeln('');
250    JclTypeInfo(TypeInfo(TScrollingWinControl)).DeclarationTo(Writer);
251    Writer.Writeln('');
252    JclTypeInfo(TypeInfo(TJclInfoWriter)).DeclarationTo(Writer);
253    Writer.Writeln('');
254    JclTypeInfo(TypeInfo(TPersistent)).DeclarationTo(Writer);
255    Writer.Writeln('');
256    JclTypeInfo(TypeInfo(TIntArray)).DeclarationTo(Writer);
257    Writer.Writeln('');
258    JclTypeInfo(TypeInfo(TIntArray2)).DeclarationTo(Writer);
259    Writer.Writeln('');
260    JclTypeInfo(TypeInfo(TEnumArray)).DeclarationTo(Writer);
261    Writer.Writeln('');
262    JclTypeInfo(TypeInfo(TRecArray)).DeclarationTo(Writer);
263    Writer.Writeln('');
264    JclTypeInfo(TypeInfo(TSetArray)).DeclarationTo(Writer);
265    Writer.Writeln('');
266    JclTypeInfo(TypeInfo(TSetArray2)).DeclarationTo(Writer);
267    Writer.Writeln('');
268    JclTypeInfo(TypeInfo(TWideStrArray)).DeclarationTo(Writer);
269  finally
270    Writer.Outdent;
271  end;
272end;
273
274procedure TForm1.Button4Click(Sender: TObject);
275var
276  Writer: IJclInfoWriter;
277
278begin
279  mmResult.Lines.Clear;
280  Writer := TJclInfoStringsWriter.Create(mmResult.Lines, 80);
281  Writer.Writeln('JclGenerateEnumType with literals:');
282  Writer.Indent;
283  try
284    JclTypeInfo(MyEnum).WriteTo(Writer);
285    Writer.Writeln;
286    JclTypeInfo(MyEnum).DeclarationTo(Writer);
287  finally
288    Writer.Outdent;
289  end;
290  Writer.Writeln;
291  Writer.Writeln('JclGenerateSubRange:');
292  Writer.Indent;
293  try
294    JclTypeInfo(MySubRange).WriteTo(Writer);
295    Writer.Writeln;
296    JclTypeInfo(MySubRange).DeclarationTo(Writer);
297  finally
298    Writer.Outdent;
299  end;
300  Writer.Writeln;
301  Writer.Writeln('JclGenerateSetType:');
302  Writer.Indent;
303  try
304    JclTypeInfo(MySet).WriteTo(Writer);
305    Writer.Writeln;
306    JclTypeInfo(MySet).DeclarationTo(Writer);
307  finally
308    Writer.Outdent;
309  end;
310  Writer.Writeln;
311  Writer.Writeln('JclGenerateEnumType based on TLargeEnum:');
312  Writer.Indent;
313  try
314    JclTypeInfo(MyCutLowerEnum).WriteTo(Writer);
315    Writer.Writeln;
316    JclTypeInfo(MyCutLowerEnum).DeclarationTo(Writer);
317  finally
318    Writer.Outdent;
319  end;
320end;
321
322initialization
323  //JclHookIs(JclIsClassByName);
324  MyEnum := JclGenerateEnumType('MyEnum', ['First value', 'Second value',
325    'Third value', 'Fourth value', 'Fifth value']);
326  MySubRange := JclGenerateSubRange(MyEnum, 'MySubRange', 1, 3);
327  MySet := JclGenerateSetType(MyEnum, 'MySet');
328  MyCutLowerEnum := JclGenerateEnumTypeBasedOn('MyCutLower', TypeInfo(TLargeEnum),
329    PREFIX_CUT_LOWERCASE);
330
331end.