Skip to content

Commit

Permalink
Deploy preview for PR 487 🛫
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 10, 2023
1 parent a95233b commit 3f7c764
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pr-preview/pr-487/examples-core-reflection-basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ <h1>
Reflection
</h1>
<p>Using the reflection system.</p>
<p><a name="md_reflection_basic_page"></a></p><p>Lets say you have a type <code><a href="structPerson.html" class="m-doc">Person</a></code>, which you want to be able to reflect. You can declare it as reflectable using the macro <a href="group__core-reflection.html#gac3d484b311cc3e471620653b289977ac" class="m-doc">CUBOS_<wbr />REFLECT</a>, for example, in your header, like this:</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;cubos/core/reflection/reflect.hpp&gt;</span>
<p><a name="md_reflection_basic_page"></a></p><p>The reflection system allows you to access type information at runtime, such as the name of the type, its fields and other traits. In fact, you can even add your own custom traits to your type. This is very useful for stuff like debugging, serialization, GUI editors, and so on.</p><p>Lets say you have a type <code><a href="structPerson.html" class="m-doc">Person</a></code>, which you want to get information of at runtime. You can declare it as reflectable using the macro <a href="group__core-reflection.html#gac3d484b311cc3e471620653b289977ac" class="m-doc">CUBOS_<wbr />REFLECT</a>, for example, in your header, like this:</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;cubos/core/reflection/reflect.hpp&gt;</span>

<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="n">CUBOS_REFLECT</span><span class="p">;</span><span class="w"> </span><span class="c1">// The important part!</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">float</span><span class="w"> </span><span class="n">weight</span><span class="p">;</span>
<span class="p">};</span></pre><p>The file <a href="reflect_8hpp.html" class="m-doc">core/<wbr />reflection/<wbr />reflect.hpp</a> is a very lightweight header which you should include when declaring types as reflectable. It only defines the reflection macros and the reflection function. Avoid including other unnecessary reflection headers, which might be heavier, in order to reduce compile times.</p><p>In your source file, you must define the reflection data for your type. This is done through the <a href="core_2samples_2reflection_2basic_2main_8cpp.html#a9d30a937e2ad48db6e0d423350e7ba2c" class="m-doc">CUBOS_<wbr />REFLECT_<wbr />IMPL</a> macro:</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;cubos/core/reflection/type.hpp&gt;</span>
Expand All @@ -63,15 +63,15 @@ <h1>
<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="p">}</span></pre><p>To access this reflection data, you should use the <a href="group__core-reflection.html#ga8eacce93bfd11e459da56437199fe36f" class="m-doc">cubos::<wbr />core::<wbr />reflection::<wbr />reflect</a> function, which is also defined in the <a href="reflect_8hpp.html" class="m-doc">core/<wbr />reflection/<wbr />reflect.hpp</a> header.</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></pre><p>To access this reflection data, you should use the <a href="group__core-reflection.html#ga8eacce93bfd11e459da56437199fe36f" class="m-doc">cubos::<wbr />core::<wbr />reflection::<wbr />reflect</a> function, which is defined in the same header as the macros.</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>

<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">personType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">reflect</span><span class="o">&lt;</span><span class="n">Person</span><span class="o">&gt;</span><span class="p">();</span>
<span class="w"> </span><span class="n">CUBOS_ASSERT</span><span class="p">(</span><span class="n">personType</span><span class="p">.</span><span class="n">name</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">&quot;Person&quot;</span><span class="p">);</span></pre><p>Lets say you want to associate your own data to your types, to describe them further. For example, imagine you&#x27;re making a GUI editor for your game and you which to display the fields of your types in a tree view, with different colors for different types. You could associate colors to your types by defining a trait:</p><pre class="m-code"><span class="k">struct</span><span class="w"> </span><span class="nc">ColorTrait</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">g</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="p">};</span></pre><p>Now, when you define your type reflection, you add your trait with the <a href="classcubos_1_1core_1_1reflection_1_1Type.html#a1c955d63fb82c71b7a9db67dcf134779" class="m-doc">Type::<wbr />with</a> method.</p><pre class="m-code"><span class="n">CUBOS_REFLECT_IMPL</span><span class="p">(</span><span class="n">Position</span><span class="p">)</span>
<span class="p">};</span></pre><p>Now, when you implement your type&#x27;s reflection function, you can add your trait with the <a href="classcubos_1_1core_1_1reflection_1_1Type.html#a1c955d63fb82c71b7a9db67dcf134779" class="m-doc">Type::<wbr />with</a> method.</p><pre class="m-code"><span class="n">CUBOS_REFLECT_IMPL</span><span class="p">(</span><span class="n">Position</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;Position&quot;</span><span class="p">).</span><span class="n">with</span><span class="p">(</span><span class="n">ColorTrait</span><span class="p">{.</span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0F</span><span class="p">,</span><span class="w"> </span><span class="p">.</span><span class="n">g</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1.0F</span><span class="p">,</span><span class="w"> </span><span class="p">.</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0F</span><span class="p">});</span>
<span class="p">}</span></pre><p>To check if a type has a trait, you use the <a href="classcubos_1_1core_1_1reflection_1_1Type.html#a18cf563593854a28f1f22e31d1fec65c" class="m-doc">Type::<wbr />has</a> method.</p><pre class="m-code"><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">positionType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">reflect</span><span class="o">&lt;</span><span class="n">Position</span><span class="o">&gt;</span><span class="p">();</span>
Expand All @@ -80,7 +80,6 @@ <h1>
<span class="w"> </span><span class="n">CUBOS_ASSERT</span><span class="p">(</span><span class="n">colorTrait</span><span class="p">.</span><span class="n">r</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">0.0F</span><span class="p">);</span>
<span class="w"> </span><span class="n">CUBOS_ASSERT</span><span class="p">(</span><span class="n">colorTrait</span><span class="p">.</span><span class="n">g</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">1.0F</span><span class="p">);</span>
<span class="w"> </span><span class="n">CUBOS_ASSERT</span><span class="p">(</span><span class="n">colorTrait</span><span class="p">.</span><span class="n">b</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mf">0.0F</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span></pre>
</div>
</div>
Expand Down

0 comments on commit 3f7c764

Please sign in to comment.