Skip to content

Commit

Permalink
feat(reflection): add TypeRegistry::Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Nov 26, 2023
1 parent 1e7929e commit 81faddb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/include/cubos/core/reflection/type_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace cubos::core::reflection
class TypeRegistry final
{
public:
using Iterator = typename memory::UnorderedBimap<const Type*, std::string>::Iterator;

/// @brief Registers the given type.
///
/// Does nothing if the type is already registered.
Expand All @@ -24,6 +26,10 @@ namespace cubos::core::reflection
/// @param type Type to register.
void insert(const Type& type);

/// @brief Calls insert for each type in the given type registry.
/// @param other Type registry to copy types from.
void insert(const TypeRegistry& other);

/// @copydoc insert(const Type&)
/// @tparam T Type to register.
template <Reflectable T>
Expand Down Expand Up @@ -62,6 +68,14 @@ namespace cubos::core::reflection
/// @return Number of registered types.
std::size_t size() const;

/// @brief Gets an iterator to the beginning of the registry.
/// @return Iterator.
Iterator begin() const;

/// @brief Gets an iterator to the end of the registry.
/// @return Iterator.
Iterator end() const;

private:
memory::UnorderedBimap<const Type*, std::string> mTypes{};
};
Expand Down
18 changes: 18 additions & 0 deletions core/src/cubos/core/reflection/type_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ void TypeRegistry::insert(const Type& type)
mTypes.insert(&type, type.name());
}

void TypeRegistry::insert(const TypeRegistry& other)
{
for (const auto& [type, _] : other)
{
this->insert(*type);
}
}

bool TypeRegistry::contains(const Type& type) const
{
return mTypes.containsLeft(&type);
Expand All @@ -35,3 +43,13 @@ std::size_t TypeRegistry::size() const
{
return mTypes.size();
}

auto TypeRegistry::begin() const -> Iterator
{
return mTypes.begin();
}

auto TypeRegistry::end() const -> Iterator
{
return mTypes.end();
}
25 changes: 25 additions & 0 deletions core/tests/reflection/type_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,38 @@ TEST_CASE("reflection::Type")
CHECK_FALSE(registry.contains("int"));
CHECK_FALSE(registry.contains<int>());
CHECK(registry.size() == 0);
CHECK(registry.begin() == registry.end());

SUBCASE("single insert")
{
registry.insert<int>();
CHECK(registry.size() == 1);
}

SUBCASE("single insert other registry")
{
TypeRegistry other{};
other.insert<int>();
registry.insert(other);
CHECK(registry.size() == 1);
}

SUBCASE("double insert")
{
registry.insert<int>();
registry.insert<int>();
CHECK(registry.size() == 1);
}

SUBCASE("double insert other registry")
{
TypeRegistry other{};
other.insert<int>();
registry.insert(other);
registry.insert(other);
CHECK(registry.size() == 1);
}

SUBCASE("with other types")
{
registry.insert<short>();
Expand All @@ -37,6 +55,13 @@ TEST_CASE("reflection::Type")
CHECK(registry.size() == 5);
}

if (registry.size() == 1)
{
CHECK(registry.begin() != registry.end());
CHECK(++registry.begin() == registry.end());
CHECK(registry.begin()->first->is<int>());
}

REQUIRE(registry.contains("int"));
CHECK(registry.at("int").is<int>());
}

0 comments on commit 81faddb

Please sign in to comment.