From 3f7c764ea167d444b4feed7f3652d5c61938cb76 Mon Sep 17 00:00:00 2001 From: RiscadoA Date: Sun, 10 Sep 2023 12:01:33 +0000 Subject: [PATCH] =?UTF-8?q?Deploy=20preview=20for=20PR=20487=20?= =?UTF-8?q?=F0=9F=9B=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pr-preview/pr-487/examples-core-reflection-basic.html | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pr-preview/pr-487/examples-core-reflection-basic.html b/pr-preview/pr-487/examples-core-reflection-basic.html index 06779ae67..725c7a9e5 100644 --- a/pr-preview/pr-487/examples-core-reflection-basic.html +++ b/pr-preview/pr-487/examples-core-reflection-basic.html @@ -49,11 +49,11 @@

Reflection

Using the reflection system.

-

Lets say you have a type Person, which you want to be able to reflect. You can declare it as reflectable using the macro CUBOS_REFLECT, for example, in your header, like this:

#include <cubos/core/reflection/reflect.hpp>
+

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.

Lets say you have a type Person, which you want to get information of at runtime. You can declare it as reflectable using the macro CUBOS_REFLECT, for example, in your header, like this:

#include <cubos/core/reflection/reflect.hpp>
 
 struct Person
 {
-    CUBOS_REFLECT;
+    CUBOS_REFLECT; // The important part!
     int age;
     float weight;
 };

The file core/reflection/reflect.hpp 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.

In your source file, you must define the reflection data for your type. This is done through the CUBOS_REFLECT_IMPL macro:

#include <cubos/core/reflection/type.hpp>
@@ -63,7 +63,7 @@ 

CUBOS_REFLECT_IMPL(Person) { return Type::create("Person"); -}

To access this reflection data, you should use the cubos::core::reflection::reflect function, which is also defined in the core/reflection/reflect.hpp header.

int main()
+}

To access this reflection data, you should use the cubos::core::reflection::reflect function, which is defined in the same header as the macros.

int main()
 {
     using cubos::core::reflection::reflect;
 
@@ -71,7 +71,7 @@ 

CUBOS_ASSERT(personType.name() == "Person");

Lets say you want to associate your own data to your types, to describe them further. For example, imagine you'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:

struct ColorTrait
 {
     float r, g, b;
-};

Now, when you define your type reflection, you add your trait with the Type::with method.

CUBOS_REFLECT_IMPL(Position)
+};

Now, when you implement your type's reflection function, you can add your trait with the Type::with method.

CUBOS_REFLECT_IMPL(Position)
 {
     return Type::create("Position").with(ColorTrait{.r = 0.0F, .g = 1.0F, .b = 0.0F});
 }

To check if a type has a trait, you use the Type::has method.

    const auto& positionType = reflect<Position>();
@@ -80,7 +80,6 @@ 

CUBOS_ASSERT(colorTrait.r == 0.0F); CUBOS_ASSERT(colorTrait.g == 1.0F); CUBOS_ASSERT(colorTrait.b == 0.0F); - return 0; }