hschumann2/TempleOS-Source-Code
0847
1 2/*Demonstrates class meta data.3Basically, we make use of the compiler's4data about a class. We can add to the5compilers data arbitrary items, either string or6or int or F64 (if you typecast).7 8This stuff is not high performance.9Don't get carried away -- it might be slow.10*/11 12U0 RankOut(I64 i)13{14 " %z",i,"Cadet\0Ensign\0Captain\0Admiral\0President\0";15}16 17class Test1Struct18{19 I64 age print_str "%2d" dft_val 38;20 I64 color dft_val RED; //Accepts expressions21 I64 rank print_str "%1d" dft_val 6/2 output_fun &RankOut;22};23 24class Test2Struct25{26 I64 age print_str "%2d" dft_val 38 percentile 54.20;27 I64 rank print_str "%1d" dft_val 5;28 I64 serial_num print_str "%6d" dft_val 123456;29};30 31U0 DumpStruct(U8 *_d,U8 *class_name=lastclass)32{//lastclass is keyword. See ::/Demo/LastClass.HC.33 CHashClass *tmpc=HashFind(class_name,Fs->hash_table,HTT_CLASS);34 U8 *print_str;35 I64 *q,dft_val;36 U0 (* fp_output_fun)(I64 i);37 F64 percentile;38 if (!tmpc) return;39 CMemberLst *ml;40 ml=tmpc->member_lst_and_root;41 while (ml) {42 "%s:",ml->str;43 44 //All our items are I64's. If you want, you can check45 //the data type of the member var. See ClassRep().46 q=_d+ml->offset;47 48 if (print_str=MemberMetaData("print_str",ml))49 "" print_str,*q;50 51 //This is slightly ambiguous -- if no meta is present it will return zero.52 if (dft_val=MemberMetaData("dft_val",ml))53 " default:%d",dft_val;54 55 //This corrects for the ambiguity, allowing zero percentile.56 if (MemberMetaFind("percentile",ml)) {//check if it exists57//We could use the CMemberLstMeta structure returned by58 //MemberMetaFind() and save a search.59 percentile=MemberMetaData("percentile",ml)(F64);60 " percentile: %5.2f",percentile;61 }62 63 if (fp_output_fun=MemberMetaData("output_fun",ml))64 (*fp_output_fun)(*q);65 '\n';66 ml=ml->next;67 }68}69 70Test1Struct t1;71t1.age=44;72t1.rank=3;73 74DumpStruct(&t1);75 76Test2Struct t2;77t2.age=22;78t2.rank=2;79t2.serial_num=55555;80 81DumpStruct(&t2);82 