Skip to content

Commit

Permalink
docs(core): improve FieldsTrait sample
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 28, 2023
1 parent 922896d commit 9b9f2ef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
56 changes: 49 additions & 7 deletions core/samples/reflection/traits/fields/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
struct Person
{
CUBOS_REFLECT;
int age;
int32_t age;
float weight;
bool dead;
};
/// [Person declaration]

/// [Reflection definition]
#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;
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]

Expand All @@ -39,16 +42,55 @@ int main()
const auto& fields = personType.get<FieldsTrait>();
/// [Accessing the trait]

CUBOS_INFO("--- Accessing the fields of a type ---");
/// [Iterating over fields]
for (const auto& field : fields)
{
CUBOS_INFO("Field '{}' of type '{}'", field.name(), field.type().name());
}
/// [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<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());
}
}
/// [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<float*>(fields.view(&person).get(*fields.field("weight"))) += 20.0F;
*static_cast<float*>(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]
12 changes: 12 additions & 0 deletions core/samples/reflection/traits/fields/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9b9f2ef

Please sign in to comment.