X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fendgame.h;h=a5e4efecafea8831c0bc4ee032ddc5c5aa5af6c8;hp=af5da0519ec8bb0bc624ea03459c9eff8dac2c79;hb=4554d8b2ac1ad260c3f7123b2a7d3a3385fa8306;hpb=48e39c5c8e671a37f09732d4a55d93d5e2d38550 diff --git a/src/endgame.h b/src/endgame.h index af5da051..a5e4efec 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -20,8 +20,8 @@ #if !defined(ENDGAME_H_INCLUDED) #define ENDGAME_H_INCLUDED -#include #include +#include #include "position.h" #include "types.h" @@ -46,6 +46,7 @@ enum EndgameType { // Scaling functions + SCALE_FUNS, KBPsK, // KB+pawns vs K KQKRPs, // KQ vs KR+pawns @@ -60,25 +61,30 @@ enum EndgameType { }; +/// Some magic to detect family type of endgame from its enum value + +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)> {}; + + /// Base and derived templates for endgame evaluation and scaling functions template struct EndgameBase { - typedef EndgameBase Base; - virtual ~EndgameBase() {} virtual Color color() const = 0; - virtual T apply(const Position&) const = 0; + virtual T operator()(const Position&) const = 0; }; -template +template::type> struct Endgame : public EndgameBase { - explicit Endgame(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {} + explicit Endgame(Color c) : strongerSide(c), weakerSide(flip(c)) {} Color color() const { return strongerSide; } - T apply(const Position&) const; + T operator()(const Position&) const; private: Color strongerSide, weakerSide; @@ -87,26 +93,28 @@ private: /// Endgames class stores in two std::map the pointers to endgame evaluation /// and scaling base objects. Then we use polymorphism to invoke the actual -/// endgame function calling its apply() method that is virtual. +/// endgame function calling its operator() method that is virtual. class Endgames { - typedef std::map* > EFMap; - typedef std::map* > SFMap; + typedef std::map*> M1; + typedef std::map*> M2; + + M1 m1; + M2 m2; + + M1& map(Value*) { return m1; } + M2& map(ScaleFactor*) { return m2; } + + template void add(const std::string& code); public: Endgames(); ~Endgames(); - template EndgameBase* get(Key key) const; - -private: - template void add(const std::string& keyCode); - - // Here we store two maps, for evaluate and scaling functions... - std::pair maps; - // ...and here is the accessing template function - template const std::map& map() const; + template EndgameBase* get(Key key) { + return map((T*)0).count(key) ? map((T*)0)[key] : NULL; + } }; #endif // !defined(ENDGAME_H_INCLUDED)