From dda0fa1a43e1096415ff8f83dd14e6322f1a8e68 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 1 Apr 2012 15:12:39 +0100 Subject: [PATCH] Use polymorphism to resolve map() overloading The 2 overload functions map() accept a pointer to EndgameBase or a pointer to EndgameBase. Because Endgame is derived from one of them we can directly use a pointer to this class to resolve the overload as is needed in Endgames::add(). Also made class Endgames fully parametrized and no more hardcoded to the types (Value or ScaleFactor) of endgames stored. No functional change. Signed-off-by: Marco Costalba --- src/endgame.cpp | 10 ++++------ src/endgame.h | 21 +++++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/endgame.cpp b/src/endgame.cpp index ec54d4e6..763a402e 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -116,10 +116,8 @@ Endgames::~Endgames() { template void Endgames::add(const string& code) { - typedef typename eg_family::type T; - - map((T*)0)[key(code, WHITE)] = new Endgame(WHITE); - map((T*)0)[key(code, BLACK)] = new Endgame(BLACK); + map((Endgame*)0)[key(code, WHITE)] = new Endgame(WHITE); + map((Endgame*)0)[key(code, BLACK)] = new Endgame(BLACK); } @@ -133,13 +131,13 @@ Value Endgame::operator()(const Position& pos) const { assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO); assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO); - // Stalemate detection with lone king + // Stalemate detection with lone king if ( pos.side_to_move() == weakerSide && !pos.in_check() && !MoveList(pos).size()) { return VALUE_DRAW; } - + Square winnerKSq = pos.king_square(strongerSide); Square loserKSq = pos.king_square(weakerSide); diff --git a/src/endgame.h b/src/endgame.h index 61b8c096..424a3591 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -61,11 +61,12 @@ enum EndgameType { }; -/// Some magic to detect family type of endgame from its enum value +/// Endgame functions can be of two types according if return a Value or a +/// ScaleFactor. Type eg_fun::type equals to either ScaleFactor or Value +/// depending if the template parameter is 0 or 1. -template struct bool_to_type { typedef Value type; }; -template<> struct bool_to_type { typedef ScaleFactor type; }; -template struct eg_family : public bool_to_type<(E > SCALE_FUNS)> {}; +template struct eg_fun { typedef Value type; }; +template<> struct eg_fun<1> { typedef ScaleFactor type; }; /// Base and derived templates for endgame evaluation and scaling functions @@ -79,7 +80,7 @@ struct EndgameBase { }; -template::type> +template SCALE_FUNS)>::type> struct Endgame : public EndgameBase { explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {} @@ -97,14 +98,14 @@ private: class Endgames { - typedef std::map*> M1; - typedef std::map*> M2; + typedef std::map::type>*> M1; + typedef std::map::type>*> M2; M1 m1; M2 m2; - M1& map(Value*) { return m1; } - M2& map(ScaleFactor*) { return m2; } + M1& map(M1::value_type::second_type) { return m1; } + M2& map(M2::value_type::second_type) { return m2; } template void add(const std::string& code); @@ -113,7 +114,7 @@ public: ~Endgames(); template EndgameBase* probe(Key key) { - return map((T*)0).count(key) ? map((T*)0)[key] : NULL; + return map((EndgameBase*)0).count(key) ? map((EndgameBase*)0)[key] : NULL; } }; -- 2.39.2