templeos-info/public/Wb/Demo/ClassMeta.HC.HTML

112 lines
8.6 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF2>/*Demonstrates class meta data.</span><span class=cF0>
<a name="l2"></a></span><span class=cF2>Basically, we make use of the compiler's</span><span class=cF0>
<a name="l3"></a></span><span class=cF2>data about a class. We can add to the</span><span class=cF0>
<a name="l4"></a></span><span class=cF2>compilers data arbitrary items, either string or</span><span class=cF0>
<a name="l5"></a></span><span class=cF2>or int or F64 (if you typecast).</span><span class=cF0>
<a name="l6"></a>
<a name="l7"></a></span><span class=cF2>This stuff is not high performance.</span><span class=cF0>
<a name="l8"></a></span><span class=cF2>Don't get carried away -- it might be slow.</span><span class=cF0>
<a name="l9"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l10"></a>
<a name="l11"></a></span><span class=cF1>U0</span><span class=cF0> RankOut(</span><span class=cF9>I64</span><span class=cF0> i)
<a name="l12"></a>{
<a name="l13"></a> </span><span class=cF6>&quot; %z&quot;</span><span class=cF0>,i,</span><span class=cF6>&quot;Cadet\0Ensign\0Captain\0Admiral\0President\0&quot;</span><span class=cF0>;
<a name="l14"></a>}
<a name="l15"></a>
<a name="l16"></a></span><span class=cF1>class</span><span class=cF0> Test1Struct
<a name="l17"></a>{
<a name="l18"></a> </span><span class=cF9>I64</span><span class=cF0> age print_str </span><span class=cF6>&quot;%2d&quot;</span><span class=cF0> dft_val 38;
<a name="l19"></a> </span><span class=cF9>I64</span><span class=cF0> color dft_val </span><span class=cF3>RED</span><span class=cF0>; </span><span class=cF2>//Accepts expressions</span><span class=cF0>
<a name="l20"></a> </span><span class=cF9>I64</span><span class=cF0> rank print_str </span><span class=cF6>&quot;%1d&quot;</span><span class=cF0> dft_val 6/2 output_fun &amp;RankOut;
<a name="l21"></a>};
<a name="l22"></a>
<a name="l23"></a></span><span class=cF1>class</span><span class=cF0> Test2Struct
<a name="l24"></a>{
<a name="l25"></a> </span><span class=cF9>I64</span><span class=cF0> age print_str </span><span class=cF6>&quot;%2d&quot;</span><span class=cF0> dft_val 38 percentile 54.20;
<a name="l26"></a> </span><span class=cF9>I64</span><span class=cF0> rank print_str </span><span class=cF6>&quot;%1d&quot;</span><span class=cF0> dft_val 5;
<a name="l27"></a> </span><span class=cF9>I64</span><span class=cF0> serial_num print_str </span><span class=cF6>&quot;%6d&quot;</span><span class=cF0> dft_val 123456;
<a name="l28"></a>};
<a name="l29"></a>
<a name="l30"></a></span><span class=cF1>U0</span><span class=cF0> DumpStruct(</span><span class=cF1>U8</span><span class=cF0> *_d,</span><span class=cF1>U8</span><span class=cF0> *class_name=</span><span class=cF1>lastclass</span><span class=cF0>)
<a name="l31"></a>{</span><span class=cF2>//lastclass is keyword. See </span><a href="/Wb/Demo/LastClass.HC.HTML#l1"><span class=cF4>::/Demo/LastClass.HC</span></a><span class=cF2>.</span><span class=cF0>
<a name="l32"></a> </span><span class=cF9>CHashClass</span><span class=cF0> *tmpc=</span><span class=cF5>HashFind</span><span class=cF0>(class_name,</span><span class=cF5>Fs</span><span class=cF0>-&gt;hash_table,</span><span class=cF3>HTT_CLASS</span><span class=cF0>);
<a name="l33"></a> </span><span class=cF1>U8</span><span class=cF0> *print_str;
<a name="l34"></a> </span><span class=cF9>I64</span><span class=cF0> *q,dft_val;
<a name="l35"></a> </span><span class=cF1>U0</span><span class=cF0> (* fp_output_fun)(</span><span class=cF9>I64</span><span class=cF0> i);
<a name="l36"></a> </span><span class=cF1>F64</span><span class=cF0> percentile;
<a name="l37"></a> </span><span class=cF1>if</span><span class=cF0> (!tmpc) </span><span class=cF1>return</span><span class=cF0>;
<a name="l38"></a> </span><span class=cF9>CMemberLst</span><span class=cF0> *ml;
<a name="l39"></a> ml=tmpc-&gt;member_lst_and_root;
<a name="l40"></a> </span><span class=cF1>while</span><span class=cF0> (ml) </span><span class=cF7>{</span><span class=cF0>
<a name="l41"></a> </span><span class=cF6>&quot;%s:&quot;</span><span class=cF0>,ml-&gt;str;
<a name="l42"></a>
<a name="l43"></a> </span><span class=cF2>//All our items are I64's.</span><span class=cF0> </span><span class=cF2>If you want, you can check</span><span class=cF0>
<a name="l44"></a> </span><span class=cF2>//the data type of the member var. See </span><a href="/Wb/Adam/ADbg.HC.HTML#l161"><span class=cF4>ClassRep</span></a><span class=cF2>().</span><span class=cF0>
<a name="l45"></a> q=_d+ml-&gt;</span><span class=cF1>offset</span><span class=cF0>;
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF1>if</span><span class=cF0> (print_str=</span><span class=cF5>MemberMetaData</span><span class=cF7>(</span><span class=cF6>&quot;print_str&quot;</span><span class=cF0>,ml</span><span class=cF7>)</span><span class=cF0>)
<a name="l48"></a> </span><span class=cF6>&quot;&quot;</span><span class=cF0> print_str,*q;
<a name="l49"></a>
<a name="l50"></a> </span><span class=cF2>//This is slightly ambiguous -- if no meta is present it will return zero.</span><span class=cF0>
<a name="l51"></a> </span><span class=cF1>if</span><span class=cF0> (dft_val=</span><span class=cF5>MemberMetaData</span><span class=cF7>(</span><span class=cF6>&quot;dft_val&quot;</span><span class=cF0>,ml</span><span class=cF7>)</span><span class=cF0>)
<a name="l52"></a> </span><span class=cF6>&quot; default:%d&quot;</span><span class=cF0>,dft_val;
<a name="l53"></a>
<a name="l54"></a> </span><span class=cF2>//This corrects for the ambiguity, allowing zero percentile.</span><span class=cF0>
<a name="l55"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>MemberMetaFind</span><span class=cF7>(</span><span class=cF6>&quot;percentile&quot;</span><span class=cF0>,ml</span><span class=cF7>)</span><span class=cF0>) {</span><span class=cF2>//check if it exists</span><span class=cF0>
<a name="l56"></a></span><span class=cF2>//We could use the </span><a href="/Wb/Kernel/KernelA.HH.HTML#l793"><span class=cF4>CMemberLstMeta</span></a><span class=cF2> structure returned by</span><span class=cF0>
<a name="l57"></a> </span><span class=cF2>//</span><a href="/Wb/Compiler/LexLib.HC.HTML#l56"><span class=cF4>MemberMetaFind</span></a><span class=cF2>() and save a search.</span><span class=cF0>
<a name="l58"></a> percentile=</span><span class=cF5>MemberMetaData</span><span class=cF0>(</span><span class=cF6>&quot;percentile&quot;</span><span class=cF0>,ml)(</span><span class=cF1>F64</span><span class=cF0>);
<a name="l59"></a> </span><span class=cF6>&quot; percentile: %5.2f&quot;</span><span class=cF0>,percentile;
<a name="l60"></a> }
<a name="l61"></a>
<a name="l62"></a> </span><span class=cF1>if</span><span class=cF0> (fp_output_fun=</span><span class=cF5>MemberMetaData</span><span class=cF7>(</span><span class=cF6>&quot;output_fun&quot;</span><span class=cF0>,ml</span><span class=cF7>)</span><span class=cF0>)
<a name="l63"></a> (*fp_output_fun)(*q);
<a name="l64"></a> </span><span class=cF6>'\n'</span><span class=cF0>;
<a name="l65"></a> ml=ml-&gt;next;
<a name="l66"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l67"></a>}
<a name="l68"></a>
<a name="l69"></a>Test1Struct t1;
<a name="l70"></a>t1.age=44;
<a name="l71"></a>t1.rank=3;
<a name="l72"></a>
<a name="l73"></a>DumpStruct(&amp;t1);
<a name="l74"></a>
<a name="l75"></a>Test2Struct t2;
<a name="l76"></a>t2.age=22;
<a name="l77"></a>t2.rank=2;
<a name="l78"></a>t2.serial_num=55555;
<a name="l79"></a>
<a name="l80"></a>DumpStruct(&amp;t2);
</span></pre></body>
</html>