Skip to content

Commit

Permalink
#6021: Add IEntityClassManager::forEachEntityClass accepting a functi…
Browse files Browse the repository at this point in the history
…on object
  • Loading branch information
codereader committed Jul 29, 2022
1 parent bf24ebb commit ce7acbb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/ieclass.h
Expand Up @@ -301,6 +301,9 @@ class IEntityClassManager :
*/
virtual void forEachEntityClass(EntityClassVisitor& visitor) = 0;

// Iterate over all entityDefs using the given function object
virtual void forEachEntityClass(const std::function<void(const IEntityClassPtr&)>& functor) = 0;

/**
* greebo: This reloads the entityDefs and modelDefs from all files. Does not
* change the scenegraph, only the contents of the EClass objects are
Expand Down
10 changes: 9 additions & 1 deletion radiantcore/eclass/EClassManager.cpp
Expand Up @@ -30,10 +30,18 @@ IEntityClassPtr EClassManager::findClass(const std::string& className)
}

void EClassManager::forEachEntityClass(EntityClassVisitor& visitor)
{
forEachEntityClass([&](const IEntityClassPtr& eclass)
{
visitor.visit(eclass);
});
}

void EClassManager::forEachEntityClass(const std::function<void(const IEntityClassPtr&)>& functor)
{
GlobalDeclarationManager().foreachDeclaration(decl::Type::EntityDef, [&](const decl::IDeclaration::Ptr& decl)
{
visitor.visit(std::static_pointer_cast<IEntityClass>(decl));
functor(std::static_pointer_cast<IEntityClass>(decl));
});
}

Expand Down
1 change: 1 addition & 0 deletions radiantcore/eclass/EClassManager.h
Expand Up @@ -31,6 +31,7 @@ class EClassManager final :
IEntityClassPtr findOrInsert(const std::string& name, bool has_brushes) override;
IEntityClassPtr findClass(const std::string& className) override;
void forEachEntityClass(EntityClassVisitor& visitor) override;
void forEachEntityClass(const std::function<void(const IEntityClassPtr&)>& functor) override;

// Find the modeldef with the given name
IModelDef::Ptr findModel(const std::string& name) override;
Expand Down
16 changes: 16 additions & 0 deletions test/EntityClass.cpp
Expand Up @@ -31,6 +31,22 @@ TEST_F(EntityClassTest, LookupEntityClass)
GlobalEntityClassManager().findClass("LiGHT")) << "Lookup should be case-insensitive";
}

TEST_F(EntityClassTest, ForeachEntityClass)
{
std::set<std::string> visitedNames;

// Nonexistent class should return null (but not throw or crash)
GlobalEntityClassManager().forEachEntityClass([&] (const IEntityClassPtr& eclass)
{
visitedNames.insert(eclass->getDeclName());
});

EXPECT_TRUE(visitedNames.count("light") > 0);
EXPECT_TRUE(visitedNames.count("light_extinguishable") > 0);
EXPECT_TRUE(visitedNames.count("dr:entity_using_modeldef") > 0);
EXPECT_TRUE(visitedNames.count("atdm:light_base") > 0);
}

TEST_F(EntityClassTest, EntityClassDefFilename)
{
auto cls = GlobalEntityClassManager().findClass("dr:entity_using_modeldef");
Expand Down

0 comments on commit ce7acbb

Please sign in to comment.