testser.pp 17 KB

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