Skip to content

Commit

Permalink
Deploy preview for PR 608 🛫
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 28, 2023
1 parent 5f4168d commit c12e005
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions pr-preview/pr-608/examples-core-reflection-traits-fields.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,25 @@ <h1>
<span class="k">struct</span><span class="w"> </span><span class="nc">Person</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">CUBOS_REFLECT</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">age</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int32_t</span><span class="w"> </span><span class="n">age</span><span class="p">;</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">weight</span><span class="p">;</span>
<span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="n">dead</span><span class="p">;</span>
<span class="p">};</span></pre><p>In its reflection definition, we&#x27;ll add the <a href="classcubos_1_1core_1_1reflection_1_1FieldsTrait.html" class="m-doc">FieldsTrait</a> trait to it with each of the fields we want to expose:</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;cubos/core/reflection/traits/fields.hpp&gt;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;cubos/core/reflection/type.hpp&gt;</span>

<span class="c1">// Since we&#39;re exposing fields of primitive types (int and float), its important to include the</span>
<span class="c1">// header which defines their reflection.</span>
<span class="c1">// Since we&#39;re exposing fields of primitive types (int32_t, float and bool), its important to</span>
<span class="c1">// include the header which defines their reflection.</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;cubos/core/reflection/external/primitives.hpp&gt;</span>

<span class="k">using</span><span class="w"> </span><span class="n">cubos</span><span class="o">::</span><span class="n">core</span><span class="o">::</span><span class="n">reflection</span><span class="o">::</span><span class="n">FieldsTrait</span><span class="p">;</span>
<span class="k">using</span><span class="w"> </span><span class="n">cubos</span><span class="o">::</span><span class="n">core</span><span class="o">::</span><span class="n">reflection</span><span class="o">::</span><span class="n">Type</span><span class="p">;</span>

<span class="n">CUBOS_REFLECT_IMPL</span><span class="p">(</span><span class="n">Person</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">Type</span><span class="o">::</span><span class="n">create</span><span class="p">(</span><span class="s">&quot;Person&quot;</span><span class="p">).</span><span class="n">with</span><span class="p">(</span>
<span class="w"> </span><span class="n">FieldsTrait</span><span class="p">().</span><span class="n">withField</span><span class="p">(</span><span class="s">&quot;age&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Person</span><span class="o">::</span><span class="n">age</span><span class="p">).</span><span class="n">withField</span><span class="p">(</span><span class="s">&quot;weight&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Person</span><span class="o">::</span><span class="n">weight</span><span class="p">));</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">Type</span><span class="o">::</span><span class="n">create</span><span class="p">(</span><span class="s">&quot;Person&quot;</span><span class="p">).</span><span class="n">with</span><span class="p">(</span><span class="n">FieldsTrait</span><span class="p">()</span>
<span class="w"> </span><span class="p">.</span><span class="n">withField</span><span class="p">(</span><span class="s">&quot;age&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Person</span><span class="o">::</span><span class="n">age</span><span class="p">)</span>
<span class="w"> </span><span class="p">.</span><span class="n">withField</span><span class="p">(</span><span class="s">&quot;weight&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Person</span><span class="o">::</span><span class="n">weight</span><span class="p">)</span>
<span class="w"> </span><span class="p">.</span><span class="n">withField</span><span class="p">(</span><span class="s">&quot;dead&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Person</span><span class="o">::</span><span class="n">dead</span><span class="p">));</span>
<span class="p">}</span></pre><aside class="m-note m-info"><h4>Note</h4><p>Make you sure you include the reflection declarations for the types of the fields you expose! In this case, we need the <a href="primitives_8hpp.html" class="m-doc">core/<wbr />reflection/<wbr />external/<wbr />primitives.hpp</a> header.</p></aside><p>Accessing this trait is the same as with any other trait:</p><pre class="m-code"><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">using</span><span class="w"> </span><span class="n">cubos</span><span class="o">::</span><span class="n">core</span><span class="o">::</span><span class="n">reflection</span><span class="o">::</span><span class="n">reflect</span><span class="p">;</span>
Expand All @@ -80,10 +83,30 @@ <h1>
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">auto</span><span class="o">&amp;</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">personType</span><span class="p">.</span><span class="n">get</span><span class="o">&lt;</span><span class="n">FieldsTrait</span><span class="o">&gt;</span><span class="p">();</span></pre><p>We can iterate over the fields of the type with it:</p><pre class="m-code"><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="k">auto</span><span class="o">&amp;</span><span class="w"> </span><span class="n">field</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">fields</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CUBOS_INFO</span><span class="p">(</span><span class="s">&quot;Field &#39;{}&#39; of type &#39;{}&#39;&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="p">.</span><span class="n">name</span><span class="p">(),</span><span class="w"> </span><span class="n">field</span><span class="p">.</span><span class="n">type</span><span class="p">().</span><span class="n">name</span><span class="p">());</span>
<span class="w"> </span><span class="p">}</span></pre><p>Its also possible to access the fields by name:</p><pre class="m-code"><span class="w"> </span><span class="n">Person</span><span class="w"> </span><span class="n">person</span><span class="p">{.</span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">21</span><span class="p">,</span><span class="w"> </span><span class="p">.</span><span class="n">weight</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">68.4F</span><span class="p">};</span>
<span class="w"> </span><span class="o">*</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="kt">float</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">fields</span><span class="p">.</span><span class="n">view</span><span class="p">(</span><span class="o">&amp;</span><span class="n">person</span><span class="p">).</span><span class="n">get</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">.</span><span class="n">field</span><span class="p">(</span><span class="s">&quot;weight&quot;</span><span class="p">)))</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mf">20.0F</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span></pre><p>This should output:</p><pre class="m-code"><span class="w"> </span><span class="c1">// Field &#39;age&#39; of type &#39;int32_t&#39;</span>
<span class="w"> </span><span class="c1">// Field &#39;weight&#39; of type &#39;float&#39;</span>
<span class="w"> </span><span class="c1">// Field &#39;dead&#39; of type &#39;bool&#39;</span></pre><p>Its also possible to access the fields of an instance of the type, and iterate over them:</p><pre class="m-code"><span class="w"> </span><span class="n">Person</span><span class="w"> </span><span class="n">person</span><span class="p">{.</span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">21</span><span class="p">,</span><span class="w"> </span><span class="p">.</span><span class="n">weight</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">68.4F</span><span class="p">,</span><span class="w"> </span><span class="p">.</span><span class="n">dead</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">false</span><span class="p">};</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">view</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fields</span><span class="p">.</span><span class="n">view</span><span class="p">(</span><span class="o">&amp;</span><span class="n">person</span><span class="p">);</span>

<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="p">[</span><span class="n">field</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">]</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">view</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">field</span><span class="o">-&gt;</span><span class="n">type</span><span class="p">().</span><span class="n">is</span><span class="o">&lt;</span><span class="kt">int32_t</span><span class="o">&gt;</span><span class="p">())</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CUBOS_INFO</span><span class="p">(</span><span class="s">&quot;Field &#39;{}&#39;: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">(),</span><span class="w"> </span><span class="o">*</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="kt">int32_t</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">value</span><span class="p">));</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">field</span><span class="o">-&gt;</span><span class="n">type</span><span class="p">().</span><span class="n">is</span><span class="o">&lt;</span><span class="kt">float</span><span class="o">&gt;</span><span class="p">())</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CUBOS_INFO</span><span class="p">(</span><span class="s">&quot;Field &#39;{}&#39;: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">(),</span><span class="w"> </span><span class="o">*</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="kt">float</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">value</span><span class="p">));</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CUBOS_INFO</span><span class="p">(</span><span class="s">&quot;Field &#39;{}&#39;: unsupported type &#39;{}&#39;&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">field</span><span class="o">-&gt;</span><span class="n">name</span><span class="p">(),</span><span class="w"> </span><span class="n">field</span><span class="o">-&gt;</span><span class="n">type</span><span class="p">().</span><span class="n">name</span><span class="p">());</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span></pre><pre class="m-code"><span class="w"> </span><span class="c1">// Field &#39;age&#39;: 21</span>
<span class="w"> </span><span class="c1">// Field &#39;weight&#39;: 68.4</span>
<span class="w"> </span><span class="c1">// Field &#39;dead&#39;: unsupported type &#39;bool&#39;</span></pre><p>Its also possible to access the fields by name:</p><pre class="m-code"><span class="w"> </span><span class="o">*</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="kt">float</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">view</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">.</span><span class="n">field</span><span class="p">(</span><span class="s">&quot;weight&quot;</span><span class="p">)))</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mf">20.0F</span><span class="p">;</span>
<span class="w"> </span><span class="n">CUBOS_INFO</span><span class="p">(</span><span class="s">&quot;New weight: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">person</span><span class="p">.</span><span class="n">weight</span><span class="p">);</span><span class="w"> </span><span class="c1">// 88.4</span>
<span class="p">}</span></pre>
<span class="p">}</span></pre><pre class="m-code"><span class="c1">// New weight: 88.4</span></pre>
</div>
</div>
</div>
Expand Down

0 comments on commit c12e005

Please sign in to comment.