rttiobj.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. Unit rttiobj;
  2. {$Mode Delphi}
  3. {$M+}
  4. Interface
  5. uses TypInfo;
  6. Const TypeNames : Array [TTYpeKind] of string[15] =
  7. ('Unknown','Integer','Char','Enumeration',
  8. 'Float','Set','Method','ShortString','LongString',
  9. 'AnsiString','WideString','Variant','Array','Record',
  10. 'Interface','Class','Object','WideChar','Bool','Int64','QWord',
  11. 'DynamicArray','RawInterface');
  12. Const OrdinalTypes = [tkInteger,tkChar,tkENumeration,tkbool];
  13. Type
  14. TMyEnum = (meFirst,meSecond,meThird);
  15. TMyEnums = Set of TMyEnum;
  16. TNotifyEvent = Procedure (Sender : TObject) of object;
  17. TMyTestObject = Class(TObject)
  18. Private
  19. FBoolean : Boolean;
  20. FByte : Byte;
  21. FChar : Char;
  22. FWord : Word;
  23. FInteger : Integer;
  24. Flongint : Longint;
  25. FCardinal : Cardinal;
  26. FReal : Real;
  27. FExtended : Extended;
  28. FMyEnum : TMyEnum;
  29. FAnsiString : AnsiSTring;
  30. FObj : TObject;
  31. FNotifyEvent : TNotifyEvent;
  32. FSetField : TMyEnums;
  33. // FInt64Field : Int64;
  34. FInt64Field : Integer;
  35. FStored : Boolean;
  36. Function GetBoolean : Boolean;
  37. Function GetByte : Byte;
  38. Function GetChar : Char;
  39. Function GetWord : Word;
  40. Function GetInteger : Integer;
  41. Function GetLongint : Longint;
  42. Function GetCardinal : Cardinal;
  43. Function GetReal : Real;
  44. Function GetExtended : Extended;
  45. Function GetAnsiString : AnsiString;
  46. Function GetMyEnum : TMyEnum;
  47. Procedure SetBoolean ( Value : Boolean);
  48. Procedure SetByte ( Value : Byte );
  49. Procedure SetChar ( Value : Char );
  50. Procedure SetWord ( Value : Word );
  51. Procedure SetInteger ( Value : Integer );
  52. Procedure SetLongint ( Value : Longint );
  53. Procedure SetCardinal ( Value : Cardinal );
  54. Procedure SetReal ( Value : Real );
  55. Procedure SetExtended ( Value : Extended );
  56. Procedure SetAnsiString ( Value : AnsiString );
  57. Procedure SetMyEnum ( Value : TMyEnum );
  58. Function GetVirtualBoolean : Boolean; virtual;
  59. Function GetVirtualByte : Byte; virtual;
  60. Function GetVirtualChar : Char; virtual;
  61. Function GetVirtualWord : Word; virtual;
  62. Function GetVirtualInteger : Integer; virtual;
  63. Function GetVirtualLongint : Longint; virtual;
  64. Function GetVirtualCardinal : Cardinal; virtual;
  65. Function GetVirtualReal : Real; virtual;
  66. Function GetVirtualExtended : Extended; virtual;
  67. Function GetVirtualAnsiString : AnsiString; virtual;
  68. Function GetVirtualMyEnum : TMyEnum; virtual;
  69. Procedure SetVirtualBoolean ( Value : Boolean); virtual;
  70. Procedure SetVirtualByte ( Value : Byte ); virtual;
  71. Procedure SetVirtualChar ( Value : Char ); virtual;
  72. Procedure SetVirtualWord ( Value : Word ); virtual;
  73. Procedure SetVirtualInteger ( Value : Integer ); virtual;
  74. Procedure SetVirtualLongint ( Value : Longint ); virtual;
  75. Procedure SetVirtualCardinal ( Value : Cardinal ); virtual;
  76. Procedure SetVirtualReal ( Value : Real ); virtual;
  77. Procedure SetVirtualExtended ( Value : Extended ); virtual;
  78. Procedure SetVirtualAnsiString ( Value : AnsiString ); virtual;
  79. Procedure SetVirtualMyEnum ( Value : TMyEnum ); virtual;
  80. Function GetStaticStored : Boolean;
  81. Function GetVirtualStored : Boolean;virtual;
  82. Public
  83. Constructor Create;
  84. Destructor Destroy;override;
  85. Procedure Notify;
  86. Published
  87. Property ObjField: TObject read FObj write FObj;
  88. Property SetField : TMyEnums Read FSetField Write FSetField;
  89. Property NotifyEvent : TNotifyEvent Read FNotifyEvent Write FNotifyEvent;
  90. // Property Int64Field : Int64 Read Fint64Field Write FInt64Field;
  91. Property Int64Field : Integer Read Fint64Field Write FInt64Field;
  92. Property BooleanField : Boolean Read FBoolean Write FBoolean;
  93. Property ByteField : Byte Read FByte Write FByte;
  94. Property CharField : Char Read FChar Write FChar;
  95. Property WordField : Word Read FWord Write FWord;
  96. Property IntegerField : Integer Read FInteger Write FInteger;
  97. Property LongintField : Longint Read FLongint Write FLongint;
  98. Property CardinalField : Cardinal Read FCardinal Write FCardinal;
  99. Property RealField : Real Read FReal Write FReal;
  100. Property ExtendedField : Extended Read FExtended Write FExtended;
  101. Property AnsiStringField : AnsiString Read FAnsiString Write FAnsiString;
  102. Property MyEnumField : TMyEnum Read FMyEnum Write FMyEnum;
  103. Property BooleanMethod : Boolean Read GetBoolean Write SetBoolean;
  104. Property ByteMethod : Byte Read GetByte Write SetByte;
  105. Property CharMethod : Char Read GetChar Write SetChar;
  106. Property WordMethod : Word Read GetWord Write SetWord;
  107. Property IntegerMethod : Integer Read GetInteger Write SetInteger;
  108. Property LongintMethod : Longint Read GetLongint Write SetLongint;
  109. Property CardinalMethod : Cardinal Read GetCardinal Write SetCardinal;
  110. Property RealMethod : Real Read GetReal Write SetReal;
  111. Property ExtendedMethod : Extended Read GetExtended Write SetExtended;
  112. Property AnsiStringMethod : AnsiString Read GetAnsiString Write SetAnsiString;
  113. Property MyEnumMethod : TMyEnum Read GetMyEnum Write SetMyEnum;
  114. Property BooleanVirtualMethod : Boolean Read GetVirtualBoolean Write SetVirtualBoolean;
  115. Property ByteVirtualMethod : Byte Read GetVirtualByte Write SetVirtualByte;
  116. Property CharVirtualMethod : Char Read GetVirtualChar Write SetVirtualChar;
  117. Property WordVirtualMethod : Word Read GetVirtualWord Write SetVirtualWord;
  118. Property IntegerVirtualMethod : Integer Read GetVirtualInteger Write SetVirtualInteger;
  119. Property LongintVirtualMethod : Longint Read GetVirtualLongint Write SetVirtualLongint;
  120. Property CardinalVirtualMethod : Cardinal Read GetVirtualCardinal Write SetVirtualCardinal;
  121. Property RealVirtualMethod : Real Read GetVirtualReal Write SetVirtualReal;
  122. Property ExtendedVirtualMethod : Extended Read GetVirtualExtended Write SetVirtualExtended;
  123. Property AnsiStringVirtualMethod : AnsiString Read GetVirtualAnsiString Write SetVirtualAnsiString;
  124. Property MyEnumVirtualMethod : TMyEnum Read GetVirtualMyEnum Write SetVirtualMyEnum;
  125. Property StoredIntegerConstFalse : Longint Read FLongint Stored False;
  126. Property StoredIntegerConstTrue : Longint Read FLongint Stored True;
  127. Property StoredIntegerField : Longint Read FLongint Stored FStored;
  128. Property StoredIntegerMethod : Longint Read Flongint Stored GetStaticStored;
  129. Property StoredIntegerVirtualMethod : Longint Read Flongint Stored GetVirtualStored;
  130. end;
  131. TNamedObject = Class(TObject)
  132. Private
  133. FObjectName : AnsiString;
  134. Public
  135. Property ObjectName : AnsiString Read FObjectName Write FObjectName;
  136. end;
  137. Const
  138. MyEnumNames : Array[TMyEnum] of string = ('meFirst','meSecond','meThird');
  139. Procedure PrintObject ( Obj: TMyTestObject);
  140. Implementation
  141. Constructor TMyTestObject.Create;
  142. begin
  143. FBoolean:=true;
  144. FByte:=1; { : Byte;}
  145. FChar:='B'; { : Char; }
  146. FWord:=3; {: Word; }
  147. FInteger:=4; {: Integer; }
  148. Flongint:=5; { : Longint; }
  149. FCardinal:=6; {: Cardinal; }
  150. FReal:=7.0; { : Real;}
  151. FExtended :=8.0; { Extended;}
  152. FMyEnum:=methird; { TMyEnum;}
  153. FAnsiString:='this is an AnsiString';
  154. end;
  155. Destructor TMyTestObject.Destroy;
  156. begin
  157. Inherited Destroy;
  158. end;
  159. Function TMyTestObject.GetBoolean : boolean;
  160. begin
  161. Result:=FBoolean;
  162. end;
  163. Function TMyTestObject.GetByte : Byte;
  164. begin
  165. Result:=FByte;
  166. end;
  167. Function TMyTestObject.GetChar : Char;
  168. begin
  169. Result:=FChar;
  170. end;
  171. Function TMyTestObject.GetWord : Word;
  172. begin
  173. Result:=FWord;
  174. end;
  175. Function TMyTestObject.GetInteger : Integer;
  176. begin
  177. Result:=FInteger;
  178. end;
  179. Function TMyTestObject.GetLongint : Longint;
  180. begin
  181. Result:=FLongint;
  182. end;
  183. Function TMyTestObject.GetCardinal : Cardinal;
  184. begin
  185. Result:=FCardinal;
  186. end;
  187. Function TMyTestObject.GetReal : Real;
  188. begin
  189. Result:=FReal;
  190. end;
  191. Function TMyTestObject.GetExtended : Extended;
  192. begin
  193. Result:=FExtended;
  194. end;
  195. Function TMyTestObject.GetAnsiString : AnsiString;
  196. begin
  197. Result:=FAnsiString;
  198. end;
  199. Function TMyTestObject.GetMyEnum : TMyEnum;
  200. begin
  201. Result:=FMyEnum;
  202. end;
  203. Procedure TMyTestObject.Setboolean ( Value : boolean );
  204. begin
  205. Fboolean:=Value;
  206. end;
  207. Procedure TMyTestObject.SetByte ( Value : Byte );
  208. begin
  209. FByte:=Value;
  210. end;
  211. Procedure TMyTestObject.SetChar ( Value : Char );
  212. begin
  213. FChar:=Value;
  214. end;
  215. Procedure TMyTestObject.SetWord ( Value : Word );
  216. begin
  217. FWord:=Value;
  218. end;
  219. Procedure TMyTestObject.SetInteger ( Value : Integer );
  220. begin
  221. FInteger:=Value;
  222. end;
  223. Procedure TMyTestObject.SetLongint ( Value : Longint );
  224. begin
  225. FLongint:=Value;
  226. end;
  227. Procedure TMyTestObject.SetCardinal ( Value : Cardinal );
  228. begin
  229. FCardinal:=Value;
  230. end;
  231. Procedure TMyTestObject.SetReal ( Value : Real );
  232. begin
  233. FReal:=Value;
  234. end;
  235. Procedure TMyTestObject.SetExtended ( Value : Extended );
  236. begin
  237. FExtended:=Value;
  238. end;
  239. Procedure TMyTestObject.SetAnsiString ( Value : AnsiString );
  240. begin
  241. FAnsiString:=Value;
  242. end;
  243. Procedure TMyTestObject.SetMyEnum ( Value : TMyEnum );
  244. begin
  245. FMyEnum:=Value;
  246. end;
  247. Function TMyTestObject.GetVirtualBoolean : boolean;
  248. begin
  249. Result:=FBoolean;
  250. end;
  251. Function TMyTestObject.GetVirtualByte : Byte;
  252. begin
  253. Result:=FByte;
  254. end;
  255. Function TMyTestObject.GetVirtualChar : Char;
  256. begin
  257. Result:=FChar;
  258. end;
  259. Function TMyTestObject.GetVirtualWord : Word;
  260. begin
  261. Result:=FWord;
  262. end;
  263. Function TMyTestObject.GetVirtualInteger : Integer;
  264. begin
  265. Result:=FInteger;
  266. end;
  267. Function TMyTestObject.GetVirtualLongint : Longint;
  268. begin
  269. Result:=FLongint;
  270. end;
  271. Function TMyTestObject.GetVirtualCardinal : Cardinal;
  272. begin
  273. Result:=FCardinal;
  274. end;
  275. Function TMyTestObject.GetVirtualReal : Real;
  276. begin
  277. Result:=FReal;
  278. end;
  279. Function TMyTestObject.GetVirtualExtended : Extended;
  280. begin
  281. Result:=FExtended;
  282. end;
  283. Function TMyTestObject.GetVirtualAnsiString : AnsiString;
  284. begin
  285. Result:=FAnsiString;
  286. end;
  287. Function TMyTestObject.GetVirtualMyEnum : TMyEnum;
  288. begin
  289. Result:=FMyEnum;
  290. end;
  291. Procedure TMyTestObject.SetVirtualboolean ( Value : boolean );
  292. begin
  293. Fboolean:=Value;
  294. end;
  295. Procedure TMyTestObject.SetVirtualByte ( Value : Byte );
  296. begin
  297. FByte:=Value;
  298. end;
  299. Procedure TMyTestObject.SetVirtualChar ( Value : Char );
  300. begin
  301. FChar:=Value;
  302. end;
  303. Procedure TMyTestObject.SetVirtualWord ( Value : Word );
  304. begin
  305. FWord:=Value;
  306. end;
  307. Procedure TMyTestObject.SetVirtualInteger ( Value : Integer );
  308. begin
  309. FInteger:=Value;
  310. end;
  311. Procedure TMyTestObject.SetVirtualLongint ( Value : Longint );
  312. begin
  313. FLongint:=Value;
  314. end;
  315. Procedure TMyTestObject.SetVirtualCardinal ( Value : Cardinal );
  316. begin
  317. FCardinal:=Value;
  318. end;
  319. Procedure TMyTestObject.SetVirtualReal ( Value : Real );
  320. begin
  321. FReal:=Value;
  322. end;
  323. Procedure TMyTestObject.SetVirtualExtended ( Value : Extended );
  324. begin
  325. FExtended:=Value;
  326. end;
  327. Procedure TMyTestObject.SetVirtualAnsiString ( Value : AnsiString );
  328. begin
  329. FAnsiString:=Value;
  330. end;
  331. Procedure TMyTestObject.SetVirtualMyEnum ( Value : TMyEnum );
  332. begin
  333. FMyEnum:=Value;
  334. end;
  335. Function TMyTestObject.GetStaticStored : Boolean;
  336. begin
  337. Result:=False;
  338. end;
  339. Function TMyTestObject.GetVirtualStored : Boolean;
  340. begin
  341. Result:=False;
  342. end;
  343. Procedure TMyTestObject.Notify;
  344. begin
  345. If Assigned(FNotifyEvent) then
  346. FNotifyEvent(Self)
  347. else
  348. Writeln('Error : No notifyevent assigned');
  349. end;
  350. Procedure PrintObject ( Obj: TMyTestObject);
  351. begin
  352. With Obj do
  353. begin
  354. Writeln ('Field properties :');
  355. Writeln ('Property booleanField : ',booleanField);
  356. Writeln ('Property ByteField : ',ByteField);
  357. Writeln ('Property CharField : ',CharField);
  358. Writeln ('Property WordField : ',WordField);
  359. Writeln ('Property IntegerField : ',IntegerField);
  360. Writeln ('Property LongintField : ',LongintField);
  361. Writeln ('Property CardinalField : ',CardinalField);
  362. Writeln ('Property RealField : ',RealField);
  363. Writeln ('Property ExtendedField : ',ExtendedFIeld);
  364. Writeln ('Property AnsiStringField : ',AnsiStringField);
  365. Writeln ('Property MyEnumField : ',ord(MyEnumField));
  366. Writeln ('Method properties :');
  367. Writeln ('Property booleanMethod : ',BooleanMethod);
  368. Writeln ('Property ByteMethod : ',ByteMethod);
  369. Writeln ('Property CharMethod : ',CharMethod);
  370. Writeln ('Property WordMethod : ',WordMethod);
  371. Writeln ('Property IntegerMethod : ',IntegerMethod);
  372. Writeln ('Property LongintMethod : ',LongintMethod);
  373. Writeln ('Property CardinalMethod : ',CardinalMethod);
  374. Writeln ('Property RealMethod : ',RealMethod);
  375. Writeln ('Property ExtendedMethod : ',ExtendedMethod);
  376. Writeln ('Property AnsiStringMethod : ',AnsiStringMethod);
  377. Writeln ('Property MyEnumMethod : ',ord(MyEnumMethod));
  378. Writeln ('VirtualMethod properties :');
  379. Writeln ('Property booleanVirtualMethod : ',BooleanVirtualMethod);
  380. Writeln ('Property ByteVirtualMethod : ',ByteVirtualMethod);
  381. Writeln ('Property CharVirtualMethod : ',CharVirtualMethod);
  382. Writeln ('Property WordVirtualMethod : ',WordVirtualMethod);
  383. Writeln ('Property IntegerVirtualMethod : ',IntegerVirtualMethod);
  384. Writeln ('Property LongintVirtualMethod : ',LongintVirtualMethod);
  385. Writeln ('Property CardinalVirtualMethod : ',CardinalVirtualMethod);
  386. Writeln ('Property RealVirtualMethod : ',RealVirtualMethod);
  387. Writeln ('Property ExtendedVirtualMethod : ',ExtendedVirtualMethod);
  388. Writeln ('Property AnsiStringVirtualMethod : ',AnsiStringVirtualMethod);
  389. Writeln ('Property MyEnumVirtualMethod : ',ord(MyEnumVirtualMethod));
  390. end;
  391. end;
  392. Procedure DumpMem ( PL : PByte );
  393. Var I,j : longint;
  394. begin
  395. For I:=1 to 16 do
  396. begin
  397. Write ((I-1)*16:3,' :');
  398. For J:=1 to 10 do
  399. begin
  400. If (PL^>31) and (PL^<129) then
  401. Write(' ',CHar(PL^))
  402. else
  403. Write (PL^:3);
  404. Write (' ');
  405. inc(pl);
  406. end;
  407. writeln;
  408. end;
  409. end;
  410. Function ProcType (PP : Byte) : String;
  411. begin
  412. Case PP and 3 of
  413. ptfield : Result:='from Field';
  414. ptstatic : Result:='with static method';
  415. ptVirtual : Result:='with virtual method';
  416. ptconst : Result:='with Const';
  417. end;
  418. end;
  419. end.