From 3b67636f0ecb2ec7e61e9f4adf02a7d4cae89f0a Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 14 Aug 2011 12:36:34 +0100 Subject: [PATCH] Indulge a bit on the template wizardy Push the template pedal a bit in our "showoff" endgame code ;-) Signed-off-by: Marco Costalba --- src/endgame.cpp | 27 +++++++++++++-------------- src/endgame.h | 11 +++++------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/endgame.cpp b/src/endgame.cpp index c55013f3..0856883b 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -93,16 +93,16 @@ namespace { return Position(fen, false, 0).get_material_key(); } - typedef EndgameBase EF; - typedef EndgameBase SF; + typedef Endgames::EMap::type EFMap; + typedef Endgames::EMap::type SFMap; } // namespace /// Endgames member definitions -template<> const Endgames::EFMap& Endgames::map() const { return maps.first; } -template<> const Endgames::SFMap& Endgames::map() const { return maps.second; } +template<> const EFMap& Endgames::map() const { return maps.first; } +template<> const SFMap& Endgames::map() const { return maps.second; } Endgames::Endgames() { @@ -125,10 +125,10 @@ Endgames::Endgames() { Endgames::~Endgames() { - for (EFMap::const_iterator it = map().begin(); it != map().end(); ++it) + for (EFMap::const_iterator it = map().begin(); it != map().end(); ++it) delete it->second; - for (SFMap::const_iterator it = map().begin(); it != map().end(); ++it) + for (SFMap::const_iterator it = map().begin(); it != map().end(); ++it) delete it->second; } @@ -137,23 +137,22 @@ void Endgames::add(const string& keyCode) { typedef Endgame EG; typedef typename EG::Base B; - typedef std::map M; + typedef typename EMap::type M; - const_cast(map()).insert(std::pair(mat_key(keyCode), new EG(WHITE))); - const_cast(map()).insert(std::pair(mat_key(swap_colors(keyCode)), new EG(BLACK))); + const_cast(map()).insert(std::pair(mat_key(keyCode), new EG(WHITE))); + const_cast(map()).insert(std::pair(mat_key(swap_colors(keyCode)), new EG(BLACK))); } template EndgameBase* Endgames::get(Key key) const { - typedef EndgameBase E; - typename std::map::const_iterator it = map().find(key); - return it != map().end() ? it->second : NULL; + typename EMap::type::const_iterator it = map().find(key); + return it != map().end() ? it->second : NULL; } // Explicit template instantiations -template EF* Endgames::get(Key key) const; -template SF* Endgames::get(Key key) const; +template EndgameBase* Endgames::get(Key key) const; +template EndgameBase* Endgames::get(Key key) const; /// Mate with KX vs K. This function is used to evaluate positions with diff --git a/src/endgame.h b/src/endgame.h index af5da051..a9634738 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -89,12 +89,11 @@ private: /// and scaling base objects. Then we use polymorphism to invoke the actual /// endgame function calling its apply() method that is virtual. -class Endgames { +struct Endgames { - typedef std::map* > EFMap; - typedef std::map* > SFMap; + template + struct EMap { typedef std::map*> type; }; -public: Endgames(); ~Endgames(); template EndgameBase* get(Key key) const; @@ -103,10 +102,10 @@ private: template void add(const std::string& keyCode); // Here we store two maps, for evaluate and scaling functions... - std::pair maps; + std::pair::type, EMap::type> maps; // ...and here is the accessing template function - template const std::map& map() const; + template const typename EMap::type& map() const; }; #endif // !defined(ENDGAME_H_INCLUDED) -- 2.39.2