diff --git a/core/include/cubos/core/reflection/type_registry.hpp b/core/include/cubos/core/reflection/type_registry.hpp index 824ee4286..e1674148c 100644 --- a/core/include/cubos/core/reflection/type_registry.hpp +++ b/core/include/cubos/core/reflection/type_registry.hpp @@ -16,6 +16,8 @@ namespace cubos::core::reflection class TypeRegistry final { public: + using Iterator = typename memory::UnorderedBimap::Iterator; + /// @brief Registers the given type. /// /// Does nothing if the type is already registered. @@ -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 @@ -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 mTypes{}; }; diff --git a/core/src/cubos/core/reflection/type_registry.cpp b/core/src/cubos/core/reflection/type_registry.cpp index 8104413ac..c86e88ff1 100644 --- a/core/src/cubos/core/reflection/type_registry.cpp +++ b/core/src/cubos/core/reflection/type_registry.cpp @@ -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); @@ -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(); +} diff --git a/core/tests/reflection/type_registry.cpp b/core/tests/reflection/type_registry.cpp index 9751a6a94..a02c92112 100644 --- a/core/tests/reflection/type_registry.cpp +++ b/core/tests/reflection/type_registry.cpp @@ -13,6 +13,7 @@ TEST_CASE("reflection::Type") CHECK_FALSE(registry.contains("int")); CHECK_FALSE(registry.contains()); CHECK(registry.size() == 0); + CHECK(registry.begin() == registry.end()); SUBCASE("single insert") { @@ -20,6 +21,14 @@ TEST_CASE("reflection::Type") CHECK(registry.size() == 1); } + SUBCASE("single insert other registry") + { + TypeRegistry other{}; + other.insert(); + registry.insert(other); + CHECK(registry.size() == 1); + } + SUBCASE("double insert") { registry.insert(); @@ -27,6 +36,15 @@ TEST_CASE("reflection::Type") CHECK(registry.size() == 1); } + SUBCASE("double insert other registry") + { + TypeRegistry other{}; + other.insert(); + registry.insert(other); + registry.insert(other); + CHECK(registry.size() == 1); + } + SUBCASE("with other types") { registry.insert(); @@ -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()); + } + REQUIRE(registry.contains("int")); CHECK(registry.at("int").is()); }