diff --git a/src/Library/BaseLibrary.hpp b/src/Library/BaseLibrary.hpp index c07f0e3..57d889e 100644 --- a/src/Library/BaseLibrary.hpp +++ b/src/Library/BaseLibrary.hpp @@ -71,6 +71,19 @@ namespace CreatureLib::Library { return _values; } + using const_iterator = typename std::unordered_map>::const_iterator; + + inline const_iterator begin() const { + return reinterpret_cast>&>( + _values.GetStdMap()) + .begin(); + } + inline const_iterator end() const { + return reinterpret_cast>&>( + _values.GetStdMap()) + .end(); + } + [[nodiscard]] size_t GetCount() const noexcept { return _values.Count(); } inline ArbUt::BorrowedPtr GetRandomValue(ArbUt::Random rand = ArbUt::Random()) const noexcept { diff --git a/tests/LibraryTests/BaseLibraryTests.cpp b/tests/LibraryTests/BaseLibraryTests.cpp new file mode 100644 index 0000000..a65e212 --- /dev/null +++ b/tests/LibraryTests/BaseLibraryTests.cpp @@ -0,0 +1,19 @@ +#ifdef TESTS_BUILD +#include "../../extern/catch.hpp" +#include "../../src/Library/BaseLibrary.hpp" +#include "../TestLibrary/TestLibrary.hpp" + +using namespace CreatureLib::Library; +TEST_CASE("Iterate over library", "[Library]") { + auto& lib = TestLibrary::Get()->GetSpeciesLibrary(); + auto i = 0; + for (auto b : *lib) { + if (i == 0) { + CHECK(b.second->GetName() == "testSpecies1"_cnc); + } + i++; + } + CHECK(i == 1); +} + +#endif \ No newline at end of file