testser.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. Program testser;
  2. { Program to test serialization }
  3. {$Mode Delphi}
  4. Uses
  5. {$ifdef go32v2}
  6. dpmiexcp,
  7. {$endif}
  8. Typinfo,classes,iostream;
  9. Const TypeNames : Array [TTYpeKind] of string[15] =
  10. ('Unknown','Integer','AnsiChar','Enumeration',
  11. 'Float','Set','Method','ShortString','LongString',
  12. 'AnsiString','WideString','Variant','Array','Record',
  13. 'Interface','Class','Object','WideChar','Bool','Int64',
  14. 'QWord','DynArray','InterfaceRaw',
  15. '', '', '', '', '', '', '');
  16. Const OrdinalTypes = [tkInteger,tkChar,tkENumeration,tkbool,tkInt64,tkQWord];
  17. Type
  18. TMyEnum = (meFirst,meSecond,meThird);
  19. TMyTestObject = Class(TComponent)
  20. Private
  21. FBoolean : Boolean;
  22. FByte : Byte;
  23. FChar : AnsiChar;
  24. FWord : Word;
  25. FInteger : Integer;
  26. Flongint : Longint;
  27. FCardinal : Cardinal;
  28. FReal : Real;
  29. FExtended : Extended;
  30. FMyEnum : TMyEnum;
  31. FAnsiString : AnsiSTring;
  32. FObj : TObject;
  33. FStored : Boolean;
  34. Function GetBoolean : Boolean;
  35. Function GetByte : Byte;
  36. Function GetChar : AnsiChar;
  37. Function GetWord : Word;
  38. Function GetInteger : Integer;
  39. Function GetLongint : Longint;
  40. Function GetCardinal : Cardinal;
  41. Function GetReal : Real;
  42. Function GetExtended : Extended;
  43. Function GetAnsiString : AnsiString;
  44. Function GetMyEnum : TMyEnum;
  45. Procedure SetBoolean ( Value : Boolean);
  46. Procedure SetByte ( Value : Byte );
  47. Procedure SetChar ( Value : AnsiChar );
  48. Procedure SetWord ( Value : Word );
  49. Procedure SetInteger ( Value : Integer );
  50. Procedure SetLongint ( Value : Longint );
  51. Procedure SetCardinal ( Value : Cardinal );
  52. Procedure SetReal ( Value : Real );
  53. Procedure SetExtended ( Value : Extended );
  54. Procedure SetAnsiString ( Value : AnsiString );
  55. Procedure SetMyEnum ( Value : TMyEnum );
  56. Function GetVirtualBoolean : Boolean; virtual;
  57. Function GetVirtualByte : Byte; virtual;
  58. Function GetVirtualChar : AnsiChar; virtual;
  59. Function GetVirtualWord : Word; virtual;
  60. Function GetVirtualInteger : Integer; virtual;
  61. Function GetVirtualLongint : Longint; virtual;
  62. Function GetVirtualCardinal : Cardinal; virtual;
  63. Function GetVirtualReal : Real; virtual;
  64. Function GetVirtualExtended : Extended; virtual;
  65. Function GetVirtualAnsiString : AnsiString; virtual;
  66. Function GetVirtualMyEnum : TMyEnum; virtual;
  67. Procedure SetVirtualBoolean ( Value : Boolean); virtual;
  68. Procedure SetVirtualByte ( Value : Byte ); virtual;
  69. Procedure SetVirtualChar ( Value : AnsiChar ); virtual;
  70. Procedure SetVirtualWord ( Value : Word ); virtual;
  71. Procedure SetVirtualInteger ( Value : Integer ); virtual;
  72. Procedure SetVirtualLongint ( Value : Longint ); virtual;
  73. Procedure SetVirtualCardinal ( Value : Cardinal ); virtual;
  74. Procedure SetVirtualReal ( Value : Real ); virtual;
  75. Procedure SetVirtualExtended ( Value : Extended ); virtual;
  76. Procedure SetVirtualAnsiString ( Value : AnsiString ); virtual;
  77. Procedure SetVirtualMyEnum ( Value : TMyEnum ); virtual;
  78. Function GetStaticStored : Boolean;
  79. Function GetVirtualStored : Boolean;virtual;
  80. Public
  81. Constructor Create;
  82. Destructor Destroy;override;
  83. Published
  84. Property ObjField: TObject read FObj write FObj;
  85. Property BooleanField : Boolean Read FBoolean Write FBoolean;
  86. Property ByteField : Byte Read FByte Write FByte;
  87. Property CharField : AnsiChar Read FChar Write FChar;
  88. Property WordField : Word Read FWord Write FWord;
  89. Property IntegerField : Integer Read FInteger Write FInteger;
  90. Property LongintField : Longint Read FLongint Write FLongint;
  91. Property CardinalField : Cardinal Read FCardinal Write FCardinal;
  92. Property RealField : Real Read FReal Write FReal;
  93. Property ExtendedField : Extended Read FExtended Write FExtended;
  94. Property AnsiStringField : AnsiString Read FAnsiString Write FAnsiString;
  95. Property MyEnumField : TMyEnum Read FMyEnum Write FMyEnum;
  96. Property BooleanMethod : Boolean Read GetBoolean Write SetBoolean;
  97. Property ByteMethod : Byte Read GetByte Write SetByte;
  98. Property CharMethod : AnsiChar Read GetChar Write SetChar;
  99. Property WordMethod : Word Read GetWord Write SetWord;
  100. Property IntegerMethod : Integer Read GetInteger Write SetInteger;
  101. Property LongintMethod : Longint Read GetLongint Write SetLongint;
  102. Property CardinalMethod : Cardinal Read GetCardinal Write SetCardinal;
  103. Property RealMethod : Real Read GetReal Write SetReal;
  104. Property ExtendedMethod : Extended Read GetExtended Write SetExtended;
  105. Property AnsiStringMethod : AnsiString Read GetAnsiString Write SetAnsiString;
  106. Property MyEnumMethod : TMyEnum Read GetMyEnum Write SetMyEnum;
  107. Property BooleanVirtualMethod : Boolean Read GetVirtualBoolean Write SetVirtualBoolean;
  108. Property ByteVirtualMethod : Byte Read GetVirtualByte Write SetVirtualByte;
  109. Property CharVirtualMethod : AnsiChar Read GetVirtualChar Write SetVirtualChar;
  110. Property WordVirtualMethod : Word Read GetVirtualWord Write SetVirtualWord;
  111. Property IntegerVirtualMethod : Integer Read GetVirtualInteger Write SetVirtualInteger;
  112. Property LongintVirtualMethod : Longint Read GetVirtualLongint Write SetVirtualLongint;
  113. Property CardinalVirtualMethod : Cardinal Read GetVirtualCardinal Write SetVirtualCardinal;
  114. Property RealVirtualMethod : Real Read GetVirtualReal Write SetVirtualReal;
  115. Property ExtendedVirtualMethod : Extended Read GetVirtualExtended Write SetVirtualExtended;
  116. Property AnsiStringVirtualMethod : AnsiString Read GetVirtualAnsiString Write SetVirtualAnsiString;
  117. Property MyEnumVirtualMethod : TMyEnum Read GetVirtualMyEnum Write SetVirtualMyEnum;
  118. Property StoredIntegerConstFalse : Longint Read FLongint Stored False;
  119. Property StoredIntegerConstTrue : Longint Read FLongint Stored True;
  120. Property StoredIntegerField : Longint Read FLongint Stored FStored;
  121. Property StoredIntegerMethod : Longint Read Flongint Stored GetStaticStored;
  122. Property StoredIntegerVirtualMethod : Longint Read Flongint Stored GetVirtualStored;
  123. end;
  124. Constructor TMyTestObject.Create;
  125. begin
  126. FBoolean:=true;
  127. FByte:=1; { : Byte;}
  128. FChar:='B'; { : AnsiChar; }
  129. FWord:=3; {: Word; }
  130. FInteger:=4; {: Integer; }
  131. Flongint:=5; { : Longint; }
  132. FCardinal:=6; {: Cardinal; }
  133. FReal:=7.0; { : Real;}
  134. FExtended :=8.0; { Extended;}
  135. FMyEnum:=methird; { TMyEnum;}
  136. FAnsiString:='this is an AnsiString';
  137. end;
  138. Destructor TMyTestObject.Destroy;
  139. begin
  140. Inherited Destroy;
  141. end;
  142. Function TMyTestObject.GetBoolean : boolean;
  143. begin
  144. Result:=FBoolean;
  145. end;
  146. Function TMyTestObject.GetByte : Byte;
  147. begin
  148. Result:=FByte;
  149. end;
  150. Function TMyTestObject.GetChar : AnsiChar;
  151. begin
  152. Result:=FChar;
  153. end;
  154. Function TMyTestObject.GetWord : Word;
  155. begin
  156. Result:=FWord;
  157. end;
  158. Function TMyTestObject.GetInteger : Integer;
  159. begin
  160. Result:=FInteger;
  161. end;
  162. Function TMyTestObject.GetLongint : Longint;
  163. begin
  164. Result:=FLongint;
  165. end;
  166. Function TMyTestObject.GetCardinal : Cardinal;
  167. begin
  168. Result:=FCardinal;
  169. end;
  170. Function TMyTestObject.GetReal : Real;
  171. begin
  172. Result:=FReal;
  173. end;
  174. Function TMyTestObject.GetExtended : Extended;
  175. begin
  176. Result:=FExtended;
  177. end;
  178. Function TMyTestObject.GetAnsiString : AnsiString;
  179. begin
  180. Result:=FAnsiString;
  181. end;
  182. Function TMyTestObject.GetMyEnum : TMyEnum;
  183. begin
  184. Result:=FMyEnum;
  185. end;
  186. Procedure TMyTestObject.Setboolean ( Value : boolean );
  187. begin
  188. Fboolean:=Value;
  189. end;
  190. Procedure TMyTestObject.SetByte ( Value : Byte );
  191. begin
  192. FByte:=Value;
  193. end;
  194. Procedure TMyTestObject.SetChar ( Value : AnsiChar );
  195. begin
  196. FChar:=Value;
  197. end;
  198. Procedure TMyTestObject.SetWord ( Value : Word );
  199. begin
  200. FWord:=Value;
  201. end;
  202. Procedure TMyTestObject.SetInteger ( Value : Integer );
  203. begin
  204. FInteger:=Value;
  205. end;
  206. Procedure TMyTestObject.SetLongint ( Value : Longint );
  207. begin
  208. FLongint:=Value;
  209. end;
  210. Procedure TMyTestObject.SetCardinal ( Value : Cardinal );
  211. begin
  212. FCardinal:=Value;
  213. end;
  214. Procedure TMyTestObject.SetReal ( Value : Real );
  215. begin
  216. FReal:=Value;
  217. end;
  218. Procedure TMyTestObject.SetExtended ( Value : Extended );
  219. begin
  220. FExtended:=Value;
  221. end;
  222. Procedure TMyTestObject.SetAnsiString ( Value : AnsiString );
  223. begin
  224. FAnsiString:=Value;
  225. end;
  226. Procedure TMyTestObject.SetMyEnum ( Value : TMyEnum );
  227. begin
  228. FMyEnum:=Value;
  229. end;
  230. Function TMyTestObject.GetVirtualBoolean : boolean;
  231. begin
  232. Result:=FBoolean;
  233. end;
  234. Function TMyTestObject.GetVirtualByte : Byte;
  235. begin
  236. Result:=FByte;
  237. end;
  238. Function TMyTestObject.GetVirtualChar : AnsiChar;
  239. begin
  240. Result:=FChar;
  241. end;
  242. Function TMyTestObject.GetVirtualWord : Word;
  243. begin
  244. Result:=FWord;
  245. end;
  246. Function TMyTestObject.GetVirtualInteger : Integer;
  247. begin
  248. Result:=FInteger;
  249. end;
  250. Function TMyTestObject.GetVirtualLongint : Longint;
  251. begin
  252. Result:=FLongint;
  253. end;
  254. Function TMyTestObject.GetVirtualCardinal : Cardinal;
  255. begin
  256. Result:=FCardinal;
  257. end;
  258. Function TMyTestObject.GetVirtualReal : Real;
  259. begin
  260. Result:=FReal;
  261. end;
  262. Function TMyTestObject.GetVirtualExtended : Extended;
  263. begin
  264. Result:=FExtended;
  265. end;
  266. Function TMyTestObject.GetVirtualAnsiString : AnsiString;
  267. begin
  268. Result:=FAnsiString;
  269. end;
  270. Function TMyTestObject.GetVirtualMyEnum : TMyEnum;
  271. begin
  272. Result:=FMyEnum;
  273. end;
  274. Procedure TMyTestObject.SetVirtualboolean ( Value : boolean );
  275. begin
  276. Fboolean:=Value;
  277. end;
  278. Procedure TMyTestObject.SetVirtualByte ( Value : Byte );
  279. begin
  280. FByte:=Value;
  281. end;
  282. Procedure TMyTestObject.SetVirtualChar ( Value : AnsiChar );
  283. begin
  284. FChar:=Value;
  285. end;
  286. Procedure TMyTestObject.SetVirtualWord ( Value : Word );
  287. begin
  288. FWord:=Value;
  289. end;
  290. Procedure TMyTestObject.SetVirtualInteger ( Value : Integer );
  291. begin
  292. FInteger:=Value;
  293. end;
  294. Procedure TMyTestObject.SetVirtualLongint ( Value : Longint );
  295. begin
  296. FLongint:=Value;
  297. end;
  298. Procedure TMyTestObject.SetVirtualCardinal ( Value : Cardinal );
  299. begin
  300. FCardinal:=Value;
  301. end;
  302. Procedure TMyTestObject.SetVirtualReal ( Value : Real );
  303. begin
  304. FReal:=Value;
  305. end;
  306. Procedure TMyTestObject.SetVirtualExtended ( Value : Extended );
  307. begin
  308. FExtended:=Value;
  309. end;
  310. Procedure TMyTestObject.SetVirtualAnsiString ( Value : AnsiString );
  311. begin
  312. FAnsiString:=Value;
  313. end;
  314. Procedure TMyTestObject.SetVirtualMyEnum ( Value : TMyEnum );
  315. begin
  316. FMyEnum:=Value;
  317. end;
  318. Function TMyTestObject.GetStaticStored : Boolean;
  319. begin
  320. Result:=False;
  321. end;
  322. Function TMyTestObject.GetVirtualStored : Boolean;
  323. begin
  324. Result:=False;
  325. end;
  326. Procedure DumpMem ( PL : PByte );
  327. Var I,j : longint;
  328. begin
  329. For I:=1 to 16 do
  330. begin
  331. Write ((I-1)*16:3,' :');
  332. For J:=1 to 10 do
  333. begin
  334. If (PL^>31) and (PL^<129) then
  335. Write(' ',AnsiChar(PL^))
  336. else
  337. Write (PL^:3);
  338. Write (' ');
  339. inc(pl);
  340. end;
  341. writeln;
  342. end;
  343. end;
  344. Function ProcType (PP : Byte) : String;
  345. begin
  346. Case PP and 3 of
  347. ptfield : Result:='from Field';
  348. ptstatic : Result:='with static method';
  349. ptVirtual : Result:='with virtual method';
  350. ptconst : Result:='with Const';
  351. end;
  352. end;
  353. Procedure DumpTypeInfo (O : TMyTestObject);
  354. Var
  355. PT : PTypeData;
  356. PI : PTypeInfo;
  357. I,J : Longint;
  358. PP : PPropList;
  359. begin
  360. PI:=O.ClassInfo;
  361. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  362. Writeln ('Type name : ',PI^.Name);
  363. PT:=GetTypeData(PI);
  364. //DumpMem(PByte(PI));
  365. If PT^.ParentInfo=Nil then
  366. Writeln ('Object has no parent info')
  367. else
  368. Writeln ('Object has parent info');
  369. Writeln ('Property Count : ',PT^.PropCount);
  370. Writeln ('Unit name : ',PT^.UnitName);
  371. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  372. GetPropInfos(PI,PP);
  373. For I:=0 to PT^.PropCount-1 do
  374. If PP^[i]<>Nil then
  375. With PP^[I]^ do
  376. begin
  377. Writeln ('Property name : ',Name);
  378. Writeln (' Type kind: ',TypeNames[PropType^.Kind]);
  379. Writeln (' Type Name: ',PropType^.Name);
  380. If GetProc=Nil then Write ('No');
  381. Writeln (' Getproc available');
  382. If SetProc=Nil then Write ('No');
  383. Writeln (' Setproc available');
  384. If StoredProc=Nil then Write ('No');
  385. Writeln (' Storedproc available');
  386. Writeln (' Get property ',proctype(Propprocs));
  387. Writeln (' Set Property ',proctype(propprocs shr 2));
  388. Writeln (' Stored Property ',proctype(propprocs shr 4));
  389. Writeln (' Default : ',Default,' Index : ',Index);
  390. Writeln (' NameIndex : ',NameIndex);
  391. end;
  392. end;
  393. Procedure PrintObject ( Obj: TMyTestObject);
  394. begin
  395. With Obj do
  396. begin
  397. Writeln ('Field properties :');
  398. Writeln ('Property booleanField : ',booleanField);
  399. Writeln ('Property ByteField : ',ByteField);
  400. Writeln ('Property CharField : ',CharField);
  401. Writeln ('Property WordField : ',WordField);
  402. Writeln ('Property IntegerField : ',IntegerField);
  403. Writeln ('Property LongintField : ',LongintField);
  404. Writeln ('Property CardinalField : ',CardinalField);
  405. Writeln ('Property RealField : ',RealField);
  406. Writeln ('Property ExtendedField : ',ExtendedFIeld);
  407. Writeln ('Property AnsiStringField : ',AnsiStringField);
  408. Writeln ('Property MyEnumField : ',ord(MyEnumField));
  409. Writeln ('Method properties :');
  410. Writeln ('Property booleanMethod : ',BooleanMethod);
  411. Writeln ('Property ByteMethod : ',ByteMethod);
  412. Writeln ('Property CharMethod : ',CharMethod);
  413. Writeln ('Property WordMethod : ',WordMethod);
  414. Writeln ('Property IntegerMethod : ',IntegerMethod);
  415. Writeln ('Property LongintMethod : ',LongintMethod);
  416. Writeln ('Property CardinalMethod : ',CardinalMethod);
  417. Writeln ('Property RealMethod : ',RealMethod);
  418. Writeln ('Property ExtendedMethod : ',ExtendedMethod);
  419. Writeln ('Property AnsiStringMethod : ',AnsiStringMethod);
  420. Writeln ('Property MyEnumMethod : ',ord(MyEnumMethod));
  421. Writeln ('VirtualMethod properties :');
  422. Writeln ('Property booleanVirtualMethod : ',BooleanVirtualMethod);
  423. Writeln ('Property ByteVirtualMethod : ',ByteVirtualMethod);
  424. Writeln ('Property CharVirtualMethod : ',CharVirtualMethod);
  425. Writeln ('Property WordVirtualMethod : ',WordVirtualMethod);
  426. Writeln ('Property IntegerVirtualMethod : ',IntegerVirtualMethod);
  427. Writeln ('Property LongintVirtualMethod : ',LongintVirtualMethod);
  428. Writeln ('Property CardinalVirtualMethod : ',CardinalVirtualMethod);
  429. Writeln ('Property RealVirtualMethod : ',RealVirtualMethod);
  430. Writeln ('Property ExtendedVirtualMethod : ',ExtendedVirtualMethod);
  431. Writeln ('Property AnsiStringVirtualMethod : ',AnsiStringVirtualMethod);
  432. Writeln ('Property MyEnumVirtualMethod : ',ord(MyEnumVirtualMethod));
  433. end;
  434. end;
  435. Procedure TestGet (O : TMyTestObject);
  436. Var
  437. PT : PTypeData;
  438. PI : PTypeInfo;
  439. I,J : Longint;
  440. PP : PPropList;
  441. prI : PPropInfo;
  442. begin
  443. PI:=O.ClassInfo;
  444. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  445. Writeln ('Type name : ',PI^.Name);
  446. PT:=GetTypeData(PI);
  447. If PT^.ParentInfo=Nil then
  448. Writeln ('Object has no parent info')
  449. else
  450. Writeln ('Object has parent info');
  451. Writeln ('Property Count : ',PT^.PropCount);
  452. Writeln ('Unit name : ',PT^.UnitName);
  453. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  454. GetPropInfos(PI,PP);
  455. For I:=0 to PT^.PropCount-1 do
  456. begin
  457. pri:=PP^[i];
  458. With Pri^ do
  459. begin
  460. Write ('(Examining ',name,' : Type : ',TypeNames[PropType^.Kind],', ');
  461. If (Proptype^.kind in Ordinaltypes) Then
  462. begin
  463. J:=GetOrdProp(O,pri);
  464. Write ('Value : ',j);
  465. If PropType^.Kind=tkenumeration then
  466. Write ('(=',GetEnumName(Proptype,J),')')
  467. end
  468. else
  469. Case pri^.proptype^.kind of
  470. tkfloat : begin
  471. Write ('Value : ');
  472. Flush(output);
  473. Write(GetFloatProp(O,pri))
  474. end;
  475. tkAstring : begin
  476. Write ('value : ');
  477. flush (output);
  478. Write(GetStrProp(O,Pri));
  479. end;
  480. else
  481. Write ('Untested type:',ord(pri^.proptype^.kind));
  482. end;
  483. Writeln (')');
  484. end;
  485. end;
  486. end;
  487. procedure testserial(O : TComponent);
  488. Var W : Twriter;
  489. S : TStream;
  490. begin
  491. Writeln(stderr,'Creating stream');
  492. S:=TIOstream.Create(iosOutput);
  493. Writeln(stderr,'Creating TTextWriter');
  494. W:=TWriter.Create(S,1024);
  495. Writeln(stderr,'Writing component TTextWriter');
  496. W.WriteComponent(O);
  497. Writeln(stderr,'Destroying stream');
  498. S.Free;
  499. Writeln(stderr,'Destroying Writer');
  500. W.Free;
  501. end;
  502. Var O : TMyTestObject;
  503. begin
  504. O:=TMyTestObject.Create;
  505. (*
  506. DumpTypeInfo(O);
  507. PrintObject(O);
  508. testget(o);
  509. *)
  510. testSerial(o);
  511. end.