这比仅仅解决您的问题更复杂。您正在执行手动双重调度,并且您有错误。我们可以修复您的错误。
但是您遇到的问题不是您的错误,而是您正在执行手动双重调度的事实。
手动双重调度容易出错。
每次添加新类型时,都必须编写 O(N) 新代码,其中 N 是现有类型的数量。这段代码是基于复制粘贴的,如果你犯了错误,他们会默默地继续错误发送一些极端情况。
如果您继续进行手动双重调度,那么无论何时您或其他任何人修改代码,您都会继续遇到错误。
C++ 不提供自己的双重调度机制。但是使用c++17我们可以自动编写它
这是一个需要线性工作来管理双重调度的系统,加上每次碰撞的工作。
对于双分派中的每种类型,您添加一个类型到pMapType
. 就是这样,调度的其余部分是为您自动编写的。X
然后从继承您的新地图类型collide_dispatcher<X>
。
如果你想让两种类型有冲突代码,写一个自由函数 do_collide(A&,B&)
。变体中更容易的一个pMapType
应该是A
. 此函数必须在两者之前定义,A
并B
为调度工作而定义。
a.collide(b)
如果运行或运行,则该代码将b.collide(a)
运行,其中A
和分别是和B
的动态类型。a
b
您也可以结交do_collide
一种或另一种类型的朋友。
无需再费周折:
struct Player;
struct Wall;
struct Monster;
using pMapType = std::variant<Player*, Wall*, Monster*>;
namespace helper {
template<std::size_t I, class T, class V>
constexpr std::size_t index_in_variant() {
if constexpr (std::is_same<T, std::variant_alternative_t<I, V>>{})
return I;
else
return index_in_variant<I+1, T, V>();
}
}
template<class T, class V>
constexpr std::size_t index_in_variant() {
return helper::index_in_variant<0, T, V>();
}
template<class Lhs, class Rhs>
constexpr bool type_order() {
return index_in_variant<Lhs*, pMapType>() < index_in_variant<Rhs*, pMapType>();
}
template<class Lhs, class Rhs>
void do_collide( Lhs&, Rhs& ) {
std::cout << "Nothing happens\n";
}
struct MapObject;
template<class D, class Base=MapObject>
struct collide_dispatcher;
struct MapObject {
virtual void collide( MapObject& ) = 0;
protected:
template<class D, class Base>
friend struct collide_dispatcher;
virtual void collide_from( pMapType ) = 0;
virtual ~MapObject() {}
};
template<class D, class Base>
struct collide_dispatcher:Base {
D* self() { return static_cast<D*>(this); }
virtual void collide( MapObject& o ) final override {
o.collide_from( self() );
}
virtual void collide_from( std::variant<Player*, Wall*, Monster*> o_var ) final override {
std::visit( [&](auto* o){
using O = std::decay_t< decltype(*o) >;
if constexpr( type_order<D,O>() ) {
do_collide( *self(), *o );
} else {
do_collide( *o, *self() );
}
}, o_var );
}
};
void do_collide( Player& lhs, Wall& rhs );
void do_collide( Player& lhs, Monster& rhs );
struct Player : collide_dispatcher<Player> {
friend void do_collide( Player& lhs, Wall& rhs ) {
std::cout << "Player hit a Wall\n";
}
friend void do_collide( Player& lhs, Monster& rhs ) {
std::cout << "Player fought a Monster\n";
}
};
void do_collide( Wall& lhs, Monster& rhs );
struct Wall : collide_dispatcher<Wall> {
friend void do_collide( Wall& lhs, Monster& rhs ) {
std::cout << "Wall blocked a Monster\n";
}
};
void do_collide( Monster& lhs, Monster& rhs );
struct Monster : collide_dispatcher<Monster> {
friend void do_collide( Monster& lhs, Monster& rhs ) {
std::cout << "Monster Match!\n";
}
};
活生生的例子。
虽然这里的管道很复杂,但这确实意味着您不需要手动进行任何双重调度。您只是在编写端点。这减少了可能出现极端情况拼写错误的地方的数量。
测试代码:
int main() {
MapObject* pPlayer = new Player();
MapObject* pWall = new Wall();
MapObject* pMonster = new Monster();
std::cout << "Player:\n";
pPlayer->collide(*pPlayer);
pPlayer->collide(*pWall);
pPlayer->collide(*pMonster);
std::cout << "Wall:\n";
pWall->collide(*pPlayer);
pWall->collide(*pWall);
pWall->collide(*pMonster);
std::cout << "Monster:\n";
pMonster->collide(*pPlayer);
pMonster->collide(*pWall);
pMonster->collide(*pMonster);
}
输出是:
Player:
Nothing happens
Player hit a Wall
Player fought a Monster
Wall:
Player hit a Wall
Nothing happens
Wall blocked a Monster
Monster:
Player fought a Monster
Wall blocked a Monster
Monster Match!
您还可以创建一个中央 typedefstd::variant<Player*, Wall*, Monster*>
并map_type_index
使用该中央 typedef 来确定其顺序,从而减少向双调度系统添加新类型的工作,以在单个位置添加类型、实现新类型并转发声明应该做某事的碰撞代码。
更重要的是,这种双重调度代码可以使继承友好;派生类型 fromWall
可以调度到Wall
重载。如果你想要这个,你必须使collide_dispatcher
方法重载为非final
,允许SpecialWall
重新重载它们。
这是c++17,但每个主要编译器的当前版本现在都支持它需要的东西。一切都可以在c++14甚至c++11中完成,但它变得更加冗长并且可能需要boost。
虽然需要线性数量的代码来定义发生的事情,但编译器将生成二次数量的代码或静态表数据来实现双重调度。所以在你的双调度表中有 10,000 多种类型之前要小心。
如果您想MapObject
具体一点,请将接口从中分离出来并final
从调度程序中删除并添加MapObject
到pMapType
struct Player;
struct Wall;
struct Monster;
struct MapObject;
using pMapType = std::variant<MapObject*, Player*, Wall*, Monster*>;
namespace helper {
template<std::size_t I, class T, class V>
constexpr std::size_t index_in_variant() {
if constexpr (std::is_same<T, std::variant_alternative_t<I, V>>{})
return I;
else
return index_in_variant<I+1, T, V>();
}
}
template<class T, class V>
constexpr std::size_t index_in_variant() {
return helper::index_in_variant<0, T, V>();
}
template<class Lhs, class Rhs>
constexpr bool type_order() {
return index_in_variant<Lhs*, pMapType>() < index_in_variant<Rhs*, pMapType>();
}
template<class Lhs, class Rhs>
void do_collide( Lhs&, Rhs& ) {
std::cout << "Nothing happens\n";
}
struct collide_interface;
template<class D, class Base=collide_interface>
struct collide_dispatcher;
struct collide_interface {
virtual void collide( collide_interface& ) = 0;
protected:
template<class D, class Base>
friend struct collide_dispatcher;
virtual void collide_from( pMapType ) = 0;
virtual ~collide_interface() {}
};
template<class D, class Base>
struct collide_dispatcher:Base {
D* self() { return static_cast<D*>(this); }
virtual void collide( collide_interface& o ) override {
o.collide_from( self() );
}
virtual void collide_from( pMapType o_var ) override {
std::visit( [&](auto* o){
using O = std::decay_t< decltype(*o) >;
if constexpr( type_order<D,O>() ) {
do_collide( *self(), *o );
} else {
do_collide( *o, *self() );
}
}, o_var );
}
};
struct MapObject:collide_dispatcher<MapObject>
{
/* nothing */
};
活生生的例子。
因为你想Player
从MapObject
你的后裔,你必须使用的Base
论点collide_dispatcher
:
void do_collide( Player& lhs, Wall& rhs );
void do_collide( Player& lhs, Monster& rhs );
struct Player : collide_dispatcher<Player, MapObject> {
friend void do_collide( Player& lhs, Wall& rhs ) {
std::cout << "Player hit a Wall\n";
}
friend void do_collide( Player& lhs, Monster& rhs ) {
std::cout << "Player fought a Monster\n";
}
};
void do_collide( Wall& lhs, Monster& rhs );
struct Wall : collide_dispatcher<Wall, MapObject> {
friend void do_collide( Wall& lhs, Monster& rhs ) {
std::cout << "Wall blocked a Monster\n";
}
};
void do_collide( Monster& lhs, Monster& rhs );
struct Monster : collide_dispatcher<Monster, MapObject> {
friend void do_collide( Monster& lhs, Monster& rhs ) {
std::cout << "Monster Match!\n";
}
};