diff --git a/core/samples/reflection/traits/fields/main.cpp b/core/samples/reflection/traits/fields/main.cpp index 7bec7efcc..f6217f411 100644 --- a/core/samples/reflection/traits/fields/main.cpp +++ b/core/samples/reflection/traits/fields/main.cpp @@ -6,8 +6,9 @@ struct Person { CUBOS_REFLECT; - int age; + int32_t age; float weight; + bool dead; }; /// [Person declaration] @@ -15,8 +16,8 @@ struct Person #include #include -// 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 using cubos::core::reflection::FieldsTrait; @@ -24,8 +25,10 @@ using cubos::core::reflection::Type; 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)); } /// [Reflection definition] @@ -39,6 +42,7 @@ int main() const auto& fields = personType.get(); /// [Accessing the trait] + CUBOS_INFO("--- Accessing the fields of a type ---"); /// [Iterating over fields] for (const auto& field : fields) { @@ -46,9 +50,47 @@ int main() } /// [Iterating over fields] + /// [Iterating over fields output] + // Field 'age' of type 'int32_t' + // Field 'weight' of type 'float' + // Field 'dead' of type 'bool' + /// [Iterating over fields output] + + CUBOS_INFO("--- Accessing the fields of an instance ---"); + /// [Iterating over fields with data] + Person person{.age = 21, .weight = 68.4F, .dead = false}; + auto view = fields.view(&person); + + for (auto [field, value] : view) + { + if (field->type().is()) + { + CUBOS_INFO("Field '{}': {}", field->name(), *static_cast(value)); + } + else if (field->type().is()) + { + CUBOS_INFO("Field '{}': {}", field->name(), *static_cast(value)); + } + else + { + CUBOS_INFO("Field '{}': unsupported type '{}'", field->name(), field->type().name()); + } + } + /// [Iterating over fields with data] + + /// [Iterating over fields with data output] + // Field 'age': 21 + // Field 'weight': 68.4 + // Field 'dead': unsupported type 'bool' + /// [Iterating over fields with data output] + + CUBOS_INFO("--- Accessing a specific field of an instance ---"); /// [Accessing fields by name] - Person person{.age = 21, .weight = 68.4F}; - *static_cast(fields.view(&person).get(*fields.field("weight"))) += 20.0F; + *static_cast(view.get(*fields.field("weight"))) += 20.0F; CUBOS_INFO("New weight: {}", person.weight); // 88.4 } /// [Accessing fields by name] + +/// [Accessing fields by name output] +// New weight: 88.4 +/// [Accessing fields by name output] diff --git a/core/samples/reflection/traits/fields/page.md b/core/samples/reflection/traits/fields/page.md index 1c3a12891..4ec631286 100644 --- a/core/samples/reflection/traits/fields/page.md +++ b/core/samples/reflection/traits/fields/page.md @@ -26,6 +26,18 @@ We can iterate over the fields of the type with it: @snippet reflection/traits/fields/main.cpp Iterating over fields +This should output: + +@snippet reflection/traits/fields/main.cpp Iterating over fields output + +Its also possible to access the fields of an instance of the type, and iterate +over them: + +@snippet reflection/traits/fields/main.cpp Iterating over fields with data +@snippet reflection/traits/fields/main.cpp Iterating over fields with data output + Its also possible to access the fields by name: @snippet reflection/traits/fields/main.cpp Accessing fields by name +@snippet reflection/traits/fields/main.cpp Accessing fields by name output +