2020-02-15 20:01:48 +00:00
|
|
|
/*Demonstrates class meta data.
|
|
|
|
Basically, we make use of the compiler's
|
2020-03-01 01:59:50 +00:00
|
|
|
data about a class. We can add to the
|
2020-02-15 20:01:48 +00:00
|
|
|
compilers data arbitrary items, either string or
|
|
|
|
or int or F64 (if you typecast).
|
|
|
|
|
|
|
|
This stuff is not high performance.
|
|
|
|
Don't get carried away -- it might be slow.
|
|
|
|
*/
|
|
|
|
|
|
|
|
U0 RankOut(I64 i)
|
|
|
|
{
|
2020-12-23 23:27:18 +00:00
|
|
|
" %z", i, "Cadet\0Ensign\0Captain\0Admiral\0President\0";
|
2020-02-15 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Test1Struct
|
|
|
|
{
|
2020-12-23 23:27:18 +00:00
|
|
|
I64 age print_str "%2d" default_val 38;
|
2020-02-20 23:40:10 +00:00
|
|
|
I64 color default_val RED; //Accepts expressions
|
2020-12-23 23:27:18 +00:00
|
|
|
I64 rank print_str "%1d" default_val 6/2 output_fun &RankOut;
|
2020-02-15 20:01:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Test2Struct
|
|
|
|
{
|
2020-12-23 23:27:18 +00:00
|
|
|
I64 age print_str "%2d" default_val 38 percentile 54.20;
|
|
|
|
I64 rank print_str "%1d" default_val 5;
|
|
|
|
I64 serial_num print_str "%6d" default_val 123456;
|
2020-02-15 20:01:48 +00:00
|
|
|
};
|
|
|
|
|
2020-12-23 23:27:18 +00:00
|
|
|
U0 DumpStruct(U8 *_d, U8 *class_name=lastclass)
|
2020-02-20 23:40:10 +00:00
|
|
|
{//lastclass is keyword. See $LK,"::/Demo/LastClass.CC"$.
|
2020-12-23 23:27:18 +00:00
|
|
|
CHashClass *tmpc = HashFind(class_name, Fs->hash_table, HTT_CLASS);
|
|
|
|
U8 *print_str;
|
|
|
|
I64 *q, default_val;
|
|
|
|
U0 (*fp_output_fun)(I64 i);
|
|
|
|
F64 percentile;
|
|
|
|
if (!tmpc)
|
|
|
|
return;
|
2020-02-20 23:40:10 +00:00
|
|
|
CMemberList *ml;
|
2020-12-23 23:27:18 +00:00
|
|
|
|
|
|
|
ml = tmpc->member_list_and_root;
|
|
|
|
while (ml)
|
|
|
|
{
|
|
|
|
"%s:", ml->str;
|
2020-02-15 20:01:48 +00:00
|
|
|
|
2020-02-20 23:40:10 +00:00
|
|
|
//All our items are I64's. If you want, you can check
|
Rename abs_addres to abs_address.
Update documentation/comments to rename addr, fun, var, stmt, blk, desc, reg, seg, ptr, dup, clus, val, and bttn, to address, function, variable, statement, block, description, register, segment, pointer, duplicate, cluster, value, and button, respectively.
2021-10-07 02:35:32 +01:00
|
|
|
//the data type of the member variable. See $LK,"ClassRep",A="MN:ClassRep"$().
|
2020-12-23 23:27:18 +00:00
|
|
|
q = _d + ml->offset;
|
2020-02-15 20:01:48 +00:00
|
|
|
|
2020-12-23 23:27:18 +00:00
|
|
|
if (print_str = MemberMetaData("print_str", ml))
|
|
|
|
"" print_str, *q;
|
2020-02-15 20:01:48 +00:00
|
|
|
|
2020-02-20 23:40:10 +00:00
|
|
|
//This is slightly ambiguous -- if no meta is present it will return zero.
|
2020-12-23 23:27:18 +00:00
|
|
|
if (default_val = MemberMetaData("default_val", ml))
|
|
|
|
" default:%d", default_val;
|
2020-02-15 20:01:48 +00:00
|
|
|
|
2020-02-20 23:40:10 +00:00
|
|
|
//This corrects for the ambiguity, allowing zero percentile.
|
2020-12-23 23:27:18 +00:00
|
|
|
if (MemberMetaFind("percentile", ml))
|
|
|
|
{//check if it exists
|
|
|
|
//We could use the $LK,"CMemberListMeta",A="MN:CMemberListMeta"$ structure returned by
|
2020-02-20 23:40:10 +00:00
|
|
|
//$LK,"MemberMetaFind",A="MN:MemberMetaFind"$() and save a search.
|
2020-12-23 23:27:18 +00:00
|
|
|
percentile = MemberMetaData("percentile", ml)(F64);
|
|
|
|
" percentile: %5.2f", percentile;
|
2020-02-20 23:40:10 +00:00
|
|
|
}
|
2020-02-15 20:01:48 +00:00
|
|
|
|
2020-12-23 23:27:18 +00:00
|
|
|
if (fp_output_fun = MemberMetaData("output_fun", ml))
|
2020-02-20 23:40:10 +00:00
|
|
|
(*fp_output_fun)(*q);
|
|
|
|
'\n';
|
2020-12-23 23:27:18 +00:00
|
|
|
ml = ml->next;
|
2020-02-20 23:40:10 +00:00
|
|
|
}
|
2020-02-15 20:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Test1Struct t1;
|
2020-12-23 23:27:18 +00:00
|
|
|
t1.age = 44;
|
|
|
|
t1.rank = 3;
|
2020-02-15 20:01:48 +00:00
|
|
|
|
|
|
|
DumpStruct(&t1);
|
|
|
|
|
|
|
|
Test2Struct t2;
|
2020-12-23 23:27:18 +00:00
|
|
|
t2.age = 22;
|
|
|
|
t2.rank = 2;
|
|
|
|
t2.serial_num = 55555;
|
2020-02-15 20:01:48 +00:00
|
|
|
|
|
|
|
DumpStruct(&t2);
|