From c12e005ace732974663c5a6f55b5fa120aefd79c Mon Sep 17 00:00:00 2001 From: RiscadoA Date: Thu, 28 Sep 2023 13:47:55 +0000 Subject: [PATCH] =?UTF-8?q?Deploy=20preview=20for=20PR=20608=20?= =?UTF-8?q?=F0=9F=9B=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...xamples-core-reflection-traits-fields.html | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/pr-preview/pr-608/examples-core-reflection-traits-fields.html b/pr-preview/pr-608/examples-core-reflection-traits-fields.html index fd00b8f5b..2d6fceb27 100644 --- a/pr-preview/pr-608/examples-core-reflection-traits-fields.html +++ b/pr-preview/pr-608/examples-core-reflection-traits-fields.html @@ -55,13 +55,14 @@

struct Person { CUBOS_REFLECT; - int age; + int32_t age; float weight; + bool dead; };

In its reflection definition, we'll add the FieldsTrait trait to it with each of the fields we want to expose:

#include <cubos/core/reflection/traits/fields.hpp>
 #include <cubos/core/reflection/type.hpp>
 
-// Since we're exposing fields of primitive types (int and float), its important to include the
-// header which defines their reflection.
+// Since we're exposing fields of primitive types (int32_t, float and bool), its important to
+// include the header which defines their reflection.
 #include <cubos/core/reflection/external/primitives.hpp>
 
 using cubos::core::reflection::FieldsTrait;
@@ -69,8 +70,10 @@ 

CUBOS_REFLECT_IMPL(Person) { - return Type::create("Person").with( - FieldsTrait().withField("age", &Person::age).withField("weight", &Person::weight)); + return Type::create("Person").with(FieldsTrait() + .withField("age", &Person::age) + .withField("weight", &Person::weight) + .withField("dead", &Person::dead)); }

Accessing this trait is the same as with any other trait:

int main()
 {
     using cubos::core::reflection::reflect;
@@ -80,10 +83,30 @@ 

const auto& fields = personType.get<FieldsTrait>();

We can iterate over the fields of the type with it:

    for (const auto& field : fields)
     {
         CUBOS_INFO("Field '{}' of type '{}'", field.name(), field.type().name());
-    }

Its also possible to access the fields by name:

    Person person{.age = 21, .weight = 68.4F};
-    *static_cast<float*>(fields.view(&person).get(*fields.field("weight"))) += 20.0F;
+    }

This should output:

    // Field 'age' of type 'int32_t'
+    // Field 'weight' of type 'float'
+    // Field 'dead' of type 'bool'

Its also possible to access the fields of an instance of the type, and iterate over them:

    Person person{.age = 21, .weight = 68.4F, .dead = false};
+    auto view = fields.view(&person);
+
+    for (auto [field, value] : view)
+    {
+        if (field->type().is<int32_t>())
+        {
+            CUBOS_INFO("Field '{}': {}", field->name(), *static_cast<int32_t*>(value));
+        }
+        else if (field->type().is<float>())
+        {
+            CUBOS_INFO("Field '{}': {}", field->name(), *static_cast<float*>(value));
+        }
+        else
+        {
+            CUBOS_INFO("Field '{}': unsupported type '{}'", field->name(), field->type().name());
+        }
+    }
    // Field 'age': 21
+    // Field 'weight': 68.4
+    // Field 'dead': unsupported type 'bool'

Its also possible to access the fields by name:

    *static_cast<float*>(view.get(*fields.field("weight"))) += 20.0F;
     CUBOS_INFO("New weight: {}", person.weight); // 88.4
-}
+}
// New weight: 88.4