An implementation of the acyclic visitor pattern without RTTI
// class hierarchy
struct entity : snape::visitable<entity>
{
public:
SNAPE_VISITABLE(entity);
// something
};
struct geometry : entity
{
public:
SNAPE_VISITABLE(geometry);
// something
};
struct model : entity
{
public:
SNAPE_VISITABLE(model);
// something
};
// visitor
template<typename _Visitor>
using visitor_entities = snape::visitor<_Visitor, snape::visitor_traits<entity>>;
struct visitor : visitor_entities<visitor>
{
public:
SNAPE_VISIT(entity, geometry, model);
public:
void visit(const entity & obj)
{
// something
}
void visit(const geometry & obj)
{
// something
}
void visit(const model & obj)
{
// something
}
};
// usage
std::vector<entity*> entities =
{
new entity,
new model,
new entity,
new geometry,
new entity,
new model
};
visitor test_visitor;
for(const auto & obj : entities)
test_visitor(*obj);
Iterate of three million items
Pattern | Performance(ms) |
---|---|
Cyclic visitor | 11.3 |
Acyclic visitor(RTTI) | 220.4 |
Snape's acyclic visitor | 23.2 |
Thanks a lot to Manu Sánchez, for the CTTI