Skip to content

Commit

Permalink
feat(reflection): add FieldsTrait::size
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Nov 12, 2023
1 parent 0b411f1 commit 1b5eb92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/include/cubos/core/reflection/traits/fields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ namespace cubos::core::reflection
/// @return Iterator.
static Iterator end();

/// @brief Returns how many fields there are in the trait.
/// @return Field count.
std::size_t size() const;

/// @brief Returns a view of the given object instance.
/// @param instance Object instance.
/// @return Object view.
Expand Down
11 changes: 11 additions & 0 deletions core/src/cubos/core/reflection/traits/fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ FieldsTrait::Iterator FieldsTrait::end()
return Iterator{nullptr};
}

std::size_t FieldsTrait::size() const
{
std::size_t i = 0;
for (const auto& field : *this)
{
(void)field;
i += 1;
}
return i;
}

FieldsTrait::View FieldsTrait::view(void* instance) const
{
return View{*this, instance};
Expand Down
3 changes: 3 additions & 0 deletions core/tests/reflection/traits/fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TEST_CASE("reflection::FieldsTrait")
auto fields = FieldsTrait();
CHECK(fields.field("foo") == nullptr);
CHECK(fields.begin() == fields.end());
CHECK(fields.size() == 0);
}

SUBCASE("single field")
Expand All @@ -39,6 +40,7 @@ TEST_CASE("reflection::FieldsTrait")
CHECK(field == &*fields.begin());
CHECK(fields.begin() != fields.end());
CHECK(++fields.begin() == fields.end());
CHECK(fields.size() == 1);

ObjectType object{};
CHECK(field->name() == "foo");
Expand Down Expand Up @@ -69,6 +71,7 @@ TEST_CASE("reflection::FieldsTrait")
REQUIRE(barField != nullptr);
CHECK(barField == &*(++fields.begin()));
CHECK(++(++fields.begin()) == fields.end());
CHECK(fields.size() == 2);

ObjectType object{};
CHECK(fooField->name() == "foo");
Expand Down

0 comments on commit 1b5eb92

Please sign in to comment.